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.
This commit is contained in:
Victor Stinner 2019-06-19 02:54:39 +02:00 committed by GitHub
parent 809ff1181c
commit 0a28f8d379
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 248 additions and 191 deletions

View file

@ -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);
}
}