[Python-checkins] cpython: Issue #3080: imp.load_module() accepts None for the module path

victor.stinner python-checkins at python.org
Sun Mar 20 22:38:24 CET 2011


http://hg.python.org/cpython/rev/7f4a4e393058
changeset:   68768:7f4a4e393058
user:        Victor Stinner <victor.stinner at haypocalc.com>
date:        Sun Mar 20 22:37:17 2011 +0100
summary:
  Issue #3080: imp.load_module() accepts None for the module path

imp.find_module() returns None as module path for builtin and frozen builtins.

files:
  Lib/test/test_importhooks.py
  Python/import.c

diff --git a/Lib/test/test_importhooks.py b/Lib/test/test_importhooks.py
--- a/Lib/test/test_importhooks.py
+++ b/Lib/test/test_importhooks.py
@@ -229,7 +229,9 @@
         i = ImpWrapper()
         sys.meta_path.append(i)
         sys.path_hooks.append(ImpWrapper)
-        mnames = ("colorsys", "urllib.parse", "distutils.core")
+        mnames = (
+            "colorsys", "urllib.parse", "distutils.core", "sys",
+        )
         for mname in mnames:
             parent = mname.split(".")[0]
             for n in list(sys.modules):
@@ -237,7 +239,8 @@
                     del sys.modules[n]
         for mname in mnames:
             m = __import__(mname, globals(), locals(), ["__dummy__"])
-            m.__loader__  # to make sure we actually handled the import
+            # to make sure we actually handled the import
+            self.assertTrue(hasattr(m, "__loader__"))
 
 
 def test_main():
diff --git a/Python/import.c b/Python/import.c
--- a/Python/import.c
+++ b/Python/import.c
@@ -3744,17 +3744,22 @@
 static PyObject *
 imp_load_module(PyObject *self, PyObject *args)
 {
-    PyObject *name, *fob, *pathname, *ret;
+    PyObject *name, *fob, *pathname, *pathname_obj, *ret;
     char *suffix; /* Unused */
     char *mode;
     int type;
     FILE *fp;
 
-    if (!PyArg_ParseTuple(args, "UOO&(ssi):load_module",
-                          &name, &fob,
-                          PyUnicode_FSDecoder, &pathname,
-                          &suffix, &mode, &type))
+    if (!PyArg_ParseTuple(args, "UOO(ssi):load_module",
+                          &name, &fob, &pathname_obj, &suffix, &mode, &type))
         return NULL;
+    if (pathname_obj != Py_None) {
+        if (!PyUnicode_FSDecoder(pathname_obj, &pathname))
+            return NULL;
+    }
+    else
+        pathname = NULL;
+
     if (*mode) {
         /* Mode must start with 'r' or 'U' and must not contain '+'.
            Implicit in this test is the assumption that the mode
@@ -3763,7 +3768,7 @@
         if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
             PyErr_Format(PyExc_ValueError,
                          "invalid file open mode %.200s", mode);
-            Py_DECREF(pathname);
+            Py_XDECREF(pathname);
             return NULL;
         }
     }
@@ -3772,12 +3777,12 @@
     else {
         fp = get_file(NULL, fob, mode);
         if (fp == NULL) {
-            Py_DECREF(pathname);
+            Py_XDECREF(pathname);
             return NULL;
         }
     }
     ret = load_module(name, fp, pathname, type, NULL);
-    Py_DECREF(pathname);
+    Py_XDECREF(pathname);
     if (fp)
         fclose(fp);
     return ret;

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list