Debian Codegraph @

pypy/module/cpyext/test/nanobind1.c

raw symbols SCIP
1 #include <Python.h>
2 #include <structmember.h>
3
4 // ----------------------------------------------------
5 // Part 1: Reproducer of reference counting issue
6 // https://foss.heptapod.net/pypy/pypy/-/issues/3844
7 // ----------------------------------------------------
8
9 PyObject *heap_type_new(PyTypeObject *tp, PyObject *args,
10 PyObject *kwds) {
11
12 PyObject *obj = PyObject_Malloc(sizeof(PyObject));
13 memset(obj, 0, sizeof(PyObject));
14 return PyObject_Init(obj, tp);
15 }
16
17 void heap_type_dealloc(PyObject *self) {
18 PyTypeObject *tp = Py_TYPE(self);
19 tp->tp_free(self);
20
21 #if PY_VERSION_HEX > 0x03080000
22 Py_DECREF(tp);
23 #endif
24 }
25
26 static PyType_Slot heap_type_slots[] = {
27 { Py_tp_new, heap_type_new },
28 { Py_tp_dealloc, heap_type_dealloc },
29 { 0, NULL }
30 };
31
32 static PyType_Spec heap_type_spec = {
33 .name = "nanobind1.heap_type",
34 .flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HEAPTYPE,
35 .slots = heap_type_slots,
36 .basicsize = (int) sizeof(PyObject),
37 .itemsize = 0
38 };
39
40 #if PY_VERSION_HEX >= 0x03090000
41 // ----------------------------------------------------
42 // Part 2: Reproducer of vector call issue #1
43 // https://foss.heptapod.net/pypy/pypy/-/issues/3845
44 // ----------------------------------------------------
45
46 typedef struct {
47 PyObject_HEAD
48 PyObject* (*vectorcall)(PyObject *, PyObject * const*, size_t, PyObject *);
49 } callable;
50
51 PyObject* my_vectorcall(PyObject *self, PyObject * const* args, size_t nargs,
52 PyObject *kwnames) {
53 return PyLong_FromLong(1234);
54 }
55
56 static struct PyMemberDef callable_members[] = {
57 // Supported starting with Python 3.9
58 { "__vectorcalloffset__", T_PYSSIZET,
59 (Py_ssize_t) offsetof(callable, vectorcall), READONLY, NULL },
60 { NULL, 0, 0, 0, NULL }
61 };
62
63 int callable_init(PyObject *self, PyObject *args, PyObject *kwds) {
64 ((callable *) self)->vectorcall = my_vectorcall;
65 return 0;
66 }
67
68 static PyType_Slot callable_slots[] = {
69 { Py_tp_init, (void *) callable_init },
70 { Py_tp_members, (void *) callable_members },
71 { Py_tp_call, (void *) PyVectorcall_Call },
72 { 0, NULL }
73 };
74
75 static PyType_Spec callable_spec = {
76 .name = "nanobind1.callable",
77 .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_VECTORCALL,
78 .slots = callable_slots,
79 .basicsize = (int) sizeof(callable),
80 .itemsize = 0
81 };
82
83 // ----------------------------------------------------
84 // Part #3: Reproducer of vector call issue #2
85 // https://foss.heptapod.net/pypy/pypy/-/issues/3845
86 // ----------------------------------------------------
87
88 static PyObject* call(PyObject* self, PyObject* arg) {
89 PyObject *value = PyLong_FromLong(1234);
90 if (!value)
91 return NULL;
92
93 PyObject *args[2] = { NULL, value };
94 size_t nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET;
95
96 PyObject *result = PyObject_Vectorcall(
97 arg, args + 1, nargsf, NULL
98 );
99
100 Py_DECREF(value);
101
102 return result;
103 }
104
105 struct PyMethodDef nanobind1_methods[] = {
106 { "call", (PyCFunction) call, METH_O, NULL },
107 { NULL, NULL, 0, NULL},
108 };
109 #else
110 struct PyMethodDef nanobind1_methods[] = {
111 { NULL, NULL, 0, NULL},
112 };
113 #endif
114
115 // ----------------------------------------------------
116 // Part #4: Reproducer of extended type object issue
117 // https://foss.heptapod.net/pypy/pypy/-/issues/3847
118 // ----------------------------------------------------
119
120 typedef struct {
121 PyHeapTypeObject ht;
122 uint8_t extra[2048];
123 } ExtendedType;
124
125 int metaclass_init(PyObject *self, PyObject *args, PyObject *kwds) {
126 int rv = PyType_Type.tp_init(self, args, kwds);
127 // printf("Got to metaclass_init.\n");
128 if (rv == 0)
129 memset(((ExtendedType *) self)->extra, 0, 2048);
130 return rv;
131 }
132
133 static PyType_Slot metaclass_slots[] = {
134 { Py_tp_init, (void *) metaclass_init },
135 { Py_tp_base, NULL }, /* filled in module init function */
136 { 0, NULL }
137 };
138
139 static PyType_Spec metaclass_spec_bad = {
140 .name = "nanobind1.metaclass_bad",
141 .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
142 .slots = metaclass_slots,
143 .itemsize = (int) sizeof(ExtendedType)
144 };
145
146 static PyType_Spec metaclass_spec_good = {
147 .name = "nanobind1.metaclass_good",
148 .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
149 .slots = metaclass_slots,
150 .basicsize = (int) sizeof(ExtendedType)
151 };
152
153 // ----------------------------------------------------
154
155 static PyModuleDef nanobind1_module = {
156 PyModuleDef_HEAD_INIT,
157 .m_name = "nanobind1",
158 .m_doc = "Reproducer for miscellaneous PyPy issues",
159 .m_size = -1,
160 .m_methods = nanobind1_methods
161 };
162
163 PyMODINIT_FUNC
164 PyInit_nanobind1(void)
165 {
166 /* done here for MSVC */
167 metaclass_slots[1].pfunc = &PyType_Type;
168
169 PyObject *m = PyModule_Create(&nanobind1_module);
170 if (m == NULL)
171 return NULL;
172
173 PyObject *heap_type = PyType_FromSpec(&heap_type_spec);
174
175 if (!heap_type || PyModule_AddObject(m, "heap_type", heap_type) < 0) {
176 Py_XDECREF(heap_type);
177 Py_DECREF(m);
178 return NULL;
179 }
180
181 #if PY_VERSION_HEX >= 0x03090000
182 PyObject *callable = PyType_FromSpec(&callable_spec);
183
184 if (!callable || PyModule_AddObject(m, "callable", callable) < 0) {
185 Py_XDECREF(callable);
186 Py_DECREF(m);
187 return NULL;
188 }
189 #endif
190
191 PyObject *metaclass_good = PyType_FromSpec(&metaclass_spec_good);
192
193 if (!metaclass_good || PyModule_AddObject(m, "metaclass_good", metaclass_good) < 0) {
194 Py_XDECREF(metaclass_good);
195 Py_DECREF(m);
196 return NULL;
197 }
198
199 PyObject *metaclass_bad = PyType_FromSpec(&metaclass_spec_bad);
200
201 if (!metaclass_bad || PyModule_AddObject(m, "metaclass_bad", metaclass_bad) < 0) {
202 Py_XDECREF(metaclass_bad);
203 Py_DECREF(m);
204 return NULL;
205 }
206
207 return m;
208 }
209