PageRenderTime 32ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/gnatcoll-gpl-2012-src/src/python/gnatcoll-python.adb

#
Ada | 1132 lines | 727 code | 164 blank | 241 comment | 14 complexity | 43161089a8dfd30681da36a9c3ea0c48 MD5 | raw file
Possible License(s): AGPL-1.0
  1. ------------------------------------------------------------------------------
  2. -- G N A T C O L L --
  3. -- --
  4. -- Copyright (C) 2003-2012, AdaCore --
  5. -- --
  6. -- This library is free software; you can redistribute it and/or modify it --
  7. -- under terms of the GNU General Public License as published by the Free --
  8. -- Software Foundation; either version 3, or (at your option) any later --
  9. -- version. This library is distributed in the hope that it will be useful, --
  10. -- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
  11. -- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. --
  12. -- --
  13. -- As a special exception under Section 7 of GPL version 3, you are granted --
  14. -- additional permissions described in the GCC Runtime Library Exception, --
  15. -- version 3.1, as published by the Free Software Foundation. --
  16. -- --
  17. -- You should have received a copy of the GNU General Public License and --
  18. -- a copy of the GCC Runtime Library Exception along with this program; --
  19. -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
  20. -- <http://www.gnu.org/licenses/>. --
  21. -- --
  22. ------------------------------------------------------------------------------
  23. with System; use System;
  24. with Interfaces.C.Strings; use Interfaces.C.Strings;
  25. with GNAT.OS_Lib; use GNAT.OS_Lib;
  26. package body GNATCOLL.Python is
  27. No_Method_Def : constant PyMethodDef := (Null_Ptr, null, 0, Null_Ptr);
  28. function Python_API_Version return Integer;
  29. pragma Import (C, Python_API_Version, "ada_python_api_version");
  30. type Methods_Access is access PyMethodDef_Array;
  31. type MethodDef_Access is access PyMethodDef;
  32. pragma Convention (C, MethodDef_Access);
  33. function PyCFunction_New
  34. (MethodDef : MethodDef_Access;
  35. Self : PyObject;
  36. Module : PyObject := null) return PyObject;
  37. pragma Import (C, PyCFunction_New, "ada_pycfunction_newex");
  38. -- Create a new callable object, which, when called from python, will call
  39. -- the Ada subprogram.
  40. -- This should be used only for standard functions, not for object methods
  41. -- Self is the first argument that will be passed to the Ada subprogram.
  42. function PyMethod_New
  43. (Func : PyObject; Self : PyObject := null; Klass : PyObject)
  44. return PyObject;
  45. pragma Import (C, PyMethod_New, "PyMethod_New");
  46. -- Create a new method, which calls Func.
  47. -- The method is unbounded if Self is null (and will be bound when the
  48. -- method is called). It is automatically bound if Self is not null.
  49. ------------------------
  50. -- PyRun_SimpleString --
  51. ------------------------
  52. function PyRun_SimpleString (Cmd : String) return Boolean is
  53. function Internal (Cmd : String) return Integer;
  54. pragma Import (C, Internal, "PyRun_SimpleString");
  55. begin
  56. return Internal (Cmd & ASCII.NUL) = 0;
  57. end PyRun_SimpleString;
  58. ------------------------
  59. -- PyImport_AddModule --
  60. ------------------------
  61. function PyImport_AddModule (Module_Name : String) return PyObject is
  62. function Internal (Name : String) return PyObject;
  63. pragma Import (C, Internal, "PyImport_AddModule");
  64. begin
  65. return Internal (Module_Name & ASCII.NUL);
  66. end PyImport_AddModule;
  67. ---------------------------
  68. -- PyImport_ImportModule --
  69. ---------------------------
  70. function PyImport_ImportModule (Module_Name : String) return PyObject is
  71. function Internal (Name : String) return PyObject;
  72. pragma Import (C, Internal, "PyImport_AddModule");
  73. begin
  74. return Internal (Module_Name & ASCII.NUL);
  75. end PyImport_ImportModule;
  76. ------------------
  77. -- PyRun_String --
  78. ------------------
  79. function PyRun_String
  80. (Str : String;
  81. Start : Interpreter_State;
  82. Globals : PyObject;
  83. Locals : PyObject) return PyObject
  84. is
  85. function Internal
  86. (Str : String;
  87. Start : Interpreter_State;
  88. Globals : PyObject;
  89. Locals : PyObject) return PyObject;
  90. pragma Import (C, Internal, "PyRun_String");
  91. begin
  92. return Internal (Str & ASCII.LF, Start, Globals, Locals);
  93. end PyRun_String;
  94. ----------------------
  95. -- PyArg_ParseTuple --
  96. ----------------------
  97. function PyArg_ParseTuple
  98. (Arg : PyObject;
  99. Format : String;
  100. Value1 : System.Address) return Boolean
  101. is
  102. function Internal
  103. (Arg : PyObject; Format : String; V1 : System.Address) return Integer;
  104. pragma Import (C, Internal, "ada_py_arg_parsetuple_ptr");
  105. begin
  106. return Internal (Arg, Format & ASCII.NUL, Value1) = 1;
  107. end PyArg_ParseTuple;
  108. function PyArg_ParseTuple
  109. (Arg : PyObject;
  110. Format : String;
  111. Value1, Value2 : System.Address) return Boolean
  112. is
  113. function Internal
  114. (Arg : PyObject; Format : String; V1, V2 : System.Address)
  115. return Integer;
  116. pragma Import (C, Internal, "ada_py_arg_parsetuple_ptr2");
  117. begin
  118. return Internal (Arg, Format & ASCII.NUL, Value1, Value2) = 1;
  119. end PyArg_ParseTuple;
  120. function PyArg_ParseTuple
  121. (Arg : PyObject;
  122. Format : String;
  123. Value1, Value2, Value3 : System.Address) return Boolean
  124. is
  125. function Internal
  126. (Arg : PyObject; Format : String; V1, V2, V3 : System.Address)
  127. return Integer;
  128. pragma Import (C, Internal, "ada_py_arg_parsetuple_ptr3");
  129. begin
  130. return Internal (Arg, Format & ASCII.NUL, Value1, Value2, Value3) = 1;
  131. end PyArg_ParseTuple;
  132. function PyArg_ParseTuple
  133. (Arg : PyObject;
  134. Format : String;
  135. Value1, Value2, Value3, Value4 : System.Address) return Boolean
  136. is
  137. function Internal
  138. (Arg : PyObject; Format : String; V1, V2, V3, V4 : System.Address)
  139. return Integer;
  140. pragma Import (C, Internal, "ada_py_arg_parsetuple_ptr4");
  141. begin
  142. return Internal
  143. (Arg, Format & ASCII.NUL, Value1, Value2, Value3, Value4) = 1;
  144. end PyArg_ParseTuple;
  145. function PyArg_ParseTuple
  146. (Arg : PyObject;
  147. Format : String;
  148. Value1, Value2, Value3, Value4, Value5 : System.Address) return Boolean
  149. is
  150. function Internal
  151. (Arg : PyObject; Format : String; V1, V2, V3, V4, V5 : System.Address)
  152. return Integer;
  153. pragma Import (C, Internal, "ada_py_arg_parsetuple_ptr5");
  154. begin
  155. return Internal
  156. (Arg, Format & ASCII.NUL, Value1, Value2, Value3, Value4, Value5) = 1;
  157. end PyArg_ParseTuple;
  158. ----------------------
  159. -- PyFunction_Check --
  160. ----------------------
  161. function PyFunction_Check (Func : PyObject) return Boolean is
  162. function Internal (Obj : PyObject) return Integer;
  163. pragma Import (C, Internal, "ada_pyfunction_check");
  164. begin
  165. return Internal (Func) = 1;
  166. end PyFunction_Check;
  167. ----------------------
  168. -- PyCallable_Check --
  169. ----------------------
  170. function PyCallable_Check (Func : PyObject) return Boolean is
  171. function Internal (Obj : PyObject) return Integer;
  172. pragma Import (C, Internal, "PyCallable_Check");
  173. begin
  174. return Internal (Func) = 1;
  175. end PyCallable_Check;
  176. --------------------
  177. -- PyString_Check --
  178. --------------------
  179. function PyString_Check (Obj : PyObject) return Boolean is
  180. function Internal (Obj : PyObject) return Integer;
  181. pragma Import (C, Internal, "ada_pystring_check");
  182. begin
  183. return Internal (Obj) = 1;
  184. end PyString_Check;
  185. ---------------------
  186. -- PyUnicode_Check --
  187. ---------------------
  188. function PyUnicode_Check (Obj : PyObject) return Boolean is
  189. function Internal (Obj : PyObject) return Integer;
  190. pragma Import (C, Internal, "ada_pyunicode_check");
  191. begin
  192. return Internal (Obj) = 1;
  193. end PyUnicode_Check;
  194. ------------------------
  195. -- PyBaseString_Check --
  196. ------------------------
  197. function PyBaseString_Check (Obj : PyObject) return Boolean is
  198. function Internal (Obj : PyObject) return Integer;
  199. pragma Import (C, Internal, "ada_pybasestring_check");
  200. begin
  201. return Internal (Obj) = 1;
  202. end PyBaseString_Check;
  203. ------------------
  204. -- PyList_Check --
  205. ------------------
  206. function PyList_Check (Obj : PyObject) return Boolean is
  207. function Internal (Obj : PyObject) return Integer;
  208. pragma Import (C, Internal, "ada_pylist_check");
  209. begin
  210. return Internal (Obj) = 1;
  211. end PyList_Check;
  212. ------------------
  213. -- PyIter_Check --
  214. ------------------
  215. function PyIter_Check (Obj : PyObject) return Boolean is
  216. function Internal (Obj : PyObject) return Integer;
  217. pragma Import (C, Internal, "ada_pyiter_check");
  218. begin
  219. return Internal (Obj) = 1;
  220. end PyIter_Check;
  221. -----------------
  222. -- PyInt_Check --
  223. -----------------
  224. function PyInt_Check (Obj : PyObject) return Boolean is
  225. function Internal (Obj : PyObject) return Integer;
  226. pragma Import (C, Internal, "ada_pyint_check");
  227. begin
  228. return Internal (Obj) = 1;
  229. end PyInt_Check;
  230. -------------------
  231. -- PyFloat_Check --
  232. -------------------
  233. function PyFloat_Check (Obj : PyObject) return Boolean is
  234. function Internal (Obj : PyObject) return Integer;
  235. pragma Import (C, Internal, "ada_pyfloat_check");
  236. begin
  237. return Internal (Obj) = 1;
  238. end PyFloat_Check;
  239. ------------------------
  240. -- PyBool_FromBoolean --
  241. ------------------------
  242. function PyBool_FromBoolean (Value : Boolean) return PyObject is
  243. function PyTrue return PyObject;
  244. pragma Import (C, PyTrue, "ada_py_true");
  245. function PyFalse return PyObject;
  246. pragma Import (C, PyFalse, "ada_py_false");
  247. Result : PyObject;
  248. begin
  249. if Value then
  250. Result := PyTrue;
  251. else
  252. Result := PyFalse;
  253. end if;
  254. Py_INCREF (Result);
  255. return Result;
  256. end PyBool_FromBoolean;
  257. ------------------
  258. -- PyBool_Check --
  259. ------------------
  260. function PyBool_Check (Obj : PyObject) return Boolean is
  261. function Internal (Obj : PyObject) return Integer;
  262. pragma Import (C, Internal, "ada_pybool_check");
  263. begin
  264. return Internal (Obj) = 1;
  265. end PyBool_Check;
  266. --------------------
  267. -- PyBool_Is_True --
  268. --------------------
  269. function PyBool_Is_True (Obj : PyObject) return Boolean is
  270. function Internal (Obj : PyObject) return Integer;
  271. pragma Import (C, Internal, "ada_pybool_is_true");
  272. begin
  273. return Internal (Obj) = 1;
  274. end PyBool_Is_True;
  275. -------------------
  276. -- PyTuple_Check --
  277. -------------------
  278. function PyTuple_Check (Obj : PyObject) return Boolean is
  279. function Internal (Obj : PyObject) return Integer;
  280. pragma Import (C, Internal, "ada_pytuple_check");
  281. begin
  282. return Internal (Obj) = 1;
  283. end PyTuple_Check;
  284. ----------------------
  285. -- PyObject_GetItem --
  286. ----------------------
  287. function PyObject_GetItem (Obj : PyObject; Key : Integer) return PyObject is
  288. K : PyObject;
  289. Result : PyObject;
  290. begin
  291. K := PyInt_FromLong (Interfaces.C.long (Key));
  292. Result := PyObject_GetItem (Obj, K);
  293. Py_DECREF (K);
  294. return Result;
  295. end PyObject_GetItem;
  296. ----------------------
  297. -- PyObject_SetItem --
  298. ----------------------
  299. procedure PyObject_SetItem
  300. (Obj : PyObject; Key : Integer; Value : PyObject)
  301. is
  302. K : PyObject;
  303. Result : Integer;
  304. pragma Unreferenced (Result);
  305. begin
  306. K := PyInt_FromLong (Interfaces.C.long (Key));
  307. Result := PyObject_SetItem (Obj, K, Value);
  308. Py_DECREF (K);
  309. end PyObject_SetItem;
  310. -----------------------
  311. -- PyString_AsString --
  312. -----------------------
  313. function PyString_AsString (Str : PyObject) return String is
  314. begin
  315. return Value (PyString_AsString (Str));
  316. end PyString_AsString;
  317. -------------------------
  318. -- PyString_FromString --
  319. -------------------------
  320. function PyString_FromString (Str : String) return PyObject is
  321. function Internal (Str : String; Size : Integer) return PyObject;
  322. pragma Import (C, Internal, "PyString_FromStringAndSize");
  323. begin
  324. return Internal (Str, Str'Length);
  325. end PyString_FromString;
  326. --------------------------
  327. -- PyUnicode_FromString --
  328. --------------------------
  329. function PyUnicode_FromString (Str : String) return PyObject is
  330. function Internal (Str : String) return PyObject;
  331. pragma Import (C, Internal, "ada_PyUnicode_FromString");
  332. begin
  333. return Internal (Str & ASCII.NUL);
  334. end PyUnicode_FromString;
  335. -------------------------------
  336. -- PyUnicode_AsEncodedString --
  337. -------------------------------
  338. function PyUnicode_AsEncodedString
  339. (Unicode : PyObject;
  340. Encoding : String;
  341. Errors : Unicode_Error_Handling := Strict)
  342. return PyObject
  343. is
  344. function Internal
  345. (Unicode : PyObject; Encoding, Errors : String) return PyObject;
  346. pragma Import (C, Internal, "ada_PyUnicode_AsEncodedString");
  347. begin
  348. case Errors is
  349. when Strict =>
  350. return Internal
  351. (Unicode, Encoding & ASCII.NUL, "strict" & ASCII.NUL);
  352. when Ignore =>
  353. return Internal
  354. (Unicode, Encoding & ASCII.NUL, "ignore" & ASCII.NUL);
  355. when Replace =>
  356. return Internal
  357. (Unicode, Encoding & ASCII.NUL, "replace" & ASCII.NUL);
  358. end case;
  359. end PyUnicode_AsEncodedString;
  360. ----------------------
  361. -- Unicode_AsString --
  362. ----------------------
  363. function Unicode_AsString
  364. (Str : PyObject; Encoding : String := "utf-8") return String
  365. is
  366. S : constant PyObject := PyUnicode_AsEncodedString
  367. (Unicode => Str, Encoding => Encoding, Errors => Replace);
  368. Result : constant String := PyString_AsString (S);
  369. begin
  370. Py_DECREF (S);
  371. return Result;
  372. end Unicode_AsString;
  373. ---------------------
  374. -- PySys_SetObject --
  375. ---------------------
  376. procedure PySys_SetObject (Name : String; Object : PyObject) is
  377. procedure Internal (Name : String; Object : PyObject);
  378. pragma Import (C, Internal, "PySys_SetObject");
  379. begin
  380. Internal (Name & ASCII.NUL, Object);
  381. end PySys_SetObject;
  382. ---------------------
  383. -- PySys_GetObject --
  384. ---------------------
  385. function PySys_GetObject (Name : String) return PyObject is
  386. function Internal (Name : String) return PyObject;
  387. pragma Import (C, Internal, "PySys_GetObject");
  388. begin
  389. return Internal (Name & ASCII.NUL);
  390. end PySys_GetObject;
  391. -------------------------
  392. -- PyObject_CallMethod --
  393. -------------------------
  394. function PyObject_CallMethod
  395. (Object : PyObject; Name : String) return PyObject
  396. is
  397. function Internal (Object : PyObject; Name : String) return PyObject;
  398. pragma Import (C, Internal, "ada_py_object_callmethod");
  399. begin
  400. return Internal (Object, Name & ASCII.NUL);
  401. end PyObject_CallMethod;
  402. function PyObject_CallMethod
  403. (Object : PyObject; Name : String; Arg1 : PyObject) return PyObject
  404. is
  405. function Internal
  406. (Object : PyObject; Name : String; Arg : PyObject) return PyObject;
  407. pragma Import (C, Internal, "ada_py_object_callmethod_obj");
  408. begin
  409. return Internal (Object, Name & ASCII.NUL, Arg1);
  410. end PyObject_CallMethod;
  411. function PyObject_CallMethod
  412. (Object : PyObject; Name : String; Arg1 : Integer) return PyObject
  413. is
  414. function Internal
  415. (Object : PyObject; Name : String; Arg : Integer) return PyObject;
  416. pragma Import (C, Internal, "ada_py_object_callmethod_int");
  417. begin
  418. return Internal (Object, Name & ASCII.NUL, Arg1);
  419. end PyObject_CallMethod;
  420. -----------------------
  421. -- Py_SetProgramName --
  422. -----------------------
  423. procedure Py_SetProgramName (Name : String) is
  424. procedure Internal (Name : String);
  425. pragma Import (C, Internal, "Py_SetProgramName");
  426. Program_Name : constant String_Access := new String'(Name & ASCII.NUL);
  427. -- As stated by the Python documentation the string passed to
  428. -- Py_SetProgramName should be in "static storage whose contents will
  429. -- not change for the duration of the program's execution"
  430. begin
  431. Internal (Program_Name.all);
  432. end Py_SetProgramName;
  433. ----------------------
  434. -- Py_SetPythonHome --
  435. ----------------------
  436. procedure Py_SetPythonHome (Home : String) is
  437. procedure Internal (Name : String);
  438. pragma Import (C, Internal, "Py_SetPythonHome");
  439. C_Home : constant String_Access := new String'(Home & ASCII.NUL);
  440. -- As stated by the Python documentation the string passed to
  441. -- Py_SetPythonHome should be in "static storage whose contents will
  442. -- not change for the duration of the program's execution"
  443. begin
  444. Internal (C_Home.all);
  445. end Py_SetPythonHome;
  446. ----------------------
  447. -- Py_CompileString --
  448. ----------------------
  449. function Py_CompileString
  450. (Cmd : String; Name : String; State : Interpreter_State)
  451. return PyCodeObject
  452. is
  453. function Internal (Cmd, Name : String; State : Interpreter_State)
  454. return PyCodeObject;
  455. pragma Import (C, Internal, "Py_CompileString");
  456. begin
  457. return Internal (Cmd & ASCII.NUL, Name & ASCII.NUL, State);
  458. end Py_CompileString;
  459. --------------------------
  460. -- PyDict_SetItemString --
  461. --------------------------
  462. procedure PyDict_SetItemString
  463. (Dict : PyDictObject; Key : String; Obj : PyObject)
  464. is
  465. S : chars_ptr := New_String (Key);
  466. Result : constant Integer := PyDict_SetItemString (Dict, S, Obj);
  467. pragma Unreferenced (Result);
  468. begin
  469. Free (S);
  470. end PyDict_SetItemString;
  471. ------------------------
  472. -- PyModule_AddObject --
  473. ------------------------
  474. function PyModule_AddObject
  475. (Module : PyObject; Name : String; Object : PyObject) return Integer
  476. is
  477. S : chars_ptr := New_String (Name);
  478. Result : Integer;
  479. begin
  480. Result := PyModule_AddObject (Module, S, Object);
  481. Free (S);
  482. return Result;
  483. end PyModule_AddObject;
  484. --------------------------
  485. -- PyDict_GetItemString --
  486. --------------------------
  487. function PyDict_GetItemString
  488. (Dict : PyDictObject; Key : String) return PyObject
  489. is
  490. S : chars_ptr := New_String (Key);
  491. Result : constant PyObject := PyDict_GetItemString (Dict, S);
  492. begin
  493. Free (S);
  494. return Result;
  495. end PyDict_GetItemString;
  496. ------------------
  497. -- Create_Tuple --
  498. ------------------
  499. function Create_Tuple (Objects : PyObject_Array) return PyObject is
  500. Tuple : constant PyObject := PyTuple_New (Objects'Length);
  501. begin
  502. for O in Objects'Range loop
  503. PyTuple_SetItem (Tuple, O - Objects'First, Objects (O));
  504. end loop;
  505. return Tuple;
  506. end Create_Tuple;
  507. ------------------------
  508. -- PyErr_NewException --
  509. ------------------------
  510. function PyErr_NewException
  511. (Name : String; Base : PyObject := null; Dict : PyObject := null)
  512. return PyObject
  513. is
  514. function Internal (Name : String; Base, Dict : PyObject) return PyObject;
  515. pragma Import (C, Internal, "PyErr_NewException");
  516. begin
  517. return Internal (Name & ASCII.NUL, Base, Dict);
  518. end PyErr_NewException;
  519. ---------------------
  520. -- PyErr_SetString --
  521. ---------------------
  522. procedure PyErr_SetString (Except : PyObject; Msg : String) is
  523. procedure Internal (Except : PyObject; Msg : String);
  524. pragma Import (C, Internal, "PyErr_SetString");
  525. begin
  526. Internal (Except, Msg & ASCII.NUL);
  527. end PyErr_SetString;
  528. ----------------------------
  529. -- PyObject_GetAttrString --
  530. ----------------------------
  531. function PyObject_GetAttrString
  532. (Object : PyObject; Name : String) return PyObject
  533. is
  534. S : chars_ptr := New_String (Name);
  535. Result : constant PyObject := PyObject_GetAttrString (Object, S);
  536. begin
  537. Free (S);
  538. return Result;
  539. end PyObject_GetAttrString;
  540. ----------------------------
  541. -- PyObject_HasAttrString --
  542. ----------------------------
  543. function PyObject_HasAttrString
  544. (Obj : PyObject; Attr_Name : String) return Boolean
  545. is
  546. function Internal (Object : PyObject; S : String) return Integer;
  547. pragma Import (C, Internal, "PyObject_HasAttrString");
  548. begin
  549. return Boolean'Val (Internal (Obj, Attr_Name & ASCII.NUL));
  550. end PyObject_HasAttrString;
  551. ----------------------------
  552. -- PyObject_SetAttrString --
  553. ----------------------------
  554. procedure PyObject_SetAttrString
  555. (Obj : PyObject; Attr_Name : String; Value : PyObject)
  556. is
  557. procedure Internal (Obj : PyObject; Name : String; Val : PyObject);
  558. pragma Import (C, Internal, "PyObject_SetAttrString");
  559. begin
  560. Internal (Obj, Attr_Name & ASCII.NUL, Value);
  561. end PyObject_SetAttrString;
  562. ----------------------------
  563. -- PyObject_SetAttrString --
  564. ----------------------------
  565. function PyObject_SetAttrString
  566. (Obj : PyObject; Attr_Name : String; Value : PyObject) return Integer
  567. is
  568. function Internal
  569. (Obj : PyObject; Name : String; Val : PyObject) return Integer;
  570. pragma Import (C, Internal, "PyObject_SetAttrString");
  571. begin
  572. return Internal (Obj, Attr_Name & ASCII.NUL, Value);
  573. end PyObject_SetAttrString;
  574. -----------------------------------
  575. -- PyObject_GenericSetAttrString --
  576. -----------------------------------
  577. function PyObject_GenericSetAttrString
  578. (Object : PyObject;
  579. Name : String;
  580. Attr : PyObject) return Integer
  581. is
  582. N : constant PyObject := PyString_FromString (Name);
  583. Result : Integer;
  584. begin
  585. Result := PyObject_GenericSetAttr (Object, N, Attr);
  586. Py_DECREF (N);
  587. return Result;
  588. end PyObject_GenericSetAttrString;
  589. -----------------
  590. -- PyDict_Next --
  591. -----------------
  592. procedure PyDict_Next
  593. (Dict : PyObject;
  594. Pos : in out Integer;
  595. Key : out PyObject;
  596. Value : out PyObject)
  597. is
  598. function Internal
  599. (Dict : PyObject; Pos, Key, Value : System.Address) return Integer;
  600. pragma Import (C, Internal, "PyDict_Next");
  601. begin
  602. if Internal (Dict, Pos'Address, Key'Address, Value'Address) = 0 then
  603. Pos := -1;
  604. end if;
  605. end PyDict_Next;
  606. --------------------
  607. -- Print_Refcount --
  608. --------------------
  609. procedure Print_Refcount (Obj : PyObject; Msg : String) is
  610. procedure Internal (Obj : PyObject; Msg : String);
  611. pragma Import (C, Internal, "ada_py_print_refcount");
  612. begin
  613. Internal (Obj, Msg & ASCII.NUL);
  614. end Print_Refcount;
  615. ------------------------
  616. -- PyFile_WriteString --
  617. ------------------------
  618. function PyFile_WriteString
  619. (Text : String; File : PyObject) return Boolean
  620. is
  621. function Internal (Text : String; File : PyObject) return Integer;
  622. pragma Import (C, Internal, "PyFile_WriteString");
  623. begin
  624. return Internal (Text & ASCII.NUL, File) /= 0;
  625. end PyFile_WriteString;
  626. -------------------
  627. -- Py_InitModule --
  628. -------------------
  629. function Py_InitModule
  630. (Module_Name : String;
  631. Methods : PyMethodDef_Array := No_MethodDef_Array;
  632. Doc : String := "") return PyObject
  633. is
  634. function Internal
  635. (N : String;
  636. Methods : System.Address;
  637. Doc : String;
  638. Self : PyObject := null;
  639. Apiver : Integer := Python_API_Version) return PyObject;
  640. pragma Import (C, Internal, "ada_Py_InitModule4");
  641. M : Methods_Access;
  642. begin
  643. if Methods /= No_MethodDef_Array then
  644. -- ??? Memory is never freed, but Python is not supposed to be killed
  645. -- before the end of the application
  646. M := new PyMethodDef_Array'(Methods & No_Method_Def);
  647. return Internal
  648. (Module_Name & ASCII.NUL, M.all'Address,
  649. Doc & ASCII.NUL);
  650. else
  651. return Internal
  652. (Module_Name & ASCII.NUL, System.Null_Address,
  653. Doc & ASCII.NUL);
  654. end if;
  655. end Py_InitModule;
  656. ----------
  657. -- Free --
  658. ----------
  659. procedure Free (Method : in out PyMethodDef) is
  660. procedure C_Free (C : Interfaces.C.Strings.chars_ptr);
  661. pragma Import (C, C_Free, "free");
  662. begin
  663. C_Free (Method.Name);
  664. C_Free (Method.Doc);
  665. Method.Name := Null_Ptr;
  666. Method.Doc := Null_Ptr;
  667. end Free;
  668. ----------
  669. -- Free --
  670. ----------
  671. procedure Free (Methods : in out PyMethodDef_Array) is
  672. begin
  673. for M in Methods'Range loop
  674. Free (Methods (M));
  675. end loop;
  676. end Free;
  677. ----------------------
  678. -- PyModule_Getname --
  679. ----------------------
  680. function PyModule_Getname (Module : PyObject) return String is
  681. function Internal (M : PyObject) return Interfaces.C.Strings.chars_ptr;
  682. pragma Import (C, Internal, "PyModule_GetName");
  683. begin
  684. return Value (Internal (Module));
  685. end PyModule_Getname;
  686. ------------------
  687. -- Add_Function --
  688. ------------------
  689. procedure Add_Function
  690. (Module : PyObject; Func : PyMethodDef; Self : PyObject := null)
  691. is
  692. C_Func : PyObject;
  693. Result : Integer;
  694. pragma Unreferenced (Result);
  695. begin
  696. if Self /= null then
  697. C_Func := PyCFunction_New
  698. (new PyMethodDef'(Func), Self,
  699. PyString_FromString (PyModule_Getname (Module)));
  700. else
  701. C_Func := PyCFunction_New
  702. (new PyMethodDef'(Func), Module,
  703. PyString_FromString (PyModule_Getname (Module)));
  704. end if;
  705. if C_Func /= null then
  706. Result := PyModule_AddObject (Module, Func.Name, C_Func);
  707. end if;
  708. end Add_Function;
  709. ----------------
  710. -- Add_Method --
  711. ----------------
  712. procedure Add_Method
  713. (Class : PyObject;
  714. Func : PyMethodDef;
  715. Self : PyObject := null;
  716. Module : PyObject)
  717. is
  718. C_Func : constant PyObject :=
  719. PyCFunction_New (new PyMethodDef'(Func), Self,
  720. PyString_FromString (PyModule_Getname (Module)));
  721. C_Meth : constant PyObject := PyMethod_New (C_Func, null, Class);
  722. Ignored : Integer;
  723. pragma Unreferenced (Ignored);
  724. begin
  725. Ignored := PyObject_SetAttrString (Class, Func.Name, C_Meth);
  726. Py_DECREF (C_Meth);
  727. end Add_Method;
  728. -----------------------
  729. -- Add_Static_Method --
  730. -----------------------
  731. procedure Add_Static_Method
  732. (Class : PyObject; Func : PyMethodDef; Self : PyObject := null;
  733. Module : PyObject)
  734. is
  735. function PyStaticMethod_New (Method : PyObject) return PyObject;
  736. pragma Import (C, PyStaticMethod_New, "PyStaticMethod_New");
  737. Def : constant MethodDef_Access := new PyMethodDef'(Func);
  738. C_Func : PyObject;
  739. Static : PyObject;
  740. Result : Integer;
  741. pragma Unreferenced (Result);
  742. begin
  743. Def.Flags := Def.Flags or METH_STATIC;
  744. -- If the documentation is not specified, store the fully qualified
  745. -- name instead. There is no way otherwise to retrieve the class name
  746. -- from the python shell.
  747. if Def.Doc = Null_Ptr then
  748. Def.Doc := New_String
  749. (PyString_AsString (PyObject_GetAttrString (Class, "__module__"))
  750. & "." & PyString_AsString (PyClass_Name (Class))
  751. & "." & Value (Func.Name));
  752. end if;
  753. C_Func := PyCFunction_New
  754. (Def, Self, PyString_FromString (PyModule_Getname (Module)));
  755. if C_Func /= null then
  756. Static := PyStaticMethod_New (C_Func);
  757. Result := PyObject_SetAttrString (Class, Func.Name, Static);
  758. Py_DECREF (Static);
  759. end if;
  760. end Add_Static_Method;
  761. ----------------------
  762. -- Add_Class_Method --
  763. ----------------------
  764. procedure Add_Class_Method
  765. (Class : PyObject; Func : PyMethodDef; Module : PyObject)
  766. is
  767. function PyClassMethod_New (Method : PyObject) return PyObject;
  768. pragma Import (C, PyClassMethod_New, "PyClassMethod_New");
  769. Def : constant MethodDef_Access := new PyMethodDef'(Func);
  770. C_Func : PyObject;
  771. Result : Integer;
  772. Meth : PyObject;
  773. pragma Unreferenced (Result);
  774. begin
  775. Def.Flags := Def.Flags or METH_CLASS;
  776. -- If the documentation is not specified, store the fully qualified
  777. -- name instead. There is no way otherwise to retrieve the class name
  778. -- from the python shell.
  779. if Def.Doc = Null_Ptr then
  780. Def.Doc := New_String
  781. (PyString_AsString (PyObject_GetAttrString (Class, "__module__"))
  782. & "." & PyString_AsString (PyClass_Name (Class))
  783. & "." & Value (Func.Name));
  784. end if;
  785. C_Func := PyCFunction_New
  786. (Def, null, PyString_FromString (PyModule_Getname (Module)));
  787. if C_Func /= null then
  788. Meth := PyClassMethod_New (C_Func);
  789. Result := PyObject_SetAttrString (Class, Func.Name, Meth);
  790. Py_DECREF (Meth);
  791. end if;
  792. end Add_Class_Method;
  793. -----------------------
  794. -- PyDescr_NewGetSet --
  795. -----------------------
  796. function PyDescr_NewGetSet
  797. (Typ : PyObject;
  798. Name : String;
  799. Setter : C_Setter := null;
  800. Getter : C_Getter := null;
  801. Doc : String := "";
  802. Closure : System.Address := System.Null_Address) return Boolean
  803. is
  804. function To_Callback is new Standard.Ada.Unchecked_Conversion
  805. (C_Getter, C_Callback);
  806. function To_Callback is new Standard.Ada.Unchecked_Conversion
  807. (C_Setter, C_Callback);
  808. function Internal
  809. (Typ : PyObject; Name : chars_ptr;
  810. Setter, Getter : C_Callback; Doc : chars_ptr;
  811. Closure : System.Address) return Integer;
  812. pragma Import (C, Internal, "ada_pydescr_newGetSet");
  813. begin
  814. return Internal
  815. (Typ, New_String (Name), To_Callback (Setter),
  816. To_Callback (Getter), New_String (Doc), Closure) /= 0;
  817. end PyDescr_NewGetSet;
  818. -----------------------
  819. -- Create_Method_Def --
  820. -----------------------
  821. function Create_Method_Def
  822. (Name : String;
  823. Func : C_Method_Vargs;
  824. Doc : String := "")
  825. return PyMethodDef is
  826. begin
  827. return (Name => New_String (Name),
  828. Func => To_Callback (Func),
  829. Flags => METH_VARGS,
  830. Doc => New_String (Doc));
  831. end Create_Method_Def;
  832. -----------------------
  833. -- Create_Method_Def --
  834. -----------------------
  835. function Create_Method_Def
  836. (Name : String;
  837. Func : C_Method_Keywords;
  838. Doc : String := "")
  839. return PyMethodDef
  840. is
  841. D : chars_ptr := Null_Ptr;
  842. begin
  843. if Doc /= "" then
  844. D := New_String (Doc);
  845. end if;
  846. return (Name => New_String (Name),
  847. Func => To_Callback (Func),
  848. Flags => METH_KEYWORDS,
  849. Doc => D);
  850. end Create_Method_Def;
  851. -------------------
  852. -- Lookup_Object --
  853. -------------------
  854. function Lookup_Object
  855. (Module : String; Name : String) return PyObject is
  856. begin
  857. return Lookup_Object (PyImport_AddModule (Module), Name);
  858. end Lookup_Object;
  859. -------------------
  860. -- Lookup_Object --
  861. -------------------
  862. function Lookup_Object
  863. (Module : PyObject; Name : String) return PyObject
  864. is
  865. Dict : PyObject;
  866. begin
  867. if Module /= null then
  868. Dict := PyModule_GetDict (Module);
  869. return PyDict_GetItemString (Dict, Name);
  870. end if;
  871. return null;
  872. end Lookup_Object;
  873. -------------
  874. -- Py_Main --
  875. -------------
  876. function Py_Main return Integer is
  877. function Internal return Integer;
  878. pragma Import (C, Internal, "ada_py_main");
  879. begin
  880. return Internal;
  881. end Py_Main;
  882. ---------------------
  883. -- PyCObject_Check --
  884. ---------------------
  885. function PyCObject_Check (Obj : PyObject) return Boolean is
  886. function Internal (Obj : PyObject) return Integer;
  887. pragma Import (C, Internal, "ada_pycobject_check");
  888. begin
  889. return Internal (Obj) = 1;
  890. end PyCObject_Check;
  891. ----------------------
  892. -- PyInstance_Check --
  893. ----------------------
  894. function PyInstance_Check (Obj : PyObject) return Boolean is
  895. function Internal (Obj : PyObject) return Integer;
  896. pragma Import (C, Internal, "ada_pyinstance_check");
  897. begin
  898. return Internal (Obj) = 1;
  899. end PyInstance_Check;
  900. --------------------
  901. -- PyMethod_Check --
  902. --------------------
  903. function PyMethod_Check (Obj : PyObject) return Boolean is
  904. function Internal (Obj : PyObject) return Integer;
  905. pragma Import (C, Internal, "ada_pymethod_check");
  906. begin
  907. return Internal (Obj) = 1;
  908. end PyMethod_Check;
  909. ------------------------
  910. -- PyClass_IsSubclass --
  911. ------------------------
  912. function PyClass_IsSubclass
  913. (Class : PyObject; Base : PyObject) return Boolean
  914. is
  915. function Internal (Class, Base : PyObject) return Integer;
  916. pragma Import (C, Internal, "ada_pyclass_is_subclass");
  917. begin
  918. return Internal (Class, Base) /= 0;
  919. end PyClass_IsSubclass;
  920. --------------
  921. -- Type_New --
  922. --------------
  923. function Type_New
  924. (Name : String;
  925. Bases : PyTuple;
  926. Dict : PyObject;
  927. Metatype : PyTypeObject := null) return PyObject
  928. is
  929. function Internal
  930. (Meta : PyTypeObject;
  931. Name : Interfaces.C.Strings.chars_ptr;
  932. Bases : PyObject;
  933. Dict : PyObject) return PyObject;
  934. pragma Import (C, Internal, "ada_type_new");
  935. C : chars_ptr := New_String (Name);
  936. Result : PyObject;
  937. begin
  938. Result := Internal (Metatype, C, Bases, Dict);
  939. Free (C);
  940. return Result;
  941. end Type_New;
  942. -------------------------
  943. -- PyObject_IsInstance --
  944. -------------------------
  945. function PyObject_IsInstance
  946. (Inst : PyObject; Cls : PyObject) return Boolean
  947. is
  948. function Internal (Inst, Cls : PyObject) return Integer;
  949. pragma Import (C, Internal, "PyObject_IsInstance");
  950. begin
  951. return Internal (Inst, Cls) /= 0;
  952. end PyObject_IsInstance;
  953. ---------------------
  954. -- PyObject_IsTrue --
  955. ---------------------
  956. function PyObject_IsTrue (Obj : PyObject) return Boolean is
  957. function Internal (Obj : PyObject) return Integer;
  958. pragma Import (C, Internal, "PyObject_IsTrue");
  959. Val : Integer;
  960. begin
  961. Val := Internal (Obj);
  962. if Val = -1 then
  963. return False; -- An error
  964. else
  965. return Val /= 0;
  966. end if;
  967. end PyObject_IsTrue;
  968. end GNATCOLL.Python;