You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

584 lines
15 KiB

  1. /* Testing module for multi-phase initialization of extension modules (PEP 489)
  2. */
  3. #include "Python.h"
  4. /* Example objects */
  5. typedef struct {
  6. PyObject_HEAD
  7. PyObject *x_attr; /* Attributes dictionary */
  8. } ExampleObject;
  9. /* Example methods */
  10. static int
  11. Example_traverse(ExampleObject *self, visitproc visit, void *arg)
  12. {
  13. Py_VISIT(self->x_attr);
  14. return 0;
  15. }
  16. static int
  17. Example_finalize(ExampleObject *self)
  18. {
  19. Py_CLEAR(self->x_attr);
  20. return 0;
  21. }
  22. static PyObject *
  23. Example_demo(ExampleObject *self, PyObject *args)
  24. {
  25. PyObject *o = NULL;
  26. if (!PyArg_ParseTuple(args, "|O:demo", &o))
  27. return NULL;
  28. if (o != NULL && PyUnicode_Check(o)) {
  29. Py_INCREF(o);
  30. return o;
  31. }
  32. Py_INCREF(Py_None);
  33. return Py_None;
  34. }
  35. static PyMethodDef Example_methods[] = {
  36. {"demo", (PyCFunction)Example_demo, METH_VARARGS,
  37. PyDoc_STR("demo() -> None")},
  38. {NULL, NULL} /* sentinel */
  39. };
  40. static PyObject *
  41. Example_getattro(ExampleObject *self, PyObject *name)
  42. {
  43. if (self->x_attr != NULL) {
  44. PyObject *v = PyDict_GetItem(self->x_attr, name);
  45. if (v != NULL) {
  46. Py_INCREF(v);
  47. return v;
  48. }
  49. }
  50. return PyObject_GenericGetAttr((PyObject *)self, name);
  51. }
  52. static int
  53. Example_setattr(ExampleObject *self, char *name, PyObject *v)
  54. {
  55. if (self->x_attr == NULL) {
  56. self->x_attr = PyDict_New();
  57. if (self->x_attr == NULL)
  58. return -1;
  59. }
  60. if (v == NULL) {
  61. int rv = PyDict_DelItemString(self->x_attr, name);
  62. if (rv < 0)
  63. PyErr_SetString(PyExc_AttributeError,
  64. "delete non-existing Example attribute");
  65. return rv;
  66. }
  67. else
  68. return PyDict_SetItemString(self->x_attr, name, v);
  69. }
  70. static PyType_Slot Example_Type_slots[] = {
  71. {Py_tp_doc, "The Example type"},
  72. {Py_tp_finalize, Example_finalize},
  73. {Py_tp_traverse, Example_traverse},
  74. {Py_tp_getattro, Example_getattro},
  75. {Py_tp_setattr, Example_setattr},
  76. {Py_tp_methods, Example_methods},
  77. {0, 0},
  78. };
  79. static PyType_Spec Example_Type_spec = {
  80. "_testimportexec.Example",
  81. sizeof(ExampleObject),
  82. 0,
  83. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE,
  84. Example_Type_slots
  85. };
  86. /* Function of two integers returning integer */
  87. PyDoc_STRVAR(testexport_foo_doc,
  88. "foo(i,j)\n\
  89. \n\
  90. Return the sum of i and j.");
  91. static PyObject *
  92. testexport_foo(PyObject *self, PyObject *args)
  93. {
  94. long i, j;
  95. long res;
  96. if (!PyArg_ParseTuple(args, "ll:foo", &i, &j))
  97. return NULL;
  98. res = i + j;
  99. return PyLong_FromLong(res);
  100. }
  101. /* Test that PyState registration fails */
  102. PyDoc_STRVAR(call_state_registration_func_doc,
  103. "register_state(0): call PyState_FindModule()\n\
  104. register_state(1): call PyState_AddModule()\n\
  105. register_state(2): call PyState_RemoveModule()");
  106. static PyObject *
  107. call_state_registration_func(PyObject *mod, PyObject *args)
  108. {
  109. int i, ret;
  110. PyModuleDef *def = PyModule_GetDef(mod);
  111. if (def == NULL) {
  112. return NULL;
  113. }
  114. if (!PyArg_ParseTuple(args, "i:call_state_registration_func", &i))
  115. return NULL;
  116. switch (i) {
  117. case 0:
  118. mod = PyState_FindModule(def);
  119. if (mod == NULL) {
  120. Py_RETURN_NONE;
  121. }
  122. return mod;
  123. case 1:
  124. ret = PyState_AddModule(mod, def);
  125. if (ret != 0) {
  126. return NULL;
  127. }
  128. break;
  129. case 2:
  130. ret = PyState_RemoveModule(def);
  131. if (ret != 0) {
  132. return NULL;
  133. }
  134. break;
  135. }
  136. Py_RETURN_NONE;
  137. }
  138. static PyType_Slot Str_Type_slots[] = {
  139. {Py_tp_base, NULL}, /* filled out in module exec function */
  140. {0, 0},
  141. };
  142. static PyType_Spec Str_Type_spec = {
  143. "_testimportexec.Str",
  144. 0,
  145. 0,
  146. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
  147. Str_Type_slots
  148. };
  149. static PyMethodDef testexport_methods[] = {
  150. {"foo", testexport_foo, METH_VARARGS,
  151. testexport_foo_doc},
  152. {"call_state_registration_func", call_state_registration_func,
  153. METH_VARARGS, call_state_registration_func_doc},
  154. {NULL, NULL} /* sentinel */
  155. };
  156. static int execfunc(PyObject *m)
  157. {
  158. PyObject *temp = NULL;
  159. /* Due to cross platform compiler issues the slots must be filled
  160. * here. It's required for portability to Windows without requiring
  161. * C++. */
  162. Str_Type_slots[0].pfunc = &PyUnicode_Type;
  163. /* Add a custom type */
  164. temp = PyType_FromSpec(&Example_Type_spec);
  165. if (temp == NULL)
  166. goto fail;
  167. if (PyModule_AddObject(m, "Example", temp) != 0)
  168. goto fail;
  169. /* Add an exception type */
  170. temp = PyErr_NewException("_testimportexec.error", NULL, NULL);
  171. if (temp == NULL)
  172. goto fail;
  173. if (PyModule_AddObject(m, "error", temp) != 0)
  174. goto fail;
  175. /* Add Str */
  176. temp = PyType_FromSpec(&Str_Type_spec);
  177. if (temp == NULL)
  178. goto fail;
  179. if (PyModule_AddObject(m, "Str", temp) != 0)
  180. goto fail;
  181. if (PyModule_AddIntConstant(m, "int_const", 1969) != 0)
  182. goto fail;
  183. if (PyModule_AddStringConstant(m, "str_const", "something different") != 0)
  184. goto fail;
  185. return 0;
  186. fail:
  187. return -1;
  188. }
  189. /* Helper for module definitions; there'll be a lot of them */
  190. #define TEST_MODULE_DEF(name, slots, methods) { \
  191. PyModuleDef_HEAD_INIT, /* m_base */ \
  192. name, /* m_name */ \
  193. PyDoc_STR("Test module " name), /* m_doc */ \
  194. 0, /* m_size */ \
  195. methods, /* m_methods */ \
  196. slots, /* m_slots */ \
  197. NULL, /* m_traverse */ \
  198. NULL, /* m_clear */ \
  199. NULL, /* m_free */ \
  200. }
  201. PyModuleDef_Slot main_slots[] = {
  202. {Py_mod_exec, execfunc},
  203. {0, NULL},
  204. };
  205. static PyModuleDef main_def = TEST_MODULE_DEF("main", main_slots, testexport_methods);
  206. PyMODINIT_FUNC
  207. PyInit__testmultiphase(PyObject *spec)
  208. {
  209. return PyModuleDef_Init(&main_def);
  210. }
  211. /**** Importing a non-module object ****/
  212. static PyModuleDef def_nonmodule;
  213. /* Create a SimpleNamespace(three=3) */
  214. static PyObject*
  215. createfunc_nonmodule(PyObject *spec, PyModuleDef *def)
  216. {
  217. PyObject *dct, *ns, *three;
  218. if (def != &def_nonmodule) {
  219. PyErr_SetString(PyExc_SystemError, "def does not match");
  220. return NULL;
  221. }
  222. dct = PyDict_New();
  223. if (dct == NULL)
  224. return NULL;
  225. three = PyLong_FromLong(3);
  226. if (three == NULL) {
  227. Py_DECREF(dct);
  228. return NULL;
  229. }
  230. PyDict_SetItemString(dct, "three", three);
  231. Py_DECREF(three);
  232. ns = _PyNamespace_New(dct);
  233. Py_DECREF(dct);
  234. return ns;
  235. }
  236. static PyModuleDef_Slot slots_create_nonmodule[] = {
  237. {Py_mod_create, createfunc_nonmodule},
  238. {0, NULL},
  239. };
  240. static PyModuleDef def_nonmodule = TEST_MODULE_DEF(
  241. "_testmultiphase_nonmodule", slots_create_nonmodule, NULL);
  242. PyMODINIT_FUNC
  243. PyInit__testmultiphase_nonmodule(PyObject *spec)
  244. {
  245. return PyModuleDef_Init(&def_nonmodule);
  246. }
  247. /**** Non-ASCII-named modules ****/
  248. static PyModuleDef def_nonascii_latin = { \
  249. PyModuleDef_HEAD_INIT, /* m_base */
  250. "_testmultiphase_nonascii_latin", /* m_name */
  251. PyDoc_STR("Module named in Czech"), /* m_doc */
  252. 0, /* m_size */
  253. NULL, /* m_methods */
  254. NULL, /* m_slots */
  255. NULL, /* m_traverse */
  256. NULL, /* m_clear */
  257. NULL, /* m_free */
  258. };
  259. PyMODINIT_FUNC
  260. PyInitU__testmultiphase_zkouka_naten_evc07gi8e(PyObject *spec)
  261. {
  262. return PyModuleDef_Init(&def_nonascii_latin);
  263. }
  264. static PyModuleDef def_nonascii_kana = { \
  265. PyModuleDef_HEAD_INIT, /* m_base */
  266. "_testmultiphase_nonascii_kana", /* m_name */
  267. PyDoc_STR("Module named in Japanese"), /* m_doc */
  268. 0, /* m_size */
  269. NULL, /* m_methods */
  270. NULL, /* m_slots */
  271. NULL, /* m_traverse */
  272. NULL, /* m_clear */
  273. NULL, /* m_free */
  274. };
  275. PyMODINIT_FUNC
  276. PyInitU_eckzbwbhc6jpgzcx415x(PyObject *spec)
  277. {
  278. return PyModuleDef_Init(&def_nonascii_kana);
  279. }
  280. /*** Module with a single-character name ***/
  281. PyMODINIT_FUNC
  282. PyInit_x(PyObject *spec)
  283. {
  284. return PyModuleDef_Init(&main_def);
  285. }
  286. /**** Testing NULL slots ****/
  287. static PyModuleDef null_slots_def = TEST_MODULE_DEF(
  288. "_testmultiphase_null_slots", NULL, NULL);
  289. PyMODINIT_FUNC
  290. PyInit__testmultiphase_null_slots(PyObject *spec)
  291. {
  292. return PyModuleDef_Init(&null_slots_def);
  293. }
  294. /**** Problematic modules ****/
  295. static PyModuleDef_Slot slots_bad_large[] = {
  296. {_Py_mod_LAST_SLOT + 1, NULL},
  297. {0, NULL},
  298. };
  299. static PyModuleDef def_bad_large = TEST_MODULE_DEF(
  300. "_testmultiphase_bad_slot_large", slots_bad_large, NULL);
  301. PyMODINIT_FUNC
  302. PyInit__testmultiphase_bad_slot_large(PyObject *spec)
  303. {
  304. return PyModuleDef_Init(&def_bad_large);
  305. }
  306. static PyModuleDef_Slot slots_bad_negative[] = {
  307. {-1, NULL},
  308. {0, NULL},
  309. };
  310. static PyModuleDef def_bad_negative = TEST_MODULE_DEF(
  311. "_testmultiphase_bad_slot_negative", slots_bad_negative, NULL);
  312. PyMODINIT_FUNC
  313. PyInit__testmultiphase_bad_slot_negative(PyObject *spec)
  314. {
  315. return PyModuleDef_Init(&def_bad_negative);
  316. }
  317. static PyModuleDef def_create_int_with_state = { \
  318. PyModuleDef_HEAD_INIT, /* m_base */
  319. "create_with_state", /* m_name */
  320. PyDoc_STR("Not a PyModuleObject object, but requests per-module state"),
  321. 10, /* m_size */
  322. NULL, /* m_methods */
  323. slots_create_nonmodule, /* m_slots */
  324. NULL, /* m_traverse */
  325. NULL, /* m_clear */
  326. NULL, /* m_free */
  327. };
  328. PyMODINIT_FUNC
  329. PyInit__testmultiphase_create_int_with_state(PyObject *spec)
  330. {
  331. return PyModuleDef_Init(&def_create_int_with_state);
  332. }
  333. static PyModuleDef def_negative_size = { \
  334. PyModuleDef_HEAD_INIT, /* m_base */
  335. "negative_size", /* m_name */
  336. PyDoc_STR("PyModuleDef with negative m_size"),
  337. -1, /* m_size */
  338. NULL, /* m_methods */
  339. slots_create_nonmodule, /* m_slots */
  340. NULL, /* m_traverse */
  341. NULL, /* m_clear */
  342. NULL, /* m_free */
  343. };
  344. PyMODINIT_FUNC
  345. PyInit__testmultiphase_negative_size(PyObject *spec)
  346. {
  347. return PyModuleDef_Init(&def_negative_size);
  348. }
  349. static PyModuleDef uninitialized_def = TEST_MODULE_DEF("main", main_slots, testexport_methods);
  350. PyMODINIT_FUNC
  351. PyInit__testmultiphase_export_uninitialized(PyObject *spec)
  352. {
  353. return (PyObject*) &uninitialized_def;
  354. }
  355. PyMODINIT_FUNC
  356. PyInit__testmultiphase_export_null(PyObject *spec)
  357. {
  358. return NULL;
  359. }
  360. PyMODINIT_FUNC
  361. PyInit__testmultiphase_export_raise(PyObject *spec)
  362. {
  363. PyErr_SetString(PyExc_SystemError, "bad export function");
  364. return NULL;
  365. }
  366. PyMODINIT_FUNC
  367. PyInit__testmultiphase_export_unreported_exception(PyObject *spec)
  368. {
  369. PyErr_SetString(PyExc_SystemError, "bad export function");
  370. return PyModuleDef_Init(&main_def);
  371. }
  372. static PyObject*
  373. createfunc_null(PyObject *spec, PyModuleDef *def)
  374. {
  375. return NULL;
  376. }
  377. PyModuleDef_Slot slots_create_null[] = {
  378. {Py_mod_create, createfunc_null},
  379. {0, NULL},
  380. };
  381. static PyModuleDef def_create_null = TEST_MODULE_DEF(
  382. "_testmultiphase_create_null", slots_create_null, NULL);
  383. PyMODINIT_FUNC
  384. PyInit__testmultiphase_create_null(PyObject *spec)
  385. {
  386. return PyModuleDef_Init(&def_create_null);
  387. }
  388. static PyObject*
  389. createfunc_raise(PyObject *spec, PyModuleDef *def)
  390. {
  391. PyErr_SetString(PyExc_SystemError, "bad create function");
  392. return NULL;
  393. }
  394. static PyModuleDef_Slot slots_create_raise[] = {
  395. {Py_mod_create, createfunc_raise},
  396. {0, NULL},
  397. };
  398. static PyModuleDef def_create_raise = TEST_MODULE_DEF(
  399. "_testmultiphase_create_null", slots_create_raise, NULL);
  400. PyMODINIT_FUNC
  401. PyInit__testmultiphase_create_raise(PyObject *spec)
  402. {
  403. return PyModuleDef_Init(&def_create_raise);
  404. }
  405. static PyObject*
  406. createfunc_unreported_exception(PyObject *spec, PyModuleDef *def)
  407. {
  408. PyErr_SetString(PyExc_SystemError, "bad create function");
  409. return PyModule_New("foo");
  410. }
  411. static PyModuleDef_Slot slots_create_unreported_exception[] = {
  412. {Py_mod_create, createfunc_unreported_exception},
  413. {0, NULL},
  414. };
  415. static PyModuleDef def_create_unreported_exception = TEST_MODULE_DEF(
  416. "_testmultiphase_create_unreported_exception", slots_create_unreported_exception, NULL);
  417. PyMODINIT_FUNC
  418. PyInit__testmultiphase_create_unreported_exception(PyObject *spec)
  419. {
  420. return PyModuleDef_Init(&def_create_unreported_exception);
  421. }
  422. static PyModuleDef_Slot slots_nonmodule_with_exec_slots[] = {
  423. {Py_mod_create, createfunc_nonmodule},
  424. {Py_mod_exec, execfunc},
  425. {0, NULL},
  426. };
  427. static PyModuleDef def_nonmodule_with_exec_slots = TEST_MODULE_DEF(
  428. "_testmultiphase_nonmodule_with_exec_slots", slots_nonmodule_with_exec_slots, NULL);
  429. PyMODINIT_FUNC
  430. PyInit__testmultiphase_nonmodule_with_exec_slots(PyObject *spec)
  431. {
  432. return PyModuleDef_Init(&def_nonmodule_with_exec_slots);
  433. }
  434. static int
  435. execfunc_err(PyObject *mod)
  436. {
  437. return -1;
  438. }
  439. static PyModuleDef_Slot slots_exec_err[] = {
  440. {Py_mod_exec, execfunc_err},
  441. {0, NULL},
  442. };
  443. static PyModuleDef def_exec_err = TEST_MODULE_DEF(
  444. "_testmultiphase_exec_err", slots_exec_err, NULL);
  445. PyMODINIT_FUNC
  446. PyInit__testmultiphase_exec_err(PyObject *spec)
  447. {
  448. return PyModuleDef_Init(&def_exec_err);
  449. }
  450. static int
  451. execfunc_raise(PyObject *spec)
  452. {
  453. PyErr_SetString(PyExc_SystemError, "bad exec function");
  454. return -1;
  455. }
  456. static PyModuleDef_Slot slots_exec_raise[] = {
  457. {Py_mod_exec, execfunc_raise},
  458. {0, NULL},
  459. };
  460. static PyModuleDef def_exec_raise = TEST_MODULE_DEF(
  461. "_testmultiphase_exec_raise", slots_exec_raise, NULL);
  462. PyMODINIT_FUNC
  463. PyInit__testmultiphase_exec_raise(PyObject *mod)
  464. {
  465. return PyModuleDef_Init(&def_exec_raise);
  466. }
  467. static int
  468. execfunc_unreported_exception(PyObject *mod)
  469. {
  470. PyErr_SetString(PyExc_SystemError, "bad exec function");
  471. return 0;
  472. }
  473. static PyModuleDef_Slot slots_exec_unreported_exception[] = {
  474. {Py_mod_exec, execfunc_unreported_exception},
  475. {0, NULL},
  476. };
  477. static PyModuleDef def_exec_unreported_exception = TEST_MODULE_DEF(
  478. "_testmultiphase_exec_unreported_exception", slots_exec_unreported_exception, NULL);
  479. PyMODINIT_FUNC
  480. PyInit__testmultiphase_exec_unreported_exception(PyObject *spec)
  481. {
  482. return PyModuleDef_Init(&def_exec_unreported_exception);
  483. }