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