From 0a28f8d379544eee897979da0ce99f0b449b49dd Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 19 Jun 2019 02:54:39 +0200 Subject: bpo-36710: Add tstate parameter in import.c (GH-14218) * Add 'tstate' parameter to many internal import.c functions. * _PyImportZip_Init() now gets 'tstate' parameter rather than 'interp'. * Add 'interp' parameter to _PyState_ClearModules() and rename it to _PyInterpreterState_ClearModules(). * Move private _PyImport_FindBuiltin() to the internal C API; add 'tstate' parameter to it. * Remove private _PyImport_AddModuleObject() from the C API: use public PyImport_AddModuleObject() instead. * Remove private _PyImport_FindExtensionObjectEx() from the C API: use private _PyImport_FindExtensionObject() instead. --- Python/pystate.c | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) (limited to 'Python/pystate.c') diff --git a/Python/pystate.c b/Python/pystate.c index 833e0fb30d..1e2b480458 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -741,28 +741,32 @@ PyState_RemoveModule(struct PyModuleDef* def) return PyList_SetItem(state->modules_by_index, index, Py_None); } -/* used by import.c:PyImport_Cleanup */ +/* Used by PyImport_Cleanup() */ void -_PyState_ClearModules(void) +_PyInterpreterState_ClearModules(PyInterpreterState *interp) { - PyInterpreterState *state = _PyInterpreterState_GET_UNSAFE(); - if (state->modules_by_index) { - Py_ssize_t i; - for (i = 0; i < PyList_GET_SIZE(state->modules_by_index); i++) { - PyObject *m = PyList_GET_ITEM(state->modules_by_index, i); - if (PyModule_Check(m)) { - /* cleanup the saved copy of module dicts */ - PyModuleDef *md = PyModule_GetDef(m); - if (md) - Py_CLEAR(md->m_base.m_copy); + if (!interp->modules_by_index) { + return; + } + + Py_ssize_t i; + for (i = 0; i < PyList_GET_SIZE(interp->modules_by_index); i++) { + PyObject *m = PyList_GET_ITEM(interp->modules_by_index, i); + if (PyModule_Check(m)) { + /* cleanup the saved copy of module dicts */ + PyModuleDef *md = PyModule_GetDef(m); + if (md) { + Py_CLEAR(md->m_base.m_copy); } } - /* Setting modules_by_index to NULL could be dangerous, so we - clear the list instead. */ - if (PyList_SetSlice(state->modules_by_index, - 0, PyList_GET_SIZE(state->modules_by_index), - NULL)) - PyErr_WriteUnraisable(state->modules_by_index); + } + + /* Setting modules_by_index to NULL could be dangerous, so we + clear the list instead. */ + if (PyList_SetSlice(interp->modules_by_index, + 0, PyList_GET_SIZE(interp->modules_by_index), + NULL)) { + PyErr_WriteUnraisable(interp->modules_by_index); } } -- cgit v1.2.1