[Python-checkins] r43638 - in python/trunk: Lib/test/test_builtin.py Python/import.c

thomas.wouters python-checkins at python.org
Tue Apr 4 18:17:02 CEST 2006


Author: thomas.wouters
Date: Tue Apr  4 18:17:02 2006
New Revision: 43638

Modified:
   python/trunk/Lib/test/test_builtin.py
   python/trunk/Python/import.c
Log:

Fix __import__("") to raise ValueError rather than return None.



Modified: python/trunk/Lib/test/test_builtin.py
==============================================================================
--- python/trunk/Lib/test/test_builtin.py	(original)
+++ python/trunk/Lib/test/test_builtin.py	Tue Apr  4 18:17:02 2006
@@ -108,6 +108,7 @@
         __import__('string')
         self.assertRaises(ImportError, __import__, 'spamspam')
         self.assertRaises(TypeError, __import__, 1, 2, 3, 4)
+        self.assertRaises(ValueError, __import__, '')
 
     def test_abs(self):
         # int

Modified: python/trunk/Python/import.c
==============================================================================
--- python/trunk/Python/import.c	(original)
+++ python/trunk/Python/import.c	Tue Apr  4 18:17:02 2006
@@ -1934,6 +1934,14 @@
 		}
 		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 @@
 	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;


More information about the Python-checkins mailing list