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

Skip to content

Commit 85cd1d6

Browse files
committed
The code in PyImport_Import() tried to save itself a bit of work and
save the __builtin__ module in a static variable. But this doesn't work across Py_Finalise()/Py_Initialize()! It also doesn't work when using multiple interpreter states created with PyInterpreterState_New(). So I'm ripping out this small optimization. This was probably broken since PyImport_Import() was introduced in 1997! We really need a better test suite for multiple interpreter states and repeatedly initializing. This fixes the problems Barry reported in Demo/embed/loop.c.
1 parent 8b41116 commit 85cd1d6

1 file changed

Lines changed: 7 additions & 13 deletions

File tree

Python/import.c

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,7 +1154,8 @@ check_case(char *buf, int len, int namelen, char *name)
11541154
name, buf);
11551155
return 0;
11561156
}
1157-
if ( namelen > fss.name[0] || strncmp(name, (char *)fss.name+1, namelen) != 0 ) {
1157+
if (namelen > fss.name[0] ||
1158+
strncmp(name, (char *)fss.name+1, namelen) != 0) {
11581159
PyErr_Format(PyExc_NameError,
11591160
"Case mismatch for module name %.100s\n(filename %.300s)",
11601161
name, fss.name);
@@ -1873,7 +1874,6 @@ PyImport_Import(PyObject *module_name)
18731874
static PyObject *silly_list = NULL;
18741875
static PyObject *builtins_str = NULL;
18751876
static PyObject *import_str = NULL;
1876-
static PyObject *standard_builtins = NULL;
18771877
PyObject *globals = NULL;
18781878
PyObject *import = NULL;
18791879
PyObject *builtins = NULL;
@@ -1894,7 +1894,7 @@ PyImport_Import(PyObject *module_name)
18941894

18951895
/* Get the builtins from current globals */
18961896
globals = PyEval_GetGlobals();
1897-
if(globals != NULL) {
1897+
if (globals != NULL) {
18981898
Py_INCREF(globals);
18991899
builtins = PyObject_GetItem(globals, builtins_str);
19001900
if (builtins == NULL)
@@ -1904,16 +1904,10 @@ PyImport_Import(PyObject *module_name)
19041904
/* No globals -- use standard builtins, and fake globals */
19051905
PyErr_Clear();
19061906

1907-
if (standard_builtins == NULL) {
1908-
standard_builtins =
1909-
PyImport_ImportModuleEx("__builtin__",
1910-
NULL, NULL, NULL);
1911-
if (standard_builtins == NULL)
1912-
return NULL;
1913-
}
1914-
1915-
builtins = standard_builtins;
1916-
Py_INCREF(builtins);
1907+
builtins = PyImport_ImportModuleEx("__builtin__",
1908+
NULL, NULL, NULL);
1909+
if (builtins == NULL)
1910+
return NULL;
19171911
globals = Py_BuildValue("{OO}", builtins_str, builtins);
19181912
if (globals == NULL)
19191913
goto err;

0 commit comments

Comments
 (0)