summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrett Cannon <bcannon@gmail.com>2010-09-19 21:39:02 (GMT)
committerBrett Cannon <bcannon@gmail.com>2010-09-19 21:39:02 (GMT)
commitbc2eff31128e9d6b2e0c5ce788acf1633bcc0ccb (patch)
tree50bef6ee339b02fca37d59524373b7a28003a98a
parente1f2f30328cd4fb0cdd77dea3f53b722d5a9a38d (diff)
downloadcpython-bc2eff31128e9d6b2e0c5ce788acf1633bcc0ccb.zip
cpython-bc2eff31128e9d6b2e0c5ce788acf1633bcc0ccb.tar.gz
cpython-bc2eff31128e9d6b2e0c5ce788acf1633bcc0ccb.tar.bz2
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.
-rw-r--r--Misc/NEWS3
-rw-r--r--Python/import.c16
2 files changed, 16 insertions, 3 deletions
diff --git a/Misc/NEWS b/Misc/NEWS
index ab01f47..40c4cdd 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.2 Alpha 3?
Core and Builtins
-----------------
+- Issue #9252: PyImport_Import no longer uses a fromlist hack to return the
+ module that was imported, but instead gets the module from sys.modules.
+
- Issue #9212: The range type_items now provides index() and count()
methods, to conform to the Sequence ABC. Patch by Daniel Urban and
Daniel Stutzbach.
diff --git a/Python/import.c b/Python/import.c
index b8cfbee..3078734 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -3044,7 +3044,7 @@ PyImport_ReloadModule(PyObject *m)
more accurately -- it invokes the __import__() function from the
builtins of the current globals. This means that the import is
done using whatever import hooks are installed in the current
- environment, e.g. by "rexec".
+ environment.
A dummy list ["__doc__"] is passed as the 4th argument so that
e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache"))
will return <module "gencache"> instead of <module "win32com">. */
@@ -3058,6 +3058,7 @@ PyImport_Import(PyObject *module_name)
PyObject *globals = NULL;
PyObject *import = NULL;
PyObject *builtins = NULL;
+ PyObject *modules = NULL;
PyObject *r = NULL;
/* Initialize constant string objects */
@@ -3068,7 +3069,7 @@ PyImport_Import(PyObject *module_name)
builtins_str = PyUnicode_InternFromString("__builtins__");
if (builtins_str == NULL)
return NULL;
- silly_list = Py_BuildValue("[s]", "__doc__");
+ silly_list = PyList_New(0);
if (silly_list == NULL)
return NULL;
}
@@ -3104,9 +3105,18 @@ PyImport_Import(PyObject *module_name)
goto err;
/* Call the __import__ function with the proper argument list
- * Always use absolute import here. */
+ Always use absolute import here.
+ Calling for side-effect of import. */
r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
globals, silly_list, 0, NULL);
+ if (r == NULL)
+ goto err;
+ Py_DECREF(r);
+
+ modules = PyImport_GetModuleDict();
+ r = PyDict_GetItem(modules, module_name);
+ if (r != NULL)
+ Py_INCREF(r);
err:
Py_XDECREF(globals);