ImportExport.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. /////////////// PyIdentifierFromString.proto ///////////////
  2. #if !defined(__Pyx_PyIdentifier_FromString)
  3. #if PY_MAJOR_VERSION < 3
  4. #define __Pyx_PyIdentifier_FromString(s) PyString_FromString(s)
  5. #else
  6. #define __Pyx_PyIdentifier_FromString(s) PyUnicode_FromString(s)
  7. #endif
  8. #endif
  9. /////////////// Import.proto ///////////////
  10. static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, int level); /*proto*/
  11. /////////////// Import ///////////////
  12. //@requires: ObjectHandling.c::PyObjectGetAttrStr
  13. //@substitute: naming
  14. static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, int level) {
  15. PyObject *empty_list = 0;
  16. PyObject *module = 0;
  17. PyObject *global_dict = 0;
  18. PyObject *empty_dict = 0;
  19. PyObject *list;
  20. #if PY_MAJOR_VERSION < 3
  21. PyObject *py_import;
  22. py_import = __Pyx_PyObject_GetAttrStr($builtins_cname, PYIDENT("__import__"));
  23. if (!py_import)
  24. goto bad;
  25. #endif
  26. if (from_list)
  27. list = from_list;
  28. else {
  29. empty_list = PyList_New(0);
  30. if (!empty_list)
  31. goto bad;
  32. list = empty_list;
  33. }
  34. global_dict = PyModule_GetDict($module_cname);
  35. if (!global_dict)
  36. goto bad;
  37. empty_dict = PyDict_New();
  38. if (!empty_dict)
  39. goto bad;
  40. {
  41. #if PY_MAJOR_VERSION >= 3
  42. if (level == -1) {
  43. // Avoid C compiler warning if strchr() evaluates to false at compile time.
  44. if ((1) && (strchr(__Pyx_MODULE_NAME, '.'))) {
  45. /* try package relative import first */
  46. module = PyImport_ImportModuleLevelObject(
  47. name, global_dict, empty_dict, list, 1);
  48. if (!module) {
  49. if (!PyErr_ExceptionMatches(PyExc_ImportError))
  50. goto bad;
  51. PyErr_Clear();
  52. }
  53. }
  54. level = 0; /* try absolute import on failure */
  55. }
  56. #endif
  57. if (!module) {
  58. #if PY_MAJOR_VERSION < 3
  59. PyObject *py_level = PyInt_FromLong(level);
  60. if (!py_level)
  61. goto bad;
  62. module = PyObject_CallFunctionObjArgs(py_import,
  63. name, global_dict, empty_dict, list, py_level, (PyObject *)NULL);
  64. Py_DECREF(py_level);
  65. #else
  66. module = PyImport_ImportModuleLevelObject(
  67. name, global_dict, empty_dict, list, level);
  68. #endif
  69. }
  70. }
  71. bad:
  72. #if PY_MAJOR_VERSION < 3
  73. Py_XDECREF(py_import);
  74. #endif
  75. Py_XDECREF(empty_list);
  76. Py_XDECREF(empty_dict);
  77. return module;
  78. }
  79. /////////////// ImportFrom.proto ///////////////
  80. static PyObject* __Pyx_ImportFrom(PyObject* module, PyObject* name); /*proto*/
  81. /////////////// ImportFrom ///////////////
  82. //@requires: ObjectHandling.c::PyObjectGetAttrStr
  83. static PyObject* __Pyx_ImportFrom(PyObject* module, PyObject* name) {
  84. PyObject* value = __Pyx_PyObject_GetAttrStr(module, name);
  85. if (unlikely(!value) && PyErr_ExceptionMatches(PyExc_AttributeError)) {
  86. PyErr_Format(PyExc_ImportError,
  87. #if PY_MAJOR_VERSION < 3
  88. "cannot import name %.230s", PyString_AS_STRING(name));
  89. #else
  90. "cannot import name %S", name);
  91. #endif
  92. }
  93. return value;
  94. }
  95. /////////////// ImportStar ///////////////
  96. //@substitute: naming
  97. /* import_all_from is an unexposed function from ceval.c */
  98. static int
  99. __Pyx_import_all_from(PyObject *locals, PyObject *v)
  100. {
  101. PyObject *all = PyObject_GetAttrString(v, "__all__");
  102. PyObject *dict, *name, *value;
  103. int skip_leading_underscores = 0;
  104. int pos, err;
  105. if (all == NULL) {
  106. if (!PyErr_ExceptionMatches(PyExc_AttributeError))
  107. return -1; /* Unexpected error */
  108. PyErr_Clear();
  109. dict = PyObject_GetAttrString(v, "__dict__");
  110. if (dict == NULL) {
  111. if (!PyErr_ExceptionMatches(PyExc_AttributeError))
  112. return -1;
  113. PyErr_SetString(PyExc_ImportError,
  114. "from-import-* object has no __dict__ and no __all__");
  115. return -1;
  116. }
  117. #if PY_MAJOR_VERSION < 3
  118. all = PyObject_CallMethod(dict, (char *)"keys", NULL);
  119. #else
  120. all = PyMapping_Keys(dict);
  121. #endif
  122. Py_DECREF(dict);
  123. if (all == NULL)
  124. return -1;
  125. skip_leading_underscores = 1;
  126. }
  127. for (pos = 0, err = 0; ; pos++) {
  128. name = PySequence_GetItem(all, pos);
  129. if (name == NULL) {
  130. if (!PyErr_ExceptionMatches(PyExc_IndexError))
  131. err = -1;
  132. else
  133. PyErr_Clear();
  134. break;
  135. }
  136. if (skip_leading_underscores &&
  137. #if PY_MAJOR_VERSION < 3
  138. likely(PyString_Check(name)) &&
  139. PyString_AS_STRING(name)[0] == '_')
  140. #else
  141. likely(PyUnicode_Check(name)) &&
  142. likely(__Pyx_PyUnicode_GET_LENGTH(name)) &&
  143. __Pyx_PyUnicode_READ_CHAR(name, 0) == '_')
  144. #endif
  145. {
  146. Py_DECREF(name);
  147. continue;
  148. }
  149. value = PyObject_GetAttr(v, name);
  150. if (value == NULL)
  151. err = -1;
  152. else if (PyDict_CheckExact(locals))
  153. err = PyDict_SetItem(locals, name, value);
  154. else
  155. err = PyObject_SetItem(locals, name, value);
  156. Py_DECREF(name);
  157. Py_XDECREF(value);
  158. if (err != 0)
  159. break;
  160. }
  161. Py_DECREF(all);
  162. return err;
  163. }
  164. static int ${import_star}(PyObject* m) {
  165. int i;
  166. int ret = -1;
  167. char* s;
  168. PyObject *locals = 0;
  169. PyObject *list = 0;
  170. #if PY_MAJOR_VERSION >= 3
  171. PyObject *utf8_name = 0;
  172. #endif
  173. PyObject *name;
  174. PyObject *item;
  175. locals = PyDict_New(); if (!locals) goto bad;
  176. if (__Pyx_import_all_from(locals, m) < 0) goto bad;
  177. list = PyDict_Items(locals); if (!list) goto bad;
  178. for(i=0; i<PyList_GET_SIZE(list); i++) {
  179. name = PyTuple_GET_ITEM(PyList_GET_ITEM(list, i), 0);
  180. item = PyTuple_GET_ITEM(PyList_GET_ITEM(list, i), 1);
  181. #if PY_MAJOR_VERSION >= 3
  182. utf8_name = PyUnicode_AsUTF8String(name);
  183. if (!utf8_name) goto bad;
  184. s = PyBytes_AS_STRING(utf8_name);
  185. if (${import_star_set}(item, name, s) < 0) goto bad;
  186. Py_DECREF(utf8_name); utf8_name = 0;
  187. #else
  188. s = PyString_AsString(name);
  189. if (!s) goto bad;
  190. if (${import_star_set}(item, name, s) < 0) goto bad;
  191. #endif
  192. }
  193. ret = 0;
  194. bad:
  195. Py_XDECREF(locals);
  196. Py_XDECREF(list);
  197. #if PY_MAJOR_VERSION >= 3
  198. Py_XDECREF(utf8_name);
  199. #endif
  200. return ret;
  201. }
  202. /////////////// SetPackagePathFromImportLib.proto ///////////////
  203. // PY_VERSION_HEX >= 0x03030000
  204. #if PY_MAJOR_VERSION >= 3 && !CYTHON_PEP489_MULTI_PHASE_INIT
  205. static int __Pyx_SetPackagePathFromImportLib(PyObject *module_name);
  206. #else
  207. #define __Pyx_SetPackagePathFromImportLib(a) 0
  208. #endif
  209. /////////////// SetPackagePathFromImportLib ///////////////
  210. //@requires: ObjectHandling.c::PyObjectGetAttrStr
  211. //@substitute: naming
  212. // PY_VERSION_HEX >= 0x03030000
  213. #if PY_MAJOR_VERSION >= 3 && !CYTHON_PEP489_MULTI_PHASE_INIT
  214. static int __Pyx_SetPackagePathFromImportLib(PyObject *module_name) {
  215. PyObject *importlib, *osmod, *ossep, *parts, *package_path;
  216. PyObject *file_path = NULL;
  217. int result;
  218. PyObject *spec;
  219. // package_path = [importlib.util.find_spec(module_name).origin.rsplit(os.sep, 1)[0]]
  220. importlib = PyImport_ImportModule("importlib.util");
  221. if (unlikely(!importlib))
  222. goto bad;
  223. spec = PyObject_CallMethod(importlib, "find_spec", "(O)", module_name);
  224. Py_DECREF(importlib);
  225. if (unlikely(!spec))
  226. goto bad;
  227. file_path = PyObject_GetAttrString(spec, "origin");
  228. Py_DECREF(spec);
  229. if (unlikely(!file_path))
  230. goto bad;
  231. if (unlikely(PyObject_SetAttrString($module_cname, "__file__", file_path) < 0))
  232. goto bad;
  233. osmod = PyImport_ImportModule("os");
  234. if (unlikely(!osmod))
  235. goto bad;
  236. ossep = PyObject_GetAttrString(osmod, "sep");
  237. Py_DECREF(osmod);
  238. if (unlikely(!ossep))
  239. goto bad;
  240. parts = PyObject_CallMethod(file_path, "rsplit", "(Oi)", ossep, 1);
  241. Py_DECREF(file_path); file_path = NULL;
  242. Py_DECREF(ossep);
  243. if (unlikely(!parts))
  244. goto bad;
  245. package_path = Py_BuildValue("[O]", PyList_GET_ITEM(parts, 0));
  246. Py_DECREF(parts);
  247. if (unlikely(!package_path))
  248. goto bad;
  249. goto set_path;
  250. bad:
  251. PyErr_WriteUnraisable(module_name);
  252. Py_XDECREF(file_path);
  253. // set an empty path list on failure
  254. PyErr_Clear();
  255. package_path = PyList_New(0);
  256. if (unlikely(!package_path))
  257. return -1;
  258. set_path:
  259. result = PyObject_SetAttrString($module_cname, "__path__", package_path);
  260. Py_DECREF(package_path);
  261. return result;
  262. }
  263. #endif
  264. /////////////// TypeImport.proto ///////////////
  265. //@substitute: naming
  266. #ifndef __PYX_HAVE_RT_ImportType_proto_$cyversion
  267. #define __PYX_HAVE_RT_ImportType_proto_$cyversion
  268. #if __STDC_VERSION__ >= 201112L
  269. #include <stdalign.h>
  270. #endif
  271. #if __STDC_VERSION__ >= 201112L || __cplusplus >= 201103L
  272. #define __PYX_GET_STRUCT_ALIGNMENT_$cyversion(s) alignof(s)
  273. #else
  274. // best guess at what the alignment could be since we can't measure it
  275. #define __PYX_GET_STRUCT_ALIGNMENT_$cyversion(s) sizeof(void*)
  276. #endif
  277. enum __Pyx_ImportType_CheckSize_$cyversion {
  278. __Pyx_ImportType_CheckSize_Error_$cyversion = 0,
  279. __Pyx_ImportType_CheckSize_Warn_$cyversion = 1,
  280. __Pyx_ImportType_CheckSize_Ignore_$cyversion = 2
  281. };
  282. static PyTypeObject *__Pyx_ImportType_$cyversion(PyObject* module, const char *module_name, const char *class_name, size_t size, size_t alignment, enum __Pyx_ImportType_CheckSize_$cyversion check_size); /*proto*/
  283. #endif
  284. /////////////// TypeImport ///////////////
  285. //@substitute: naming
  286. #ifndef __PYX_HAVE_RT_ImportType_$cyversion
  287. #define __PYX_HAVE_RT_ImportType_$cyversion
  288. static PyTypeObject *__Pyx_ImportType_$cyversion(PyObject *module, const char *module_name, const char *class_name,
  289. size_t size, size_t alignment, enum __Pyx_ImportType_CheckSize_$cyversion check_size)
  290. {
  291. PyObject *result = 0;
  292. char warning[200];
  293. Py_ssize_t basicsize;
  294. Py_ssize_t itemsize;
  295. #ifdef Py_LIMITED_API
  296. PyObject *py_basicsize;
  297. PyObject *py_itemsize;
  298. #endif
  299. result = PyObject_GetAttrString(module, class_name);
  300. if (!result)
  301. goto bad;
  302. if (!PyType_Check(result)) {
  303. PyErr_Format(PyExc_TypeError,
  304. "%.200s.%.200s is not a type object",
  305. module_name, class_name);
  306. goto bad;
  307. }
  308. #ifndef Py_LIMITED_API
  309. basicsize = ((PyTypeObject *)result)->tp_basicsize;
  310. itemsize = ((PyTypeObject *)result)->tp_itemsize;
  311. #else
  312. py_basicsize = PyObject_GetAttrString(result, "__basicsize__");
  313. if (!py_basicsize)
  314. goto bad;
  315. basicsize = PyLong_AsSsize_t(py_basicsize);
  316. Py_DECREF(py_basicsize);
  317. py_basicsize = 0;
  318. if (basicsize == (Py_ssize_t)-1 && PyErr_Occurred())
  319. goto bad;
  320. py_itemsize = PyObject_GetAttrString(result, "__itemsize__");
  321. if (!py_itemsize)
  322. goto bad;
  323. itemsize = PyLong_AsSsize_t(py_itemsize);
  324. Py_DECREF(py_itemsize);
  325. py_itemsize = 0;
  326. if (itemsize == (Py_ssize_t)-1 && PyErr_Occurred())
  327. goto bad;
  328. #endif
  329. if (itemsize) {
  330. // If itemsize is smaller than the alignment the struct can end up with some extra
  331. // padding at the end. In this case we need to work out the maximum size that
  332. // the padding could be when calculating the range of valid struct sizes.
  333. if (size % alignment) {
  334. // if this is true we've probably calculated the alignment wrongly
  335. // (most likely because alignof isn't available)
  336. alignment = size % alignment;
  337. }
  338. if (itemsize < (Py_ssize_t)alignment)
  339. itemsize = (Py_ssize_t)alignment;
  340. }
  341. if ((size_t)(basicsize + itemsize) < size) {
  342. PyErr_Format(PyExc_ValueError,
  343. "%.200s.%.200s size changed, may indicate binary incompatibility. "
  344. "Expected %zd from C header, got %zd from PyObject",
  345. module_name, class_name, size, basicsize);
  346. goto bad;
  347. }
  348. if (check_size == __Pyx_ImportType_CheckSize_Error_$cyversion && (size_t)basicsize != size) {
  349. PyErr_Format(PyExc_ValueError,
  350. "%.200s.%.200s size changed, may indicate binary incompatibility. "
  351. "Expected %zd from C header, got %zd from PyObject",
  352. module_name, class_name, size, basicsize);
  353. goto bad;
  354. }
  355. else if (check_size == __Pyx_ImportType_CheckSize_Warn_$cyversion && (size_t)basicsize > size) {
  356. PyOS_snprintf(warning, sizeof(warning),
  357. "%s.%s size changed, may indicate binary incompatibility. "
  358. "Expected %zd from C header, got %zd from PyObject",
  359. module_name, class_name, size, basicsize);
  360. if (PyErr_WarnEx(NULL, warning, 0) < 0) goto bad;
  361. }
  362. /* check_size == __Pyx_ImportType_CheckSize_Ignore does not warn nor error */
  363. return (PyTypeObject *)result;
  364. bad:
  365. Py_XDECREF(result);
  366. return NULL;
  367. }
  368. #endif
  369. /////////////// FunctionImport.proto ///////////////
  370. //@substitute: naming
  371. static int __Pyx_ImportFunction_$cyversion(PyObject *module, const char *funcname, void (**f)(void), const char *sig); /*proto*/
  372. /////////////// FunctionImport ///////////////
  373. //@substitute: naming
  374. #ifndef __PYX_HAVE_RT_ImportFunction_$cyversion
  375. #define __PYX_HAVE_RT_ImportFunction_$cyversion
  376. static int __Pyx_ImportFunction_$cyversion(PyObject *module, const char *funcname, void (**f)(void), const char *sig) {
  377. PyObject *d = 0;
  378. PyObject *cobj = 0;
  379. union {
  380. void (*fp)(void);
  381. void *p;
  382. } tmp;
  383. d = PyObject_GetAttrString(module, (char *)"$api_name");
  384. if (!d)
  385. goto bad;
  386. cobj = PyDict_GetItemString(d, funcname);
  387. if (!cobj) {
  388. PyErr_Format(PyExc_ImportError,
  389. "%.200s does not export expected C function %.200s",
  390. PyModule_GetName(module), funcname);
  391. goto bad;
  392. }
  393. #if PY_VERSION_HEX >= 0x02070000
  394. if (!PyCapsule_IsValid(cobj, sig)) {
  395. PyErr_Format(PyExc_TypeError,
  396. "C function %.200s.%.200s has wrong signature (expected %.500s, got %.500s)",
  397. PyModule_GetName(module), funcname, sig, PyCapsule_GetName(cobj));
  398. goto bad;
  399. }
  400. tmp.p = PyCapsule_GetPointer(cobj, sig);
  401. #else
  402. {const char *desc, *s1, *s2;
  403. desc = (const char *)PyCObject_GetDesc(cobj);
  404. if (!desc)
  405. goto bad;
  406. s1 = desc; s2 = sig;
  407. while (*s1 != '\0' && *s1 == *s2) { s1++; s2++; }
  408. if (*s1 != *s2) {
  409. PyErr_Format(PyExc_TypeError,
  410. "C function %.200s.%.200s has wrong signature (expected %.500s, got %.500s)",
  411. PyModule_GetName(module), funcname, sig, desc);
  412. goto bad;
  413. }
  414. tmp.p = PyCObject_AsVoidPtr(cobj);}
  415. #endif
  416. *f = tmp.fp;
  417. if (!(*f))
  418. goto bad;
  419. Py_DECREF(d);
  420. return 0;
  421. bad:
  422. Py_XDECREF(d);
  423. return -1;
  424. }
  425. #endif
  426. /////////////// FunctionExport.proto ///////////////
  427. static int __Pyx_ExportFunction(const char *name, void (*f)(void), const char *sig); /*proto*/
  428. /////////////// FunctionExport ///////////////
  429. //@substitute: naming
  430. static int __Pyx_ExportFunction(const char *name, void (*f)(void), const char *sig) {
  431. PyObject *d = 0;
  432. PyObject *cobj = 0;
  433. union {
  434. void (*fp)(void);
  435. void *p;
  436. } tmp;
  437. d = PyObject_GetAttrString($module_cname, (char *)"$api_name");
  438. if (!d) {
  439. PyErr_Clear();
  440. d = PyDict_New();
  441. if (!d)
  442. goto bad;
  443. Py_INCREF(d);
  444. if (PyModule_AddObject($module_cname, (char *)"$api_name", d) < 0)
  445. goto bad;
  446. }
  447. tmp.fp = f;
  448. #if PY_VERSION_HEX >= 0x02070000
  449. cobj = PyCapsule_New(tmp.p, sig, 0);
  450. #else
  451. cobj = PyCObject_FromVoidPtrAndDesc(tmp.p, (void *)sig, 0);
  452. #endif
  453. if (!cobj)
  454. goto bad;
  455. if (PyDict_SetItemString(d, name, cobj) < 0)
  456. goto bad;
  457. Py_DECREF(cobj);
  458. Py_DECREF(d);
  459. return 0;
  460. bad:
  461. Py_XDECREF(cobj);
  462. Py_XDECREF(d);
  463. return -1;
  464. }
  465. /////////////// VoidPtrImport.proto ///////////////
  466. //@substitute: naming
  467. static int __Pyx_ImportVoidPtr_$cyversion(PyObject *module, const char *name, void **p, const char *sig); /*proto*/
  468. /////////////// VoidPtrImport ///////////////
  469. //@substitute: naming
  470. #ifndef __PYX_HAVE_RT_ImportVoidPtr_$cyversion
  471. #define __PYX_HAVE_RT_ImportVoidPtr_$cyversion
  472. static int __Pyx_ImportVoidPtr_$cyversion(PyObject *module, const char *name, void **p, const char *sig) {
  473. PyObject *d = 0;
  474. PyObject *cobj = 0;
  475. d = PyObject_GetAttrString(module, (char *)"$api_name");
  476. if (!d)
  477. goto bad;
  478. cobj = PyDict_GetItemString(d, name);
  479. if (!cobj) {
  480. PyErr_Format(PyExc_ImportError,
  481. "%.200s does not export expected C variable %.200s",
  482. PyModule_GetName(module), name);
  483. goto bad;
  484. }
  485. #if PY_VERSION_HEX >= 0x02070000
  486. if (!PyCapsule_IsValid(cobj, sig)) {
  487. PyErr_Format(PyExc_TypeError,
  488. "C variable %.200s.%.200s has wrong signature (expected %.500s, got %.500s)",
  489. PyModule_GetName(module), name, sig, PyCapsule_GetName(cobj));
  490. goto bad;
  491. }
  492. *p = PyCapsule_GetPointer(cobj, sig);
  493. #else
  494. {const char *desc, *s1, *s2;
  495. desc = (const char *)PyCObject_GetDesc(cobj);
  496. if (!desc)
  497. goto bad;
  498. s1 = desc; s2 = sig;
  499. while (*s1 != '\0' && *s1 == *s2) { s1++; s2++; }
  500. if (*s1 != *s2) {
  501. PyErr_Format(PyExc_TypeError,
  502. "C variable %.200s.%.200s has wrong signature (expected %.500s, got %.500s)",
  503. PyModule_GetName(module), name, sig, desc);
  504. goto bad;
  505. }
  506. *p = PyCObject_AsVoidPtr(cobj);}
  507. #endif
  508. if (!(*p))
  509. goto bad;
  510. Py_DECREF(d);
  511. return 0;
  512. bad:
  513. Py_XDECREF(d);
  514. return -1;
  515. }
  516. #endif
  517. /////////////// VoidPtrExport.proto ///////////////
  518. static int __Pyx_ExportVoidPtr(PyObject *name, void *p, const char *sig); /*proto*/
  519. /////////////// VoidPtrExport ///////////////
  520. //@substitute: naming
  521. //@requires: ObjectHandling.c::PyObjectSetAttrStr
  522. static int __Pyx_ExportVoidPtr(PyObject *name, void *p, const char *sig) {
  523. PyObject *d;
  524. PyObject *cobj = 0;
  525. d = PyDict_GetItem($moddict_cname, PYIDENT("$api_name"));
  526. Py_XINCREF(d);
  527. if (!d) {
  528. d = PyDict_New();
  529. if (!d)
  530. goto bad;
  531. if (__Pyx_PyObject_SetAttrStr($module_cname, PYIDENT("$api_name"), d) < 0)
  532. goto bad;
  533. }
  534. #if PY_VERSION_HEX >= 0x02070000
  535. cobj = PyCapsule_New(p, sig, 0);
  536. #else
  537. cobj = PyCObject_FromVoidPtrAndDesc(p, (void *)sig, 0);
  538. #endif
  539. if (!cobj)
  540. goto bad;
  541. if (PyDict_SetItem(d, name, cobj) < 0)
  542. goto bad;
  543. Py_DECREF(cobj);
  544. Py_DECREF(d);
  545. return 0;
  546. bad:
  547. Py_XDECREF(cobj);
  548. Py_XDECREF(d);
  549. return -1;
  550. }
  551. /////////////// SetVTable.proto ///////////////
  552. static int __Pyx_SetVtable(PyObject *dict, void *vtable); /*proto*/
  553. /////////////// SetVTable ///////////////
  554. static int __Pyx_SetVtable(PyObject *dict, void *vtable) {
  555. #if PY_VERSION_HEX >= 0x02070000
  556. PyObject *ob = PyCapsule_New(vtable, 0, 0);
  557. #else
  558. PyObject *ob = PyCObject_FromVoidPtr(vtable, 0);
  559. #endif
  560. if (!ob)
  561. goto bad;
  562. if (PyDict_SetItem(dict, PYIDENT("__pyx_vtable__"), ob) < 0)
  563. goto bad;
  564. Py_DECREF(ob);
  565. return 0;
  566. bad:
  567. Py_XDECREF(ob);
  568. return -1;
  569. }
  570. /////////////// GetVTable.proto ///////////////
  571. static void* __Pyx_GetVtable(PyObject *dict); /*proto*/
  572. /////////////// GetVTable ///////////////
  573. static void* __Pyx_GetVtable(PyObject *dict) {
  574. void* ptr;
  575. PyObject *ob = PyObject_GetItem(dict, PYIDENT("__pyx_vtable__"));
  576. if (!ob)
  577. goto bad;
  578. #if PY_VERSION_HEX >= 0x02070000
  579. ptr = PyCapsule_GetPointer(ob, 0);
  580. #else
  581. ptr = PyCObject_AsVoidPtr(ob);
  582. #endif
  583. if (!ptr && !PyErr_Occurred())
  584. PyErr_SetString(PyExc_RuntimeError, "invalid vtable found for imported type");
  585. Py_DECREF(ob);
  586. return ptr;
  587. bad:
  588. Py_XDECREF(ob);
  589. return NULL;
  590. }
  591. /////////////// MergeVTables.proto ///////////////
  592. //@requires: GetVTable
  593. static int __Pyx_MergeVtables(PyTypeObject *type); /*proto*/
  594. /////////////// MergeVTables ///////////////
  595. static int __Pyx_MergeVtables(PyTypeObject *type) {
  596. int i;
  597. void** base_vtables;
  598. void* unknown = (void*)-1;
  599. PyObject* bases = type->tp_bases;
  600. int base_depth = 0;
  601. {
  602. PyTypeObject* base = type->tp_base;
  603. while (base) {
  604. base_depth += 1;
  605. base = base->tp_base;
  606. }
  607. }
  608. base_vtables = (void**) malloc(sizeof(void*) * (size_t)(base_depth + 1));
  609. base_vtables[0] = unknown;
  610. // Could do MRO resolution of individual methods in the future, assuming
  611. // compatible vtables, but for now simply require a common vtable base.
  612. // Note that if the vtables of various bases are extended separately,
  613. // resolution isn't possible and we must reject it just as when the
  614. // instance struct is so extended. (It would be good to also do this
  615. // check when a multiple-base class is created in pure Python as well.)
  616. for (i = 1; i < PyTuple_GET_SIZE(bases); i++) {
  617. void* base_vtable = __Pyx_GetVtable(((PyTypeObject*)PyTuple_GET_ITEM(bases, i))->tp_dict);
  618. if (base_vtable != NULL) {
  619. int j;
  620. PyTypeObject* base = type->tp_base;
  621. for (j = 0; j < base_depth; j++) {
  622. if (base_vtables[j] == unknown) {
  623. base_vtables[j] = __Pyx_GetVtable(base->tp_dict);
  624. base_vtables[j + 1] = unknown;
  625. }
  626. if (base_vtables[j] == base_vtable) {
  627. break;
  628. } else if (base_vtables[j] == NULL) {
  629. // No more potential matching bases (with vtables).
  630. goto bad;
  631. }
  632. base = base->tp_base;
  633. }
  634. }
  635. }
  636. PyErr_Clear();
  637. free(base_vtables);
  638. return 0;
  639. bad:
  640. PyErr_Format(
  641. PyExc_TypeError,
  642. "multiple bases have vtable conflict: '%s' and '%s'",
  643. type->tp_base->tp_name, ((PyTypeObject*)PyTuple_GET_ITEM(bases, i))->tp_name);
  644. free(base_vtables);
  645. return -1;
  646. }
  647. /////////////// ImportNumPyArray.proto ///////////////
  648. static PyObject *__pyx_numpy_ndarray = NULL;
  649. static PyObject* __Pyx_ImportNumPyArrayTypeIfAvailable(void); /*proto*/
  650. /////////////// ImportNumPyArray.cleanup ///////////////
  651. Py_CLEAR(__pyx_numpy_ndarray);
  652. /////////////// ImportNumPyArray ///////////////
  653. //@requires: ImportExport.c::Import
  654. static PyObject* __Pyx__ImportNumPyArray(void) {
  655. PyObject *numpy_module, *ndarray_object = NULL;
  656. numpy_module = __Pyx_Import(PYIDENT("numpy"), NULL, 0);
  657. if (likely(numpy_module)) {
  658. ndarray_object = PyObject_GetAttrString(numpy_module, "ndarray");
  659. Py_DECREF(numpy_module);
  660. }
  661. if (unlikely(!ndarray_object)) {
  662. // ImportError, AttributeError, ...
  663. PyErr_Clear();
  664. }
  665. if (unlikely(!ndarray_object || !PyObject_TypeCheck(ndarray_object, &PyType_Type))) {
  666. Py_XDECREF(ndarray_object);
  667. Py_INCREF(Py_None);
  668. ndarray_object = Py_None;
  669. }
  670. return ndarray_object;
  671. }
  672. static CYTHON_INLINE PyObject* __Pyx_ImportNumPyArrayTypeIfAvailable(void) {
  673. if (unlikely(!__pyx_numpy_ndarray)) {
  674. __pyx_numpy_ndarray = __Pyx__ImportNumPyArray();
  675. }
  676. Py_INCREF(__pyx_numpy_ndarray);
  677. return __pyx_numpy_ndarray;
  678. }