CommonStructures.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /////////////// FetchCommonType.proto ///////////////
  2. static PyTypeObject* __Pyx_FetchCommonType(PyTypeObject* type);
  3. /////////////// FetchCommonType ///////////////
  4. static PyTypeObject* __Pyx_FetchCommonType(PyTypeObject* type) {
  5. PyObject* fake_module;
  6. PyTypeObject* cached_type = NULL;
  7. fake_module = PyImport_AddModule((char*) "_cython_" CYTHON_ABI);
  8. if (!fake_module) return NULL;
  9. Py_INCREF(fake_module);
  10. cached_type = (PyTypeObject*) PyObject_GetAttrString(fake_module, type->tp_name);
  11. if (cached_type) {
  12. if (!PyType_Check((PyObject*)cached_type)) {
  13. PyErr_Format(PyExc_TypeError,
  14. "Shared Cython type %.200s is not a type object",
  15. type->tp_name);
  16. goto bad;
  17. }
  18. if (cached_type->tp_basicsize != type->tp_basicsize) {
  19. PyErr_Format(PyExc_TypeError,
  20. "Shared Cython type %.200s has the wrong size, try recompiling",
  21. type->tp_name);
  22. goto bad;
  23. }
  24. } else {
  25. if (!PyErr_ExceptionMatches(PyExc_AttributeError)) goto bad;
  26. PyErr_Clear();
  27. if (PyType_Ready(type) < 0) goto bad;
  28. if (PyObject_SetAttrString(fake_module, type->tp_name, (PyObject*) type) < 0)
  29. goto bad;
  30. Py_INCREF(type);
  31. cached_type = type;
  32. }
  33. done:
  34. Py_DECREF(fake_module);
  35. // NOTE: always returns owned reference, or NULL on error
  36. return cached_type;
  37. bad:
  38. Py_XDECREF(cached_type);
  39. cached_type = NULL;
  40. goto done;
  41. }
  42. /////////////// FetchCommonPointer.proto ///////////////
  43. static void* __Pyx_FetchCommonPointer(void* pointer, const char* name);
  44. /////////////// FetchCommonPointer ///////////////
  45. static void* __Pyx_FetchCommonPointer(void* pointer, const char* name) {
  46. #if PY_VERSION_HEX >= 0x02070000
  47. PyObject* fake_module = NULL;
  48. PyObject* capsule = NULL;
  49. void* value = NULL;
  50. fake_module = PyImport_AddModule((char*) "_cython_" CYTHON_ABI);
  51. if (!fake_module) return NULL;
  52. Py_INCREF(fake_module);
  53. capsule = PyObject_GetAttrString(fake_module, name);
  54. if (!capsule) {
  55. if (!PyErr_ExceptionMatches(PyExc_AttributeError)) goto bad;
  56. PyErr_Clear();
  57. capsule = PyCapsule_New(pointer, name, NULL);
  58. if (!capsule) goto bad;
  59. if (PyObject_SetAttrString(fake_module, name, capsule) < 0)
  60. goto bad;
  61. }
  62. value = PyCapsule_GetPointer(capsule, name);
  63. bad:
  64. Py_XDECREF(capsule);
  65. Py_DECREF(fake_module);
  66. return value;
  67. #else
  68. return pointer;
  69. #endif
  70. }