diff Python/import.c @ 72454:8c6fea5794b2

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.
author Victor Stinner <victor.stinner@haypocalc.com>
date Fri, 23 Sep 2011 18:59:08 +0200
parents 03843a8d3244 125887a41a6f
children 8beaa9a37387
line wrap: on
line diff
--- a/Python/import.c	Fri Sep 23 01:31:04 2011 +0200
+++ b/Python/import.c	Fri Sep 23 18:59:08 2011 +0200
@@ -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);