Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 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 | |
Miss Islington (bot) | 136905f | 2018-03-16 23:03:56 -0700 | [diff] [blame^] | 13 | typedef struct { |
| 14 | PyObject *integer; |
| 15 | } testmultiphase_state; |
| 16 | |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 17 | /* Example methods */ |
| 18 | |
Nick Coghlan | 53f9502 | 2015-06-04 21:52:57 +1000 | [diff] [blame] | 19 | static int |
| 20 | Example_traverse(ExampleObject *self, visitproc visit, void *arg) |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 21 | { |
Nick Coghlan | 53f9502 | 2015-06-04 21:52:57 +1000 | [diff] [blame] | 22 | Py_VISIT(self->x_attr); |
| 23 | return 0; |
| 24 | } |
| 25 | |
| 26 | static int |
| 27 | Example_finalize(ExampleObject *self) |
| 28 | { |
| 29 | Py_CLEAR(self->x_attr); |
| 30 | return 0; |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | static PyObject * |
| 34 | Example_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 Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 43 | Py_RETURN_NONE; |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | |
| 47 | static PyMethodDef Example_methods[] = { |
| 48 | {"demo", (PyCFunction)Example_demo, METH_VARARGS, |
| 49 | PyDoc_STR("demo() -> None")}, |
| 50 | {NULL, NULL} /* sentinel */ |
| 51 | }; |
| 52 | |
| 53 | static PyObject * |
| 54 | Example_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 | |
| 66 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 67 | Example_setattr(ExampleObject *self, const char *name, PyObject *v) |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 68 | { |
| 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 | |
| 85 | static PyType_Slot Example_Type_slots[] = { |
| 86 | {Py_tp_doc, "The Example type"}, |
Nick Coghlan | 53f9502 | 2015-06-04 21:52:57 +1000 | [diff] [blame] | 87 | {Py_tp_finalize, Example_finalize}, |
| 88 | {Py_tp_traverse, Example_traverse}, |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 89 | {Py_tp_getattro, Example_getattro}, |
| 90 | {Py_tp_setattr, Example_setattr}, |
| 91 | {Py_tp_methods, Example_methods}, |
| 92 | {0, 0}, |
| 93 | }; |
| 94 | |
| 95 | static PyType_Spec Example_Type_spec = { |
| 96 | "_testimportexec.Example", |
| 97 | sizeof(ExampleObject), |
| 98 | 0, |
Nick Coghlan | 53f9502 | 2015-06-04 21:52:57 +1000 | [diff] [blame] | 99 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 100 | Example_Type_slots |
| 101 | }; |
| 102 | |
| 103 | /* Function of two integers returning integer */ |
| 104 | |
| 105 | PyDoc_STRVAR(testexport_foo_doc, |
| 106 | "foo(i,j)\n\ |
| 107 | \n\ |
| 108 | Return the sum of i and j."); |
| 109 | |
| 110 | static PyObject * |
| 111 | testexport_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 | |
| 123 | PyDoc_STRVAR(call_state_registration_func_doc, |
| 124 | "register_state(0): call PyState_FindModule()\n\ |
| 125 | register_state(1): call PyState_AddModule()\n\ |
| 126 | register_state(2): call PyState_RemoveModule()"); |
| 127 | |
| 128 | static PyObject * |
| 129 | call_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 | |
| 162 | static PyType_Slot Str_Type_slots[] = { |
| 163 | {Py_tp_base, NULL}, /* filled out in module exec function */ |
| 164 | {0, 0}, |
| 165 | }; |
| 166 | |
| 167 | static 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 | |
| 175 | static 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 | |
| 183 | static 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) | 136905f | 2018-03-16 23:03:56 -0700 | [diff] [blame^] | 225 | |
| 226 | #define TEST_MODULE_DEF_EX(name, slots, methods, statesize, traversefunc) { \ |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 227 | PyModuleDef_HEAD_INIT, /* m_base */ \ |
| 228 | name, /* m_name */ \ |
| 229 | PyDoc_STR("Test module " name), /* m_doc */ \ |
Miss Islington (bot) | 136905f | 2018-03-16 23:03:56 -0700 | [diff] [blame^] | 230 | statesize, /* m_size */ \ |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 231 | methods, /* m_methods */ \ |
| 232 | slots, /* m_slots */ \ |
Miss Islington (bot) | 136905f | 2018-03-16 23:03:56 -0700 | [diff] [blame^] | 233 | traversefunc, /* m_traverse */ \ |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 234 | NULL, /* m_clear */ \ |
| 235 | NULL, /* m_free */ \ |
| 236 | } |
| 237 | |
Miss Islington (bot) | 136905f | 2018-03-16 23:03:56 -0700 | [diff] [blame^] | 238 | #define TEST_MODULE_DEF(name, slots, methods) TEST_MODULE_DEF_EX(name, slots, methods, 0, NULL) |
| 239 | |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 240 | PyModuleDef_Slot main_slots[] = { |
| 241 | {Py_mod_exec, execfunc}, |
| 242 | {0, NULL}, |
| 243 | }; |
| 244 | |
| 245 | static PyModuleDef main_def = TEST_MODULE_DEF("main", main_slots, testexport_methods); |
| 246 | |
| 247 | PyMODINIT_FUNC |
| 248 | PyInit__testmultiphase(PyObject *spec) |
| 249 | { |
| 250 | return PyModuleDef_Init(&main_def); |
| 251 | } |
| 252 | |
| 253 | |
| 254 | /**** Importing a non-module object ****/ |
| 255 | |
| 256 | static PyModuleDef def_nonmodule; |
Nick Coghlan | 8682f57 | 2016-08-21 17:41:56 +1000 | [diff] [blame] | 257 | static PyModuleDef def_nonmodule_with_methods; |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 258 | |
| 259 | /* Create a SimpleNamespace(three=3) */ |
| 260 | static PyObject* |
| 261 | createfunc_nonmodule(PyObject *spec, PyModuleDef *def) |
| 262 | { |
| 263 | PyObject *dct, *ns, *three; |
| 264 | |
Nick Coghlan | 8682f57 | 2016-08-21 17:41:56 +1000 | [diff] [blame] | 265 | if (def != &def_nonmodule && def != &def_nonmodule_with_methods) { |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 266 | 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 Coghlan | a48db2b | 2015-05-24 01:03:46 +1000 | [diff] [blame] | 280 | Py_DECREF(three); |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 281 | |
| 282 | ns = _PyNamespace_New(dct); |
| 283 | Py_DECREF(dct); |
| 284 | return ns; |
| 285 | } |
| 286 | |
| 287 | static PyModuleDef_Slot slots_create_nonmodule[] = { |
| 288 | {Py_mod_create, createfunc_nonmodule}, |
| 289 | {0, NULL}, |
| 290 | }; |
| 291 | |
| 292 | static PyModuleDef def_nonmodule = TEST_MODULE_DEF( |
| 293 | "_testmultiphase_nonmodule", slots_create_nonmodule, NULL); |
| 294 | |
| 295 | PyMODINIT_FUNC |
| 296 | PyInit__testmultiphase_nonmodule(PyObject *spec) |
| 297 | { |
| 298 | return PyModuleDef_Init(&def_nonmodule); |
| 299 | } |
| 300 | |
Nick Coghlan | 8682f57 | 2016-08-21 17:41:56 +1000 | [diff] [blame] | 301 | PyDoc_STRVAR(nonmodule_bar_doc, |
| 302 | "bar(i,j)\n\ |
| 303 | \n\ |
| 304 | Return the difference of i - j."); |
| 305 | |
| 306 | static PyObject * |
| 307 | nonmodule_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 | |
| 317 | static PyMethodDef nonmodule_methods[] = { |
| 318 | {"bar", nonmodule_bar, METH_VARARGS, nonmodule_bar_doc}, |
| 319 | {NULL, NULL} /* sentinel */ |
| 320 | }; |
| 321 | |
| 322 | static PyModuleDef def_nonmodule_with_methods = TEST_MODULE_DEF( |
| 323 | "_testmultiphase_nonmodule_with_methods", slots_create_nonmodule, nonmodule_methods); |
| 324 | |
| 325 | PyMODINIT_FUNC |
| 326 | PyInit__testmultiphase_nonmodule_with_methods(PyObject *spec) |
| 327 | { |
| 328 | return PyModuleDef_Init(&def_nonmodule_with_methods); |
| 329 | } |
| 330 | |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 331 | /**** Non-ASCII-named modules ****/ |
| 332 | |
| 333 | static 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 | |
| 345 | PyMODINIT_FUNC |
| 346 | PyInitU__testmultiphase_zkouka_naten_evc07gi8e(PyObject *spec) |
| 347 | { |
| 348 | return PyModuleDef_Init(&def_nonascii_latin); |
| 349 | } |
| 350 | |
| 351 | static 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 | |
| 363 | PyMODINIT_FUNC |
| 364 | PyInitU_eckzbwbhc6jpgzcx415x(PyObject *spec) |
| 365 | { |
| 366 | return PyModuleDef_Init(&def_nonascii_kana); |
| 367 | } |
| 368 | |
Benjamin Peterson | e20056c | 2015-05-29 17:10:30 -0500 | [diff] [blame] | 369 | /*** Module with a single-character name ***/ |
| 370 | |
| 371 | PyMODINIT_FUNC |
| 372 | PyInit_x(PyObject *spec) |
| 373 | { |
| 374 | return PyModuleDef_Init(&main_def); |
| 375 | } |
| 376 | |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 377 | /**** Testing NULL slots ****/ |
| 378 | |
| 379 | static PyModuleDef null_slots_def = TEST_MODULE_DEF( |
| 380 | "_testmultiphase_null_slots", NULL, NULL); |
| 381 | |
| 382 | PyMODINIT_FUNC |
| 383 | PyInit__testmultiphase_null_slots(PyObject *spec) |
| 384 | { |
| 385 | return PyModuleDef_Init(&null_slots_def); |
| 386 | } |
| 387 | |
| 388 | /**** Problematic modules ****/ |
| 389 | |
| 390 | static PyModuleDef_Slot slots_bad_large[] = { |
| 391 | {_Py_mod_LAST_SLOT + 1, NULL}, |
| 392 | {0, NULL}, |
| 393 | }; |
| 394 | |
| 395 | static PyModuleDef def_bad_large = TEST_MODULE_DEF( |
| 396 | "_testmultiphase_bad_slot_large", slots_bad_large, NULL); |
| 397 | |
| 398 | PyMODINIT_FUNC |
| 399 | PyInit__testmultiphase_bad_slot_large(PyObject *spec) |
| 400 | { |
| 401 | return PyModuleDef_Init(&def_bad_large); |
| 402 | } |
| 403 | |
| 404 | static PyModuleDef_Slot slots_bad_negative[] = { |
| 405 | {-1, NULL}, |
| 406 | {0, NULL}, |
| 407 | }; |
| 408 | |
| 409 | static PyModuleDef def_bad_negative = TEST_MODULE_DEF( |
| 410 | "_testmultiphase_bad_slot_negative", slots_bad_negative, NULL); |
| 411 | |
| 412 | PyMODINIT_FUNC |
| 413 | PyInit__testmultiphase_bad_slot_negative(PyObject *spec) |
| 414 | { |
| 415 | return PyModuleDef_Init(&def_bad_negative); |
| 416 | } |
| 417 | |
| 418 | static 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 | |
| 430 | PyMODINIT_FUNC |
| 431 | PyInit__testmultiphase_create_int_with_state(PyObject *spec) |
| 432 | { |
| 433 | return PyModuleDef_Init(&def_create_int_with_state); |
| 434 | } |
| 435 | |
| 436 | |
| 437 | static 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 | |
| 449 | PyMODINIT_FUNC |
| 450 | PyInit__testmultiphase_negative_size(PyObject *spec) |
| 451 | { |
| 452 | return PyModuleDef_Init(&def_negative_size); |
| 453 | } |
| 454 | |
| 455 | |
| 456 | static PyModuleDef uninitialized_def = TEST_MODULE_DEF("main", main_slots, testexport_methods); |
| 457 | |
| 458 | PyMODINIT_FUNC |
| 459 | PyInit__testmultiphase_export_uninitialized(PyObject *spec) |
| 460 | { |
| 461 | return (PyObject*) &uninitialized_def; |
| 462 | } |
| 463 | |
| 464 | PyMODINIT_FUNC |
| 465 | PyInit__testmultiphase_export_null(PyObject *spec) |
| 466 | { |
| 467 | return NULL; |
| 468 | } |
| 469 | |
| 470 | PyMODINIT_FUNC |
| 471 | PyInit__testmultiphase_export_raise(PyObject *spec) |
| 472 | { |
| 473 | PyErr_SetString(PyExc_SystemError, "bad export function"); |
| 474 | return NULL; |
| 475 | } |
| 476 | |
| 477 | PyMODINIT_FUNC |
| 478 | PyInit__testmultiphase_export_unreported_exception(PyObject *spec) |
| 479 | { |
| 480 | PyErr_SetString(PyExc_SystemError, "bad export function"); |
| 481 | return PyModuleDef_Init(&main_def); |
| 482 | } |
| 483 | |
| 484 | static PyObject* |
| 485 | createfunc_null(PyObject *spec, PyModuleDef *def) |
| 486 | { |
| 487 | return NULL; |
| 488 | } |
| 489 | |
| 490 | PyModuleDef_Slot slots_create_null[] = { |
| 491 | {Py_mod_create, createfunc_null}, |
| 492 | {0, NULL}, |
| 493 | }; |
| 494 | |
| 495 | static PyModuleDef def_create_null = TEST_MODULE_DEF( |
| 496 | "_testmultiphase_create_null", slots_create_null, NULL); |
| 497 | |
| 498 | PyMODINIT_FUNC |
| 499 | PyInit__testmultiphase_create_null(PyObject *spec) |
| 500 | { |
| 501 | return PyModuleDef_Init(&def_create_null); |
| 502 | } |
| 503 | |
| 504 | static PyObject* |
| 505 | createfunc_raise(PyObject *spec, PyModuleDef *def) |
| 506 | { |
| 507 | PyErr_SetString(PyExc_SystemError, "bad create function"); |
| 508 | return NULL; |
| 509 | } |
| 510 | |
| 511 | static PyModuleDef_Slot slots_create_raise[] = { |
| 512 | {Py_mod_create, createfunc_raise}, |
| 513 | {0, NULL}, |
| 514 | }; |
| 515 | |
| 516 | static PyModuleDef def_create_raise = TEST_MODULE_DEF( |
| 517 | "_testmultiphase_create_null", slots_create_raise, NULL); |
| 518 | |
| 519 | PyMODINIT_FUNC |
| 520 | PyInit__testmultiphase_create_raise(PyObject *spec) |
| 521 | { |
| 522 | return PyModuleDef_Init(&def_create_raise); |
| 523 | } |
| 524 | |
| 525 | static PyObject* |
| 526 | createfunc_unreported_exception(PyObject *spec, PyModuleDef *def) |
| 527 | { |
| 528 | PyErr_SetString(PyExc_SystemError, "bad create function"); |
| 529 | return PyModule_New("foo"); |
| 530 | } |
| 531 | |
| 532 | static PyModuleDef_Slot slots_create_unreported_exception[] = { |
| 533 | {Py_mod_create, createfunc_unreported_exception}, |
| 534 | {0, NULL}, |
| 535 | }; |
| 536 | |
| 537 | static PyModuleDef def_create_unreported_exception = TEST_MODULE_DEF( |
| 538 | "_testmultiphase_create_unreported_exception", slots_create_unreported_exception, NULL); |
| 539 | |
| 540 | PyMODINIT_FUNC |
| 541 | PyInit__testmultiphase_create_unreported_exception(PyObject *spec) |
| 542 | { |
| 543 | return PyModuleDef_Init(&def_create_unreported_exception); |
| 544 | } |
| 545 | |
| 546 | static PyModuleDef_Slot slots_nonmodule_with_exec_slots[] = { |
| 547 | {Py_mod_create, createfunc_nonmodule}, |
| 548 | {Py_mod_exec, execfunc}, |
| 549 | {0, NULL}, |
| 550 | }; |
| 551 | |
| 552 | static PyModuleDef def_nonmodule_with_exec_slots = TEST_MODULE_DEF( |
| 553 | "_testmultiphase_nonmodule_with_exec_slots", slots_nonmodule_with_exec_slots, NULL); |
| 554 | |
| 555 | PyMODINIT_FUNC |
| 556 | PyInit__testmultiphase_nonmodule_with_exec_slots(PyObject *spec) |
| 557 | { |
| 558 | return PyModuleDef_Init(&def_nonmodule_with_exec_slots); |
| 559 | } |
| 560 | |
| 561 | static int |
| 562 | execfunc_err(PyObject *mod) |
| 563 | { |
| 564 | return -1; |
| 565 | } |
| 566 | |
| 567 | static PyModuleDef_Slot slots_exec_err[] = { |
| 568 | {Py_mod_exec, execfunc_err}, |
| 569 | {0, NULL}, |
| 570 | }; |
| 571 | |
| 572 | static PyModuleDef def_exec_err = TEST_MODULE_DEF( |
| 573 | "_testmultiphase_exec_err", slots_exec_err, NULL); |
| 574 | |
| 575 | PyMODINIT_FUNC |
| 576 | PyInit__testmultiphase_exec_err(PyObject *spec) |
| 577 | { |
| 578 | return PyModuleDef_Init(&def_exec_err); |
| 579 | } |
| 580 | |
| 581 | static int |
| 582 | execfunc_raise(PyObject *spec) |
| 583 | { |
| 584 | PyErr_SetString(PyExc_SystemError, "bad exec function"); |
| 585 | return -1; |
| 586 | } |
| 587 | |
| 588 | static PyModuleDef_Slot slots_exec_raise[] = { |
| 589 | {Py_mod_exec, execfunc_raise}, |
| 590 | {0, NULL}, |
| 591 | }; |
| 592 | |
| 593 | static PyModuleDef def_exec_raise = TEST_MODULE_DEF( |
| 594 | "_testmultiphase_exec_raise", slots_exec_raise, NULL); |
| 595 | |
| 596 | PyMODINIT_FUNC |
| 597 | PyInit__testmultiphase_exec_raise(PyObject *mod) |
| 598 | { |
| 599 | return PyModuleDef_Init(&def_exec_raise); |
| 600 | } |
| 601 | |
| 602 | static int |
| 603 | execfunc_unreported_exception(PyObject *mod) |
| 604 | { |
| 605 | PyErr_SetString(PyExc_SystemError, "bad exec function"); |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | static PyModuleDef_Slot slots_exec_unreported_exception[] = { |
| 610 | {Py_mod_exec, execfunc_unreported_exception}, |
| 611 | {0, NULL}, |
| 612 | }; |
| 613 | |
| 614 | static PyModuleDef def_exec_unreported_exception = TEST_MODULE_DEF( |
| 615 | "_testmultiphase_exec_unreported_exception", slots_exec_unreported_exception, NULL); |
| 616 | |
| 617 | PyMODINIT_FUNC |
| 618 | PyInit__testmultiphase_exec_unreported_exception(PyObject *spec) |
| 619 | { |
| 620 | return PyModuleDef_Init(&def_exec_unreported_exception); |
| 621 | } |
Nick Coghlan | 9d3c61c | 2015-09-05 21:05:05 +1000 | [diff] [blame] | 622 | |
Miss Islington (bot) | 136905f | 2018-03-16 23:03:56 -0700 | [diff] [blame^] | 623 | static int |
| 624 | bad_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 | |
| 632 | static int |
| 633 | execfunc_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 | |
| 647 | static PyModuleDef_Slot slots_with_bad_traverse[] = { |
| 648 | {Py_mod_exec, execfunc_with_bad_traverse}, |
| 649 | {0, NULL} |
| 650 | }; |
| 651 | |
| 652 | static 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 | |
| 656 | PyMODINIT_FUNC |
| 657 | PyInit__testmultiphase_with_bad_traverse(PyObject *spec) { |
| 658 | return PyModuleDef_Init(&def_with_bad_traverse); |
| 659 | } |
| 660 | |
Nick Coghlan | 9d3c61c | 2015-09-05 21:05:05 +1000 | [diff] [blame] | 661 | /*** Helper for imp test ***/ |
| 662 | |
| 663 | static PyModuleDef imp_dummy_def = TEST_MODULE_DEF("imp_dummy", main_slots, testexport_methods); |
| 664 | |
| 665 | PyMODINIT_FUNC |
| 666 | PyInit_imp_dummy(PyObject *spec) |
| 667 | { |
| 668 | return PyModuleDef_Init(&imp_dummy_def); |
| 669 | } |
Miss Islington (bot) | 136905f | 2018-03-16 23:03:56 -0700 | [diff] [blame^] | 670 | |