Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-28411: Remove PyInterpreterState.modules. #3606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Doc/whatsnew/3.7.rst
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,9 @@ Changes in the Python API
and module are affected by this change. (Contributed by INADA Naoki and
Eugene Toder in :issue:`29463`.)

* ``PyInterpreterState`` no longer has a ``modules`` field. Instead use
``sys.modules``.

* The *mode* argument of :func:`os.makedirs` no longer affects the file
permission bits of newly-created intermediate-level directories.
To set their file permission bits you can set the umask before invoking
Expand Down
1 change: 0 additions & 1 deletion Include/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ typedef struct _is {

int64_t id;

PyObject *modules;
PyObject *modules_by_index;
PyObject *sysdict;
PyObject *builtins;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
``PyInterpreterState`` has a "modules" field that is copied into
``sys.modules`` during interpreter startup. This causes problems if a
program replaces ``sys.modules`` with something else. To solve this we
eliminate ``PyInterpreterState.modules``.
3 changes: 1 addition & 2 deletions Modules/_pickle.c
Original file line number Diff line number Diff line change
Expand Up @@ -1684,7 +1684,6 @@ whichmodule(PyObject *global, PyObject *dotted_path)
Py_ssize_t i;
PyObject *modules;
_Py_IDENTIFIER(__module__);
_Py_IDENTIFIER(modules);
_Py_IDENTIFIER(__main__);

module_name = _PyObject_GetAttrId(global, &PyId___module__);
Expand All @@ -1705,7 +1704,7 @@ whichmodule(PyObject *global, PyObject *dotted_path)
assert(module_name == NULL);

/* Fallback on walking sys.modules */
modules = _PySys_GetObjectId(&PyId_modules);
modules = PyImport_GetModuleDict();
if (modules == NULL) {
PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
return NULL;
Expand Down
21 changes: 15 additions & 6 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,17 @@ _PyImport_Fini(void)
PyObject *
PyImport_GetModuleDict(void)
{
PyInterpreterState *interp = PyThreadState_GET()->interp;
if (interp->modules == NULL) {
Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
PyObject *sysdict = PyThreadState_GET()->interp->sysdict;
if (sysdict == NULL) {
Py_FatalError("PyImport_GetModuleDict: no sys module!");
}

_Py_IDENTIFIER(modules);
PyObject *modules = _PyDict_GetItemId(sysdict, &PyId_modules);
if (modules == NULL) {
Py_FatalError("lost sys.modules");
}
return interp->modules;
return modules;
}

/* In some corner cases it is important to be sure that the import
Expand All @@ -304,7 +310,11 @@ PyImport_GetModuleDict(void)
int
_PyImport_IsInitialized(PyInterpreterState *interp)
{
if (interp->modules == NULL)
if (interp->sysdict == NULL)
return 0;
_Py_IDENTIFIER(modules);
PyObject *modules = _PyDict_GetItemId(interp->sysdict, &PyId_modules);
if (modules == NULL)
return 0;
return 1;
}
Expand Down Expand Up @@ -543,7 +553,6 @@ PyImport_Cleanup(void)
/* Clear and delete the modules directory. Actual modules will
still be there only if imported during the execution of some
destructor. */
interp->modules = NULL;
Py_DECREF(modules);

/* Once more */
Expand Down
2 changes: 0 additions & 2 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,6 @@ void _Py_InitializeCore(const _PyCoreConfig *config)
PyObject *modules = PyDict_New();
if (modules == NULL)
Py_FatalError("Py_InitializeCore: can't make modules dictionary");
interp->modules = modules;

sysmod = _PySys_BeginInit();
if (sysmod == NULL)
Expand Down Expand Up @@ -1210,7 +1209,6 @@ Py_NewInterpreter(void)
PyObject *modules = PyDict_New();
if (modules == NULL)
Py_FatalError("Py_NewInterpreter: can't make modules dictionary");
interp->modules = modules;

sysmod = _PyImport_FindBuiltin("sys", modules);
if (sysmod != NULL) {
Expand Down
2 changes: 0 additions & 2 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ PyInterpreterState_New(void)
PyMem_RawMalloc(sizeof(PyInterpreterState));

if (interp != NULL) {
interp->modules = NULL;
interp->modules_by_index = NULL;
interp->sysdict = NULL;
interp->builtins = NULL;
Expand Down Expand Up @@ -154,7 +153,6 @@ PyInterpreterState_Clear(PyInterpreterState *interp)
Py_CLEAR(interp->codec_search_path);
Py_CLEAR(interp->codec_search_cache);
Py_CLEAR(interp->codec_error_registry);
Py_CLEAR(interp->modules);
Py_CLEAR(interp->modules_by_index);
Py_CLEAR(interp->sysdict);
Py_CLEAR(interp->builtins);
Expand Down