[Python-checkins] cpython (merge 3.2 -> default): Merge 3.2: Issue #7732: Don't open a directory as a file anymore while

victor.stinner python-checkins at python.org
Fri Sep 23 18:59:13 CEST 2011


http://hg.python.org/cpython/rev/8c6fea5794b2
changeset:   72454:8c6fea5794b2
parent:      72452:3b46f2e2d280
parent:      72453:125887a41a6f
user:        Victor Stinner <victor.stinner at haypocalc.com>
date:        Fri Sep 23 18:59:08 2011 +0200
summary:
  Merge 3.2: Issue #7732: Don't open a directory as a file anymore while
importing a module. Ignore the direcotry if its name matchs the module name
(e.g.  "__init__.py") and raise a ImportError instead.

files:
  Lib/test/test_import.py |  9 +++++++++
  Misc/NEWS               |  6 +++++-
  Python/import.c         |  9 +++++++++
  3 files changed, 23 insertions(+), 1 deletions(-)


diff --git a/Lib/test/test_import.py b/Lib/test/test_import.py
--- a/Lib/test/test_import.py
+++ b/Lib/test/test_import.py
@@ -139,6 +139,15 @@
             self.assertIs(orig_path, new_os.path)
             self.assertIsNot(orig_getenv, new_os.getenv)
 
+    def test_bug7732(self):
+        source = TESTFN + '.py'
+        os.mkdir(source)
+        try:
+            self.assertRaisesRegex(ImportError, '^No module',
+                imp.find_module, TESTFN, ["."])
+        finally:
+            os.rmdir(source)
+
     def test_module_with_large_stack(self, module='longlist'):
         # Regression test for http://bugs.python.org/issue561858.
         filename = module + '.py'
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,10 @@
 Core and Builtins
 -----------------
 
+- Issue #7732: Don't open a directory as a file anymore while importing a
+  module. Ignore the direcotry if its name matchs the module name (e.g.
+  "__init__.py") and raise a ImportError instead.
+
 - Issue #13021: Missing decref on an error path.  Thanks to Suman Saha for
   finding the bug and providing a patch.
 
@@ -291,7 +295,7 @@
   ZLIB_RUNTIME_VERSION, in the zlib module. Patch by Torsten Landschoff.
 
 - Issue #12959: Add collections.ChainMap to collections.__all__.
- 
+
 - Issue #8933: distutils' PKG-INFO files and packaging's METADATA files will
   now correctly report Metadata-Version: 1.1 instead of 1.0 if a Classifier or
   Download-URL field is present.
diff --git a/Python/import.c b/Python/import.c
--- a/Python/import.c
+++ b/Python/import.c
@@ -1892,6 +1892,8 @@
         }
 
         for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
+            struct stat statbuf;
+
             filemode = fdp->mode;
             if (filemode[0] == 'U')
                 filemode = "r" PY_STDIOTEXTMODE;
@@ -1905,6 +1907,13 @@
             if (Py_VerboseFlag > 1)
                 PySys_FormatStderr("# trying %R\n", filename);
 
+            if (_Py_stat(filename, &statbuf) == 0 &&         /* it exists */
+                S_ISDIR(statbuf.st_mode))           /* it's a directory */
+            {
+                Py_DECREF(filename);
+                continue;
+            }
+
             fp = _Py_fopen(filename, filemode);
             if (fp == NULL) {
                 Py_DECREF(filename);

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


More information about the Python-checkins mailing list