1 | n/a | |
---|
2 | n/a | /* Testing module for multi-phase initialization of extension modules (PEP 489) |
---|
3 | n/a | */ |
---|
4 | n/a | |
---|
5 | n/a | #include "Python.h" |
---|
6 | n/a | |
---|
7 | n/a | /* Example objects */ |
---|
8 | n/a | typedef struct { |
---|
9 | n/a | PyObject_HEAD |
---|
10 | n/a | PyObject *x_attr; /* Attributes dictionary */ |
---|
11 | n/a | } ExampleObject; |
---|
12 | n/a | |
---|
13 | n/a | /* Example methods */ |
---|
14 | n/a | |
---|
15 | n/a | static int |
---|
16 | n/a | Example_traverse(ExampleObject *self, visitproc visit, void *arg) |
---|
17 | n/a | { |
---|
18 | n/a | Py_VISIT(self->x_attr); |
---|
19 | n/a | return 0; |
---|
20 | n/a | } |
---|
21 | n/a | |
---|
22 | n/a | static int |
---|
23 | n/a | Example_finalize(ExampleObject *self) |
---|
24 | n/a | { |
---|
25 | n/a | Py_CLEAR(self->x_attr); |
---|
26 | n/a | return 0; |
---|
27 | n/a | } |
---|
28 | n/a | |
---|
29 | n/a | static PyObject * |
---|
30 | n/a | Example_demo(ExampleObject *self, PyObject *args) |
---|
31 | n/a | { |
---|
32 | n/a | PyObject *o = NULL; |
---|
33 | n/a | if (!PyArg_ParseTuple(args, "|O:demo", &o)) |
---|
34 | n/a | return NULL; |
---|
35 | n/a | if (o != NULL && PyUnicode_Check(o)) { |
---|
36 | n/a | Py_INCREF(o); |
---|
37 | n/a | return o; |
---|
38 | n/a | } |
---|
39 | n/a | Py_RETURN_NONE; |
---|
40 | n/a | } |
---|
41 | n/a | |
---|
42 | n/a | |
---|
43 | n/a | static PyMethodDef Example_methods[] = { |
---|
44 | n/a | {"demo", (PyCFunction)Example_demo, METH_VARARGS, |
---|
45 | n/a | PyDoc_STR("demo() -> None")}, |
---|
46 | n/a | {NULL, NULL} /* sentinel */ |
---|
47 | n/a | }; |
---|
48 | n/a | |
---|
49 | n/a | static PyObject * |
---|
50 | n/a | Example_getattro(ExampleObject *self, PyObject *name) |
---|
51 | n/a | { |
---|
52 | n/a | if (self->x_attr != NULL) { |
---|
53 | n/a | PyObject *v = PyDict_GetItem(self->x_attr, name); |
---|
54 | n/a | if (v != NULL) { |
---|
55 | n/a | Py_INCREF(v); |
---|
56 | n/a | return v; |
---|
57 | n/a | } |
---|
58 | n/a | } |
---|
59 | n/a | return PyObject_GenericGetAttr((PyObject *)self, name); |
---|
60 | n/a | } |
---|
61 | n/a | |
---|
62 | n/a | static int |
---|
63 | n/a | Example_setattr(ExampleObject *self, const char *name, PyObject *v) |
---|
64 | n/a | { |
---|
65 | n/a | if (self->x_attr == NULL) { |
---|
66 | n/a | self->x_attr = PyDict_New(); |
---|
67 | n/a | if (self->x_attr == NULL) |
---|
68 | n/a | return -1; |
---|
69 | n/a | } |
---|
70 | n/a | if (v == NULL) { |
---|
71 | n/a | int rv = PyDict_DelItemString(self->x_attr, name); |
---|
72 | n/a | if (rv < 0) |
---|
73 | n/a | PyErr_SetString(PyExc_AttributeError, |
---|
74 | n/a | "delete non-existing Example attribute"); |
---|
75 | n/a | return rv; |
---|
76 | n/a | } |
---|
77 | n/a | else |
---|
78 | n/a | return PyDict_SetItemString(self->x_attr, name, v); |
---|
79 | n/a | } |
---|
80 | n/a | |
---|
81 | n/a | static PyType_Slot Example_Type_slots[] = { |
---|
82 | n/a | {Py_tp_doc, "The Example type"}, |
---|
83 | n/a | {Py_tp_finalize, Example_finalize}, |
---|
84 | n/a | {Py_tp_traverse, Example_traverse}, |
---|
85 | n/a | {Py_tp_getattro, Example_getattro}, |
---|
86 | n/a | {Py_tp_setattr, Example_setattr}, |
---|
87 | n/a | {Py_tp_methods, Example_methods}, |
---|
88 | n/a | {0, 0}, |
---|
89 | n/a | }; |
---|
90 | n/a | |
---|
91 | n/a | static PyType_Spec Example_Type_spec = { |
---|
92 | n/a | "_testimportexec.Example", |
---|
93 | n/a | sizeof(ExampleObject), |
---|
94 | n/a | 0, |
---|
95 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, |
---|
96 | n/a | Example_Type_slots |
---|
97 | n/a | }; |
---|
98 | n/a | |
---|
99 | n/a | /* Function of two integers returning integer */ |
---|
100 | n/a | |
---|
101 | n/a | PyDoc_STRVAR(testexport_foo_doc, |
---|
102 | n/a | "foo(i,j)\n\ |
---|
103 | n/a | \n\ |
---|
104 | n/a | Return the sum of i and j."); |
---|
105 | n/a | |
---|
106 | n/a | static PyObject * |
---|
107 | n/a | testexport_foo(PyObject *self, PyObject *args) |
---|
108 | n/a | { |
---|
109 | n/a | long i, j; |
---|
110 | n/a | long res; |
---|
111 | n/a | if (!PyArg_ParseTuple(args, "ll:foo", &i, &j)) |
---|
112 | n/a | return NULL; |
---|
113 | n/a | res = i + j; |
---|
114 | n/a | return PyLong_FromLong(res); |
---|
115 | n/a | } |
---|
116 | n/a | |
---|
117 | n/a | /* Test that PyState registration fails */ |
---|
118 | n/a | |
---|
119 | n/a | PyDoc_STRVAR(call_state_registration_func_doc, |
---|
120 | n/a | "register_state(0): call PyState_FindModule()\n\ |
---|
121 | n/a | register_state(1): call PyState_AddModule()\n\ |
---|
122 | n/a | register_state(2): call PyState_RemoveModule()"); |
---|
123 | n/a | |
---|
124 | n/a | static PyObject * |
---|
125 | n/a | call_state_registration_func(PyObject *mod, PyObject *args) |
---|
126 | n/a | { |
---|
127 | n/a | int i, ret; |
---|
128 | n/a | PyModuleDef *def = PyModule_GetDef(mod); |
---|
129 | n/a | if (def == NULL) { |
---|
130 | n/a | return NULL; |
---|
131 | n/a | } |
---|
132 | n/a | if (!PyArg_ParseTuple(args, "i:call_state_registration_func", &i)) |
---|
133 | n/a | return NULL; |
---|
134 | n/a | switch (i) { |
---|
135 | n/a | case 0: |
---|
136 | n/a | mod = PyState_FindModule(def); |
---|
137 | n/a | if (mod == NULL) { |
---|
138 | n/a | Py_RETURN_NONE; |
---|
139 | n/a | } |
---|
140 | n/a | return mod; |
---|
141 | n/a | case 1: |
---|
142 | n/a | ret = PyState_AddModule(mod, def); |
---|
143 | n/a | if (ret != 0) { |
---|
144 | n/a | return NULL; |
---|
145 | n/a | } |
---|
146 | n/a | break; |
---|
147 | n/a | case 2: |
---|
148 | n/a | ret = PyState_RemoveModule(def); |
---|
149 | n/a | if (ret != 0) { |
---|
150 | n/a | return NULL; |
---|
151 | n/a | } |
---|
152 | n/a | break; |
---|
153 | n/a | } |
---|
154 | n/a | Py_RETURN_NONE; |
---|
155 | n/a | } |
---|
156 | n/a | |
---|
157 | n/a | |
---|
158 | n/a | static PyType_Slot Str_Type_slots[] = { |
---|
159 | n/a | {Py_tp_base, NULL}, /* filled out in module exec function */ |
---|
160 | n/a | {0, 0}, |
---|
161 | n/a | }; |
---|
162 | n/a | |
---|
163 | n/a | static PyType_Spec Str_Type_spec = { |
---|
164 | n/a | "_testimportexec.Str", |
---|
165 | n/a | 0, |
---|
166 | n/a | 0, |
---|
167 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
---|
168 | n/a | Str_Type_slots |
---|
169 | n/a | }; |
---|
170 | n/a | |
---|
171 | n/a | static PyMethodDef testexport_methods[] = { |
---|
172 | n/a | {"foo", testexport_foo, METH_VARARGS, |
---|
173 | n/a | testexport_foo_doc}, |
---|
174 | n/a | {"call_state_registration_func", call_state_registration_func, |
---|
175 | n/a | METH_VARARGS, call_state_registration_func_doc}, |
---|
176 | n/a | {NULL, NULL} /* sentinel */ |
---|
177 | n/a | }; |
---|
178 | n/a | |
---|
179 | n/a | static int execfunc(PyObject *m) |
---|
180 | n/a | { |
---|
181 | n/a | PyObject *temp = NULL; |
---|
182 | n/a | |
---|
183 | n/a | /* Due to cross platform compiler issues the slots must be filled |
---|
184 | n/a | * here. It's required for portability to Windows without requiring |
---|
185 | n/a | * C++. */ |
---|
186 | n/a | Str_Type_slots[0].pfunc = &PyUnicode_Type; |
---|
187 | n/a | |
---|
188 | n/a | /* Add a custom type */ |
---|
189 | n/a | temp = PyType_FromSpec(&Example_Type_spec); |
---|
190 | n/a | if (temp == NULL) |
---|
191 | n/a | goto fail; |
---|
192 | n/a | if (PyModule_AddObject(m, "Example", temp) != 0) |
---|
193 | n/a | goto fail; |
---|
194 | n/a | |
---|
195 | n/a | /* Add an exception type */ |
---|
196 | n/a | temp = PyErr_NewException("_testimportexec.error", NULL, NULL); |
---|
197 | n/a | if (temp == NULL) |
---|
198 | n/a | goto fail; |
---|
199 | n/a | if (PyModule_AddObject(m, "error", temp) != 0) |
---|
200 | n/a | goto fail; |
---|
201 | n/a | |
---|
202 | n/a | /* Add Str */ |
---|
203 | n/a | temp = PyType_FromSpec(&Str_Type_spec); |
---|
204 | n/a | if (temp == NULL) |
---|
205 | n/a | goto fail; |
---|
206 | n/a | if (PyModule_AddObject(m, "Str", temp) != 0) |
---|
207 | n/a | goto fail; |
---|
208 | n/a | |
---|
209 | n/a | if (PyModule_AddIntConstant(m, "int_const", 1969) != 0) |
---|
210 | n/a | goto fail; |
---|
211 | n/a | |
---|
212 | n/a | if (PyModule_AddStringConstant(m, "str_const", "something different") != 0) |
---|
213 | n/a | goto fail; |
---|
214 | n/a | |
---|
215 | n/a | return 0; |
---|
216 | n/a | fail: |
---|
217 | n/a | return -1; |
---|
218 | n/a | } |
---|
219 | n/a | |
---|
220 | n/a | /* Helper for module definitions; there'll be a lot of them */ |
---|
221 | n/a | #define TEST_MODULE_DEF(name, slots, methods) { \ |
---|
222 | n/a | PyModuleDef_HEAD_INIT, /* m_base */ \ |
---|
223 | n/a | name, /* m_name */ \ |
---|
224 | n/a | PyDoc_STR("Test module " name), /* m_doc */ \ |
---|
225 | n/a | 0, /* m_size */ \ |
---|
226 | n/a | methods, /* m_methods */ \ |
---|
227 | n/a | slots, /* m_slots */ \ |
---|
228 | n/a | NULL, /* m_traverse */ \ |
---|
229 | n/a | NULL, /* m_clear */ \ |
---|
230 | n/a | NULL, /* m_free */ \ |
---|
231 | n/a | } |
---|
232 | n/a | |
---|
233 | n/a | PyModuleDef_Slot main_slots[] = { |
---|
234 | n/a | {Py_mod_exec, execfunc}, |
---|
235 | n/a | {0, NULL}, |
---|
236 | n/a | }; |
---|
237 | n/a | |
---|
238 | n/a | static PyModuleDef main_def = TEST_MODULE_DEF("main", main_slots, testexport_methods); |
---|
239 | n/a | |
---|
240 | n/a | PyMODINIT_FUNC |
---|
241 | n/a | PyInit__testmultiphase(PyObject *spec) |
---|
242 | n/a | { |
---|
243 | n/a | return PyModuleDef_Init(&main_def); |
---|
244 | n/a | } |
---|
245 | n/a | |
---|
246 | n/a | |
---|
247 | n/a | /**** Importing a non-module object ****/ |
---|
248 | n/a | |
---|
249 | n/a | static PyModuleDef def_nonmodule; |
---|
250 | n/a | static PyModuleDef def_nonmodule_with_methods; |
---|
251 | n/a | |
---|
252 | n/a | /* Create a SimpleNamespace(three=3) */ |
---|
253 | n/a | static PyObject* |
---|
254 | n/a | createfunc_nonmodule(PyObject *spec, PyModuleDef *def) |
---|
255 | n/a | { |
---|
256 | n/a | PyObject *dct, *ns, *three; |
---|
257 | n/a | |
---|
258 | n/a | if (def != &def_nonmodule && def != &def_nonmodule_with_methods) { |
---|
259 | n/a | PyErr_SetString(PyExc_SystemError, "def does not match"); |
---|
260 | n/a | return NULL; |
---|
261 | n/a | } |
---|
262 | n/a | |
---|
263 | n/a | dct = PyDict_New(); |
---|
264 | n/a | if (dct == NULL) |
---|
265 | n/a | return NULL; |
---|
266 | n/a | |
---|
267 | n/a | three = PyLong_FromLong(3); |
---|
268 | n/a | if (three == NULL) { |
---|
269 | n/a | Py_DECREF(dct); |
---|
270 | n/a | return NULL; |
---|
271 | n/a | } |
---|
272 | n/a | PyDict_SetItemString(dct, "three", three); |
---|
273 | n/a | Py_DECREF(three); |
---|
274 | n/a | |
---|
275 | n/a | ns = _PyNamespace_New(dct); |
---|
276 | n/a | Py_DECREF(dct); |
---|
277 | n/a | return ns; |
---|
278 | n/a | } |
---|
279 | n/a | |
---|
280 | n/a | static PyModuleDef_Slot slots_create_nonmodule[] = { |
---|
281 | n/a | {Py_mod_create, createfunc_nonmodule}, |
---|
282 | n/a | {0, NULL}, |
---|
283 | n/a | }; |
---|
284 | n/a | |
---|
285 | n/a | static PyModuleDef def_nonmodule = TEST_MODULE_DEF( |
---|
286 | n/a | "_testmultiphase_nonmodule", slots_create_nonmodule, NULL); |
---|
287 | n/a | |
---|
288 | n/a | PyMODINIT_FUNC |
---|
289 | n/a | PyInit__testmultiphase_nonmodule(PyObject *spec) |
---|
290 | n/a | { |
---|
291 | n/a | return PyModuleDef_Init(&def_nonmodule); |
---|
292 | n/a | } |
---|
293 | n/a | |
---|
294 | n/a | PyDoc_STRVAR(nonmodule_bar_doc, |
---|
295 | n/a | "bar(i,j)\n\ |
---|
296 | n/a | \n\ |
---|
297 | n/a | Return the difference of i - j."); |
---|
298 | n/a | |
---|
299 | n/a | static PyObject * |
---|
300 | n/a | nonmodule_bar(PyObject *self, PyObject *args) |
---|
301 | n/a | { |
---|
302 | n/a | long i, j; |
---|
303 | n/a | long res; |
---|
304 | n/a | if (!PyArg_ParseTuple(args, "ll:bar", &i, &j)) |
---|
305 | n/a | return NULL; |
---|
306 | n/a | res = i - j; |
---|
307 | n/a | return PyLong_FromLong(res); |
---|
308 | n/a | } |
---|
309 | n/a | |
---|
310 | n/a | static PyMethodDef nonmodule_methods[] = { |
---|
311 | n/a | {"bar", nonmodule_bar, METH_VARARGS, nonmodule_bar_doc}, |
---|
312 | n/a | {NULL, NULL} /* sentinel */ |
---|
313 | n/a | }; |
---|
314 | n/a | |
---|
315 | n/a | static PyModuleDef def_nonmodule_with_methods = TEST_MODULE_DEF( |
---|
316 | n/a | "_testmultiphase_nonmodule_with_methods", slots_create_nonmodule, nonmodule_methods); |
---|
317 | n/a | |
---|
318 | n/a | PyMODINIT_FUNC |
---|
319 | n/a | PyInit__testmultiphase_nonmodule_with_methods(PyObject *spec) |
---|
320 | n/a | { |
---|
321 | n/a | return PyModuleDef_Init(&def_nonmodule_with_methods); |
---|
322 | n/a | } |
---|
323 | n/a | |
---|
324 | n/a | /**** Non-ASCII-named modules ****/ |
---|
325 | n/a | |
---|
326 | n/a | static PyModuleDef def_nonascii_latin = { \ |
---|
327 | n/a | PyModuleDef_HEAD_INIT, /* m_base */ |
---|
328 | n/a | "_testmultiphase_nonascii_latin", /* m_name */ |
---|
329 | n/a | PyDoc_STR("Module named in Czech"), /* m_doc */ |
---|
330 | n/a | 0, /* m_size */ |
---|
331 | n/a | NULL, /* m_methods */ |
---|
332 | n/a | NULL, /* m_slots */ |
---|
333 | n/a | NULL, /* m_traverse */ |
---|
334 | n/a | NULL, /* m_clear */ |
---|
335 | n/a | NULL, /* m_free */ |
---|
336 | n/a | }; |
---|
337 | n/a | |
---|
338 | n/a | PyMODINIT_FUNC |
---|
339 | n/a | PyInitU__testmultiphase_zkouka_naten_evc07gi8e(PyObject *spec) |
---|
340 | n/a | { |
---|
341 | n/a | return PyModuleDef_Init(&def_nonascii_latin); |
---|
342 | n/a | } |
---|
343 | n/a | |
---|
344 | n/a | static PyModuleDef def_nonascii_kana = { \ |
---|
345 | n/a | PyModuleDef_HEAD_INIT, /* m_base */ |
---|
346 | n/a | "_testmultiphase_nonascii_kana", /* m_name */ |
---|
347 | n/a | PyDoc_STR("Module named in Japanese"), /* m_doc */ |
---|
348 | n/a | 0, /* m_size */ |
---|
349 | n/a | NULL, /* m_methods */ |
---|
350 | n/a | NULL, /* m_slots */ |
---|
351 | n/a | NULL, /* m_traverse */ |
---|
352 | n/a | NULL, /* m_clear */ |
---|
353 | n/a | NULL, /* m_free */ |
---|
354 | n/a | }; |
---|
355 | n/a | |
---|
356 | n/a | PyMODINIT_FUNC |
---|
357 | n/a | PyInitU_eckzbwbhc6jpgzcx415x(PyObject *spec) |
---|
358 | n/a | { |
---|
359 | n/a | return PyModuleDef_Init(&def_nonascii_kana); |
---|
360 | n/a | } |
---|
361 | n/a | |
---|
362 | n/a | /*** Module with a single-character name ***/ |
---|
363 | n/a | |
---|
364 | n/a | PyMODINIT_FUNC |
---|
365 | n/a | PyInit_x(PyObject *spec) |
---|
366 | n/a | { |
---|
367 | n/a | return PyModuleDef_Init(&main_def); |
---|
368 | n/a | } |
---|
369 | n/a | |
---|
370 | n/a | /**** Testing NULL slots ****/ |
---|
371 | n/a | |
---|
372 | n/a | static PyModuleDef null_slots_def = TEST_MODULE_DEF( |
---|
373 | n/a | "_testmultiphase_null_slots", NULL, NULL); |
---|
374 | n/a | |
---|
375 | n/a | PyMODINIT_FUNC |
---|
376 | n/a | PyInit__testmultiphase_null_slots(PyObject *spec) |
---|
377 | n/a | { |
---|
378 | n/a | return PyModuleDef_Init(&null_slots_def); |
---|
379 | n/a | } |
---|
380 | n/a | |
---|
381 | n/a | /**** Problematic modules ****/ |
---|
382 | n/a | |
---|
383 | n/a | static PyModuleDef_Slot slots_bad_large[] = { |
---|
384 | n/a | {_Py_mod_LAST_SLOT + 1, NULL}, |
---|
385 | n/a | {0, NULL}, |
---|
386 | n/a | }; |
---|
387 | n/a | |
---|
388 | n/a | static PyModuleDef def_bad_large = TEST_MODULE_DEF( |
---|
389 | n/a | "_testmultiphase_bad_slot_large", slots_bad_large, NULL); |
---|
390 | n/a | |
---|
391 | n/a | PyMODINIT_FUNC |
---|
392 | n/a | PyInit__testmultiphase_bad_slot_large(PyObject *spec) |
---|
393 | n/a | { |
---|
394 | n/a | return PyModuleDef_Init(&def_bad_large); |
---|
395 | n/a | } |
---|
396 | n/a | |
---|
397 | n/a | static PyModuleDef_Slot slots_bad_negative[] = { |
---|
398 | n/a | {-1, NULL}, |
---|
399 | n/a | {0, NULL}, |
---|
400 | n/a | }; |
---|
401 | n/a | |
---|
402 | n/a | static PyModuleDef def_bad_negative = TEST_MODULE_DEF( |
---|
403 | n/a | "_testmultiphase_bad_slot_negative", slots_bad_negative, NULL); |
---|
404 | n/a | |
---|
405 | n/a | PyMODINIT_FUNC |
---|
406 | n/a | PyInit__testmultiphase_bad_slot_negative(PyObject *spec) |
---|
407 | n/a | { |
---|
408 | n/a | return PyModuleDef_Init(&def_bad_negative); |
---|
409 | n/a | } |
---|
410 | n/a | |
---|
411 | n/a | static PyModuleDef def_create_int_with_state = { \ |
---|
412 | n/a | PyModuleDef_HEAD_INIT, /* m_base */ |
---|
413 | n/a | "create_with_state", /* m_name */ |
---|
414 | n/a | PyDoc_STR("Not a PyModuleObject object, but requests per-module state"), |
---|
415 | n/a | 10, /* m_size */ |
---|
416 | n/a | NULL, /* m_methods */ |
---|
417 | n/a | slots_create_nonmodule, /* m_slots */ |
---|
418 | n/a | NULL, /* m_traverse */ |
---|
419 | n/a | NULL, /* m_clear */ |
---|
420 | n/a | NULL, /* m_free */ |
---|
421 | n/a | }; |
---|
422 | n/a | |
---|
423 | n/a | PyMODINIT_FUNC |
---|
424 | n/a | PyInit__testmultiphase_create_int_with_state(PyObject *spec) |
---|
425 | n/a | { |
---|
426 | n/a | return PyModuleDef_Init(&def_create_int_with_state); |
---|
427 | n/a | } |
---|
428 | n/a | |
---|
429 | n/a | |
---|
430 | n/a | static PyModuleDef def_negative_size = { \ |
---|
431 | n/a | PyModuleDef_HEAD_INIT, /* m_base */ |
---|
432 | n/a | "negative_size", /* m_name */ |
---|
433 | n/a | PyDoc_STR("PyModuleDef with negative m_size"), |
---|
434 | n/a | -1, /* m_size */ |
---|
435 | n/a | NULL, /* m_methods */ |
---|
436 | n/a | slots_create_nonmodule, /* m_slots */ |
---|
437 | n/a | NULL, /* m_traverse */ |
---|
438 | n/a | NULL, /* m_clear */ |
---|
439 | n/a | NULL, /* m_free */ |
---|
440 | n/a | }; |
---|
441 | n/a | |
---|
442 | n/a | PyMODINIT_FUNC |
---|
443 | n/a | PyInit__testmultiphase_negative_size(PyObject *spec) |
---|
444 | n/a | { |
---|
445 | n/a | return PyModuleDef_Init(&def_negative_size); |
---|
446 | n/a | } |
---|
447 | n/a | |
---|
448 | n/a | |
---|
449 | n/a | static PyModuleDef uninitialized_def = TEST_MODULE_DEF("main", main_slots, testexport_methods); |
---|
450 | n/a | |
---|
451 | n/a | PyMODINIT_FUNC |
---|
452 | n/a | PyInit__testmultiphase_export_uninitialized(PyObject *spec) |
---|
453 | n/a | { |
---|
454 | n/a | return (PyObject*) &uninitialized_def; |
---|
455 | n/a | } |
---|
456 | n/a | |
---|
457 | n/a | PyMODINIT_FUNC |
---|
458 | n/a | PyInit__testmultiphase_export_null(PyObject *spec) |
---|
459 | n/a | { |
---|
460 | n/a | return NULL; |
---|
461 | n/a | } |
---|
462 | n/a | |
---|
463 | n/a | PyMODINIT_FUNC |
---|
464 | n/a | PyInit__testmultiphase_export_raise(PyObject *spec) |
---|
465 | n/a | { |
---|
466 | n/a | PyErr_SetString(PyExc_SystemError, "bad export function"); |
---|
467 | n/a | return NULL; |
---|
468 | n/a | } |
---|
469 | n/a | |
---|
470 | n/a | PyMODINIT_FUNC |
---|
471 | n/a | PyInit__testmultiphase_export_unreported_exception(PyObject *spec) |
---|
472 | n/a | { |
---|
473 | n/a | PyErr_SetString(PyExc_SystemError, "bad export function"); |
---|
474 | n/a | return PyModuleDef_Init(&main_def); |
---|
475 | n/a | } |
---|
476 | n/a | |
---|
477 | n/a | static PyObject* |
---|
478 | n/a | createfunc_null(PyObject *spec, PyModuleDef *def) |
---|
479 | n/a | { |
---|
480 | n/a | return NULL; |
---|
481 | n/a | } |
---|
482 | n/a | |
---|
483 | n/a | PyModuleDef_Slot slots_create_null[] = { |
---|
484 | n/a | {Py_mod_create, createfunc_null}, |
---|
485 | n/a | {0, NULL}, |
---|
486 | n/a | }; |
---|
487 | n/a | |
---|
488 | n/a | static PyModuleDef def_create_null = TEST_MODULE_DEF( |
---|
489 | n/a | "_testmultiphase_create_null", slots_create_null, NULL); |
---|
490 | n/a | |
---|
491 | n/a | PyMODINIT_FUNC |
---|
492 | n/a | PyInit__testmultiphase_create_null(PyObject *spec) |
---|
493 | n/a | { |
---|
494 | n/a | return PyModuleDef_Init(&def_create_null); |
---|
495 | n/a | } |
---|
496 | n/a | |
---|
497 | n/a | static PyObject* |
---|
498 | n/a | createfunc_raise(PyObject *spec, PyModuleDef *def) |
---|
499 | n/a | { |
---|
500 | n/a | PyErr_SetString(PyExc_SystemError, "bad create function"); |
---|
501 | n/a | return NULL; |
---|
502 | n/a | } |
---|
503 | n/a | |
---|
504 | n/a | static PyModuleDef_Slot slots_create_raise[] = { |
---|
505 | n/a | {Py_mod_create, createfunc_raise}, |
---|
506 | n/a | {0, NULL}, |
---|
507 | n/a | }; |
---|
508 | n/a | |
---|
509 | n/a | static PyModuleDef def_create_raise = TEST_MODULE_DEF( |
---|
510 | n/a | "_testmultiphase_create_null", slots_create_raise, NULL); |
---|
511 | n/a | |
---|
512 | n/a | PyMODINIT_FUNC |
---|
513 | n/a | PyInit__testmultiphase_create_raise(PyObject *spec) |
---|
514 | n/a | { |
---|
515 | n/a | return PyModuleDef_Init(&def_create_raise); |
---|
516 | n/a | } |
---|
517 | n/a | |
---|
518 | n/a | static PyObject* |
---|
519 | n/a | createfunc_unreported_exception(PyObject *spec, PyModuleDef *def) |
---|
520 | n/a | { |
---|
521 | n/a | PyErr_SetString(PyExc_SystemError, "bad create function"); |
---|
522 | n/a | return PyModule_New("foo"); |
---|
523 | n/a | } |
---|
524 | n/a | |
---|
525 | n/a | static PyModuleDef_Slot slots_create_unreported_exception[] = { |
---|
526 | n/a | {Py_mod_create, createfunc_unreported_exception}, |
---|
527 | n/a | {0, NULL}, |
---|
528 | n/a | }; |
---|
529 | n/a | |
---|
530 | n/a | static PyModuleDef def_create_unreported_exception = TEST_MODULE_DEF( |
---|
531 | n/a | "_testmultiphase_create_unreported_exception", slots_create_unreported_exception, NULL); |
---|
532 | n/a | |
---|
533 | n/a | PyMODINIT_FUNC |
---|
534 | n/a | PyInit__testmultiphase_create_unreported_exception(PyObject *spec) |
---|
535 | n/a | { |
---|
536 | n/a | return PyModuleDef_Init(&def_create_unreported_exception); |
---|
537 | n/a | } |
---|
538 | n/a | |
---|
539 | n/a | static PyModuleDef_Slot slots_nonmodule_with_exec_slots[] = { |
---|
540 | n/a | {Py_mod_create, createfunc_nonmodule}, |
---|
541 | n/a | {Py_mod_exec, execfunc}, |
---|
542 | n/a | {0, NULL}, |
---|
543 | n/a | }; |
---|
544 | n/a | |
---|
545 | n/a | static PyModuleDef def_nonmodule_with_exec_slots = TEST_MODULE_DEF( |
---|
546 | n/a | "_testmultiphase_nonmodule_with_exec_slots", slots_nonmodule_with_exec_slots, NULL); |
---|
547 | n/a | |
---|
548 | n/a | PyMODINIT_FUNC |
---|
549 | n/a | PyInit__testmultiphase_nonmodule_with_exec_slots(PyObject *spec) |
---|
550 | n/a | { |
---|
551 | n/a | return PyModuleDef_Init(&def_nonmodule_with_exec_slots); |
---|
552 | n/a | } |
---|
553 | n/a | |
---|
554 | n/a | static int |
---|
555 | n/a | execfunc_err(PyObject *mod) |
---|
556 | n/a | { |
---|
557 | n/a | return -1; |
---|
558 | n/a | } |
---|
559 | n/a | |
---|
560 | n/a | static PyModuleDef_Slot slots_exec_err[] = { |
---|
561 | n/a | {Py_mod_exec, execfunc_err}, |
---|
562 | n/a | {0, NULL}, |
---|
563 | n/a | }; |
---|
564 | n/a | |
---|
565 | n/a | static PyModuleDef def_exec_err = TEST_MODULE_DEF( |
---|
566 | n/a | "_testmultiphase_exec_err", slots_exec_err, NULL); |
---|
567 | n/a | |
---|
568 | n/a | PyMODINIT_FUNC |
---|
569 | n/a | PyInit__testmultiphase_exec_err(PyObject *spec) |
---|
570 | n/a | { |
---|
571 | n/a | return PyModuleDef_Init(&def_exec_err); |
---|
572 | n/a | } |
---|
573 | n/a | |
---|
574 | n/a | static int |
---|
575 | n/a | execfunc_raise(PyObject *spec) |
---|
576 | n/a | { |
---|
577 | n/a | PyErr_SetString(PyExc_SystemError, "bad exec function"); |
---|
578 | n/a | return -1; |
---|
579 | n/a | } |
---|
580 | n/a | |
---|
581 | n/a | static PyModuleDef_Slot slots_exec_raise[] = { |
---|
582 | n/a | {Py_mod_exec, execfunc_raise}, |
---|
583 | n/a | {0, NULL}, |
---|
584 | n/a | }; |
---|
585 | n/a | |
---|
586 | n/a | static PyModuleDef def_exec_raise = TEST_MODULE_DEF( |
---|
587 | n/a | "_testmultiphase_exec_raise", slots_exec_raise, NULL); |
---|
588 | n/a | |
---|
589 | n/a | PyMODINIT_FUNC |
---|
590 | n/a | PyInit__testmultiphase_exec_raise(PyObject *mod) |
---|
591 | n/a | { |
---|
592 | n/a | return PyModuleDef_Init(&def_exec_raise); |
---|
593 | n/a | } |
---|
594 | n/a | |
---|
595 | n/a | static int |
---|
596 | n/a | execfunc_unreported_exception(PyObject *mod) |
---|
597 | n/a | { |
---|
598 | n/a | PyErr_SetString(PyExc_SystemError, "bad exec function"); |
---|
599 | n/a | return 0; |
---|
600 | n/a | } |
---|
601 | n/a | |
---|
602 | n/a | static PyModuleDef_Slot slots_exec_unreported_exception[] = { |
---|
603 | n/a | {Py_mod_exec, execfunc_unreported_exception}, |
---|
604 | n/a | {0, NULL}, |
---|
605 | n/a | }; |
---|
606 | n/a | |
---|
607 | n/a | static PyModuleDef def_exec_unreported_exception = TEST_MODULE_DEF( |
---|
608 | n/a | "_testmultiphase_exec_unreported_exception", slots_exec_unreported_exception, NULL); |
---|
609 | n/a | |
---|
610 | n/a | PyMODINIT_FUNC |
---|
611 | n/a | PyInit__testmultiphase_exec_unreported_exception(PyObject *spec) |
---|
612 | n/a | { |
---|
613 | n/a | return PyModuleDef_Init(&def_exec_unreported_exception); |
---|
614 | n/a | } |
---|
615 | n/a | |
---|
616 | n/a | /*** Helper for imp test ***/ |
---|
617 | n/a | |
---|
618 | n/a | static PyModuleDef imp_dummy_def = TEST_MODULE_DEF("imp_dummy", main_slots, testexport_methods); |
---|
619 | n/a | |
---|
620 | n/a | PyMODINIT_FUNC |
---|
621 | n/a | PyInit_imp_dummy(PyObject *spec) |
---|
622 | n/a | { |
---|
623 | n/a | return PyModuleDef_Init(&imp_dummy_def); |
---|
624 | n/a | } |
---|