Debian Codegraph @

pypy/module/cpyext/src/typeobject.c

raw symbols SCIP
1 #include "Python.h"
2
3 PyObject *
4 PyType_FromSpec(PyType_Spec *spec)
5 {
6 return PyType_FromSpecWithBases(spec, NULL);
7 }
8 /*
9 * Mangle to _PyPy_subtype_dealloc for translation.
10 * For tests, we want to mangle as if it were a c-api function so
11 * it will not be confused with the host's similarly named function
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 /* CPYEXT_TESTS */
22 #define _Py_subtype_dealloc _PyPy_subtype_dealloc
23 #endif /* CPYEXT_TESTS */
24 void
25 _Py_subtype_dealloc(PyObject *obj)
26 {
27 PyTypeObject *pto = obj->ob_type;
28 PyTypeObject *base = pto;
29 /* This wrapper is created on a specific type, call it w_A.
30 We wish to call the dealloc function from one of the base classes of w_A,
31 the first of which is not this function itself.
32 w_obj is an instance of w_A or one of its subclasses. So climb up the
33 inheritance chain until base.c_tp_dealloc is exactly this_func, and then
34 continue on up until they differ.
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 /* XXX call tp_del if necessary */
47 base->tp_dealloc(obj);
48 /* XXX cpy decrefs the pto here but we do it in the base-dealloc
49 hopefully this does not clash with the memory model assumed in
50 extension modules */
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 /* type test with subclassing support */
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 /* CPYEXT_TESTS */
88 #define PyType_IsSubtype PyPyType_IsSubtype
89 #endif /* CPYEXT_TESTS */
90 int
91 PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
92 {
93 PyObject *mro;
94
95 mro = a->tp_mro;
96 if (mro != NULL) {
97 /* Deal with multiple inheritance without recursion
98 by walking the MRO tuple */
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 /* a is not completely initialized yet; follow tp_base */
110 return type_is_subtype_base_chain(a, b);
111 }
112
113 /* Get the module of the first superclass where the module has the
114 * given PyModuleDef.
115 * Implemented by walking the MRO, is relatively slow.
116 */
117 PyObject *
118 PyType_GetModuleByDef(PyTypeObject *type, struct PyModuleDef *def)
119 {
120 assert(PyType_Check(type));
121 PyObject *mro = type->tp_mro;
122 // The type must be ready
123 assert(mro != NULL);
124 assert(PyTuple_Check(mro));
125 // mro_invoke() ensures that the type MRO cannot be empty, so we don't have
126 // to check i < PyTuple_GET_SIZE(mro) at the first loop iteration.
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 // Static types in the MRO need to be skipped
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 }