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

Skip to content

Commit bc2eff3

Browse files
committed
PyImport_Import was using the old import hack of sticking a dummy value into
fromlist to get __import__ to return the module desired. Now it uses the proper approach of fetching the module from sys.modules. Closes issue #9252. Thanks to Alexander Belopolsky for the bug report.
1 parent e1f2f30 commit bc2eff3

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.2 Alpha 3?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #9252: PyImport_Import no longer uses a fromlist hack to return the
14+
module that was imported, but instead gets the module from sys.modules.
15+
1316
- Issue #9212: The range type_items now provides index() and count()
1417
methods, to conform to the Sequence ABC. Patch by Daniel Urban and
1518
Daniel Stutzbach.

Python/import.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3044,7 +3044,7 @@ PyImport_ReloadModule(PyObject *m)
30443044
more accurately -- it invokes the __import__() function from the
30453045
builtins of the current globals. This means that the import is
30463046
done using whatever import hooks are installed in the current
3047-
environment, e.g. by "rexec".
3047+
environment.
30483048
A dummy list ["__doc__"] is passed as the 4th argument so that
30493049
e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache"))
30503050
will return <module "gencache"> instead of <module "win32com">. */
@@ -3058,6 +3058,7 @@ PyImport_Import(PyObject *module_name)
30583058
PyObject *globals = NULL;
30593059
PyObject *import = NULL;
30603060
PyObject *builtins = NULL;
3061+
PyObject *modules = NULL;
30613062
PyObject *r = NULL;
30623063

30633064
/* Initialize constant string objects */
@@ -3068,7 +3069,7 @@ PyImport_Import(PyObject *module_name)
30683069
builtins_str = PyUnicode_InternFromString("__builtins__");
30693070
if (builtins_str == NULL)
30703071
return NULL;
3071-
silly_list = Py_BuildValue("[s]", "__doc__");
3072+
silly_list = PyList_New(0);
30723073
if (silly_list == NULL)
30733074
return NULL;
30743075
}
@@ -3104,9 +3105,18 @@ PyImport_Import(PyObject *module_name)
31043105
goto err;
31053106

31063107
/* Call the __import__ function with the proper argument list
3107-
* Always use absolute import here. */
3108+
Always use absolute import here.
3109+
Calling for side-effect of import. */
31083110
r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
31093111
globals, silly_list, 0, NULL);
3112+
if (r == NULL)
3113+
goto err;
3114+
Py_DECREF(r);
3115+
3116+
modules = PyImport_GetModuleDict();
3117+
r = PyDict_GetItem(modules, module_name);
3118+
if (r != NULL)
3119+
Py_INCREF(r);
31103120

31113121
err:
31123122
Py_XDECREF(globals);

0 commit comments

Comments
 (0)