1
2 /* Testing module for multi-phase initialization of extension modules (PEP 489)
3 */
4 #ifndef Py_BUILD_CORE_BUILTIN
5 # define Py_BUILD_CORE_MODULE 1
6 #endif
7
8 #include "Python.h"
9 #include "pycore_namespace.h" // _PyNamespace_New()
10
11 /* State for testing module state access from methods */
12
13 typedef struct {
14 int counter;
15 } meth_state;
16
17 /*[clinic input]
18 module _testmultiphase
19
20 class _testmultiphase.StateAccessType "StateAccessTypeObject *" "!StateAccessType"
21 [clinic start generated code]*/
22 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=bab9f2fe3bd312ff]*/
23
24 /* Example objects */
25 typedef struct {
26 PyObject_HEAD
27 PyObject *x_attr; /* Attributes dictionary */
28 } ExampleObject;
29
30 typedef struct {
31 PyObject *integer;
32 } testmultiphase_state;
33
34 typedef struct {
35 PyObject_HEAD
36 } StateAccessTypeObject;
37
38 /* Example methods */
39
40 static int
41 Example_traverse(ExampleObject *self, visitproc visit, void *arg)
42 {
43 Py_VISIT(self->x_attr);
44 return 0;
45 }
46
47 static void
48 Example_finalize(ExampleObject *self)
49 {
50 Py_CLEAR(self->x_attr);
51 }
52
53 static PyObject *
54 Example_demo(ExampleObject *self, PyObject *args)
55 {
56 PyObject *o = NULL;
57 if (!PyArg_ParseTuple(args, "|O:demo", &o))
58 return NULL;
59 if (o != NULL && PyUnicode_Check(o)) {
60 return Py_NewRef(o);
61 }
62 Py_RETURN_NONE;
63 }
64
65 #include "clinic/_testmultiphase.c.h"
66
67 static PyMethodDef Example_methods[] = {
68 {"demo", (PyCFunction)Example_demo, METH_VARARGS,
69 PyDoc_STR("demo() -> None")},
70 {NULL, NULL} /* sentinel */
71 };
72
73 static PyObject *
74 Example_getattro(ExampleObject *self, PyObject *name)
75 {
76 if (self->x_attr != NULL) {
77 PyObject *v = PyDict_GetItemWithError(self->x_attr, name);
78 if (v != NULL) {
79 return Py_NewRef(v);
80 }
81 else if (PyErr_Occurred()) {
82 return NULL;
83 }
84 }
85 return PyObject_GenericGetAttr((PyObject *)self, name);
86 }
87
88 static int
89 Example_setattr(ExampleObject *self, const char *name, PyObject *v)
90 {
91 if (self->x_attr == NULL) {
92 self->x_attr = PyDict_New();
93 if (self->x_attr == NULL)
94 return -1;
95 }
96 if (v == NULL) {
97 int rv = PyDict_DelItemString(self->x_attr, name);
98 if (rv < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
99 PyErr_SetString(PyExc_AttributeError,
100 "delete non-existing Example attribute");
101 return rv;
102 }
103 else
104 return PyDict_SetItemString(self->x_attr, name, v);
105 }
106
107 static PyType_Slot Example_Type_slots[] = {
108 {Py_tp_doc, "The Example type"},
109 {Py_tp_finalize, Example_finalize},
110 {Py_tp_traverse, Example_traverse},
111 {Py_tp_getattro, Example_getattro},
112 {Py_tp_setattr, Example_setattr},
113 {Py_tp_methods, Example_methods},
114 {0, 0},
115 };
116
117 static PyType_Spec Example_Type_spec = {
118 "_testimportexec.Example",
119 sizeof(ExampleObject),
120 0,
121 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
122 Example_Type_slots
123 };
124
125
126 static PyModuleDef def_meth_state_access;
127 static PyModuleDef def_nonmodule;
128 static PyModuleDef def_nonmodule_with_methods;
129
130 /*[clinic input]
131 _testmultiphase.StateAccessType.get_defining_module
132
133 cls: defining_class
134
135 Return the module of the defining class.
136
137 Also tests that result of PyType_GetModuleByDef matches defining_class's
138 module.
139 [clinic start generated code]*/
140
141 static PyObject *
142 _testmultiphase_StateAccessType_get_defining_module_impl(StateAccessTypeObject *self,
143 PyTypeObject *cls)
144 /*[clinic end generated code: output=ba2a14284a5d0921 input=d2c7245c8a9d06f8]*/
145 {
146 PyObject *retval;
147 retval = PyType_GetModule(cls);
148 if (retval == NULL) {
149 return NULL;
150 }
151 assert(PyType_GetModuleByDef(Py_TYPE(self), &def_meth_state_access) == retval);
152 return Py_NewRef(retval);
153 }
154
155 /*[clinic input]
156 _testmultiphase.StateAccessType.getmodulebydef_bad_def
157
158 cls: defining_class
159
160 Test that result of PyType_GetModuleByDef with a bad def is NULL.
161 [clinic start generated code]*/
162
163 static PyObject *
164 _testmultiphase_StateAccessType_getmodulebydef_bad_def_impl(StateAccessTypeObject *self,
165 PyTypeObject *cls)
166 /*[clinic end generated code: output=64509074dfcdbd31 input=edaff09aa4788204]*/
167 {
168 PyType_GetModuleByDef(Py_TYPE(self), &def_nonmodule); // should raise
169 assert(PyErr_Occurred());
170 return NULL;
171 }
172
173 /*[clinic input]
174 _testmultiphase.StateAccessType.increment_count_clinic
175
176 cls: defining_class
177 /
178 n: int = 1
179 *
180 twice: bool = False
181
182 Add 'n' from the module-state counter.
183
184 Pass 'twice' to double that amount.
185
186 This tests Argument Clinic support for defining_class.
187 [clinic start generated code]*/
188
189 static PyObject *
190 _testmultiphase_StateAccessType_increment_count_clinic_impl(StateAccessTypeObject *self,
191 PyTypeObject *cls,
192 int n, int twice)
193 /*[clinic end generated code: output=3b34f86bc5473204 input=551d482e1fe0b8f5]*/
194 {
195 meth_state *m_state = PyType_GetModuleState(cls);
196 if (twice) {
197 n *= 2;
198 }
199 m_state->counter += n;
200
201 Py_RETURN_NONE;
202 }
203
204 PyDoc_STRVAR(_StateAccessType_decrement_count__doc__,
205 "decrement_count($self, /, n=1, *, twice=None)\n"
206 "--\n"
207 "\n"
208 "Add 'n' from the module-state counter.\n"
209 "Pass 'twice' to double that amount.\n"
210 "(This is to test both positional and keyword arguments.");
211
212 // Intentionally does not use Argument Clinic
213 static PyObject *
214 _StateAccessType_increment_count_noclinic(StateAccessTypeObject *self,
215 PyTypeObject *defining_class,
216 PyObject *const *args,
217 Py_ssize_t nargs,
218 PyObject *kwnames)
219 {
220 if (!_PyArg_CheckPositional("StateAccessTypeObject.decrement_count", nargs, 0, 1)) {
221 return NULL;
222 }
223 long n = 1;
224 if (nargs) {
225 n = PyLong_AsLong(args[0]);
226 if (PyErr_Occurred()) {
227 return NULL;
228 }
229 }
230 if (kwnames && PyTuple_Check(kwnames)) {
231 if (PyTuple_GET_SIZE(kwnames) > 1 ||
232 PyUnicode_CompareWithASCIIString(
233 PyTuple_GET_ITEM(kwnames, 0),
234 "twice"
235 )) {
236 PyErr_SetString(
237 PyExc_TypeError,
238 "decrement_count only takes 'twice' keyword argument"
239 );
240 return NULL;
241 }
242 n *= 2;
243 }
244 meth_state *m_state = PyType_GetModuleState(defining_class);
245 m_state->counter += n;
246
247 Py_RETURN_NONE;
248 }
249
250 /*[clinic input]
251 _testmultiphase.StateAccessType.get_count
252
253 cls: defining_class
254
255 Return the value of the module-state counter.
256 [clinic start generated code]*/
257
258 static PyObject *
259 _testmultiphase_StateAccessType_get_count_impl(StateAccessTypeObject *self,
260 PyTypeObject *cls)
261 /*[clinic end generated code: output=64600f95b499a319 input=d5d181f12384849f]*/
262 {
263 meth_state *m_state = PyType_GetModuleState(cls);
264 return PyLong_FromLong(m_state->counter);
265 }
266
267 static PyMethodDef StateAccessType_methods[] = {
268 _TESTMULTIPHASE_STATEACCESSTYPE_GET_DEFINING_MODULE_METHODDEF
269 _TESTMULTIPHASE_STATEACCESSTYPE_GETMODULEBYDEF_BAD_DEF_METHODDEF
270 _TESTMULTIPHASE_STATEACCESSTYPE_GET_COUNT_METHODDEF
271 _TESTMULTIPHASE_STATEACCESSTYPE_INCREMENT_COUNT_CLINIC_METHODDEF
272 {
273 "increment_count_noclinic",
274 _PyCFunction_CAST(_StateAccessType_increment_count_noclinic),
275 METH_METHOD|METH_FASTCALL|METH_KEYWORDS,
276 _StateAccessType_decrement_count__doc__
277 },
278 {NULL, NULL} /* sentinel */
279 };
280
281 static PyType_Slot StateAccessType_Type_slots[] = {
282 {Py_tp_doc, "Type for testing per-module state access from methods."},
283 {Py_tp_methods, StateAccessType_methods},
284 {0, NULL}
285 };
286
287 static PyType_Spec StateAccessType_spec = {
288 "_testimportexec.StateAccessType",
289 sizeof(StateAccessTypeObject),
290 0,
291 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_FINALIZE | Py_TPFLAGS_BASETYPE,
292 StateAccessType_Type_slots
293 };
294
295 /* Function of two integers returning integer */
296
297 PyDoc_STRVAR(testexport_foo_doc,
298 "foo(i,j)\n\
299 \n\
300 Return the sum of i and j.");
301
302 static PyObject *
303 testexport_foo(PyObject *self, PyObject *args)
304 {
305 long i, j;
306 long res;
307 if (!PyArg_ParseTuple(args, "ll:foo", &i, &j))
308 return NULL;
309 res = i + j;
310 return PyLong_FromLong(res);
311 }
312
313 /* Test that PyState registration fails */
314
315 PyDoc_STRVAR(call_state_registration_func_doc,
316 "register_state(0): call PyState_FindModule()\n\
317 register_state(1): call PyState_AddModule()\n\
318 register_state(2): call PyState_RemoveModule()");
319
320 static PyObject *
321 call_state_registration_func(PyObject *mod, PyObject *args)
322 {
323 int i, ret;
324 PyModuleDef *def = PyModule_GetDef(mod);
325 if (def == NULL) {
326 return NULL;
327 }
328 if (!PyArg_ParseTuple(args, "i:call_state_registration_func", &i))
329 return NULL;
330 switch (i) {
331 case 0:
332 mod = PyState_FindModule(def);
333 if (mod == NULL) {
334 Py_RETURN_NONE;
335 }
336 return mod;
337 case 1:
338 ret = PyState_AddModule(mod, def);
339 if (ret != 0) {
340 return NULL;
341 }
342 break;
343 case 2:
344 ret = PyState_RemoveModule(def);
345 if (ret != 0) {
346 return NULL;
347 }
348 break;
349 }
350 Py_RETURN_NONE;
351 }
352
353
354 static PyType_Slot Str_Type_slots[] = {
355 {Py_tp_base, NULL}, /* filled out in module exec function */
356 {0, 0},
357 };
358
359 static PyType_Spec Str_Type_spec = {
360 "_testimportexec.Str",
361 0,
362 0,
363 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
364 Str_Type_slots
365 };
366
367 static PyMethodDef testexport_methods[] = {
368 {"foo", testexport_foo, METH_VARARGS,
369 testexport_foo_doc},
370 {"call_state_registration_func", call_state_registration_func,
371 METH_VARARGS, call_state_registration_func_doc},
372 {NULL, NULL} /* sentinel */
373 };
374
375 static int execfunc(PyObject *m)
376 {
377 PyObject *temp = NULL;
378
379 /* Due to cross platform compiler issues the slots must be filled
380 * here. It's required for portability to Windows without requiring
381 * C++. */
382 Str_Type_slots[0].pfunc = &PyUnicode_Type;
383
384 /* Add a custom type */
385 temp = PyType_FromSpec(&Example_Type_spec);
386 if (temp == NULL) {
387 goto fail;
388 }
389 if (PyModule_AddObject(m, "Example", temp) != 0) {
390 Py_DECREF(temp);
391 goto fail;
392 }
393
394
395 /* Add an exception type */
396 temp = PyErr_NewException("_testimportexec.error", NULL, NULL);
397 if (temp == NULL) {
398 goto fail;
399 }
400 if (PyModule_AddObject(m, "error", temp) != 0) {
401 Py_DECREF(temp);
402 goto fail;
403 }
404
405 /* Add Str */
406 temp = PyType_FromSpec(&Str_Type_spec);
407 if (temp == NULL) {
408 goto fail;
409 }
410 if (PyModule_AddObject(m, "Str", temp) != 0) {
411 Py_DECREF(temp);
412 goto fail;
413 }
414
415 if (PyModule_AddIntConstant(m, "int_const", 1969) != 0) {
416 goto fail;
417 }
418
419 if (PyModule_AddStringConstant(m, "str_const", "something different") != 0) {
420 goto fail;
421 }
422
423 return 0;
424 fail:
425 return -1;
426 }
427
428 /* Helper for module definitions; there'll be a lot of them */
429
430 #define TEST_MODULE_DEF(name, slots, methods) { \
431 PyModuleDef_HEAD_INIT, /* m_base */ \
432 name, /* m_name */ \
433 PyDoc_STR("Test module " name), /* m_doc */ \
434 0, /* m_size */ \
435 methods, /* m_methods */ \
436 slots, /* m_slots */ \
437 NULL, /* m_traverse */ \
438 NULL, /* m_clear */ \
439 NULL, /* m_free */ \
440 }
441
442 static PyModuleDef_Slot main_slots[] = {
443 {Py_mod_exec, execfunc},
444 {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
445 {0, NULL},
446 };
447
448 static PyModuleDef main_def = TEST_MODULE_DEF("main", main_slots, testexport_methods);
449
450 PyMODINIT_FUNC
451 PyInit__testmultiphase(void)
452 {
453 return PyModuleDef_Init(&main_def);
454 }
455
456
457 /**** Importing a non-module object ****/
458
459 /* Create a SimpleNamespace(three=3) */
460 static PyObject*
461 createfunc_nonmodule(PyObject *spec, PyModuleDef *def)
462 {
463 PyObject *dct, *ns, *three;
464
465 if (def != &def_nonmodule && def != &def_nonmodule_with_methods) {
466 PyErr_SetString(PyExc_SystemError, "def does not match");
467 return NULL;
468 }
469
470 dct = PyDict_New();
471 if (dct == NULL)
472 return NULL;
473
474 three = PyLong_FromLong(3);
475 if (three == NULL) {
476 Py_DECREF(dct);
477 return NULL;
478 }
479 PyDict_SetItemString(dct, "three", three);
480 Py_DECREF(three);
481
482 ns = _PyNamespace_New(dct);
483 Py_DECREF(dct);
484 return ns;
485 }
486
487 static PyModuleDef_Slot slots_create_nonmodule[] = {
488 {Py_mod_create, createfunc_nonmodule},
489 {0, NULL},
490 };
491
492 static PyModuleDef def_nonmodule = TEST_MODULE_DEF(
493 "_testmultiphase_nonmodule", slots_create_nonmodule, NULL);
494
495 PyMODINIT_FUNC
496 PyInit__testmultiphase_nonmodule(void)
497 {
498 return PyModuleDef_Init(&def_nonmodule);
499 }
500
501 PyDoc_STRVAR(nonmodule_bar_doc,
502 "bar(i,j)\n\
503 \n\
504 Return the difference of i - j.");
505
506 static PyObject *
507 nonmodule_bar(PyObject *self, PyObject *args)
508 {
509 long i, j;
510 long res;
511 if (!PyArg_ParseTuple(args, "ll:bar", &i, &j))
512 return NULL;
513 res = i - j;
514 return PyLong_FromLong(res);
515 }
516
517 static PyMethodDef nonmodule_methods[] = {
518 {"bar", nonmodule_bar, METH_VARARGS, nonmodule_bar_doc},
519 {NULL, NULL} /* sentinel */
520 };
521
522 static PyModuleDef def_nonmodule_with_methods = TEST_MODULE_DEF(
523 "_testmultiphase_nonmodule_with_methods", slots_create_nonmodule, nonmodule_methods);
524
525 PyMODINIT_FUNC
526 PyInit__testmultiphase_nonmodule_with_methods(void)
527 {
528 return PyModuleDef_Init(&def_nonmodule_with_methods);
529 }
530
531 /**** Non-ASCII-named modules ****/
532
533 static PyModuleDef def_nonascii_latin = { \
534 PyModuleDef_HEAD_INIT, /* m_base */
535 "_testmultiphase_nonascii_latin", /* m_name */
536 PyDoc_STR("Module named in Czech"), /* m_doc */
537 0, /* m_size */
538 NULL, /* m_methods */
539 NULL, /* m_slots */
540 NULL, /* m_traverse */
541 NULL, /* m_clear */
542 NULL, /* m_free */
543 };
544
545 PyMODINIT_FUNC
546 PyInitU__testmultiphase_zkouka_naten_evc07gi8e(void)
547 {
548 return PyModuleDef_Init(&def_nonascii_latin);
549 }
550
551 static PyModuleDef def_nonascii_kana = { \
552 PyModuleDef_HEAD_INIT, /* m_base */
553 "_testmultiphase_nonascii_kana", /* m_name */
554 PyDoc_STR("Module named in Japanese"), /* m_doc */
555 0, /* m_size */
556 NULL, /* m_methods */
557 NULL, /* m_slots */
558 NULL, /* m_traverse */
559 NULL, /* m_clear */
560 NULL, /* m_free */
561 };
562
563 PyMODINIT_FUNC
564 PyInitU_eckzbwbhc6jpgzcx415x(void)
565 {
566 return PyModuleDef_Init(&def_nonascii_kana);
567 }
568
569 /*** Module with a single-character name ***/
570
571 PyMODINIT_FUNC
572 PyInit_x(void)
573 {
574 return PyModuleDef_Init(&main_def);
575 }
576
577 /**** Testing NULL slots ****/
578
579 static PyModuleDef null_slots_def = TEST_MODULE_DEF(
580 "_testmultiphase_null_slots", NULL, NULL);
581
582 PyMODINIT_FUNC
583 PyInit__testmultiphase_null_slots(void)
584 {
585 return PyModuleDef_Init(&null_slots_def);
586 }
587
588 /**** Problematic modules ****/
589
590 static PyModuleDef_Slot slots_bad_large[] = {
591 {_Py_mod_LAST_SLOT + 1, NULL},
592 {0, NULL},
593 };
594
595 static PyModuleDef def_bad_large = TEST_MODULE_DEF(
596 "_testmultiphase_bad_slot_large", slots_bad_large, NULL);
597
598 PyMODINIT_FUNC
599 PyInit__testmultiphase_bad_slot_large(void)
600 {
601 return PyModuleDef_Init(&def_bad_large);
602 }
603
604 static PyModuleDef_Slot slots_bad_negative[] = {
605 {-1, NULL},
606 {0, NULL},
607 };
608
609 static PyModuleDef def_bad_negative = TEST_MODULE_DEF(
610 "_testmultiphase_bad_slot_negative", slots_bad_negative, NULL);
611
612 PyMODINIT_FUNC
613 PyInit__testmultiphase_bad_slot_negative(void)
614 {
615 return PyModuleDef_Init(&def_bad_negative);
616 }
617
618 static PyModuleDef def_create_int_with_state = { \
619 PyModuleDef_HEAD_INIT, /* m_base */
620 "create_with_state", /* m_name */
621 PyDoc_STR("Not a PyModuleObject object, but requests per-module state"),
622 10, /* m_size */
623 NULL, /* m_methods */
624 slots_create_nonmodule, /* m_slots */
625 NULL, /* m_traverse */
626 NULL, /* m_clear */
627 NULL, /* m_free */
628 };
629
630 PyMODINIT_FUNC
631 PyInit__testmultiphase_create_int_with_state(void)
632 {
633 return PyModuleDef_Init(&def_create_int_with_state);
634 }
635
636
637 static PyModuleDef def_negative_size = { \
638 PyModuleDef_HEAD_INIT, /* m_base */
639 "negative_size", /* m_name */
640 PyDoc_STR("PyModuleDef with negative m_size"),
641 -1, /* m_size */
642 NULL, /* m_methods */
643 slots_create_nonmodule, /* m_slots */
644 NULL, /* m_traverse */
645 NULL, /* m_clear */
646 NULL, /* m_free */
647 };
648
649 PyMODINIT_FUNC
650 PyInit__testmultiphase_negative_size(void)
651 {
652 return PyModuleDef_Init(&def_negative_size);
653 }
654
655
656 static PyModuleDef uninitialized_def = TEST_MODULE_DEF("main", main_slots, testexport_methods);
657
658 PyMODINIT_FUNC
659 PyInit__testmultiphase_export_uninitialized(void)
660 {
661 return (PyObject*) &uninitialized_def;
662 }
663
664 PyMODINIT_FUNC
665 PyInit__testmultiphase_export_null(void)
666 {
667 return NULL;
668 }
669
670 PyMODINIT_FUNC
671 PyInit__testmultiphase_export_raise(void)
672 {
673 PyErr_SetString(PyExc_SystemError, "bad export function");
674 return NULL;
675 }
676
677 PyMODINIT_FUNC
678 PyInit__testmultiphase_export_unreported_exception(void)
679 {
680 PyErr_SetString(PyExc_SystemError, "bad export function");
681 return PyModuleDef_Init(&main_def);
682 }
683
684 static PyObject*
685 createfunc_noop(PyObject *spec, PyModuleDef *def)
686 {
687 return PyModule_New("spam");
688 }
689
690 static PyModuleDef_Slot slots_multiple_create_slots[] = {
691 {Py_mod_create, createfunc_noop},
692 {Py_mod_create, createfunc_noop},
693 {0, NULL},
694 };
695
696 static PyModuleDef def_multiple_create_slots = TEST_MODULE_DEF(
697 "_testmultiphase_multiple_create_slots", slots_multiple_create_slots, NULL);
698
699 PyMODINIT_FUNC
700 PyInit__testmultiphase_multiple_create_slots(void)
701 {
702 return PyModuleDef_Init(&def_multiple_create_slots);
703 }
704
705 static PyObject*
706 createfunc_null(PyObject *spec, PyModuleDef *def)
707 {
708 return NULL;
709 }
710
711 static PyModuleDef_Slot slots_create_null[] = {
712 {Py_mod_create, createfunc_null},
713 {0, NULL},
714 };
715
716 static PyModuleDef def_create_null = TEST_MODULE_DEF(
717 "_testmultiphase_create_null", slots_create_null, NULL);
718
719 PyMODINIT_FUNC
720 PyInit__testmultiphase_create_null(void)
721 {
722 return PyModuleDef_Init(&def_create_null);
723 }
724
725 static PyObject*
726 createfunc_raise(PyObject *spec, PyModuleDef *def)
727 {
728 PyErr_SetString(PyExc_SystemError, "bad create function");
729 return NULL;
730 }
731
732 static PyModuleDef_Slot slots_create_raise[] = {
733 {Py_mod_create, createfunc_raise},
734 {0, NULL},
735 };
736
737 static PyModuleDef def_create_raise = TEST_MODULE_DEF(
738 "_testmultiphase_create_null", slots_create_raise, NULL);
739
740 PyMODINIT_FUNC
741 PyInit__testmultiphase_create_raise(void)
742 {
743 return PyModuleDef_Init(&def_create_raise);
744 }
745
746 static PyObject*
747 createfunc_unreported_exception(PyObject *spec, PyModuleDef *def)
748 {
749 PyErr_SetString(PyExc_SystemError, "bad create function");
750 return PyModule_New("foo");
751 }
752
753 static PyModuleDef_Slot slots_create_unreported_exception[] = {
754 {Py_mod_create, createfunc_unreported_exception},
755 {0, NULL},
756 };
757
758 static PyModuleDef def_create_unreported_exception = TEST_MODULE_DEF(
759 "_testmultiphase_create_unreported_exception", slots_create_unreported_exception, NULL);
760
761 PyMODINIT_FUNC
762 PyInit__testmultiphase_create_unreported_exception(void)
763 {
764 return PyModuleDef_Init(&def_create_unreported_exception);
765 }
766
767 static PyModuleDef_Slot slots_nonmodule_with_exec_slots[] = {
768 {Py_mod_create, createfunc_nonmodule},
769 {Py_mod_exec, execfunc},
770 {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
771 {0, NULL},
772 };
773
774 static PyModuleDef def_nonmodule_with_exec_slots = TEST_MODULE_DEF(
775 "_testmultiphase_nonmodule_with_exec_slots", slots_nonmodule_with_exec_slots, NULL);
776
777 PyMODINIT_FUNC
778 PyInit__testmultiphase_nonmodule_with_exec_slots(void)
779 {
780 return PyModuleDef_Init(&def_nonmodule_with_exec_slots);
781 }
782
783 static int
784 execfunc_err(PyObject *mod)
785 {
786 return -1;
787 }
788
789 static PyModuleDef_Slot slots_exec_err[] = {
790 {Py_mod_exec, execfunc_err},
791 {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
792 {0, NULL},
793 };
794
795 static PyModuleDef def_exec_err = TEST_MODULE_DEF(
796 "_testmultiphase_exec_err", slots_exec_err, NULL);
797
798 PyMODINIT_FUNC
799 PyInit__testmultiphase_exec_err(void)
800 {
801 return PyModuleDef_Init(&def_exec_err);
802 }
803
804 static int
805 execfunc_raise(PyObject *spec)
806 {
807 PyErr_SetString(PyExc_SystemError, "bad exec function");
808 return -1;
809 }
810
811 static PyModuleDef_Slot slots_exec_raise[] = {
812 {Py_mod_exec, execfunc_raise},
813 {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
814 {0, NULL},
815 };
816
817 static PyModuleDef def_exec_raise = TEST_MODULE_DEF(
818 "_testmultiphase_exec_raise", slots_exec_raise, NULL);
819
820 PyMODINIT_FUNC
821 PyInit__testmultiphase_exec_raise(void)
822 {
823 return PyModuleDef_Init(&def_exec_raise);
824 }
825
826 static int
827 execfunc_unreported_exception(PyObject *mod)
828 {
829 PyErr_SetString(PyExc_SystemError, "bad exec function");
830 return 0;
831 }
832
833 static PyModuleDef_Slot slots_exec_unreported_exception[] = {
834 {Py_mod_exec, execfunc_unreported_exception},
835 {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
836 {0, NULL},
837 };
838
839 static PyModuleDef def_exec_unreported_exception = TEST_MODULE_DEF(
840 "_testmultiphase_exec_unreported_exception", slots_exec_unreported_exception, NULL);
841
842 PyMODINIT_FUNC
843 PyInit__testmultiphase_exec_unreported_exception(void)
844 {
845 return PyModuleDef_Init(&def_exec_unreported_exception);
846 }
847
848 static int
849 meth_state_access_exec(PyObject *m)
850 {
851 PyObject *temp;
852 meth_state *m_state;
853
854 m_state = PyModule_GetState(m);
855 if (m_state == NULL) {
856 return -1;
857 }
858
859 temp = PyType_FromModuleAndSpec(m, &StateAccessType_spec, NULL);
860 if (temp == NULL) {
861 return -1;
862 }
863 if (PyModule_AddObject(m, "StateAccessType", temp) != 0) {
864 Py_DECREF(temp);
865 return -1;
866 }
867
868
869 return 0;
870 }
871
872 static PyModuleDef_Slot meth_state_access_slots[] = {
873 {Py_mod_exec, meth_state_access_exec},
874 {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
875 {0, NULL}
876 };
877
878 static PyModuleDef def_meth_state_access = {
879 PyModuleDef_HEAD_INIT,
880 .m_name = "_testmultiphase_meth_state_access",
881 .m_doc = PyDoc_STR("Module testing access"
882 " to state from methods."),
883 .m_size = sizeof(meth_state),
884 .m_slots = meth_state_access_slots,
885 };
886
887 PyMODINIT_FUNC
888 PyInit__testmultiphase_meth_state_access(void)
889 {
890 return PyModuleDef_Init(&def_meth_state_access);
891 }
892
893 static PyModuleDef def_module_state_shared = {
894 PyModuleDef_HEAD_INIT,
895 .m_name = "_test_module_state_shared",
896 .m_doc = PyDoc_STR("Regression Test module for single-phase init."),
897 .m_size = -1,
898 };
899
900 PyMODINIT_FUNC
901 PyInit__test_module_state_shared(void)
902 {
903 PyObject *module = PyModule_Create(&def_module_state_shared);
904 if (module == NULL) {
905 return NULL;
906 }
907
908 if (PyModule_AddObjectRef(module, "Error", PyExc_Exception) < 0) {
909 Py_DECREF(module);
910 return NULL;
911 }
912 return module;
913 }
914
915
916 /* multiple interpreters support */
917
918 static PyModuleDef_Slot slots_multiple_multiple_interpreters_slots[] = {
919 {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
920 {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
921 {0, NULL},
922 };
923
924 static PyModuleDef def_multiple_multiple_interpreters_slots = TEST_MODULE_DEF(
925 "_testmultiphase_multiple_multiple_interpreters_slots",
926 slots_multiple_multiple_interpreters_slots,
927 NULL);
928
929 PyMODINIT_FUNC
930 PyInit__testmultiphase_multiple_multiple_interpreters_slots(void)
931 {
932 return PyModuleDef_Init(&def_multiple_multiple_interpreters_slots);
933 }
934
935 static PyModuleDef_Slot non_isolated_slots[] = {
936 {Py_mod_exec, execfunc},
937 {Py_mod_multiple_interpreters, Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED},
938 {0, NULL},
939 };
940
941 static PyModuleDef non_isolated_def = TEST_MODULE_DEF("_test_non_isolated",
942 non_isolated_slots,
943 testexport_methods);
944
945 PyMODINIT_FUNC
946 PyInit__test_non_isolated(void)
947 {
948 return PyModuleDef_Init(&non_isolated_def);
949 }
950
951
952 static PyModuleDef_Slot shared_gil_only_slots[] = {
953 {Py_mod_exec, execfunc},
954 /* Note that Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED is the default.
955 We put it here explicitly to draw attention to the contrast
956 with Py_MOD_PER_INTERPRETER_GIL_SUPPORTED. */
957 {Py_mod_multiple_interpreters, Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED},
958 {0, NULL},
959 };
960
961 static PyModuleDef shared_gil_only_def = TEST_MODULE_DEF("_test_shared_gil_only",
962 shared_gil_only_slots,
963 testexport_methods);
964
965 PyMODINIT_FUNC
966 PyInit__test_shared_gil_only(void)
967 {
968 return PyModuleDef_Init(&shared_gil_only_def);
969 }