]> granicus.if.org Git - python/commitdiff
Fix __import__("") to raise ValueError rather than return None.
authorThomas Wouters <thomas@python.org>
Tue, 4 Apr 2006 16:17:02 +0000 (16:17 +0000)
committerThomas Wouters <thomas@python.org>
Tue, 4 Apr 2006 16:17:02 +0000 (16:17 +0000)
Lib/test/test_builtin.py
Python/import.c

index fa36095735bb99b5948475bc395784decf826703..27f659db86ca572b077fbe50c3122b2329a5df89 100644 (file)
@@ -108,6 +108,7 @@ class BuiltinTest(unittest.TestCase):
         __import__('string')
         self.assertRaises(ImportError, __import__, 'spamspam')
         self.assertRaises(TypeError, __import__, 1, 2, 3, 4)
+        self.assertRaises(ValueError, __import__, '')
 
     def test_abs(self):
         # int
index 9d63891a9f8207be38e99745e23cff16adc16207..6a47d957245e8a6801cb01806d163cda0283b5f8 100644 (file)
@@ -1934,6 +1934,14 @@ import_module_level(char *name, PyObject *globals, PyObject *locals,
                }
                tail = next;
        }
+       if (tail == Py_None) {
+               /* If tail is Py_None, both get_parent and load_next found
+                  an empty module name: someone called __import__("") or
+                  doctored faulty bytecode */
+               PyErr_SetString(PyExc_ValueError,
+                               "Empty module name");
+               return NULL;
+       }
 
        if (fromlist != NULL) {
                if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
@@ -2094,7 +2102,8 @@ load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
        PyObject *result;
 
        if (strlen(name) == 0) {
-               /* empty module name only happens in 'from . import' */
+               /* completely empty module name should only happen in
+                  'from . import' (or '__import__("")')*/
                Py_INCREF(mod);
                *p_name = NULL;
                return mod;