summaryrefslogtreecommitdiffstats
path: root/Modules/_testmultiphase.c
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2020-03-17 17:09:46 (GMT)
committerGitHub <noreply@github.com>2020-03-17 17:09:46 (GMT)
commit5b1ef200d31a74a9b478d0217d73ed0a659a8a06 (patch)
tree3a3afde33a456ac49284058c00b1d685c54bcf3c /Modules/_testmultiphase.c
parent52268941f37e3e27bd01792b081877ec3bc9ce12 (diff)
downloadcpython-5b1ef200d31a74a9b478d0217d73ed0a659a8a06.zip
cpython-5b1ef200d31a74a9b478d0217d73ed0a659a8a06.tar.gz
cpython-5b1ef200d31a74a9b478d0217d73ed0a659a8a06.tar.bz2
bpo-39824: module_traverse() don't call m_traverse if md_state=NULL (GH-18738)
Extension modules: m_traverse, m_clear and m_free functions of PyModuleDef are no longer called if the module state was requested but is not allocated yet. This is the case immediately after the module is created and before the module is executed (Py_mod_exec function). More precisely, these functions are not called if m_size is greater than 0 and the module state (as returned by PyModule_GetState()) is NULL. Extension modules without module state (m_size <= 0) are not affected. Co-Authored-By: Petr Viktorin <encukou@gmail.com>
Diffstat (limited to 'Modules/_testmultiphase.c')
-rw-r--r--Modules/_testmultiphase.c54
1 files changed, 3 insertions, 51 deletions
diff --git a/Modules/_testmultiphase.c b/Modules/_testmultiphase.c
index 4933abb..eadc46f 100644
--- a/Modules/_testmultiphase.c
+++ b/Modules/_testmultiphase.c
@@ -225,20 +225,18 @@ static int execfunc(PyObject *m)
/* Helper for module definitions; there'll be a lot of them */
-#define TEST_MODULE_DEF_EX(name, slots, methods, statesize, traversefunc) { \
+#define TEST_MODULE_DEF(name, slots, methods) { \
PyModuleDef_HEAD_INIT, /* m_base */ \
name, /* m_name */ \
PyDoc_STR("Test module " name), /* m_doc */ \
- statesize, /* m_size */ \
+ 0, /* m_size */ \
methods, /* m_methods */ \
slots, /* m_slots */ \
- traversefunc, /* m_traverse */ \
+ NULL, /* m_traverse */ \
NULL, /* m_clear */ \
NULL, /* m_free */ \
}
-#define TEST_MODULE_DEF(name, slots, methods) TEST_MODULE_DEF_EX(name, slots, methods, 0, NULL)
-
static PyModuleDef_Slot main_slots[] = {
{Py_mod_exec, execfunc},
{0, NULL},
@@ -622,52 +620,6 @@ PyInit__testmultiphase_exec_unreported_exception(PyObject *spec)
return PyModuleDef_Init(&def_exec_unreported_exception);
}
-static int
-bad_traverse(PyObject *self, visitproc visit, void *arg) {
- testmultiphase_state *m_state;
-
- m_state = PyModule_GetState(self);
-
- /* The following assertion mimics any traversal function that doesn't correctly handle
- * the case during module creation where the module state hasn't been created yet.
- *
- * The check that it is used to test only runs in debug mode, so it is OK that the
- * assert() will get compiled out in fully optimised release builds.
- */
- assert(m_state != NULL);
- Py_VISIT(m_state->integer);
- return 0;
-}
-
-static int
-execfunc_with_bad_traverse(PyObject *mod) {
- testmultiphase_state *m_state;
-
- m_state = PyModule_GetState(mod);
- if (m_state == NULL) {
- return -1;
- }
-
- m_state->integer = PyLong_FromLong(0x7fffffff);
- Py_INCREF(m_state->integer);
-
- return 0;
-}
-
-static PyModuleDef_Slot slots_with_bad_traverse[] = {
- {Py_mod_exec, execfunc_with_bad_traverse},
- {0, NULL}
-};
-
-static PyModuleDef def_with_bad_traverse = TEST_MODULE_DEF_EX(
- "_testmultiphase_with_bad_traverse", slots_with_bad_traverse, NULL,
- sizeof(testmultiphase_state), bad_traverse);
-
-PyMODINIT_FUNC
-PyInit__testmultiphase_with_bad_traverse(PyObject *spec) {
- return PyModuleDef_Init(&def_with_bad_traverse);
-}
-
/*** Helper for imp test ***/
static PyModuleDef imp_dummy_def = TEST_MODULE_DEF("imp_dummy", main_slots, testexport_methods);