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

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


http://hg.python.org/cpython/rev/125887a41a6f
changeset: 72453:125887a41a6f
branch: 3.2
parent: 72451:5ceab07bcd02
user: Victor Stinner <victor.stinner at haypocalc.com>
date: Fri Sep 23 18:54:40 2011 +0200
summary:
 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, 22 insertions(+), 2 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.
 
@@ -77,7 +81,7 @@
 
 Extension Modules
 -----------------
- 
+
 - Issue #13022: Fix: _multiprocessing.recvfd() doesn't check that
 file descriptor was actually received.
 
diff --git a/Python/import.c b/Python/import.c
--- a/Python/import.c
+++ b/Python/import.c
@@ -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;
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /