blob: 8090a485f4a9f18bfc0333cff3f1158db4d690e6 [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
Miss Islington (bot)136905f2018-03-16 23:03:56 -070013typedef 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
26static int
27Example_finalize(ExampleObject *self)
28{
29 Py_CLEAR(self->x_attr);
30 return 0;
Nick Coghland5cacbb2015-05-23 22:24:10 +100031}
32
33static PyObject *
34Example_demo(ExampleObject *self, PyObject *args)
35{
36 PyObject *o = NULL;
37 if (!PyArg_ParseTuple(args, "|O:demo", &o))
38 return NULL;
39 if (o != NULL && PyUnicode_Check(o)) {
40 Py_INCREF(o);
41 return o;
42 }
Serhiy Storchaka228b12e2017-01-23 09:47:21 +020043 Py_RETURN_NONE;
Nick Coghland5cacbb2015-05-23 22:24:10 +100044}
45
46
47static PyMethodDef Example_methods[] = {
48 {"demo", (PyCFunction)Example_demo, METH_VARARGS,
49 PyDoc_STR("demo() -> None")},
50 {NULL, NULL} /* sentinel */
51};
52
53static PyObject *
54Example_getattro(ExampleObject *self, PyObject *name)
55{
56 if (self->x_attr != NULL) {
57 PyObject *v = PyDict_GetItem(self->x_attr, name);
58 if (v != NULL) {
59 Py_INCREF(v);
60 return v;
61 }
62 }
63 return PyObject_GenericGetAttr((PyObject *)self, name);
64}
65
66static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020067Example_setattr(ExampleObject *self, const char *name, PyObject *v)
Nick Coghland5cacbb2015-05-23 22:24:10 +100068{
69 if (self->x_attr == NULL) {
70 self->x_attr = PyDict_New();
71 if (self->x_attr == NULL)
72 return -1;
73 }
74 if (v == NULL) {
75 int rv = PyDict_DelItemString(self->x_attr, name);
76 if (rv < 0)
77 PyErr_SetString(PyExc_AttributeError,
78 "delete non-existing Example attribute");
79 return rv;
80 }
81 else
82 return PyDict_SetItemString(self->x_attr, name, v);
83}
84
85static PyType_Slot Example_Type_slots[] = {
86 {Py_tp_doc, "The Example type"},
Nick Coghlan53f95022015-06-04 21:52:57 +100087 {Py_tp_finalize, Example_finalize},
88 {Py_tp_traverse, Example_traverse},
Nick Coghland5cacbb2015-05-23 22:24:10 +100089 {Py_tp_getattro, Example_getattro},
90 {Py_tp_setattr, Example_setattr},
91 {Py_tp_methods, Example_methods},
92 {0, 0},
93};
94
95static PyType_Spec Example_Type_spec = {
96 "_testimportexec.Example",
97 sizeof(ExampleObject),
98 0,
Nick Coghlan53f95022015-06-04 21:52:57 +100099 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE,
Nick Coghland5cacbb2015-05-23 22:24:10 +1000100 Example_Type_slots
101};
102
103/* Function of two integers returning integer */
104
105PyDoc_STRVAR(testexport_foo_doc,
106"foo(i,j)\n\
107\n\
108Return the sum of i and j.");
109
110static PyObject *
111testexport_foo(PyObject *self, PyObject *args)
112{
113 long i, j;
114 long res;
115 if (!PyArg_ParseTuple(args, "ll:foo", &i, &j))
116 return NULL;
117 res = i + j;
118 return PyLong_FromLong(res);
119}
120
121/* Test that PyState registration fails */
122
123PyDoc_STRVAR(call_state_registration_func_doc,
124"register_state(0): call PyState_FindModule()\n\
125register_state(1): call PyState_AddModule()\n\
126register_state(2): call PyState_RemoveModule()");
127
128static PyObject *
129call_state_registration_func(PyObject *mod, PyObject *args)
130{
131 int i, ret;
132 PyModuleDef *def = PyModule_GetDef(mod);
133 if (def == NULL) {
134 return NULL;
135 }
136 if (!PyArg_ParseTuple(args, "i:call_state_registration_func", &i))
137 return NULL;
138 switch (i) {
139 case 0:
140 mod = PyState_FindModule(def);
141 if (mod == NULL) {
142 Py_RETURN_NONE;
143 }
144 return mod;
145 case 1:
146 ret = PyState_AddModule(mod, def);
147 if (ret != 0) {
148 return NULL;
149 }
150 break;
151 case 2:
152 ret = PyState_RemoveModule(def);
153 if (ret != 0) {
154 return NULL;
155 }
156 break;
157 }
158 Py_RETURN_NONE;
159}
160
161
162static PyType_Slot Str_Type_slots[] = {
163 {Py_tp_base, NULL}, /* filled out in module exec function */
164 {0, 0},
165};
166
167static PyType_Spec Str_Type_spec = {
168 "_testimportexec.Str",
169 0,
170 0,
171 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
172 Str_Type_slots
173};
174
175static PyMethodDef testexport_methods[] = {
176 {"foo", testexport_foo, METH_VARARGS,
177 testexport_foo_doc},
178 {"call_state_registration_func", call_state_registration_func,
179 METH_VARARGS, call_state_registration_func_doc},
180 {NULL, NULL} /* sentinel */
181};
182
183static int execfunc(PyObject *m)
184{
185 PyObject *temp = NULL;
186
187 /* Due to cross platform compiler issues the slots must be filled
188 * here. It's required for portability to Windows without requiring
189 * C++. */
190 Str_Type_slots[0].pfunc = &PyUnicode_Type;
191
192 /* Add a custom type */
193 temp = PyType_FromSpec(&Example_Type_spec);
194 if (temp == NULL)
195 goto fail;
196 if (PyModule_AddObject(m, "Example", temp) != 0)
197 goto fail;
198
199 /* Add an exception type */
200 temp = PyErr_NewException("_testimportexec.error", NULL, NULL);
201 if (temp == NULL)
202 goto fail;
203 if (PyModule_AddObject(m, "error", temp) != 0)
204 goto fail;
205
206 /* Add Str */
207 temp = PyType_FromSpec(&Str_Type_spec);
208 if (temp == NULL)
209 goto fail;
210 if (PyModule_AddObject(m, "Str", temp) != 0)
211 goto fail;
212
213 if (PyModule_AddIntConstant(m, "int_const", 1969) != 0)
214 goto fail;
215
216 if (PyModule_AddStringConstant(m, "str_const", "something different") != 0)
217 goto fail;
218
219 return 0;
220 fail:
221 return -1;
222}
223
224/* Helper for module definitions; there'll be a lot of them */
Miss Islington (bot)136905f2018-03-16 23:03:56 -0700225
226#define TEST_MODULE_DEF_EX(name, slots, methods, statesize, traversefunc) { \
Nick Coghland5cacbb2015-05-23 22:24:10 +1000227 PyModuleDef_HEAD_INIT, /* m_base */ \
228 name, /* m_name */ \
229 PyDoc_STR("Test module " name), /* m_doc */ \
Miss Islington (bot)136905f2018-03-16 23:03:56 -0700230 statesize, /* m_size */ \
Nick Coghland5cacbb2015-05-23 22:24:10 +1000231 methods, /* m_methods */ \
232 slots, /* m_slots */ \
Miss Islington (bot)136905f2018-03-16 23:03:56 -0700233 traversefunc, /* m_traverse */ \
Nick Coghland5cacbb2015-05-23 22:24:10 +1000234 NULL, /* m_clear */ \
235 NULL, /* m_free */ \
236}
237
Miss Islington (bot)136905f2018-03-16 23:03:56 -0700238#define TEST_MODULE_DEF(name, slots, methods) TEST_MODULE_DEF_EX(name, slots, methods, 0, NULL)
239
Nick Coghland5cacbb2015-05-23 22:24:10 +1000240PyModuleDef_Slot main_slots[] = {
241 {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
490PyModuleDef_Slot slots_create_null[] = {
491 {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
Miss Islington (bot)136905f2018-03-16 23:03:56 -0700623static int
624bad_traverse(PyObject *self, visitproc visit, void *arg) {
625 testmultiphase_state *m_state;
626
627 m_state = PyModule_GetState(self);
628 Py_VISIT(m_state->integer);
629 return 0;
630}
631
632static int
633execfunc_with_bad_traverse(PyObject *mod) {
634 testmultiphase_state *m_state;
635
636 m_state = PyModule_GetState(mod);
637 if (m_state == NULL) {
638 return -1;
639 }
640
641 m_state->integer = PyLong_FromLong(0x7fffffff);
642 Py_INCREF(m_state->integer);
643
644 return 0;
645}
646
647static PyModuleDef_Slot slots_with_bad_traverse[] = {
648 {Py_mod_exec, execfunc_with_bad_traverse},
649 {0, NULL}
650};
651
652static PyModuleDef def_with_bad_traverse = TEST_MODULE_DEF_EX(
653 "_testmultiphase_with_bad_traverse", slots_with_bad_traverse, NULL,
654 sizeof(testmultiphase_state), bad_traverse);
655
656PyMODINIT_FUNC
657PyInit__testmultiphase_with_bad_traverse(PyObject *spec) {
658 return PyModuleDef_Init(&def_with_bad_traverse);
659}
660
Nick Coghlan9d3c61c2015-09-05 21:05:05 +1000661/*** Helper for imp test ***/
662
663static PyModuleDef imp_dummy_def = TEST_MODULE_DEF("imp_dummy", main_slots, testexport_methods);
664
665PyMODINIT_FUNC
666PyInit_imp_dummy(PyObject *spec)
667{
668 return PyModuleDef_Init(&imp_dummy_def);
669}
Miss Islington (bot)136905f2018-03-16 23:03:56 -0700670