[Python-checkins] r59876 - in python/trunk: Lib/test/test_import.py Misc/NEWS Python/import.c

christian.heimes python-checkins at python.org
Wed Jan 9 20:56:33 CET 2008


Author: christian.heimes
Date: Wed Jan  9 20:56:33 2008
New Revision: 59876

Modified:
   python/trunk/Lib/test/test_import.py
   python/trunk/Misc/NEWS
   python/trunk/Python/import.c
Log:
Fixed #1776. __import__() no longer imports modules by file name

Modified: python/trunk/Lib/test/test_import.py
==============================================================================
--- python/trunk/Lib/test/test_import.py	(original)
+++ python/trunk/Lib/test/test_import.py	Wed Jan  9 20:56:33 2008
@@ -1,4 +1,4 @@
-from test.test_support import TESTFN, run_unittest, catch_warning
+from test.test_support import TESTFN, run_unittest, catch_warning
 
 import unittest
 import os
@@ -223,6 +223,16 @@
             warnings.simplefilter('error', ImportWarning)
             self.assertRaises(ImportWarning, __import__, "site-packages")
 
+    def test_importbyfilename(self):
+        path = os.path.abspath(TESTFN)
+        try:
+            __import__(path)
+        except ImportError, err:
+            self.assertEqual("Import by filename is not supported.",
+                              err.args[0])
+        else:
+            self.fail("import by path didn't raise an exception")
+
 class PathsTests(unittest.TestCase):
     path = TESTFN
 

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Wed Jan  9 20:56:33 2008
@@ -12,6 +12,10 @@
 Core and builtins
 -----------------
 
+- Bug #1776: __import__ must not accept filenames. Python 2.6 does no longer
+  support module loading by filename. It worked on some system by coincident
+  but it was never intended to work.
+
 - Patch #1668: renamed THREADDEBUG envvar to PYTHONTHREADDEBUG.
 
 - Patch #602345: Add -B command line option, PYTHONDONTWRITEBYTECODE envvar

Modified: python/trunk/Python/import.c
==============================================================================
--- python/trunk/Python/import.c	(original)
+++ python/trunk/Python/import.c	Wed Jan  9 20:56:33 2008
@@ -2055,6 +2055,16 @@
 	Py_ssize_t buflen = 0;
 	PyObject *parent, *head, *next, *tail;
 
+	if (strchr(name, '/') != NULL
+#ifdef MS_WINDOWS
+	    || strchr(name, '\\') != NULL
+#endif
+		) {
+		PyErr_SetString(PyExc_ImportError,
+				"Import by filename is not supported.");
+		return NULL;
+	}
+
 	parent = get_parent(globals, buf, &buflen, level);
 	if (parent == NULL)
 		return NULL;


More information about the Python-checkins mailing list