PageRenderTime 25ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/gnatcoll-gpl-2012-src/src/python/python_support.c

#
C | 490 lines | 393 code | 70 blank | 27 comment | 42 complexity | a3d7e7ff0c6d301cad99d8a4a74f485a 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 is free software; you can redistribute it and/or modify it under --
  7. -- terms of the GNU General Public License as published by the Free Soft- --
  8. -- ware Foundation; either version 3, or (at your option) any later ver- --
  9. -- sion. This software 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. See the GNU General Public --
  12. -- License for more details. You should have received a copy of the GNU --
  13. -- General Public License distributed with this software; see file --
  14. -- COPYING3. If not, go to http://www.gnu.org/licenses for a complete copy --
  15. -- of the license. --
  16. ----------------------------------------------------------------------------*/
  17. /* Force a value for the macro. It will only work for gcc, but otherwise
  18. * we cannot use the mingwin python with gcc on Windows*/
  19. #define PY_LONG_LONG long long
  20. #include <Python.h>
  21. #include <compile.h> /* PyCodeObject definition in older versions*/
  22. #undef DEBUG
  23. /* #define DEBUG */
  24. PyObject *
  25. ada_Py_InitModule4
  26. (char *name, PyMethodDef *methods,
  27. char *doc, PyObject *self,
  28. int apiver)
  29. {
  30. return Py_InitModule4 (name, methods, doc, self, apiver);
  31. }
  32. PyObject *
  33. ada_pycfunction_newex (PyMethodDef *ml, PyObject *self, PyObject *module)
  34. {
  35. PyObject *method = PyCFunction_New (ml, self);
  36. #if (PY_MAJOR_VERSION > 2 \
  37. || (PY_MAJOR_VERSION == 2 \
  38. && (PY_MINOR_VERSION > 3 \
  39. || (PY_MINOR_VERSION == 3 \
  40. && PY_MICRO_VERSION >= 3))))
  41. ((PyCFunctionObject*)method)->m_module = module;
  42. Py_XINCREF (module);
  43. #endif
  44. return method;
  45. }
  46. int
  47. ada_pyget_refcount (PyObject* obj)
  48. {
  49. return obj->ob_refcnt;
  50. }
  51. char*
  52. ada_py_refcount_msg (PyObject* obj)
  53. {
  54. static char msg[200];
  55. if (obj) {
  56. snprintf (msg, 199, "%p (%s, rc=%ld)",
  57. obj, obj->ob_type->tp_name, obj->ob_refcnt);
  58. } else {
  59. msg[0] = '\0';
  60. }
  61. return msg;
  62. }
  63. void
  64. ada_py_print_refcount (PyObject* obj, char* msg)
  65. {
  66. if (obj)
  67. printf ("DEBUG %s %s\n", msg, ada_py_refcount_msg (obj));
  68. }
  69. void
  70. ada_py_incref (PyObject* obj)
  71. {
  72. Py_INCREF (obj);
  73. #ifdef DEBUG
  74. ada_py_print_refcount (obj, "after incref");
  75. #endif
  76. }
  77. void
  78. ada_py_decref (PyObject* obj)
  79. {
  80. #ifdef DEBUG
  81. ada_py_print_refcount (obj, "before decref");
  82. #endif
  83. Py_DECREF (obj);
  84. }
  85. void
  86. ada_py_xincref (PyObject* obj)
  87. {
  88. Py_XINCREF (obj);
  89. #ifdef DEBUG
  90. ada_py_print_refcount (obj, "after xincref");
  91. #endif
  92. }
  93. void
  94. ada_py_xdecref (PyObject* obj)
  95. {
  96. #ifdef DEBUG
  97. ada_py_print_refcount (obj, "before xdecref");
  98. #endif
  99. Py_XDECREF (obj);
  100. }
  101. int
  102. ada_pybasestring_check (PyObject* obj)
  103. {
  104. return PyString_Check (obj) || PyUnicode_Check (obj);
  105. }
  106. int
  107. ada_pystring_check (PyObject* obj)
  108. {
  109. return PyString_Check (obj);
  110. }
  111. PyObject* ada_PyUnicode_AsEncodedString
  112. (PyObject *unicode, const char *encoding, const char *errors)
  113. {
  114. #ifdef Py_UNICODE_WIDE
  115. return PyUnicodeUCS4_AsEncodedString (unicode, encoding, errors);
  116. #else
  117. return PyUnicodeUCS2_AsEncodedString (unicode, encoding, errors);
  118. #endif
  119. }
  120. PyObject* ada_PyUnicode_FromString (const char *u)
  121. {
  122. #if PY_VERSION_HEX >= 0x02060000
  123. #ifdef Py_UNICODE_WIDE
  124. return PyUnicodeUCS4_FromString (u);
  125. #else
  126. return PyUnicodeUCS2_FromString (u);
  127. #endif
  128. #else
  129. /* Not available in this version */
  130. return 0;
  131. #endif
  132. }
  133. int
  134. ada_pyunicode_check (PyObject* obj)
  135. {
  136. return PyUnicode_Check (obj);
  137. }
  138. int
  139. ada_pyint_check (PyObject* obj)
  140. {
  141. return PyInt_Check (obj);
  142. }
  143. int
  144. ada_pyfloat_check (PyObject* obj)
  145. {
  146. return PyFloat_Check (obj);
  147. }
  148. int
  149. ada_pybool_check (PyObject* obj)
  150. {
  151. #ifdef PyBool_Check
  152. return PyBool_Check (obj);
  153. #else
  154. return 0;
  155. #endif
  156. }
  157. int
  158. ada_pybool_is_true (PyObject* obj)
  159. {
  160. return PyObject_IsTrue (obj);
  161. }
  162. int
  163. ada_pyfunction_check (PyObject* obj)
  164. {
  165. return PyFunction_Check (obj);
  166. }
  167. PyObject*
  168. ada_pyfunction_get_globals (PyObject* obj)
  169. {
  170. return PyFunction_GET_GLOBALS (obj);
  171. }
  172. PyObject*
  173. ada_pyfunction_get_code (PyObject* obj)
  174. {
  175. return PyFunction_GET_CODE (obj);
  176. }
  177. PyObject*
  178. ada_pyfunction_get_closure (PyObject* obj)
  179. {
  180. return PyFunction_GET_CLOSURE (obj);
  181. }
  182. PyObject*
  183. ada_pyfunction_get_defaults (PyObject* obj)
  184. {
  185. return PyFunction_GET_DEFAULTS (obj);
  186. }
  187. PyObject* ada_PyEval_EvalCodeEx
  188. (PyCodeObject *co,
  189. PyObject *globals,
  190. PyObject *locals,
  191. PyObject *args,
  192. PyObject *kwds,
  193. PyObject *defs,
  194. PyObject *closure)
  195. {
  196. /* Code copied from funcobject.c::function_call() */
  197. PyObject **k, **d;
  198. PyObject* result;
  199. int nk, nd;
  200. if (defs != NULL && PyTuple_Check(defs)) {
  201. d = &PyTuple_GET_ITEM((PyTupleObject *)defs, 0);
  202. nd = PyTuple_Size(defs);
  203. } else {
  204. d = NULL;
  205. nd = 0;
  206. }
  207. if (kwds != NULL && PyDict_Check(kwds)) {
  208. int i = 0;
  209. #if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION==2 && PY_MINOR_VERSION>=5)
  210. Py_ssize_t pos = 0;
  211. #else
  212. int pos = 0;
  213. #endif
  214. nk = PyDict_Size(kwds);
  215. k = PyMem_NEW(PyObject *, 2*nk);
  216. if (k == NULL) {
  217. PyErr_NoMemory();
  218. return NULL;
  219. }
  220. while (PyDict_Next(kwds, &pos, &k[i], &k[i+1]))
  221. i += 2;
  222. nk = i/2;
  223. /* XXX This is broken if the caller deletes dict items! */
  224. } else {
  225. k = NULL;
  226. nk = 0;
  227. }
  228. result = (PyObject*) PyEval_EvalCodeEx
  229. (co, globals, locals,
  230. &PyTuple_GET_ITEM (args, 0) /* args */,
  231. PyTuple_Size (args) /* argcount */,
  232. k /* kws */, nk /* kwcount */, d /* defs */, nd /* defcount */, closure);
  233. if (k != NULL) {
  234. PyMem_DEL (k);
  235. }
  236. return result;
  237. }
  238. int
  239. ada_pycobject_check (PyObject* obj)
  240. {
  241. return PyCObject_Check (obj);
  242. }
  243. int
  244. ada_pytuple_check (PyObject* obj)
  245. {
  246. return PyTuple_Check (obj);
  247. }
  248. int
  249. ada_pylist_check (PyObject* obj)
  250. {
  251. return PyList_Check (obj);
  252. }
  253. int
  254. ada_pyiter_check (PyObject* obj)
  255. {
  256. return PyIter_Check (obj);
  257. }
  258. int
  259. ada_pyinstance_check (PyObject* obj)
  260. {
  261. return PyInstance_Check (obj);
  262. }
  263. int
  264. ada_pymethod_check (PyObject* obj)
  265. {
  266. return PyMethod_Check (obj);
  267. }
  268. PyTypeObject*
  269. ada_gettypeobject (PyObject* obj)
  270. {
  271. return (PyTypeObject*)(obj->ob_type);
  272. }
  273. int
  274. ada_python_api_version ()
  275. {
  276. return PYTHON_API_VERSION;
  277. }
  278. PyObject* ada_py_none ()
  279. {
  280. return Py_None;
  281. }
  282. PyObject* ada_py_false()
  283. {
  284. return Py_False;
  285. }
  286. PyObject*
  287. ada_py_true()
  288. {
  289. return Py_True;
  290. }
  291. PyObject*
  292. ada_pyclass_name(PyObject* obj)
  293. {
  294. if (PyClass_Check (obj)) {
  295. return ((PyClassObject*)obj)->cl_name;
  296. } else {
  297. /* Derives from object, not a real class */
  298. return PyObject_GetAttrString (obj, "__name__");
  299. }
  300. }
  301. int
  302. ada_pyclass_is_subclass (PyObject* class, PyObject* base)
  303. {
  304. if (!class || !base) {
  305. return -1;
  306. } else if (PyClass_Check (class)) {
  307. return PyClass_IsSubclass (class, base);
  308. } else {
  309. return PyObject_TypeCheck (class, base->ob_type);
  310. }
  311. }
  312. PyObject *
  313. ada_py_object_callmethod (PyObject *o, char *m)
  314. {
  315. return PyObject_CallMethod (o, m, "");
  316. }
  317. PyObject *
  318. ada_py_object_callmethod_obj (PyObject *o, char *m, PyObject *arg)
  319. {
  320. return PyObject_CallMethod (o, m, "(O)", arg);
  321. }
  322. PyObject *
  323. ada_py_object_callmethod_int (PyObject *o, char *m, int arg)
  324. {
  325. return PyObject_CallMethod (o, m, "(i)", arg);
  326. }
  327. int
  328. ada_py_arg_parsetuple_ptr (PyObject *o, char *fmt, void *arg1)
  329. {
  330. return PyArg_ParseTuple (o, fmt, arg1);
  331. }
  332. int
  333. ada_py_arg_parsetuple_ptr2 (PyObject *o, char *fmt, void *arg1, void *arg2)
  334. {
  335. return PyArg_ParseTuple (o, fmt, arg1, arg2);
  336. }
  337. int
  338. ada_py_arg_parsetuple_ptr3
  339. (PyObject *o, char *fmt, void *arg1, void * arg2, void *arg3)
  340. {
  341. return PyArg_ParseTuple (o, fmt, arg1, arg2, arg3);
  342. }
  343. int
  344. ada_py_arg_parsetuple_ptr4
  345. (PyObject *o, char *fmt, void *arg1, void * arg2, void *arg3, void *arg4)
  346. {
  347. return PyArg_ParseTuple (o, fmt, arg1, arg2, arg3, arg4);
  348. }
  349. int
  350. ada_py_arg_parsetuple_ptr5
  351. (PyObject *o, char *fmt,
  352. void *arg1, void * arg2, void *arg3, void *arg4, void *arg5)
  353. {
  354. return PyArg_ParseTuple (o, fmt, arg1, arg2, arg3, arg4, arg5);
  355. }
  356. extern int gnat_argc;
  357. extern char **gnat_argv;
  358. int
  359. ada_py_main ()
  360. {
  361. return Py_Main (gnat_argc, gnat_argv);
  362. }
  363. PyObject*
  364. ada_type_new (PyTypeObject* meta, char* name, PyObject* bases, PyObject* dict)
  365. {
  366. PyTypeObject* m = meta;
  367. PyObject *args, *kwargs, *b=NULL;
  368. PyObject* result;
  369. PyObject* str;
  370. if (dict == NULL) {
  371. printf ("ada_type_new requires a non-null dict\n");
  372. return NULL;
  373. }
  374. if (meta == NULL) {
  375. m = &PyType_Type;
  376. }
  377. /* Construct the parameter list. Do not use keyword arguments, since the
  378. __init__ of the builtin types do not accept them, and tp_new will try to
  379. call __init__, resulting in an error
  380. */
  381. args = PyTuple_New (3);
  382. kwargs = PyDict_New ();
  383. str = PyString_FromString (name);
  384. PyTuple_SET_ITEM (args, 0, str); /* steal reference to str */
  385. if (bases == NULL) {
  386. b = PyTuple_New (0);
  387. PyTuple_SET_ITEM (args, 1, b); /* steal ref to b */
  388. } else {
  389. PyTuple_SetItem (args, 1, bases); /* increase refcount for bases */
  390. }
  391. PyTuple_SetItem (args, 2, dict); /* increase refcount for dict */
  392. result = PyType_Type.tp_new (m, args, kwargs);
  393. Py_XDECREF (args);
  394. Py_XDECREF (kwargs);
  395. return result;
  396. }
  397. int
  398. ada_pydescr_newGetSet (PyTypeObject* type,
  399. char* name,
  400. setter set,
  401. getter get,
  402. char* doc,
  403. void* closure)
  404. {
  405. struct PyGetSetDef *descr =
  406. (struct PyGetSetDef*)malloc (sizeof (struct PyGetSetDef));
  407. PyObject* prop;
  408. descr->name = name;
  409. descr->get = get;
  410. descr->set = set;
  411. descr->doc = doc;
  412. descr->closure = closure;
  413. prop = PyDescr_NewGetSet (type, descr);
  414. if (prop == NULL) {
  415. return 0;
  416. } else {
  417. PyObject_SetAttrString ((PyObject*)type, name, prop);
  418. return 1;
  419. }
  420. }