blob: eadc46fbf1867529aaddf23a899c32b5e5a752a8 [file] [log] [blame]
Nick Coghland5cacbb2015-05-23 22:24:10 +10001
2/* Testing module for multi-phase initialization of extension modules (PEP 489)
3 */
4
5#include "Python.h"
6
7/* Example objects */
8typedef struct {
9 PyObject_HEAD
10 PyObject *x_attr; /* Attributes dictionary */
11} ExampleObject;
12
Marcel Plchc2b0b122018-03-17 06:41:20 +010013typedef struct {
14 PyObject *integer;
15} testmultiphase_state;
16
Nick Coghland5cacbb2015-05-23 22:24:10 +100017/* Example methods */
18
Nick Coghlan53f95022015-06-04 21:52:57 +100019static int
20Example_traverse(ExampleObject *self, visitproc visit, void *arg)
Nick Coghland5cacbb2015-05-23 22:24:10 +100021{
Nick Coghlan53f95022015-06-04 21:52:57 +100022 Py_VISIT(self->x_attr);
23 return 0;
24}
25
Serhiy Storchaka19de8b32018-05-26 10:51:58 +030026static void
Nick Coghlan53f95022015-06-04 21:52:57 +100027Example_finalize(ExampleObject *self)
28{
29 Py_CLEAR(self->x_attr);
Nick Coghland5cacbb2015-05-23 22:24:10 +100030}
31
32static PyObject *
33Example_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 }
Serhiy Storchaka228b12e2017-01-23 09:47:21 +020042 Py_RETURN_NONE;
Nick Coghland5cacbb2015-05-23 22:24:10 +100043}
44
45
46static PyMethodDef Example_methods[] = {
47 {"demo", (PyCFunction)Example_demo, METH_VARARGS,
48 PyDoc_STR("demo() -> None")},
49 {NULL, NULL} /* sentinel */
50};
51
52static PyObject *
53Example_getattro(ExampleObject *self, PyObject *name)
54{
55 if (self->x_attr != NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +020056 PyObject *v = PyDict_GetItemWithError(self->x_attr, name);
Nick Coghland5cacbb2015-05-23 22:24:10 +100057 if (v != NULL) {
58 Py_INCREF(v);
59 return v;
60 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +020061 else if (PyErr_Occurred()) {
62 return NULL;
63 }
Nick Coghland5cacbb2015-05-23 22:24:10 +100064 }
65 return PyObject_GenericGetAttr((PyObject *)self, name);
66}
67
68static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020069Example_setattr(ExampleObject *self, const char *name, PyObject *v)
Nick Coghland5cacbb2015-05-23 22:24:10 +100070{
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);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +020078 if (rv < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
Nick Coghland5cacbb2015-05-23 22:24:10 +100079 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
87static PyType_Slot Example_Type_slots[] = {
88 {Py_tp_doc, "The Example type"},
Nick Coghlan53f95022015-06-04 21:52:57 +100089 {Py_tp_finalize, Example_finalize},
90 {Py_tp_traverse, Example_traverse},
Nick Coghland5cacbb2015-05-23 22:24:10 +100091 {Py_tp_getattro, Example_getattro},
92 {Py_tp_setattr, Example_setattr},
93 {Py_tp_methods, Example_methods},
94 {0, 0},
95};
96
97static PyType_Spec Example_Type_spec = {
98 "_testimportexec.Example",
99 sizeof(ExampleObject),
100 0,
Antoine Pitrouada319b2019-05-29 22:12:38 +0200101 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
Nick Coghland5cacbb2015-05-23 22:24:10 +1000102 Example_Type_slots
103};
104
105/* Function of two integers returning integer */
106
107PyDoc_STRVAR(testexport_foo_doc,
108"foo(i,j)\n\
109\n\
110Return the sum of i and j.");
111
112static PyObject *
113testexport_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
125PyDoc_STRVAR(call_state_registration_func_doc,
126"register_state(0): call PyState_FindModule()\n\
127register_state(1): call PyState_AddModule()\n\
128register_state(2): call PyState_RemoveModule()");
129
130static PyObject *
131call_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
164static PyType_Slot Str_Type_slots[] = {
165 {Py_tp_base, NULL}, /* filled out in module exec function */
166 {0, 0},
167};
168
169static 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
177static 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
185static 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 */
Marcel Plchc2b0b122018-03-17 06:41:20 +0100227
Victor Stinner5b1ef202020-03-17 18:09:46 +0100228#define TEST_MODULE_DEF(name, slots, methods) { \
Nick Coghland5cacbb2015-05-23 22:24:10 +1000229 PyModuleDef_HEAD_INIT, /* m_base */ \
230 name, /* m_name */ \
231 PyDoc_STR("Test module " name), /* m_doc */ \
Victor Stinner5b1ef202020-03-17 18:09:46 +0100232 0, /* m_size */ \
Nick Coghland5cacbb2015-05-23 22:24:10 +1000233 methods, /* m_methods */ \
234 slots, /* m_slots */ \
Victor Stinner5b1ef202020-03-17 18:09:46 +0100235 NULL, /* m_traverse */ \
Nick Coghland5cacbb2015-05-23 22:24:10 +1000236 NULL, /* m_clear */ \
237 NULL, /* m_free */ \
238}
239
Benjamin Petersoncb4bae72018-07-06 21:05:51 -0700240static PyModuleDef_Slot main_slots[] = {
Nick Coghland5cacbb2015-05-23 22:24:10 +1000241 {Py_mod_exec, execfunc},
242 {0, NULL},
243};
244
245static PyModuleDef main_def = TEST_MODULE_DEF("main", main_slots, testexport_methods);
246
247PyMODINIT_FUNC
248PyInit__testmultiphase(PyObject *spec)
249{
250 return PyModuleDef_Init(&main_def);
251}
252
253
254/**** Importing a non-module object ****/
255
256static PyModuleDef def_nonmodule;
Nick Coghlan8682f572016-08-21 17:41:56 +1000257static PyModuleDef def_nonmodule_with_methods;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000258
259/* Create a SimpleNamespace(three=3) */
260static PyObject*
261createfunc_nonmodule(PyObject *spec, PyModuleDef *def)
262{
263 PyObject *dct, *ns, *three;
264
Nick Coghlan8682f572016-08-21 17:41:56 +1000265 if (def != &def_nonmodule && def != &def_nonmodule_with_methods) {
Nick Coghland5cacbb2015-05-23 22:24:10 +1000266 PyErr_SetString(PyExc_SystemError, "def does not match");
267 return NULL;
268 }
269
270 dct = PyDict_New();
271 if (dct == NULL)
272 return NULL;
273
274 three = PyLong_FromLong(3);
275 if (three == NULL) {
276 Py_DECREF(dct);
277 return NULL;
278 }
279 PyDict_SetItemString(dct, "three", three);
Nick Coghlana48db2b2015-05-24 01:03:46 +1000280 Py_DECREF(three);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000281
282 ns = _PyNamespace_New(dct);
283 Py_DECREF(dct);
284 return ns;
285}
286
287static PyModuleDef_Slot slots_create_nonmodule[] = {
288 {Py_mod_create, createfunc_nonmodule},
289 {0, NULL},
290};
291
292static PyModuleDef def_nonmodule = TEST_MODULE_DEF(
293 "_testmultiphase_nonmodule", slots_create_nonmodule, NULL);
294
295PyMODINIT_FUNC
296PyInit__testmultiphase_nonmodule(PyObject *spec)
297{
298 return PyModuleDef_Init(&def_nonmodule);
299}
300
Nick Coghlan8682f572016-08-21 17:41:56 +1000301PyDoc_STRVAR(nonmodule_bar_doc,
302"bar(i,j)\n\
303\n\
304Return the difference of i - j.");
305
306static PyObject *
307nonmodule_bar(PyObject *self, PyObject *args)
308{
309 long i, j;
310 long res;
311 if (!PyArg_ParseTuple(args, "ll:bar", &i, &j))
312 return NULL;
313 res = i - j;
314 return PyLong_FromLong(res);
315}
316
317static PyMethodDef nonmodule_methods[] = {
318 {"bar", nonmodule_bar, METH_VARARGS, nonmodule_bar_doc},
319 {NULL, NULL} /* sentinel */
320};
321
322static PyModuleDef def_nonmodule_with_methods = TEST_MODULE_DEF(
323 "_testmultiphase_nonmodule_with_methods", slots_create_nonmodule, nonmodule_methods);
324
325PyMODINIT_FUNC
326PyInit__testmultiphase_nonmodule_with_methods(PyObject *spec)
327{
328 return PyModuleDef_Init(&def_nonmodule_with_methods);
329}
330
Nick Coghland5cacbb2015-05-23 22:24:10 +1000331/**** Non-ASCII-named modules ****/
332
333static PyModuleDef def_nonascii_latin = { \
334 PyModuleDef_HEAD_INIT, /* m_base */
335 "_testmultiphase_nonascii_latin", /* m_name */
336 PyDoc_STR("Module named in Czech"), /* m_doc */
337 0, /* m_size */
338 NULL, /* m_methods */
339 NULL, /* m_slots */
340 NULL, /* m_traverse */
341 NULL, /* m_clear */
342 NULL, /* m_free */
343};
344
345PyMODINIT_FUNC
346PyInitU__testmultiphase_zkouka_naten_evc07gi8e(PyObject *spec)
347{
348 return PyModuleDef_Init(&def_nonascii_latin);
349}
350
351static PyModuleDef def_nonascii_kana = { \
352 PyModuleDef_HEAD_INIT, /* m_base */
353 "_testmultiphase_nonascii_kana", /* m_name */
354 PyDoc_STR("Module named in Japanese"), /* m_doc */
355 0, /* m_size */
356 NULL, /* m_methods */
357 NULL, /* m_slots */
358 NULL, /* m_traverse */
359 NULL, /* m_clear */
360 NULL, /* m_free */
361};
362
363PyMODINIT_FUNC
364PyInitU_eckzbwbhc6jpgzcx415x(PyObject *spec)
365{
366 return PyModuleDef_Init(&def_nonascii_kana);
367}
368
Benjamin Petersone20056c2015-05-29 17:10:30 -0500369/*** Module with a single-character name ***/
370
371PyMODINIT_FUNC
372PyInit_x(PyObject *spec)
373{
374 return PyModuleDef_Init(&main_def);
375}
376
Nick Coghland5cacbb2015-05-23 22:24:10 +1000377/**** Testing NULL slots ****/
378
379static PyModuleDef null_slots_def = TEST_MODULE_DEF(
380 "_testmultiphase_null_slots", NULL, NULL);
381
382PyMODINIT_FUNC
383PyInit__testmultiphase_null_slots(PyObject *spec)
384{
385 return PyModuleDef_Init(&null_slots_def);
386}
387
388/**** Problematic modules ****/
389
390static PyModuleDef_Slot slots_bad_large[] = {
391 {_Py_mod_LAST_SLOT + 1, NULL},
392 {0, NULL},
393};
394
395static PyModuleDef def_bad_large = TEST_MODULE_DEF(
396 "_testmultiphase_bad_slot_large", slots_bad_large, NULL);
397
398PyMODINIT_FUNC
399PyInit__testmultiphase_bad_slot_large(PyObject *spec)
400{
401 return PyModuleDef_Init(&def_bad_large);
402}
403
404static PyModuleDef_Slot slots_bad_negative[] = {
405 {-1, NULL},
406 {0, NULL},
407};
408
409static PyModuleDef def_bad_negative = TEST_MODULE_DEF(
410 "_testmultiphase_bad_slot_negative", slots_bad_negative, NULL);
411
412PyMODINIT_FUNC
413PyInit__testmultiphase_bad_slot_negative(PyObject *spec)
414{
415 return PyModuleDef_Init(&def_bad_negative);
416}
417
418static PyModuleDef def_create_int_with_state = { \
419 PyModuleDef_HEAD_INIT, /* m_base */
420 "create_with_state", /* m_name */
421 PyDoc_STR("Not a PyModuleObject object, but requests per-module state"),
422 10, /* m_size */
423 NULL, /* m_methods */
424 slots_create_nonmodule, /* m_slots */
425 NULL, /* m_traverse */
426 NULL, /* m_clear */
427 NULL, /* m_free */
428};
429
430PyMODINIT_FUNC
431PyInit__testmultiphase_create_int_with_state(PyObject *spec)
432{
433 return PyModuleDef_Init(&def_create_int_with_state);
434}
435
436
437static PyModuleDef def_negative_size = { \
438 PyModuleDef_HEAD_INIT, /* m_base */
439 "negative_size", /* m_name */
440 PyDoc_STR("PyModuleDef with negative m_size"),
441 -1, /* m_size */
442 NULL, /* m_methods */
443 slots_create_nonmodule, /* m_slots */
444 NULL, /* m_traverse */
445 NULL, /* m_clear */
446 NULL, /* m_free */
447};
448
449PyMODINIT_FUNC
450PyInit__testmultiphase_negative_size(PyObject *spec)
451{
452 return PyModuleDef_Init(&def_negative_size);
453}
454
455
456static PyModuleDef uninitialized_def = TEST_MODULE_DEF("main", main_slots, testexport_methods);
457
458PyMODINIT_FUNC
459PyInit__testmultiphase_export_uninitialized(PyObject *spec)
460{
461 return (PyObject*) &uninitialized_def;
462}
463
464PyMODINIT_FUNC
465PyInit__testmultiphase_export_null(PyObject *spec)
466{
467 return NULL;
468}
469
470PyMODINIT_FUNC
471PyInit__testmultiphase_export_raise(PyObject *spec)
472{
473 PyErr_SetString(PyExc_SystemError, "bad export function");
474 return NULL;
475}
476
477PyMODINIT_FUNC
478PyInit__testmultiphase_export_unreported_exception(PyObject *spec)
479{
480 PyErr_SetString(PyExc_SystemError, "bad export function");
481 return PyModuleDef_Init(&main_def);
482}
483
484static PyObject*
485createfunc_null(PyObject *spec, PyModuleDef *def)
486{
487 return NULL;
488}
489
Benjamin Petersoncb4bae72018-07-06 21:05:51 -0700490static PyModuleDef_Slot slots_create_null[] = {
Nick Coghland5cacbb2015-05-23 22:24:10 +1000491 {Py_mod_create, createfunc_null},
492 {0, NULL},
493};
494
495static PyModuleDef def_create_null = TEST_MODULE_DEF(
496 "_testmultiphase_create_null", slots_create_null, NULL);
497
498PyMODINIT_FUNC
499PyInit__testmultiphase_create_null(PyObject *spec)
500{
501 return PyModuleDef_Init(&def_create_null);
502}
503
504static PyObject*
505createfunc_raise(PyObject *spec, PyModuleDef *def)
506{
507 PyErr_SetString(PyExc_SystemError, "bad create function");
508 return NULL;
509}
510
511static PyModuleDef_Slot slots_create_raise[] = {
512 {Py_mod_create, createfunc_raise},
513 {0, NULL},
514};
515
516static PyModuleDef def_create_raise = TEST_MODULE_DEF(
517 "_testmultiphase_create_null", slots_create_raise, NULL);
518
519PyMODINIT_FUNC
520PyInit__testmultiphase_create_raise(PyObject *spec)
521{
522 return PyModuleDef_Init(&def_create_raise);
523}
524
525static PyObject*
526createfunc_unreported_exception(PyObject *spec, PyModuleDef *def)
527{
528 PyErr_SetString(PyExc_SystemError, "bad create function");
529 return PyModule_New("foo");
530}
531
532static PyModuleDef_Slot slots_create_unreported_exception[] = {
533 {Py_mod_create, createfunc_unreported_exception},
534 {0, NULL},
535};
536
537static PyModuleDef def_create_unreported_exception = TEST_MODULE_DEF(
538 "_testmultiphase_create_unreported_exception", slots_create_unreported_exception, NULL);
539
540PyMODINIT_FUNC
541PyInit__testmultiphase_create_unreported_exception(PyObject *spec)
542{
543 return PyModuleDef_Init(&def_create_unreported_exception);
544}
545
546static PyModuleDef_Slot slots_nonmodule_with_exec_slots[] = {
547 {Py_mod_create, createfunc_nonmodule},
548 {Py_mod_exec, execfunc},
549 {0, NULL},
550};
551
552static PyModuleDef def_nonmodule_with_exec_slots = TEST_MODULE_DEF(
553 "_testmultiphase_nonmodule_with_exec_slots", slots_nonmodule_with_exec_slots, NULL);
554
555PyMODINIT_FUNC
556PyInit__testmultiphase_nonmodule_with_exec_slots(PyObject *spec)
557{
558 return PyModuleDef_Init(&def_nonmodule_with_exec_slots);
559}
560
561static int
562execfunc_err(PyObject *mod)
563{
564 return -1;
565}
566
567static PyModuleDef_Slot slots_exec_err[] = {
568 {Py_mod_exec, execfunc_err},
569 {0, NULL},
570};
571
572static PyModuleDef def_exec_err = TEST_MODULE_DEF(
573 "_testmultiphase_exec_err", slots_exec_err, NULL);
574
575PyMODINIT_FUNC
576PyInit__testmultiphase_exec_err(PyObject *spec)
577{
578 return PyModuleDef_Init(&def_exec_err);
579}
580
581static int
582execfunc_raise(PyObject *spec)
583{
584 PyErr_SetString(PyExc_SystemError, "bad exec function");
585 return -1;
586}
587
588static PyModuleDef_Slot slots_exec_raise[] = {
589 {Py_mod_exec, execfunc_raise},
590 {0, NULL},
591};
592
593static PyModuleDef def_exec_raise = TEST_MODULE_DEF(
594 "_testmultiphase_exec_raise", slots_exec_raise, NULL);
595
596PyMODINIT_FUNC
597PyInit__testmultiphase_exec_raise(PyObject *mod)
598{
599 return PyModuleDef_Init(&def_exec_raise);
600}
601
602static int
603execfunc_unreported_exception(PyObject *mod)
604{
605 PyErr_SetString(PyExc_SystemError, "bad exec function");
606 return 0;
607}
608
609static PyModuleDef_Slot slots_exec_unreported_exception[] = {
610 {Py_mod_exec, execfunc_unreported_exception},
611 {0, NULL},
612};
613
614static PyModuleDef def_exec_unreported_exception = TEST_MODULE_DEF(
615 "_testmultiphase_exec_unreported_exception", slots_exec_unreported_exception, NULL);
616
617PyMODINIT_FUNC
618PyInit__testmultiphase_exec_unreported_exception(PyObject *spec)
619{
620 return PyModuleDef_Init(&def_exec_unreported_exception);
621}
Nick Coghlan9d3c61c2015-09-05 21:05:05 +1000622
623/*** Helper for imp test ***/
624
625static PyModuleDef imp_dummy_def = TEST_MODULE_DEF("imp_dummy", main_slots, testexport_methods);
626
627PyMODINIT_FUNC
628PyInit_imp_dummy(PyObject *spec)
629{
630 return PyModuleDef_Init(&imp_dummy_def);
631}
Marcel Plchc2b0b122018-03-17 06:41:20 +0100632