Commit 0a28f8d3 authored by Victor Stinner's avatar Victor Stinner Committed by GitHub

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.
parent 809ff118
......@@ -11,21 +11,14 @@ PyMODINIT_FUNC PyInit__imp(void);
PyAPI_FUNC(int) _PyImport_IsInitialized(PyInterpreterState *);
PyAPI_FUNC(PyObject *) _PyImport_GetModuleId(struct _Py_Identifier *name);
PyAPI_FUNC(PyObject *) _PyImport_AddModuleObject(PyObject *name,
PyObject *modules);
PyAPI_FUNC(int) _PyImport_SetModule(PyObject *name, PyObject *module);
PyAPI_FUNC(int) _PyImport_SetModuleString(const char *name, PyObject* module);
PyAPI_FUNC(void) _PyImport_AcquireLock(void);
PyAPI_FUNC(int) _PyImport_ReleaseLock(void);
PyAPI_FUNC(PyObject *) _PyImport_FindBuiltin(
const char *name, /* UTF-8 encoded string */
PyObject *modules
);
PyAPI_FUNC(PyObject *) _PyImport_FindExtensionObject(PyObject *, PyObject *);
PyAPI_FUNC(PyObject *) _PyImport_FindExtensionObjectEx(PyObject *, PyObject *,
PyObject *);
PyAPI_FUNC(int) _PyImport_FixupBuiltin(
PyObject *mod,
const char *name, /* UTF-8 encoded string */
......
......@@ -148,7 +148,6 @@ struct _ts {
PyAPI_FUNC(PyInterpreterState *) _PyInterpreterState_Get(void);
PyAPI_FUNC(int) _PyState_AddModule(PyObject*, struct PyModuleDef*);
PyAPI_FUNC(void) _PyState_ClearModules(void);
PyAPI_FUNC(PyThreadState *) _PyThreadState_Prealloc(PyInterpreterState *);
/* Similar to PyThreadState_Get(), but don't issue a fatal error
......
......@@ -5,6 +5,11 @@
extern "C" {
#endif
PyAPI_FUNC(PyObject *) _PyImport_FindBuiltin(
PyThreadState *tstate,
const char *name /* UTF-8 encoded string */
);
extern void _PyImport_ReInitLock(void);
#ifdef __cplusplus
......
......@@ -55,7 +55,7 @@ extern int _PyFloat_Init(void);
extern PyStatus _Py_HashRandomization_Init(const PyConfig *);
extern PyStatus _PyTypes_Init(void);
extern PyStatus _PyImportZip_Init(PyInterpreterState *interp);
extern PyStatus _PyImportZip_Init(PyThreadState *tstate);
/* Various internal finalizers */
......
......@@ -310,6 +310,9 @@ PyAPI_FUNC(PyThreadState *) _PyThreadState_Swap(
PyAPI_FUNC(PyStatus) _PyInterpreterState_Enable(_PyRuntimeState *runtime);
PyAPI_FUNC(void) _PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime);
/* Used by PyImport_Cleanup() */
extern void _PyInterpreterState_ClearModules(PyInterpreterState *interp);
PyAPI_FUNC(void) _PyGILState_Reinit(_PyRuntimeState *runtime);
#ifdef __cplusplus
......
This diff is collapsed.
......@@ -6,6 +6,7 @@
#undef Yield /* undefine macro conflicting with <winbase.h> */
#include "pycore_ceval.h"
#include "pycore_context.h"
#include "pycore_import.h" /* _PyImport_FindBuiltin */
#include "pycore_initconfig.h"
#include "pycore_fileutils.h"
#include "pycore_hamt.h"
......@@ -197,17 +198,17 @@ init_importlib(PyInterpreterState *interp, PyObject *sysmod)
}
static PyStatus
init_importlib_external(PyInterpreterState *interp)
init_importlib_external(PyThreadState *tstate)
{
PyObject *value;
value = PyObject_CallMethod(interp->importlib,
value = PyObject_CallMethod(tstate->interp->importlib,
"_install_external_importers", "");
if (value == NULL) {
PyErr_Print();
return _PyStatus_ERR("external importer setup failed");
}
Py_DECREF(value);
return _PyImportZip_Init(interp);
return _PyImportZip_Init(tstate);
}
/* Helper functions to better handle the legacy C locale
......@@ -924,7 +925,7 @@ pyinit_main(_PyRuntimeState *runtime, PyInterpreterState *interp)
return _PyStatus_ERR("can't finish initializing sys");
}
PyStatus status = init_importlib_external(interp);
PyStatus status = init_importlib_external(tstate);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
......@@ -1449,7 +1450,7 @@ new_interpreter(PyThreadState **tstate_p)
}
interp->modules = modules;
PyObject *sysmod = _PyImport_FindBuiltin("sys", modules);
PyObject *sysmod = _PyImport_FindBuiltin(tstate, "sys");
if (sysmod != NULL) {
interp->sysdict = PyModule_GetDict(sysmod);
if (interp->sysdict == NULL) {
......@@ -1465,7 +1466,7 @@ new_interpreter(PyThreadState **tstate_p)
goto handle_error;
}
PyObject *bimod = _PyImport_FindBuiltin("builtins", modules);
PyObject *bimod = _PyImport_FindBuiltin(tstate, "builtins");
if (bimod != NULL) {
interp->builtins = PyModule_GetDict(bimod);
if (interp->builtins == NULL)
......@@ -1497,7 +1498,7 @@ new_interpreter(PyThreadState **tstate_p)
return status;
}
status = init_importlib_external(interp);
status = init_importlib_external(tstate);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
......
......@@ -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) {
if (!interp->modules_by_index) {
return;
}
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);
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)
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);
if (PyList_SetSlice(interp->modules_by_index,
0, PyList_GET_SIZE(interp->modules_by_index),
NULL)) {
PyErr_WriteUnraisable(interp->modules_by_index);
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment