mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
bpo-28411: Remove "modules" field from Py_InterpreterState. (#1638)
sys.modules is the one true source.
This commit is contained in:
parent
f5ea83f486
commit
86b7afdfee
18 changed files with 261 additions and 111 deletions
|
@ -44,7 +44,6 @@ static PyObject *
|
|||
get_warnings_attr(const char *attr, int try_import)
|
||||
{
|
||||
static PyObject *warnings_str = NULL;
|
||||
PyObject *all_modules;
|
||||
PyObject *warnings_module, *obj;
|
||||
|
||||
if (warnings_str == NULL) {
|
||||
|
@ -64,9 +63,7 @@ get_warnings_attr(const char *attr, int try_import)
|
|||
}
|
||||
}
|
||||
else {
|
||||
all_modules = PyImport_GetModuleDict();
|
||||
|
||||
warnings_module = PyDict_GetItem(all_modules, warnings_str);
|
||||
warnings_module = _PyImport_GetModule(warnings_str);
|
||||
if (warnings_module == NULL)
|
||||
return NULL;
|
||||
|
||||
|
|
|
@ -2685,7 +2685,7 @@ _PyBuiltin_Init(void)
|
|||
PyType_Ready(&PyZip_Type) < 0)
|
||||
return NULL;
|
||||
|
||||
mod = PyModule_Create(&builtinsmodule);
|
||||
mod = _PyModule_CreateInitialized(&builtinsmodule, PYTHON_API_VERSION);
|
||||
if (mod == NULL)
|
||||
return NULL;
|
||||
dict = PyModule_GetDict(mod);
|
||||
|
|
|
@ -5054,7 +5054,7 @@ import_from(PyObject *v, PyObject *name)
|
|||
Py_DECREF(pkgname);
|
||||
return NULL;
|
||||
}
|
||||
x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname);
|
||||
x = _PyImport_GetModule(fullmodname);
|
||||
Py_DECREF(fullmodname);
|
||||
if (x == NULL) {
|
||||
goto error;
|
||||
|
|
205
Python/import.c
205
Python/import.c
|
@ -303,10 +303,115 @@ _PyImport_Fini(void)
|
|||
PyObject *
|
||||
PyImport_GetModuleDict(void)
|
||||
{
|
||||
PyInterpreterState *interp = PyThreadState_GET()->interp;
|
||||
if (interp->modules == NULL)
|
||||
Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
|
||||
return interp->modules;
|
||||
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 modules;
|
||||
}
|
||||
|
||||
/* In some corner cases it is important to be sure that the import
|
||||
machinery has been initialized (or not cleaned up yet). For
|
||||
example, see issue #4236 and PyModule_Create2(). */
|
||||
|
||||
int
|
||||
_PyImport_IsInitialized(PyInterpreterState *interp)
|
||||
{
|
||||
if (interp->sysdict == NULL)
|
||||
return 0;
|
||||
_Py_IDENTIFIER(modules);
|
||||
PyObject *modules = _PyDict_GetItemId(interp->sysdict, &PyId_modules);
|
||||
if (modules == NULL)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
PyObject *
|
||||
_PyImport_GetModule(PyObject *name)
|
||||
{
|
||||
PyObject *modules = PyImport_GetModuleDict();
|
||||
if (PyDict_CheckExact(modules)) {
|
||||
return PyDict_GetItem(modules, name);
|
||||
}
|
||||
|
||||
PyObject *mod = PyObject_GetItem(modules, name);
|
||||
// For backward-comaptibility we copy the behavior of PyDict_GetItem().
|
||||
if (PyErr_Occurred()) {
|
||||
PyErr_Clear();
|
||||
}
|
||||
Py_XDECREF(mod);
|
||||
return mod;
|
||||
}
|
||||
|
||||
PyObject *
|
||||
_PyImport_GetModuleWithError(PyObject *name)
|
||||
{
|
||||
PyObject *modules = PyImport_GetModuleDict();
|
||||
if (PyDict_CheckExact(modules)) {
|
||||
return PyDict_GetItemWithError(modules, name);
|
||||
}
|
||||
|
||||
PyObject *mod = PyObject_GetItem(modules, name);
|
||||
// For backward-comaptibility we copy the behavior
|
||||
// of PyDict_GetItemWithError().
|
||||
if (PyErr_ExceptionMatches(PyExc_KeyError)) {
|
||||
PyErr_Clear();
|
||||
}
|
||||
return mod;
|
||||
}
|
||||
|
||||
PyObject *
|
||||
_PyImport_GetModuleId(struct _Py_Identifier *nameid)
|
||||
{
|
||||
PyObject *name = _PyUnicode_FromId(nameid); /* borrowed */
|
||||
if (name == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
return _PyImport_GetModule(name);
|
||||
}
|
||||
|
||||
int
|
||||
_PyImport_SetModule(PyObject *name, PyObject *m)
|
||||
{
|
||||
PyObject *modules = PyImport_GetModuleDict();
|
||||
return PyObject_SetItem(modules, name, m);
|
||||
}
|
||||
|
||||
int
|
||||
_PyImport_SetModuleString(const char *name, PyObject *m)
|
||||
{
|
||||
PyObject *modules = PyImport_GetModuleDict();
|
||||
return PyMapping_SetItemString(modules, name, m);
|
||||
}
|
||||
|
||||
PyObject *
|
||||
PyImport_GetModule(PyObject *name)
|
||||
{
|
||||
PyObject *m;
|
||||
PyObject *modules = PyImport_GetModuleDict();
|
||||
if (modules == NULL) {
|
||||
PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
|
||||
return NULL;
|
||||
}
|
||||
Py_INCREF(modules);
|
||||
if (PyDict_CheckExact(modules)) {
|
||||
m = PyDict_GetItemWithError(modules, name); /* borrowed */
|
||||
Py_XINCREF(m);
|
||||
}
|
||||
else {
|
||||
m = PyObject_GetItem(modules, name);
|
||||
if (PyErr_ExceptionMatches(PyExc_KeyError)) {
|
||||
PyErr_Clear();
|
||||
}
|
||||
}
|
||||
Py_DECREF(modules);
|
||||
return m;
|
||||
}
|
||||
|
||||
|
||||
|
@ -336,7 +441,7 @@ PyImport_Cleanup(void)
|
|||
Py_ssize_t pos;
|
||||
PyObject *key, *value, *dict;
|
||||
PyInterpreterState *interp = PyThreadState_GET()->interp;
|
||||
PyObject *modules = interp->modules;
|
||||
PyObject *modules = PyImport_GetModuleDict();
|
||||
PyObject *weaklist = NULL;
|
||||
const char * const *p;
|
||||
|
||||
|
@ -398,7 +503,7 @@ PyImport_Cleanup(void)
|
|||
if (Py_VerboseFlag && PyUnicode_Check(key))
|
||||
PySys_FormatStderr("# cleanup[2] removing %U\n", key);
|
||||
STORE_MODULE_WEAKREF(key, value);
|
||||
PyDict_SetItem(modules, key, Py_None);
|
||||
PyObject_SetItem(modules, key, Py_None);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -465,7 +570,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 */
|
||||
|
@ -524,9 +628,9 @@ PyImport_GetMagicTag(void)
|
|||
|
||||
int
|
||||
_PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
|
||||
PyObject *filename)
|
||||
PyObject *filename, PyObject *modules)
|
||||
{
|
||||
PyObject *modules, *dict, *key;
|
||||
PyObject *dict, *key;
|
||||
struct PyModuleDef *def;
|
||||
int res;
|
||||
if (extensions == NULL) {
|
||||
|
@ -543,11 +647,10 @@ _PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
|
|||
PyErr_BadInternalCall();
|
||||
return -1;
|
||||
}
|
||||
modules = PyImport_GetModuleDict();
|
||||
if (PyDict_SetItem(modules, name, mod) < 0)
|
||||
if (PyObject_SetItem(modules, name, mod) < 0)
|
||||
return -1;
|
||||
if (_PyState_AddModule(mod, def) < 0) {
|
||||
PyDict_DelItem(modules, name);
|
||||
PyMapping_DelItem(modules, name);
|
||||
return -1;
|
||||
}
|
||||
if (def->m_size == -1) {
|
||||
|
@ -575,20 +678,28 @@ _PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
|
|||
}
|
||||
|
||||
int
|
||||
_PyImport_FixupBuiltin(PyObject *mod, const char *name)
|
||||
_PyImport_FixupBuiltin(PyObject *mod, const char *name, PyObject *modules)
|
||||
{
|
||||
int res;
|
||||
PyObject *nameobj;
|
||||
nameobj = PyUnicode_InternFromString(name);
|
||||
if (nameobj == NULL)
|
||||
return -1;
|
||||
res = _PyImport_FixupExtensionObject(mod, nameobj, nameobj);
|
||||
res = _PyImport_FixupExtensionObject(mod, nameobj, nameobj, modules);
|
||||
Py_DECREF(nameobj);
|
||||
return res;
|
||||
}
|
||||
|
||||
PyObject *
|
||||
_PyImport_FindExtensionObject(PyObject *name, PyObject *filename)
|
||||
{
|
||||
PyObject *modules = PyImport_GetModuleDict();
|
||||
return _PyImport_FindExtensionObjectEx(name, filename, modules);
|
||||
}
|
||||
|
||||
PyObject *
|
||||
_PyImport_FindExtensionObjectEx(PyObject *name, PyObject *filename,
|
||||
PyObject *modules)
|
||||
{
|
||||
PyObject *mod, *mdict, *key;
|
||||
PyModuleDef* def;
|
||||
|
@ -605,7 +716,7 @@ _PyImport_FindExtensionObject(PyObject *name, PyObject *filename)
|
|||
/* Module does not support repeated initialization */
|
||||
if (def->m_base.m_copy == NULL)
|
||||
return NULL;
|
||||
mod = PyImport_AddModuleObject(name);
|
||||
mod = _PyImport_AddModuleObject(name, modules);
|
||||
if (mod == NULL)
|
||||
return NULL;
|
||||
mdict = PyModule_GetDict(mod);
|
||||
|
@ -620,14 +731,14 @@ _PyImport_FindExtensionObject(PyObject *name, PyObject *filename)
|
|||
mod = def->m_base.m_init();
|
||||
if (mod == NULL)
|
||||
return NULL;
|
||||
if (PyDict_SetItem(PyImport_GetModuleDict(), name, mod) == -1) {
|
||||
if (PyObject_SetItem(modules, name, mod) == -1) {
|
||||
Py_DECREF(mod);
|
||||
return NULL;
|
||||
}
|
||||
Py_DECREF(mod);
|
||||
}
|
||||
if (_PyState_AddModule(mod, def) < 0) {
|
||||
PyDict_DelItem(PyImport_GetModuleDict(), name);
|
||||
PyMapping_DelItem(modules, name);
|
||||
Py_DECREF(mod);
|
||||
return NULL;
|
||||
}
|
||||
|
@ -639,13 +750,13 @@ _PyImport_FindExtensionObject(PyObject *name, PyObject *filename)
|
|||
}
|
||||
|
||||
PyObject *
|
||||
_PyImport_FindBuiltin(const char *name)
|
||||
_PyImport_FindBuiltin(const char *name, PyObject *modules)
|
||||
{
|
||||
PyObject *res, *nameobj;
|
||||
nameobj = PyUnicode_InternFromString(name);
|
||||
if (nameobj == NULL)
|
||||
return NULL;
|
||||
res = _PyImport_FindExtensionObject(nameobj, nameobj);
|
||||
res = _PyImport_FindExtensionObjectEx(nameobj, nameobj, modules);
|
||||
Py_DECREF(nameobj);
|
||||
return res;
|
||||
}
|
||||
|
@ -660,19 +771,34 @@ PyObject *
|
|||
PyImport_AddModuleObject(PyObject *name)
|
||||
{
|
||||
PyObject *modules = PyImport_GetModuleDict();
|
||||
PyObject *m;
|
||||
return _PyImport_AddModuleObject(name, modules);
|
||||
}
|
||||
|
||||
if ((m = PyDict_GetItemWithError(modules, name)) != NULL &&
|
||||
PyModule_Check(m)) {
|
||||
return m;
|
||||
PyObject *
|
||||
_PyImport_AddModuleObject(PyObject *name, PyObject *modules)
|
||||
{
|
||||
PyObject *m;
|
||||
if (PyDict_CheckExact(modules)) {
|
||||
m = PyDict_GetItemWithError(modules, name);
|
||||
}
|
||||
else {
|
||||
m = PyObject_GetItem(modules, name);
|
||||
// For backward-comaptibility we copy the behavior
|
||||
// of PyDict_GetItemWithError().
|
||||
if (PyErr_ExceptionMatches(PyExc_KeyError)) {
|
||||
PyErr_Clear();
|
||||
}
|
||||
}
|
||||
if (PyErr_Occurred()) {
|
||||
return NULL;
|
||||
}
|
||||
if (m != NULL && PyModule_Check(m)) {
|
||||
return m;
|
||||
}
|
||||
m = PyModule_NewObject(name);
|
||||
if (m == NULL)
|
||||
return NULL;
|
||||
if (PyDict_SetItem(modules, name, m) != 0) {
|
||||
if (PyObject_SetItem(modules, name, m) != 0) {
|
||||
Py_DECREF(m);
|
||||
return NULL;
|
||||
}
|
||||
|
@ -699,11 +825,13 @@ static void
|
|||
remove_module(PyObject *name)
|
||||
{
|
||||
PyObject *modules = PyImport_GetModuleDict();
|
||||
if (PyDict_GetItem(modules, name) == NULL)
|
||||
return;
|
||||
if (PyDict_DelItem(modules, name) < 0)
|
||||
if (PyMapping_DelItem(modules, name) < 0) {
|
||||
if (!PyMapping_HasKey(modules, name)) {
|
||||
return;
|
||||
}
|
||||
Py_FatalError("import: deleting existing key in"
|
||||
"sys.modules failed");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -812,7 +940,6 @@ module_dict_for_exec(PyObject *name)
|
|||
static PyObject *
|
||||
exec_code_in_module(PyObject *name, PyObject *module_dict, PyObject *code_object)
|
||||
{
|
||||
PyObject *modules = PyImport_GetModuleDict();
|
||||
PyObject *v, *m;
|
||||
|
||||
v = PyEval_EvalCode(code_object, module_dict, module_dict);
|
||||
|
@ -822,7 +949,8 @@ exec_code_in_module(PyObject *name, PyObject *module_dict, PyObject *code_object
|
|||
}
|
||||
Py_DECREF(v);
|
||||
|
||||
if ((m = PyDict_GetItem(modules, name)) == NULL) {
|
||||
m = _PyImport_GetModule(name);
|
||||
if (m == NULL) {
|
||||
PyErr_Format(PyExc_ImportError,
|
||||
"Loaded module %R not found in sys.modules",
|
||||
name);
|
||||
|
@ -1055,6 +1183,7 @@ _imp_create_builtin(PyObject *module, PyObject *spec)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
PyObject *modules = NULL;
|
||||
for (p = PyImport_Inittab; p->name != NULL; p++) {
|
||||
PyModuleDef *def;
|
||||
if (_PyUnicode_EqualToASCIIString(name, p->name)) {
|
||||
|
@ -1080,7 +1209,11 @@ _imp_create_builtin(PyObject *module, PyObject *spec)
|
|||
return NULL;
|
||||
}
|
||||
def->m_base.m_init = p->initfunc;
|
||||
if (_PyImport_FixupExtensionObject(mod, name, name) < 0) {
|
||||
if (modules == NULL) {
|
||||
modules = PyImport_GetModuleDict();
|
||||
}
|
||||
if (_PyImport_FixupExtensionObject(mod, name, name,
|
||||
modules) < 0) {
|
||||
Py_DECREF(name);
|
||||
return NULL;
|
||||
}
|
||||
|
@ -1524,7 +1657,7 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
|
|||
Py_INCREF(abs_name);
|
||||
}
|
||||
|
||||
mod = PyDict_GetItem(interp->modules, abs_name);
|
||||
mod = _PyImport_GetModule(abs_name);
|
||||
if (mod != NULL && mod != Py_None) {
|
||||
_Py_IDENTIFIER(__spec__);
|
||||
_Py_IDENTIFIER(_initializing);
|
||||
|
@ -1611,7 +1744,7 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
|
|||
goto error;
|
||||
}
|
||||
|
||||
final_mod = PyDict_GetItem(interp->modules, to_return);
|
||||
final_mod = _PyImport_GetModule(to_return);
|
||||
Py_DECREF(to_return);
|
||||
if (final_mod == NULL) {
|
||||
PyErr_Format(PyExc_KeyError,
|
||||
|
@ -1664,10 +1797,10 @@ PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals
|
|||
PyObject *
|
||||
PyImport_ReloadModule(PyObject *m)
|
||||
{
|
||||
_Py_IDENTIFIER(imp);
|
||||
_Py_IDENTIFIER(reload);
|
||||
PyObject *reloaded_module = NULL;
|
||||
PyObject *modules = PyImport_GetModuleDict();
|
||||
PyObject *imp = PyDict_GetItemString(modules, "imp");
|
||||
PyObject *imp = _PyImport_GetModuleId(&PyId_imp);
|
||||
if (imp == NULL) {
|
||||
imp = PyImport_ImportModule("imp");
|
||||
if (imp == NULL) {
|
||||
|
@ -1702,7 +1835,6 @@ PyImport_Import(PyObject *module_name)
|
|||
PyObject *globals = NULL;
|
||||
PyObject *import = NULL;
|
||||
PyObject *builtins = NULL;
|
||||
PyObject *modules = NULL;
|
||||
PyObject *r = NULL;
|
||||
|
||||
/* Initialize constant string objects */
|
||||
|
@ -1757,8 +1889,7 @@ PyImport_Import(PyObject *module_name)
|
|||
goto err;
|
||||
Py_DECREF(r);
|
||||
|
||||
modules = PyImport_GetModuleDict();
|
||||
r = PyDict_GetItemWithError(modules, module_name);
|
||||
r = _PyImport_GetModule(module_name);
|
||||
if (r != NULL) {
|
||||
Py_INCREF(r);
|
||||
}
|
||||
|
|
|
@ -215,7 +215,8 @@ _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
|
|||
else
|
||||
Py_INCREF(path);
|
||||
|
||||
if (_PyImport_FixupExtensionObject(m, name_unicode, path) < 0)
|
||||
PyObject *modules = PyImport_GetModuleDict();
|
||||
if (_PyImport_FixupExtensionObject(m, name_unicode, path, modules) < 0)
|
||||
goto error;
|
||||
|
||||
Py_DECREF(name_unicode);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -97,7 +97,6 @@ PyInterpreterState_New(void)
|
|||
if (head_mutex == NULL)
|
||||
Py_FatalError("Can't initialize threads for interpreter");
|
||||
#endif
|
||||
interp->modules = NULL;
|
||||
interp->modules_by_index = NULL;
|
||||
interp->sysdict = NULL;
|
||||
interp->builtins = NULL;
|
||||
|
@ -158,7 +157,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);
|
||||
|
|
|
@ -159,13 +159,11 @@ static PyObject *
|
|||
sys_displayhook(PyObject *self, PyObject *o)
|
||||
{
|
||||
PyObject *outf;
|
||||
PyInterpreterState *interp = PyThreadState_GET()->interp;
|
||||
PyObject *modules = interp->modules;
|
||||
PyObject *builtins;
|
||||
static PyObject *newline = NULL;
|
||||
int err;
|
||||
|
||||
builtins = _PyDict_GetItemId(modules, &PyId_builtins);
|
||||
builtins = _PyImport_GetModuleId(&PyId_builtins);
|
||||
if (builtins == NULL) {
|
||||
PyErr_SetString(PyExc_RuntimeError, "lost builtins module");
|
||||
return NULL;
|
||||
|
@ -1929,7 +1927,7 @@ _PySys_BeginInit(void)
|
|||
PyObject *m, *sysdict, *version_info;
|
||||
int res;
|
||||
|
||||
m = PyModule_Create(&sysmodule);
|
||||
m = _PyModule_CreateInitialized(&sysmodule, PYTHON_API_VERSION);
|
||||
if (m == NULL)
|
||||
return NULL;
|
||||
sysdict = PyModule_GetDict(m);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue