diff Python/import.c @ 64838:dd3080b80dda

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.
author Brett Cannon <bcannon@gmail.com>
date Sun, 19 Sep 2010 21:39:02 +0000
parents 20a2153b0dda
children c620fe87c703
line wrap: on
line diff
--- a/Python/import.c	Sun Sep 19 13:56:11 2010 +0000
+++ b/Python/import.c	Sun Sep 19 21:39:02 2010 +0000
@@ -3044,7 +3044,7 @@
    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 @@
     PyObject *globals = NULL;
     PyObject *import = NULL;
     PyObject *builtins = NULL;
+    PyObject *modules = NULL;
     PyObject *r = NULL;
 
     /* Initialize constant string objects */
@@ -3068,7 +3069,7 @@
         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 @@
         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);