| 1 |
#include "Python.h" |
| 2 |
|
| 3 |
PyObject * |
| 4 |
PyType_FromSpec(PyType_Spec *spec) |
| 5 |
{ |
| 6 |
return PyType_FromSpecWithBases(spec, NULL); |
| 7 |
} |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#ifdef CPYEXT_TESTS |
| 15 |
#define _Py_subtype_dealloc _cpyexttest_subtype_dealloc |
| 16 |
#ifdef __GNUC__ |
| 17 |
__attribute__((visibility("default"))) |
| 18 |
#else |
| 19 |
__declspec(dllexport) |
| 20 |
#endif |
| 21 |
#else |
| 22 |
#define _Py_subtype_dealloc _PyPy_subtype_dealloc |
| 23 |
#endif |
| 24 |
void |
| 25 |
_Py_subtype_dealloc(PyObject *obj) |
| 26 |
{ |
| 27 |
PyTypeObject *pto = obj->ob_type; |
| 28 |
PyTypeObject *base = pto; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
while (base->tp_dealloc != &_Py_subtype_dealloc) |
| 37 |
{ |
| 38 |
base = base->tp_base; |
| 39 |
assert(base); |
| 40 |
} |
| 41 |
while (base->tp_dealloc == &_Py_subtype_dealloc) |
| 42 |
{ |
| 43 |
base = base->tp_base; |
| 44 |
assert(base); |
| 45 |
} |
| 46 |
|
| 47 |
base->tp_dealloc(obj); |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
} |
| 52 |
|
| 53 |
unsigned long |
| 54 |
PyType_GetFlags(PyTypeObject *type) |
| 55 |
{ |
| 56 |
return type->tp_flags; |
| 57 |
} |
| 58 |
|
| 59 |
PyObject * |
| 60 |
PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 61 |
{ |
| 62 |
return type->tp_alloc(type, 0); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
static int |
| 69 |
type_is_subtype_base_chain(PyTypeObject *a, PyTypeObject *b) |
| 70 |
{ |
| 71 |
do { |
| 72 |
if (a == b) |
| 73 |
return 1; |
| 74 |
a = a->tp_base; |
| 75 |
} while (a != NULL); |
| 76 |
|
| 77 |
return (b == &PyBaseObject_Type); |
| 78 |
} |
| 79 |
|
| 80 |
#ifdef CPYEXT_TESTS |
| 81 |
#define PyType_IsSubtype cpyexttestType_IsSubtype |
| 82 |
#ifdef __GNUC__ |
| 83 |
__attribute__((visibility("default"))) |
| 84 |
#else |
| 85 |
__declspec(dllexport) |
| 86 |
#endif |
| 87 |
#else |
| 88 |
#define PyType_IsSubtype PyPyType_IsSubtype |
| 89 |
#endif |
| 90 |
int |
| 91 |
PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b) |
| 92 |
{ |
| 93 |
PyObject *mro; |
| 94 |
|
| 95 |
mro = a->tp_mro; |
| 96 |
if (mro != NULL) { |
| 97 |
|
| 98 |
|
| 99 |
Py_ssize_t i, n; |
| 100 |
assert(PyTuple_Check(mro)); |
| 101 |
n = PyTuple_GET_SIZE(mro); |
| 102 |
for (i = 0; i < n; i++) { |
| 103 |
if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b) |
| 104 |
return 1; |
| 105 |
} |
| 106 |
return 0; |
| 107 |
} |
| 108 |
else |
| 109 |
|
| 110 |
return type_is_subtype_base_chain(a, b); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
PyObject * |
| 118 |
PyType_GetModuleByDef(PyTypeObject *type, struct PyModuleDef *def) |
| 119 |
{ |
| 120 |
assert(PyType_Check(type)); |
| 121 |
PyObject *mro = type->tp_mro; |
| 122 |
|
| 123 |
assert(mro != NULL); |
| 124 |
assert(PyTuple_Check(mro)); |
| 125 |
|
| 126 |
|
| 127 |
assert(PyTuple_GET_SIZE(mro) >= 1); |
| 128 |
Py_ssize_t n = PyTuple_GET_SIZE(mro); |
| 129 |
for (Py_ssize_t i = 0; i < n; i++) { |
| 130 |
PyObject *super = PyTuple_GET_ITEM(mro, i); |
| 131 |
if(!PyType_HasFeature((PyTypeObject *)super, Py_TPFLAGS_HEAPTYPE)) { |
| 132 |
|
| 133 |
continue; |
| 134 |
} |
| 135 |
|
| 136 |
PyHeapTypeObject *ht = (PyHeapTypeObject*)super; |
| 137 |
PyObject *module = ht->ht_module; |
| 138 |
if (module && PyModule_GetDef(module) == def) { |
| 139 |
return module; |
| 140 |
} |
| 141 |
} |
| 142 |
PyErr_Format( |
| 143 |
PyExc_TypeError, |
| 144 |
"PyType_GetModuleByDef: No superclass of '%s' has the given module", |
| 145 |
type->tp_name); |
| 146 |
return NULL; |
| 147 |
} |