summaryrefslogtreecommitdiffstats
path: root/Python/pylifecycle.c
diff options
context:
space:
mode:
Diffstat (limited to 'Python/pylifecycle.c')
-rw-r--r--Python/pylifecycle.c66
1 files changed, 32 insertions, 34 deletions
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index eaa7b7f..662405b 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -41,6 +41,7 @@ _Py_IDENTIFIER(name);
_Py_IDENTIFIER(stdin);
_Py_IDENTIFIER(stdout);
_Py_IDENTIFIER(stderr);
+_Py_IDENTIFIER(threading);
#ifdef __cplusplus
extern "C" {
@@ -262,7 +263,6 @@ initimport(PyInterpreterState *interp, PyObject *sysmod)
{
PyObject *importlib;
PyObject *impmod;
- PyObject *sys_modules;
PyObject *value;
/* Import _importlib through its frozen version, _frozen_importlib. */
@@ -293,11 +293,7 @@ initimport(PyInterpreterState *interp, PyObject *sysmod)
else if (Py_VerboseFlag) {
PySys_FormatStderr("import _imp # builtin\n");
}
- sys_modules = PyImport_GetModuleDict();
- if (Py_VerboseFlag) {
- PySys_FormatStderr("import sys # builtin\n");
- }
- if (PyDict_SetItemString(sys_modules, "_imp", impmod) < 0) {
+ if (_PyImport_SetModuleString("_imp", impmod) < 0) {
Py_FatalError("Py_Initialize: can't save _imp to sys.modules");
}
@@ -647,10 +643,20 @@ void _Py_InitializeCore(const _PyCoreConfig *config)
if (!_PyFloat_Init())
Py_FatalError("Py_InitializeCore: can't init float");
- interp->modules = PyDict_New();
- if (interp->modules == NULL)
+ PyObject *modules = PyDict_New();
+ if (modules == NULL)
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 */
if (_PyUnicode_Init() < 0)
Py_FatalError("Py_InitializeCore: can't initialize unicode");
@@ -661,7 +667,7 @@ void _Py_InitializeCore(const _PyCoreConfig *config)
bimod = _PyBuiltin_Init();
if (bimod == NULL)
Py_FatalError("Py_InitializeCore: can't initialize builtins modules");
- _PyImport_FixupBuiltin(bimod, "builtins");
+ _PyImport_FixupBuiltin(bimod, "builtins", modules);
interp->builtins = PyModule_GetDict(bimod);
if (interp->builtins == NULL)
Py_FatalError("Py_InitializeCore: can't initialize builtins dict");
@@ -670,17 +676,6 @@ void _Py_InitializeCore(const _PyCoreConfig *config)
/* initialize builtin exceptions */
_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
infrastructure for the io module in place. */
pstderr = PyFile_NewStdPrinter(fileno(stderr));
@@ -1178,9 +1173,22 @@ Py_NewInterpreter(void)
/* XXX The following is lax in error checking */
- interp->modules = PyDict_New();
+ PyObject *modules = PyDict_New();
+ if (modules == NULL)
+ Py_FatalError("Py_NewInterpreter: can't make modules dictionary");
- bimod = _PyImport_FindBuiltin("builtins");
+ sysmod = _PyImport_FindBuiltin("sys", modules);
+ 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) {
interp->builtins = PyModule_GetDict(bimod);
if (interp->builtins == NULL)
@@ -1191,18 +1199,9 @@ Py_NewInterpreter(void)
/* initialize builtin exceptions */
_PyExc_Init(bimod);
- sysmod = _PyImport_FindBuiltin("sys");
if (bimod != NULL && sysmod != NULL) {
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
infrastructure for the io module in place. */
pstderr = PyFile_NewStdPrinter(fileno(stderr));
@@ -1882,14 +1881,13 @@ wait_for_thread_shutdown(void)
#ifdef WITH_THREAD
_Py_IDENTIFIER(_shutdown);
PyObject *result;
- PyThreadState *tstate = PyThreadState_GET();
- PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
- "threading");
+ PyObject *threading = _PyImport_GetModuleId(&PyId_threading);
if (threading == NULL) {
/* threading not imported */
PyErr_Clear();
return;
}
+ Py_INCREF(threading);
result = _PyObject_CallMethodId(threading, &PyId__shutdown, NULL);
if (result == NULL) {
PyErr_WriteUnraisable(threading);