ImportExport.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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. }
  343. if (check_size == __Pyx_ImportType_CheckSize_Error_$cyversion && (size_t)basicsize != size) {
  344. }
  345. else if (check_size == __Pyx_ImportType_CheckSize_Warn_$cyversion && (size_t)basicsize > size) {
  346. }
  347. /* check_size == __Pyx_ImportType_CheckSize_Ignore does not warn nor error */
  348. return (PyTypeObject *)result;
  349. bad:
  350. Py_XDECREF(result);
  351. return NULL;
  352. }
  353. #endif
  354. /////////////// FunctionImport.proto ///////////////
  355. //@substitute: naming
  356. static int __Pyx_ImportFunction_$cyversion(PyObject *module, const char *funcname, void (**f)(void), const char *sig); /*proto*/
  357. /////////////// FunctionImport ///////////////
  358. //@substitute: naming
  359. #ifndef __PYX_HAVE_RT_ImportFunction_$cyversion
  360. #define __PYX_HAVE_RT_ImportFunction_$cyversion
  361. static int __Pyx_ImportFunction_$cyversion(PyObject *module, const char *funcname, void (**f)(void), const char *sig) {
  362. PyObject *d = 0;
  363. PyObject *cobj = 0;
  364. union {
  365. void (*fp)(void);
  366. void *p;
  367. } tmp;
  368. d = PyObject_GetAttrString(module, (char *)"$api_name");
  369. if (!d)
  370. goto bad;
  371. cobj = PyDict_GetItemString(d, funcname);
  372. if (!cobj) {
  373. PyErr_Format(PyExc_ImportError,
  374. "%.200s does not export expected C function %.200s",
  375. PyModule_GetName(module), funcname);
  376. goto bad;
  377. }
  378. #if PY_VERSION_HEX >= 0x02070000
  379. if (!PyCapsule_IsValid(cobj, sig)) {
  380. PyErr_Format(PyExc_TypeError,
  381. "C function %.200s.%.200s has wrong signature (expected %.500s, got %.500s)",
  382. PyModule_GetName(module), funcname, sig, PyCapsule_GetName(cobj));
  383. goto bad;
  384. }
  385. tmp.p = PyCapsule_GetPointer(cobj, sig);
  386. #else
  387. {const char *desc, *s1, *s2;
  388. desc = (const char *)PyCObject_GetDesc(cobj);
  389. if (!desc)
  390. goto bad;
  391. s1 = desc; s2 = sig;
  392. while (*s1 != '\0' && *s1 == *s2) { s1++; s2++; }
  393. if (*s1 != *s2) {
  394. PyErr_Format(PyExc_TypeError,
  395. "C function %.200s.%.200s has wrong signature (expected %.500s, got %.500s)",
  396. PyModule_GetName(module), funcname, sig, desc);
  397. goto bad;
  398. }
  399. tmp.p = PyCObject_AsVoidPtr(cobj);}
  400. #endif
  401. *f = tmp.fp;
  402. if (!(*f))
  403. goto bad;
  404. Py_DECREF(d);
  405. return 0;
  406. bad:
  407. Py_XDECREF(d);
  408. return -1;
  409. }
  410. #endif
  411. /////////////// FunctionExport.proto ///////////////
  412. static int __Pyx_ExportFunction(const char *name, void (*f)(void), const char *sig); /*proto*/
  413. /////////////// FunctionExport ///////////////
  414. //@substitute: naming
  415. static int __Pyx_ExportFunction(const char *name, void (*f)(void), const char *sig) {
  416. PyObject *d = 0;
  417. PyObject *cobj = 0;
  418. union {
  419. void (*fp)(void);
  420. void *p;
  421. } tmp;
  422. d = PyObject_GetAttrString($module_cname, (char *)"$api_name");
  423. if (!d) {
  424. PyErr_Clear();
  425. d = PyDict_New();
  426. if (!d)
  427. goto bad;
  428. Py_INCREF(d);
  429. if (PyModule_AddObject($module_cname, (char *)"$api_name", d) < 0)
  430. goto bad;
  431. }
  432. tmp.fp = f;
  433. #if PY_VERSION_HEX >= 0x02070000
  434. cobj = PyCapsule_New(tmp.p, sig, 0);
  435. #else
  436. cobj = PyCObject_FromVoidPtrAndDesc(tmp.p, (void *)sig, 0);
  437. #endif
  438. if (!cobj)
  439. goto bad;
  440. if (PyDict_SetItemString(d, name, cobj) < 0)
  441. goto bad;
  442. Py_DECREF(cobj);
  443. Py_DECREF(d);
  444. return 0;
  445. bad:
  446. Py_XDECREF(cobj);
  447. Py_XDECREF(d);
  448. return -1;
  449. }
  450. /////////////// VoidPtrImport.proto ///////////////
  451. //@substitute: naming
  452. static int __Pyx_ImportVoidPtr_$cyversion(PyObject *module, const char *name, void **p, const char *sig); /*proto*/
  453. /////////////// VoidPtrImport ///////////////
  454. //@substitute: naming
  455. #ifndef __PYX_HAVE_RT_ImportVoidPtr_$cyversion
  456. #define __PYX_HAVE_RT_ImportVoidPtr_$cyversion
  457. static int __Pyx_ImportVoidPtr_$cyversion(PyObject *module, const char *name, void **p, const char *sig) {
  458. PyObject *d = 0;
  459. PyObject *cobj = 0;
  460. d = PyObject_GetAttrString(module, (char *)"$api_name");
  461. if (!d)
  462. goto bad;
  463. cobj = PyDict_GetItemString(d, name);
  464. if (!cobj) {
  465. PyErr_Format(PyExc_ImportError,
  466. "%.200s does not export expected C variable %.200s",
  467. PyModule_GetName(module), name);
  468. goto bad;
  469. }
  470. #if PY_VERSION_HEX >= 0x02070000
  471. if (!PyCapsule_IsValid(cobj, sig)) {
  472. PyErr_Format(PyExc_TypeError,
  473. "C variable %.200s.%.200s has wrong signature (expected %.500s, got %.500s)",
  474. PyModule_GetName(module), name, sig, PyCapsule_GetName(cobj));
  475. goto bad;
  476. }
  477. *p = PyCapsule_GetPointer(cobj, sig);
  478. #else
  479. {const char *desc, *s1, *s2;
  480. desc = (const char *)PyCObject_GetDesc(cobj);
  481. if (!desc)
  482. goto bad;
  483. s1 = desc; s2 = sig;
  484. while (*s1 != '\0' && *s1 == *s2) { s1++; s2++; }
  485. if (*s1 != *s2) {
  486. PyErr_Format(PyExc_TypeError,
  487. "C variable %.200s.%.200s has wrong signature (expected %.500s, got %.500s)",
  488. PyModule_GetName(module), name, sig, desc);
  489. goto bad;
  490. }
  491. *p = PyCObject_AsVoidPtr(cobj);}
  492. #endif
  493. if (!(*p))
  494. goto bad;
  495. Py_DECREF(d);
  496. return 0;
  497. bad:
  498. Py_XDECREF(d);
  499. return -1;
  500. }
  501. #endif
  502. /////////////// VoidPtrExport.proto ///////////////
  503. static int __Pyx_ExportVoidPtr(PyObject *name, void *p, const char *sig); /*proto*/
  504. /////////////// VoidPtrExport ///////////////
  505. //@substitute: naming
  506. //@requires: ObjectHandling.c::PyObjectSetAttrStr
  507. static int __Pyx_ExportVoidPtr(PyObject *name, void *p, const char *sig) {
  508. PyObject *d;
  509. PyObject *cobj = 0;
  510. d = PyDict_GetItem($moddict_cname, PYIDENT("$api_name"));
  511. Py_XINCREF(d);
  512. if (!d) {
  513. d = PyDict_New();
  514. if (!d)
  515. goto bad;
  516. if (__Pyx_PyObject_SetAttrStr($module_cname, PYIDENT("$api_name"), d) < 0)
  517. goto bad;
  518. }
  519. #if PY_VERSION_HEX >= 0x02070000
  520. cobj = PyCapsule_New(p, sig, 0);
  521. #else
  522. cobj = PyCObject_FromVoidPtrAndDesc(p, (void *)sig, 0);
  523. #endif
  524. if (!cobj)
  525. goto bad;
  526. if (PyDict_SetItem(d, name, cobj) < 0)
  527. goto bad;
  528. Py_DECREF(cobj);
  529. Py_DECREF(d);
  530. return 0;
  531. bad:
  532. Py_XDECREF(cobj);
  533. Py_XDECREF(d);
  534. return -1;
  535. }
  536. /////////////// SetVTable.proto ///////////////
  537. static int __Pyx_SetVtable(PyObject *dict, void *vtable); /*proto*/
  538. /////////////// SetVTable ///////////////
  539. static int __Pyx_SetVtable(PyObject *dict, void *vtable) {
  540. #if PY_VERSION_HEX >= 0x02070000
  541. PyObject *ob = PyCapsule_New(vtable, 0, 0);
  542. #else
  543. PyObject *ob = PyCObject_FromVoidPtr(vtable, 0);
  544. #endif
  545. if (!ob)
  546. goto bad;
  547. if (PyDict_SetItem(dict, PYIDENT("__pyx_vtable__"), ob) < 0)
  548. goto bad;
  549. Py_DECREF(ob);
  550. return 0;
  551. bad:
  552. Py_XDECREF(ob);
  553. return -1;
  554. }
  555. /////////////// GetVTable.proto ///////////////
  556. static void* __Pyx_GetVtable(PyObject *dict); /*proto*/
  557. /////////////// GetVTable ///////////////
  558. static void* __Pyx_GetVtable(PyObject *dict) {
  559. void* ptr;
  560. PyObject *ob = PyObject_GetItem(dict, PYIDENT("__pyx_vtable__"));
  561. if (!ob)
  562. goto bad;
  563. #if PY_VERSION_HEX >= 0x02070000
  564. ptr = PyCapsule_GetPointer(ob, 0);
  565. #else
  566. ptr = PyCObject_AsVoidPtr(ob);
  567. #endif
  568. if (!ptr && !PyErr_Occurred())
  569. PyErr_SetString(PyExc_RuntimeError, "invalid vtable found for imported type");
  570. Py_DECREF(ob);
  571. return ptr;
  572. bad:
  573. Py_XDECREF(ob);
  574. return NULL;
  575. }
  576. /////////////// MergeVTables.proto ///////////////
  577. //@requires: GetVTable
  578. static int __Pyx_MergeVtables(PyTypeObject *type); /*proto*/
  579. /////////////// MergeVTables ///////////////
  580. static int __Pyx_MergeVtables(PyTypeObject *type) {
  581. int i;
  582. void** base_vtables;
  583. void* unknown = (void*)-1;
  584. PyObject* bases = type->tp_bases;
  585. int base_depth = 0;
  586. {
  587. PyTypeObject* base = type->tp_base;
  588. while (base) {
  589. base_depth += 1;
  590. base = base->tp_base;
  591. }
  592. }
  593. base_vtables = (void**) malloc(sizeof(void*) * (size_t)(base_depth + 1));
  594. base_vtables[0] = unknown;
  595. // Could do MRO resolution of individual methods in the future, assuming
  596. // compatible vtables, but for now simply require a common vtable base.
  597. // Note that if the vtables of various bases are extended separately,
  598. // resolution isn't possible and we must reject it just as when the
  599. // instance struct is so extended. (It would be good to also do this
  600. // check when a multiple-base class is created in pure Python as well.)
  601. for (i = 1; i < PyTuple_GET_SIZE(bases); i++) {
  602. void* base_vtable = __Pyx_GetVtable(((PyTypeObject*)PyTuple_GET_ITEM(bases, i))->tp_dict);
  603. if (base_vtable != NULL) {
  604. int j;
  605. PyTypeObject* base = type->tp_base;
  606. for (j = 0; j < base_depth; j++) {
  607. if (base_vtables[j] == unknown) {
  608. base_vtables[j] = __Pyx_GetVtable(base->tp_dict);
  609. base_vtables[j + 1] = unknown;
  610. }
  611. if (base_vtables[j] == base_vtable) {
  612. break;
  613. } else if (base_vtables[j] == NULL) {
  614. // No more potential matching bases (with vtables).
  615. goto bad;
  616. }
  617. base = base->tp_base;
  618. }
  619. }
  620. }
  621. PyErr_Clear();
  622. free(base_vtables);
  623. return 0;
  624. bad:
  625. PyErr_Format(
  626. PyExc_TypeError,
  627. "multiple bases have vtable conflict: '%s' and '%s'",
  628. type->tp_base->tp_name, ((PyTypeObject*)PyTuple_GET_ITEM(bases, i))->tp_name);
  629. free(base_vtables);
  630. return -1;
  631. }
  632. /////////////// ImportNumPyArray.proto ///////////////
  633. static PyObject *__pyx_numpy_ndarray = NULL;
  634. static PyObject* __Pyx_ImportNumPyArrayTypeIfAvailable(void); /*proto*/
  635. /////////////// ImportNumPyArray.cleanup ///////////////
  636. Py_CLEAR(__pyx_numpy_ndarray);
  637. /////////////// ImportNumPyArray ///////////////
  638. //@requires: ImportExport.c::Import
  639. static PyObject* __Pyx__ImportNumPyArray(void) {
  640. PyObject *numpy_module, *ndarray_object = NULL;
  641. numpy_module = __Pyx_Import(PYIDENT("numpy"), NULL, 0);
  642. if (likely(numpy_module)) {
  643. ndarray_object = PyObject_GetAttrString(numpy_module, "ndarray");
  644. Py_DECREF(numpy_module);
  645. }
  646. if (unlikely(!ndarray_object)) {
  647. // ImportError, AttributeError, ...
  648. PyErr_Clear();
  649. }
  650. if (unlikely(!ndarray_object || !PyObject_TypeCheck(ndarray_object, &PyType_Type))) {
  651. Py_XDECREF(ndarray_object);
  652. Py_INCREF(Py_None);
  653. ndarray_object = Py_None;
  654. }
  655. return ndarray_object;
  656. }
  657. static CYTHON_INLINE PyObject* __Pyx_ImportNumPyArrayTypeIfAvailable(void) {
  658. if (unlikely(!__pyx_numpy_ndarray)) {
  659. __pyx_numpy_ndarray = __Pyx__ImportNumPyArray();
  660. }
  661. Py_INCREF(__pyx_numpy_ndarray);
  662. return __pyx_numpy_ndarray;
  663. }