Commit 93c92f7d authored by Eric Snow's avatar Eric Snow Committed by GitHub

bpo-31404: Revert "remove modules from Py_InterpreterState (#1638)" (#3565)

PR #1638, for bpo-28411, causes problems in some (very) edge cases. Until that gets sorted out, we're reverting the merge. PR #3506, a fix on top of #1638, is also getting reverted.
parent 13ad3b7a
...@@ -204,13 +204,6 @@ Importing Modules ...@@ -204,13 +204,6 @@ Importing Modules
Return the dictionary used for the module administration (a.k.a. Return the dictionary used for the module administration (a.k.a.
``sys.modules``). Note that this is a per-interpreter variable. ``sys.modules``). Note that this is a per-interpreter variable.
.. c:function:: PyObject* PyImport_GetModule(PyObject *name)
Return the already imported module with the given name. If the
module has not been imported yet then returns NULL but does not set
an error. Returns NULL and sets an error if the lookup failed.
.. versionadded:: 3.7
.. c:function:: PyObject* PyImport_GetImporter(PyObject *path) .. c:function:: PyObject* PyImport_GetImporter(PyObject *path)
......
...@@ -486,9 +486,6 @@ Changes in the Python API ...@@ -486,9 +486,6 @@ Changes in the Python API
and module are affected by this change. (Contributed by INADA Naoki and and module are affected by this change. (Contributed by INADA Naoki and
Eugene Toder in :issue:`29463`.) 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 * The *mode* argument of :func:`os.makedirs` no longer affects the file
permission bits of newly-created intermediate-level directories. permission bits of newly-created intermediate-level directories.
To set their file permission bits you can set the umask before invoking To set their file permission bits you can set the umask before invoking
......
...@@ -38,25 +38,11 @@ PyAPI_FUNC(PyObject *) PyImport_ExecCodeModuleObject( ...@@ -38,25 +38,11 @@ PyAPI_FUNC(PyObject *) PyImport_ExecCodeModuleObject(
); );
#endif #endif
PyAPI_FUNC(PyObject *) PyImport_GetModuleDict(void); PyAPI_FUNC(PyObject *) PyImport_GetModuleDict(void);
#ifndef Py_LIMITED_API
PyAPI_FUNC(int) _PyImport_IsInitialized(PyInterpreterState *);
#endif
PyAPI_FUNC(PyObject *) PyImport_GetModule(PyObject *name);
#ifndef Py_LIMITED_API
PyAPI_FUNC(PyObject *) _PyImport_GetModule(PyObject *name);
PyAPI_FUNC(PyObject *) _PyImport_GetModuleWithError(PyObject *name);
PyAPI_FUNC(PyObject *) _PyImport_GetModuleId(struct _Py_Identifier *name);
PyAPI_FUNC(int) _PyImport_SetModule(PyObject *name, PyObject *module);
PyAPI_FUNC(int) _PyImport_SetModuleString(const char *name, PyObject* module);
#endif
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
PyAPI_FUNC(PyObject *) PyImport_AddModuleObject( PyAPI_FUNC(PyObject *) PyImport_AddModuleObject(
PyObject *name PyObject *name
); );
#endif #endif
#ifndef Py_LIMITED_API
PyAPI_FUNC(PyObject *) _PyImport_AddModuleObject(PyObject *, PyObject *);
#endif
PyAPI_FUNC(PyObject *) PyImport_AddModule( PyAPI_FUNC(PyObject *) PyImport_AddModule(
const char *name /* UTF-8 encoded string */ const char *name /* UTF-8 encoded string */
); );
...@@ -106,19 +92,14 @@ PyAPI_FUNC(int) _PyImport_ReleaseLock(void); ...@@ -106,19 +92,14 @@ PyAPI_FUNC(int) _PyImport_ReleaseLock(void);
PyAPI_FUNC(void) _PyImport_ReInitLock(void); PyAPI_FUNC(void) _PyImport_ReInitLock(void);
PyAPI_FUNC(PyObject *) _PyImport_FindBuiltin( PyAPI_FUNC(PyObject *) _PyImport_FindBuiltin(
const char *name, /* UTF-8 encoded string */ const char *name /* UTF-8 encoded string */
PyObject *modules
); );
PyAPI_FUNC(PyObject *) _PyImport_FindExtensionObject(PyObject *, PyObject *); PyAPI_FUNC(PyObject *) _PyImport_FindExtensionObject(PyObject *, PyObject *);
PyAPI_FUNC(PyObject *) _PyImport_FindExtensionObjectEx(PyObject *, PyObject *,
PyObject *);
PyAPI_FUNC(int) _PyImport_FixupBuiltin( PyAPI_FUNC(int) _PyImport_FixupBuiltin(
PyObject *mod, PyObject *mod,
const char *name, /* UTF-8 encoded string */ const char *name /* UTF-8 encoded string */
PyObject *modules
); );
PyAPI_FUNC(int) _PyImport_FixupExtensionObject(PyObject*, PyObject *, PyAPI_FUNC(int) _PyImport_FixupExtensionObject(PyObject*, PyObject *, PyObject *);
PyObject *, PyObject *);
struct _inittab { struct _inittab {
const char *name; /* ASCII encoded string */ const char *name; /* ASCII encoded string */
......
...@@ -191,10 +191,6 @@ PyAPI_FUNC(int) PyModule_ExecDef(PyObject *module, PyModuleDef *def); ...@@ -191,10 +191,6 @@ PyAPI_FUNC(int) PyModule_ExecDef(PyObject *module, PyModuleDef *def);
PyAPI_FUNC(PyObject *) PyModule_Create2(struct PyModuleDef*, PyAPI_FUNC(PyObject *) PyModule_Create2(struct PyModuleDef*,
int apiver); int apiver);
#ifndef Py_LIMITED_API
PyAPI_FUNC(PyObject *) _PyModule_CreateInitialized(struct PyModuleDef*,
int apiver);
#endif
#ifdef Py_LIMITED_API #ifdef Py_LIMITED_API
#define PyModule_Create(module) \ #define PyModule_Create(module) \
......
...@@ -727,13 +727,14 @@ PyAPI_FUNC(Py_ssize_t) _Py_GetRefTotal(void); ...@@ -727,13 +727,14 @@ PyAPI_FUNC(Py_ssize_t) _Py_GetRefTotal(void);
/* Py_REF_DEBUG also controls the display of refcounts and memory block /* Py_REF_DEBUG also controls the display of refcounts and memory block
* allocations at the interactive prompt and at interpreter shutdown * allocations at the interactive prompt and at interpreter shutdown
*/ */
PyAPI_FUNC(PyObject *) _PyDebug_XOptionShowRefCount(void);
PyAPI_FUNC(void) _PyDebug_PrintTotalRefs(void); PyAPI_FUNC(void) _PyDebug_PrintTotalRefs(void);
#define _PY_DEBUG_PRINT_TOTAL_REFS() _PyDebug_PrintTotalRefs()
#else #else
#define _Py_INC_REFTOTAL #define _Py_INC_REFTOTAL
#define _Py_DEC_REFTOTAL #define _Py_DEC_REFTOTAL
#define _Py_REF_DEBUG_COMMA #define _Py_REF_DEBUG_COMMA
#define _Py_CHECK_REFCNT(OP) /* a semicolon */; #define _Py_CHECK_REFCNT(OP) /* a semicolon */;
#define _PY_DEBUG_PRINT_TOTAL_REFS()
#endif /* Py_REF_DEBUG */ #endif /* Py_REF_DEBUG */
#ifdef COUNT_ALLOCS #ifdef COUNT_ALLOCS
......
...@@ -53,6 +53,7 @@ typedef struct _is { ...@@ -53,6 +53,7 @@ typedef struct _is {
int64_t id; int64_t id;
PyObject *modules;
PyObject *modules_by_index; PyObject *modules_by_index;
PyObject *sysdict; PyObject *sysdict;
PyObject *builtins; PyObject *builtins;
...@@ -60,6 +61,8 @@ typedef struct _is { ...@@ -60,6 +61,8 @@ typedef struct _is {
/* Used in Python/sysmodule.c. */ /* Used in Python/sysmodule.c. */
int check_interval; int check_interval;
PyObject *warnoptions;
PyObject *xoptions;
/* Used in Modules/_threadmodule.c. */ /* Used in Modules/_threadmodule.c. */
long num_threads; long num_threads;
......
``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``.
...@@ -6425,7 +6425,9 @@ _pickle_Unpickler_find_class_impl(UnpicklerObject *self, ...@@ -6425,7 +6425,9 @@ _pickle_Unpickler_find_class_impl(UnpicklerObject *self,
/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/ /*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
{ {
PyObject *global; PyObject *global;
PyObject *modules_dict;
PyObject *module; PyObject *module;
_Py_IDENTIFIER(modules);
/* Try to map the old names used in Python 2.x to the new ones used in /* Try to map the old names used in Python 2.x to the new ones used in
Python 3.x. We do this only with old pickle protocols and when the Python 3.x. We do this only with old pickle protocols and when the
...@@ -6482,7 +6484,13 @@ _pickle_Unpickler_find_class_impl(UnpicklerObject *self, ...@@ -6482,7 +6484,13 @@ _pickle_Unpickler_find_class_impl(UnpicklerObject *self,
} }
} }
module = PyImport_GetModule(module_name); modules_dict = _PySys_GetObjectId(&PyId_modules);
if (modules_dict == NULL) {
PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
return NULL;
}
module = PyDict_GetItemWithError(modules_dict, module_name);
if (module == NULL) { if (module == NULL) {
if (PyErr_Occurred()) if (PyErr_Occurred())
return NULL; return NULL;
...@@ -6490,11 +6498,11 @@ _pickle_Unpickler_find_class_impl(UnpicklerObject *self, ...@@ -6490,11 +6498,11 @@ _pickle_Unpickler_find_class_impl(UnpicklerObject *self,
if (module == NULL) if (module == NULL)
return NULL; return NULL;
global = getattribute(module, global_name, self->proto >= 4); global = getattribute(module, global_name, self->proto >= 4);
Py_DECREF(module);
} }
else { else {
global = getattribute(module, global_name, self->proto >= 4); global = getattribute(module, global_name, self->proto >= 4);
} }
Py_DECREF(module);
return global; return global;
} }
......
...@@ -1643,6 +1643,7 @@ MODULE_INITFUNC(void) ...@@ -1643,6 +1643,7 @@ MODULE_INITFUNC(void)
PyObject *errors_module; PyObject *errors_module;
PyObject *modelmod_name; PyObject *modelmod_name;
PyObject *model_module; PyObject *model_module;
PyObject *sys_modules;
PyObject *tmpnum, *tmpstr; PyObject *tmpnum, *tmpstr;
PyObject *codes_dict; PyObject *codes_dict;
PyObject *rev_codes_dict; PyObject *rev_codes_dict;
...@@ -1692,6 +1693,11 @@ MODULE_INITFUNC(void) ...@@ -1692,6 +1693,11 @@ MODULE_INITFUNC(void)
*/ */
PyModule_AddStringConstant(m, "native_encoding", "UTF-8"); PyModule_AddStringConstant(m, "native_encoding", "UTF-8");
sys_modules = PySys_GetObject("modules");
if (sys_modules == NULL) {
Py_DECREF(m);
return NULL;
}
d = PyModule_GetDict(m); d = PyModule_GetDict(m);
if (d == NULL) { if (d == NULL) {
Py_DECREF(m); Py_DECREF(m);
...@@ -1701,7 +1707,7 @@ MODULE_INITFUNC(void) ...@@ -1701,7 +1707,7 @@ MODULE_INITFUNC(void)
if (errors_module == NULL) { if (errors_module == NULL) {
errors_module = PyModule_New(MODULE_NAME ".errors"); errors_module = PyModule_New(MODULE_NAME ".errors");
if (errors_module != NULL) { if (errors_module != NULL) {
_PyImport_SetModule(errmod_name, errors_module); PyDict_SetItem(sys_modules, errmod_name, errors_module);
/* gives away the reference to errors_module */ /* gives away the reference to errors_module */
PyModule_AddObject(m, "errors", errors_module); PyModule_AddObject(m, "errors", errors_module);
} }
...@@ -1711,7 +1717,7 @@ MODULE_INITFUNC(void) ...@@ -1711,7 +1717,7 @@ MODULE_INITFUNC(void)
if (model_module == NULL) { if (model_module == NULL) {
model_module = PyModule_New(MODULE_NAME ".model"); model_module = PyModule_New(MODULE_NAME ".model");
if (model_module != NULL) { if (model_module != NULL) {
_PyImport_SetModule(modelmod_name, model_module); PyDict_SetItem(sys_modules, modelmod_name, model_module);
/* gives away the reference to model_module */ /* gives away the reference to model_module */
PyModule_AddObject(m, "model", model_module); PyModule_AddObject(m, "model", model_module);
} }
......
...@@ -161,18 +161,12 @@ _add_methods_to_object(PyObject *module, PyObject *name, PyMethodDef *functions) ...@@ -161,18 +161,12 @@ _add_methods_to_object(PyObject *module, PyObject *name, PyMethodDef *functions)
PyObject * PyObject *
PyModule_Create2(struct PyModuleDef* module, int module_api_version) PyModule_Create2(struct PyModuleDef* module, int module_api_version)
{
if (!_PyImport_IsInitialized(PyThreadState_GET()->interp))
Py_FatalError("Python import machinery not initialized");
return _PyModule_CreateInitialized(module, module_api_version);
}
PyObject *
_PyModule_CreateInitialized(struct PyModuleDef* module, int module_api_version)
{ {
const char* name; const char* name;
PyModuleObject *m; PyModuleObject *m;
PyInterpreterState *interp = PyThreadState_Get()->interp;
if (interp->modules == NULL)
Py_FatalError("Python import machinery not initialized");
if (!PyModuleDef_Init(module)) if (!PyModuleDef_Init(module))
return NULL; return NULL;
name = module->m_name; name = module->m_name;
......
...@@ -29,23 +29,20 @@ _Py_GetRefTotal(void) ...@@ -29,23 +29,20 @@ _Py_GetRefTotal(void)
return total; return total;
} }
PyObject *
_PyDebug_XOptionShowRefCount(void)
{
PyObject *xoptions = PySys_GetXOptions();
if (xoptions == NULL)
return NULL;
_Py_IDENTIFIER(showrefcount);
return _PyDict_GetItemId(xoptions, &PyId_showrefcount);
}
void void
_PyDebug_PrintTotalRefs(void) { _PyDebug_PrintTotalRefs(void) {
fprintf(stderr, PyObject *xoptions, *value;
"[%" PY_FORMAT_SIZE_T "d refs, " _Py_IDENTIFIER(showrefcount);
"%" PY_FORMAT_SIZE_T "d blocks]\n",
_Py_GetRefTotal(), _Py_GetAllocatedBlocks()); xoptions = PySys_GetXOptions();
if (xoptions == NULL)
return;
value = _PyDict_GetItemId(xoptions, &PyId_showrefcount);
if (value == Py_True)
fprintf(stderr,
"[%" PY_FORMAT_SIZE_T "d refs, "
"%" PY_FORMAT_SIZE_T "d blocks]\n",
_Py_GetRefTotal(), _Py_GetAllocatedBlocks());
} }
#endif /* Py_REF_DEBUG */ #endif /* Py_REF_DEBUG */
......
...@@ -3902,6 +3902,7 @@ import_copyreg(void) ...@@ -3902,6 +3902,7 @@ import_copyreg(void)
{ {
PyObject *copyreg_str; PyObject *copyreg_str;
PyObject *copyreg_module; PyObject *copyreg_module;
PyInterpreterState *interp = PyThreadState_GET()->interp;
_Py_IDENTIFIER(copyreg); _Py_IDENTIFIER(copyreg);
copyreg_str = _PyUnicode_FromId(&PyId_copyreg); copyreg_str = _PyUnicode_FromId(&PyId_copyreg);
...@@ -3913,7 +3914,7 @@ import_copyreg(void) ...@@ -3913,7 +3914,7 @@ import_copyreg(void)
by storing a reference to the cached module in a static variable, but by storing a reference to the cached module in a static variable, but
this broke when multiple embedded interpreters were in use (see issue this broke when multiple embedded interpreters were in use (see issue
#17408 and #19088). */ #17408 and #19088). */
copyreg_module = _PyImport_GetModuleWithError(copyreg_str); copyreg_module = PyDict_GetItemWithError(interp->modules, copyreg_str);
if (copyreg_module != NULL) { if (copyreg_module != NULL) {
Py_INCREF(copyreg_module); Py_INCREF(copyreg_module);
return copyreg_module; return copyreg_module;
......
...@@ -38,6 +38,7 @@ static PyObject * ...@@ -38,6 +38,7 @@ static PyObject *
get_warnings_attr(const char *attr, int try_import) get_warnings_attr(const char *attr, int try_import)
{ {
static PyObject *warnings_str = NULL; static PyObject *warnings_str = NULL;
PyObject *all_modules;
PyObject *warnings_module, *obj; PyObject *warnings_module, *obj;
if (warnings_str == NULL) { if (warnings_str == NULL) {
...@@ -57,7 +58,9 @@ get_warnings_attr(const char *attr, int try_import) ...@@ -57,7 +58,9 @@ get_warnings_attr(const char *attr, int try_import)
} }
} }
else { else {
warnings_module = _PyImport_GetModule(warnings_str); all_modules = PyImport_GetModuleDict();
warnings_module = PyDict_GetItem(all_modules, warnings_str);
if (warnings_module == NULL) if (warnings_module == NULL)
return NULL; return NULL;
......
...@@ -2685,7 +2685,7 @@ _PyBuiltin_Init(void) ...@@ -2685,7 +2685,7 @@ _PyBuiltin_Init(void)
PyType_Ready(&PyZip_Type) < 0) PyType_Ready(&PyZip_Type) < 0)
return NULL; return NULL;
mod = _PyModule_CreateInitialized(&builtinsmodule, PYTHON_API_VERSION); mod = PyModule_Create(&builtinsmodule);
if (mod == NULL) if (mod == NULL)
return NULL; return NULL;
dict = PyModule_GetDict(mod); dict = PyModule_GetDict(mod);
......
...@@ -4935,7 +4935,7 @@ import_from(PyObject *v, PyObject *name) ...@@ -4935,7 +4935,7 @@ import_from(PyObject *v, PyObject *name)
Py_DECREF(pkgname); Py_DECREF(pkgname);
return NULL; return NULL;
} }
x = _PyImport_GetModule(fullmodname); x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname);
Py_DECREF(fullmodname); Py_DECREF(fullmodname);
if (x == NULL) { if (x == NULL) {
goto error; goto error;
......
This diff is collapsed.
...@@ -215,8 +215,7 @@ _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp) ...@@ -215,8 +215,7 @@ _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
else else
Py_INCREF(path); Py_INCREF(path);
PyObject *modules = PyImport_GetModuleDict(); if (_PyImport_FixupExtensionObject(m, name_unicode, path) < 0)
if (_PyImport_FixupExtensionObject(m, name_unicode, path, modules) < 0)
goto error; goto error;
Py_DECREF(name_unicode); Py_DECREF(name_unicode);
......
...@@ -42,7 +42,6 @@ _Py_IDENTIFIER(name); ...@@ -42,7 +42,6 @@ _Py_IDENTIFIER(name);
_Py_IDENTIFIER(stdin); _Py_IDENTIFIER(stdin);
_Py_IDENTIFIER(stdout); _Py_IDENTIFIER(stdout);
_Py_IDENTIFIER(stderr); _Py_IDENTIFIER(stderr);
_Py_IDENTIFIER(threading);
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
...@@ -284,6 +283,7 @@ initimport(PyInterpreterState *interp, PyObject *sysmod) ...@@ -284,6 +283,7 @@ initimport(PyInterpreterState *interp, PyObject *sysmod)
{ {
PyObject *importlib; PyObject *importlib;
PyObject *impmod; PyObject *impmod;
PyObject *sys_modules;
PyObject *value; PyObject *value;
/* Import _importlib through its frozen version, _frozen_importlib. */ /* Import _importlib through its frozen version, _frozen_importlib. */
...@@ -314,7 +314,11 @@ initimport(PyInterpreterState *interp, PyObject *sysmod) ...@@ -314,7 +314,11 @@ initimport(PyInterpreterState *interp, PyObject *sysmod)
else if (Py_VerboseFlag) { else if (Py_VerboseFlag) {
PySys_FormatStderr("import _imp # builtin\n"); PySys_FormatStderr("import _imp # builtin\n");
} }
if (_PyImport_SetModuleString("_imp", impmod) < 0) { sys_modules = PyImport_GetModuleDict();
if (Py_VerboseFlag) {
PySys_FormatStderr("import sys # builtin\n");
}
if (PyDict_SetItemString(sys_modules, "_imp", impmod) < 0) {
Py_FatalError("Py_Initialize: can't save _imp to sys.modules"); Py_FatalError("Py_Initialize: can't save _imp to sys.modules");
} }
...@@ -671,20 +675,10 @@ void _Py_InitializeCore(const _PyCoreConfig *config) ...@@ -671,20 +675,10 @@ void _Py_InitializeCore(const _PyCoreConfig *config)
if (!_PyFloat_Init()) if (!_PyFloat_Init())
Py_FatalError("Py_InitializeCore: can't init float"); Py_FatalError("Py_InitializeCore: can't init float");
PyObject *modules = PyDict_New(); interp->modules = PyDict_New();
if (modules == NULL) if (interp->modules == NULL)
Py_FatalError("Py_InitializeCore: can't make modules dictionary"); Py_FatalError("Py_InitializeCore: can't make modules dictionary");
sysmod = _PySys_BeginInit();
if (sysmod == NULL)
Py_FatalError("Py_InitializeCore: can't initialize sys");
interp->sysdict = PyModule_GetDict(sysmod);
if (interp->sysdict == NULL)
Py_FatalError("Py_InitializeCore: can't initialize sys dict");
Py_INCREF(interp->sysdict);
PyDict_SetItemString(interp->sysdict, "modules", modules);
_PyImport_FixupBuiltin(sysmod, "sys", modules);
/* Init Unicode implementation; relies on the codec registry */ /* Init Unicode implementation; relies on the codec registry */
if (_PyUnicode_Init() < 0) if (_PyUnicode_Init() < 0)
Py_FatalError("Py_InitializeCore: can't initialize unicode"); Py_FatalError("Py_InitializeCore: can't initialize unicode");
...@@ -695,7 +689,7 @@ void _Py_InitializeCore(const _PyCoreConfig *config) ...@@ -695,7 +689,7 @@ void _Py_InitializeCore(const _PyCoreConfig *config)
bimod = _PyBuiltin_Init(); bimod = _PyBuiltin_Init();
if (bimod == NULL) if (bimod == NULL)
Py_FatalError("Py_InitializeCore: can't initialize builtins modules"); Py_FatalError("Py_InitializeCore: can't initialize builtins modules");
_PyImport_FixupBuiltin(bimod, "builtins", modules); _PyImport_FixupBuiltin(bimod, "builtins");
interp->builtins = PyModule_GetDict(bimod); interp->builtins = PyModule_GetDict(bimod);
if (interp->builtins == NULL) if (interp->builtins == NULL)
Py_FatalError("Py_InitializeCore: can't initialize builtins dict"); Py_FatalError("Py_InitializeCore: can't initialize builtins dict");
...@@ -704,6 +698,17 @@ void _Py_InitializeCore(const _PyCoreConfig *config) ...@@ -704,6 +698,17 @@ void _Py_InitializeCore(const _PyCoreConfig *config)
/* initialize builtin exceptions */ /* initialize builtin exceptions */
_PyExc_Init(bimod); _PyExc_Init(bimod);
sysmod = _PySys_BeginInit();
if (sysmod == NULL)
Py_FatalError("Py_InitializeCore: can't initialize sys");
interp->sysdict = PyModule_GetDict(sysmod);
if (interp->sysdict == NULL)
Py_FatalError("Py_InitializeCore: can't initialize sys dict");
Py_INCREF(interp->sysdict);
_PyImport_FixupBuiltin(sysmod, "sys");
PyDict_SetItemString(interp->sysdict, "modules",
interp->modules);
/* Set up a preliminary stderr printer until we have enough /* Set up a preliminary stderr printer until we have enough
infrastructure for the io module in place. */ infrastructure for the io module in place. */
pstderr = PyFile_NewStdPrinter(fileno(stderr)); pstderr = PyFile_NewStdPrinter(fileno(stderr));
...@@ -1006,11 +1011,6 @@ Py_FinalizeEx(void) ...@@ -1006,11 +1011,6 @@ Py_FinalizeEx(void)
while (_PyGC_CollectIfEnabled() > 0) while (_PyGC_CollectIfEnabled() > 0)
/* nothing */; /* nothing */;
#endif #endif
#ifdef Py_REF_DEBUG
PyObject *showrefcount = _PyDebug_XOptionShowRefCount();
#endif
/* Destroy all modules */ /* Destroy all modules */
PyImport_Cleanup(); PyImport_Cleanup();
...@@ -1058,10 +1058,7 @@ Py_FinalizeEx(void) ...@@ -1058,10 +1058,7 @@ Py_FinalizeEx(void)
/* dump hash stats */ /* dump hash stats */
_PyHash_Fini(); _PyHash_Fini();
#ifdef Py_REF_DEBUG _PY_DEBUG_PRINT_TOTAL_REFS();
if (showrefcount == Py_True)
_PyDebug_PrintTotalRefs();
#endif
#ifdef Py_TRACE_REFS #ifdef Py_TRACE_REFS
/* Display all objects still alive -- this can invoke arbitrary /* Display all objects still alive -- this can invoke arbitrary
...@@ -1206,22 +1203,9 @@ Py_NewInterpreter(void) ...@@ -1206,22 +1203,9 @@ Py_NewInterpreter(void)
/* XXX The following is lax in error checking */ /* XXX The following is lax in error checking */
PyObject *modules = PyDict_New(); interp->modules = PyDict_New();
if (modules == NULL)
Py_FatalError("Py_NewInterpreter: can't make modules dictionary");
sysmod = _PyImport_FindBuiltin("sys", modules); bimod = _PyImport_FindBuiltin("builtins");
if (sysmod != NULL) {
interp->sysdict = PyModule_GetDict(sysmod);
if (interp->sysdict == NULL)
goto handle_error;
Py_INCREF(interp->sysdict);
PyDict_SetItemString(interp->sysdict, "modules", modules);
PySys_SetPath(Py_GetPath());
_PySys_EndInit(interp->sysdict);
}
bimod = _PyImport_FindBuiltin("builtins", modules);
if (bimod != NULL) { if (bimod != NULL) {
interp->builtins = PyModule_GetDict(bimod); interp->builtins = PyModule_GetDict(bimod);
if (interp->builtins == NULL) if (interp->builtins == NULL)
...@@ -1232,9 +1216,18 @@ Py_NewInterpreter(void) ...@@ -1232,9 +1216,18 @@ Py_NewInterpreter(void)
/* initialize builtin exceptions */ /* initialize builtin exceptions */
_PyExc_Init(bimod); _PyExc_Init(bimod);
sysmod = _PyImport_FindBuiltin("sys");
if (bimod != NULL && sysmod != NULL) { if (bimod != NULL && sysmod != NULL) {
PyObject *pstderr; PyObject *pstderr;
interp->sysdict = PyModule_GetDict(sysmod);
if (interp->sysdict == NULL)
goto handle_error;
Py_INCREF(interp->sysdict);
_PySys_EndInit(interp->sysdict);
PySys_SetPath(Py_GetPath());
PyDict_SetItemString(interp->sysdict, "modules",
interp->modules);
/* Set up a preliminary stderr printer until we have enough /* Set up a preliminary stderr printer until we have enough
infrastructure for the io module in place. */ infrastructure for the io module in place. */
pstderr = PyFile_NewStdPrinter(fileno(stderr)); pstderr = PyFile_NewStdPrinter(fileno(stderr));
...@@ -1910,13 +1903,14 @@ wait_for_thread_shutdown(void) ...@@ -1910,13 +1903,14 @@ wait_for_thread_shutdown(void)
{ {
_Py_IDENTIFIER(_shutdown); _Py_IDENTIFIER(_shutdown);
PyObject *result; PyObject *result;
PyObject *threading = _PyImport_GetModuleId(&PyId_threading); PyThreadState *tstate = PyThreadState_GET();
PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
"threading");
if (threading == NULL) { if (threading == NULL) {
/* threading not imported */ /* threading not imported */
PyErr_Clear(); PyErr_Clear();
return; return;
} }
Py_INCREF(threading);
result = _PyObject_CallMethodId(threading, &PyId__shutdown, NULL); result = _PyObject_CallMethodId(threading, &PyId__shutdown, NULL);
if (result == NULL) { if (result == NULL) {
PyErr_WriteUnraisable(threading); PyErr_WriteUnraisable(threading);
......
...@@ -90,12 +90,15 @@ PyInterpreterState_New(void) ...@@ -90,12 +90,15 @@ PyInterpreterState_New(void)
PyMem_RawMalloc(sizeof(PyInterpreterState)); PyMem_RawMalloc(sizeof(PyInterpreterState));
if (interp != NULL) { if (interp != NULL) {
interp->modules = NULL;
interp->modules_by_index = NULL; interp->modules_by_index = NULL;
interp->sysdict = NULL; interp->sysdict = NULL;
interp->builtins = NULL; interp->builtins = NULL;
interp->builtins_copy = NULL; interp->builtins_copy = NULL;
interp->tstate_head = NULL; interp->tstate_head = NULL;
interp->check_interval = 100; interp->check_interval = 100;
interp->warnoptions = NULL;
interp->xoptions = NULL;
interp->num_threads = 0; interp->num_threads = 0;
interp->pythread_stacksize = 0; interp->pythread_stacksize = 0;
interp->codec_search_path = NULL; interp->codec_search_path = NULL;
...@@ -153,6 +156,7 @@ PyInterpreterState_Clear(PyInterpreterState *interp) ...@@ -153,6 +156,7 @@ PyInterpreterState_Clear(PyInterpreterState *interp)
Py_CLEAR(interp->codec_search_path); Py_CLEAR(interp->codec_search_path);
Py_CLEAR(interp->codec_search_cache); Py_CLEAR(interp->codec_search_cache);
Py_CLEAR(interp->codec_error_registry); Py_CLEAR(interp->codec_error_registry);
Py_CLEAR(interp->modules);
Py_CLEAR(interp->modules_by_index); Py_CLEAR(interp->modules_by_index);
Py_CLEAR(interp->sysdict); Py_CLEAR(interp->sysdict);
Py_CLEAR(interp->builtins); Py_CLEAR(interp->builtins);
......
...@@ -113,10 +113,7 @@ PyRun_InteractiveLoopFlags(FILE *fp, const char *filename_str, PyCompilerFlags * ...@@ -113,10 +113,7 @@ PyRun_InteractiveLoopFlags(FILE *fp, const char *filename_str, PyCompilerFlags *
err = -1; err = -1;
for (;;) { for (;;) {
ret = PyRun_InteractiveOneObject(fp, filename, flags); ret = PyRun_InteractiveOneObject(fp, filename, flags);
#ifdef Py_REF_DEBUG _PY_DEBUG_PRINT_TOTAL_REFS();
if (_PyDebug_XOptionShowRefCount() == Py_True)
_PyDebug_PrintTotalRefs();
#endif
if (ret == E_EOF) { if (ret == E_EOF) {
err = 0; err = 0;
break; break;
......
...@@ -36,14 +36,12 @@ extern const char *PyWin_DLLVersionString; ...@@ -36,14 +36,12 @@ extern const char *PyWin_DLLVersionString;
_Py_IDENTIFIER(_); _Py_IDENTIFIER(_);
_Py_IDENTIFIER(__sizeof__); _Py_IDENTIFIER(__sizeof__);
_Py_IDENTIFIER(_xoptions);
_Py_IDENTIFIER(buffer); _Py_IDENTIFIER(buffer);
_Py_IDENTIFIER(builtins); _Py_IDENTIFIER(builtins);
_Py_IDENTIFIER(encoding); _Py_IDENTIFIER(encoding);
_Py_IDENTIFIER(path); _Py_IDENTIFIER(path);
_Py_IDENTIFIER(stdout); _Py_IDENTIFIER(stdout);
_Py_IDENTIFIER(stderr); _Py_IDENTIFIER(stderr);
_Py_IDENTIFIER(warnoptions);
_Py_IDENTIFIER(write); _Py_IDENTIFIER(write);
PyObject * PyObject *
...@@ -162,11 +160,13 @@ static PyObject * ...@@ -162,11 +160,13 @@ static PyObject *
sys_displayhook(PyObject *self, PyObject *o) sys_displayhook(PyObject *self, PyObject *o)
{ {
PyObject *outf; PyObject *outf;
PyInterpreterState *interp = PyThreadState_GET()->interp;
PyObject *modules = interp->modules;
PyObject *builtins; PyObject *builtins;
static PyObject *newline = NULL; static PyObject *newline = NULL;
int err; int err;
builtins = _PyImport_GetModuleId(&PyId_builtins); builtins = _PyDict_GetItemId(modules, &PyId_builtins);
if (builtins == NULL) { if (builtins == NULL) {
PyErr_SetString(PyExc_RuntimeError, "lost builtins module"); PyErr_SetString(PyExc_RuntimeError, "lost builtins module");
return NULL; return NULL;
...@@ -1481,17 +1481,13 @@ list_builtin_module_names(void) ...@@ -1481,17 +1481,13 @@ list_builtin_module_names(void)
static PyObject * static PyObject *
get_warnoptions(void) get_warnoptions(void)
{ {
PyObject *warnoptions = _PySys_GetObjectId(&PyId_warnoptions); PyObject *warnoptions = PyThreadState_GET()->interp->warnoptions;
if (warnoptions == NULL || !PyList_Check(warnoptions)) { if (warnoptions == NULL || !PyList_Check(warnoptions)) {
Py_XDECREF(warnoptions); Py_XDECREF(warnoptions);
warnoptions = PyList_New(0); warnoptions = PyList_New(0);
if (warnoptions == NULL) if (warnoptions == NULL)
return NULL; return NULL;
if (_PySys_SetObjectId(&PyId_warnoptions, warnoptions)) { PyThreadState_GET()->interp->warnoptions = warnoptions;
Py_DECREF(warnoptions);
return NULL;
}
Py_DECREF(warnoptions);
} }
return warnoptions; return warnoptions;
} }
...@@ -1499,7 +1495,7 @@ get_warnoptions(void) ...@@ -1499,7 +1495,7 @@ get_warnoptions(void)
void void
PySys_ResetWarnOptions(void) PySys_ResetWarnOptions(void)
{ {
PyObject *warnoptions = _PySys_GetObjectId(&PyId_warnoptions); PyObject *warnoptions = PyThreadState_GET()->interp->warnoptions;
if (warnoptions == NULL || !PyList_Check(warnoptions)) if (warnoptions == NULL || !PyList_Check(warnoptions))
return; return;
PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL); PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL);
...@@ -1528,24 +1524,20 @@ PySys_AddWarnOption(const wchar_t *s) ...@@ -1528,24 +1524,20 @@ PySys_AddWarnOption(const wchar_t *s)
int int
PySys_HasWarnOptions(void) PySys_HasWarnOptions(void)
{ {
PyObject *warnoptions = _PySys_GetObjectId(&PyId_warnoptions); PyObject *warnoptions = PyThreadState_GET()->interp->warnoptions;
return (warnoptions != NULL && (PyList_Size(warnoptions) > 0)) ? 1 : 0; return (warnoptions != NULL && (PyList_Size(warnoptions) > 0)) ? 1 : 0;
} }
static PyObject * static PyObject *
get_xoptions(void) get_xoptions(void)
{ {
PyObject *xoptions = _PySys_GetObjectId(&PyId__xoptions); PyObject *xoptions = PyThreadState_GET()->interp->xoptions;
if (xoptions == NULL || !PyDict_Check(xoptions)) { if (xoptions == NULL || !PyDict_Check(xoptions)) {
Py_XDECREF(xoptions); Py_XDECREF(xoptions);
xoptions = PyDict_New(); xoptions = PyDict_New();
if (xoptions == NULL) if (xoptions == NULL)
return NULL; return NULL;
if (_PySys_SetObjectId(&PyId__xoptions, xoptions)) { PyThreadState_GET()->interp->xoptions = xoptions;
Py_DECREF(xoptions);
return NULL;
}
Py_DECREF(xoptions);
} }
return xoptions; return xoptions;
} }
...@@ -1947,7 +1939,7 @@ _PySys_BeginInit(void) ...@@ -1947,7 +1939,7 @@ _PySys_BeginInit(void)
PyObject *m, *sysdict, *version_info; PyObject *m, *sysdict, *version_info;
int res; int res;
m = _PyModule_CreateInitialized(&sysmodule, PYTHON_API_VERSION); m = PyModule_Create(&sysmodule);
if (m == NULL) if (m == NULL)
return NULL; return NULL;
sysdict = PyModule_GetDict(m); sysdict = PyModule_GetDict(m);
...@@ -2094,6 +2086,16 @@ _PySys_BeginInit(void) ...@@ -2094,6 +2086,16 @@ _PySys_BeginInit(void)
#undef SET_SYS_FROM_STRING_BORROW #undef SET_SYS_FROM_STRING_BORROW
/* Updating the sys namespace, returning integer error codes */ /* Updating the sys namespace, returning integer error codes */
#define SET_SYS_FROM_STRING_BORROW_INT_RESULT(key, value) \
do { \
PyObject *v = (value); \
if (v == NULL) \
return -1; \
res = PyDict_SetItemString(sysdict, key, v); \
if (res < 0) { \
return res; \
} \
} while (0)
#define SET_SYS_FROM_STRING_INT_RESULT(key, value) \ #define SET_SYS_FROM_STRING_INT_RESULT(key, value) \
do { \ do { \
PyObject *v = (value); \ PyObject *v = (value); \
...@@ -2138,11 +2140,15 @@ _PySys_EndInit(PyObject *sysdict) ...@@ -2138,11 +2140,15 @@ _PySys_EndInit(PyObject *sysdict)
SET_SYS_FROM_STRING_INT_RESULT("base_exec_prefix", SET_SYS_FROM_STRING_INT_RESULT("base_exec_prefix",
PyUnicode_FromWideChar(Py_GetExecPrefix(), -1)); PyUnicode_FromWideChar(Py_GetExecPrefix(), -1));
if (get_warnoptions() == NULL) PyObject *warnoptions = get_warnoptions();
if (warnoptions == NULL)
return -1; return -1;
SET_SYS_FROM_STRING_BORROW_INT_RESULT("warnoptions", warnoptions);
if (get_xoptions() == NULL) PyObject *xoptions = get_xoptions();
if (xoptions == NULL)
return -1; return -1;
SET_SYS_FROM_STRING_BORROW_INT_RESULT("_xoptions", xoptions);
if (PyErr_Occurred()) if (PyErr_Occurred())
return -1; return -1;
...@@ -2150,6 +2156,7 @@ _PySys_EndInit(PyObject *sysdict) ...@@ -2150,6 +2156,7 @@ _PySys_EndInit(PyObject *sysdict)
} }
#undef SET_SYS_FROM_STRING_INT_RESULT #undef SET_SYS_FROM_STRING_INT_RESULT
#undef SET_SYS_FROM_STRING_BORROW_INT_RESULT
static PyObject * static PyObject *
makepathobject(const wchar_t *path, wchar_t delim) makepathobject(const wchar_t *path, wchar_t delim)
......
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