ImportExport.c 22 KB

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