diff Python/import.c @ 72453:125887a41a6f 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:54:40 +0200
parents 4247f5e221c6
children 608975eafe86 8c6fea5794b2
line wrap: on
line diff
--- a/Python/import.c	Fri Sep 23 01:29:44 2011 +0200
+++ b/Python/import.c	Fri Sep 23 18:54:40 2011 +0200
@@ -1763,6 +1763,7 @@
         saved_namelen = namelen;
 #endif /* PYOS_OS2 */
         for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
+            struct stat statbuf;
 #if defined(PYOS_OS2) && defined(HAVE_DYNAMIC_LOADING)
             /* OS/2 limits DLLs to 8 character names (w/o
                extension)
@@ -1791,10 +1792,16 @@
             strcpy(buf+len, fdp->suffix);
             if (Py_VerboseFlag > 1)
                 PySys_WriteStderr("# trying %s\n", buf);
+
             filemode = fdp->mode;
             if (filemode[0] == 'U')
                 filemode = "r" PY_STDIOTEXTMODE;
-            fp = fopen(buf, filemode);
+
+            if (stat(buf, &statbuf) == 0 && S_ISDIR(statbuf.st_mode))
+                /* it's a directory */
+                fp = NULL;
+            else
+                fp = fopen(buf, filemode);
             if (fp != NULL) {
                 if (case_ok(buf, len, namelen, name))
                     break;