Debian Codegraph @

lib_pypy/_testmultiphase.c

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