Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a1fe1f8

Browse files
author
Victor Stinner
committed
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.
2 parents 92c144e + 53ffdc5 commit a1fe1f8

3 files changed

Lines changed: 23 additions & 1 deletion

File tree

Lib/test/test_import.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,15 @@ def test_imp_module(self):
139139
self.assertIs(orig_path, new_os.path)
140140
self.assertIsNot(orig_getenv, new_os.getenv)
141141

142+
def test_bug7732(self):
143+
source = TESTFN + '.py'
144+
os.mkdir(source)
145+
try:
146+
self.assertRaisesRegex(ImportError, '^No module',
147+
imp.find_module, TESTFN, ["."])
148+
finally:
149+
os.rmdir(source)
150+
142151
def test_module_with_large_stack(self, module='longlist'):
143152
# Regression test for http://bugs.python.org/issue561858.
144153
filename = module + '.py'

Misc/NEWS

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ What's New in Python 3.3 Alpha 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #7732: Don't open a directory as a file anymore while importing a
14+
module. Ignore the direcotry if its name matchs the module name (e.g.
15+
"__init__.py") and raise a ImportError instead.
16+
1317
- Issue #13021: Missing decref on an error path. Thanks to Suman Saha for
1418
finding the bug and providing a patch.
1519

@@ -291,7 +295,7 @@ Library
291295
ZLIB_RUNTIME_VERSION, in the zlib module. Patch by Torsten Landschoff.
292296

293297
- Issue #12959: Add collections.ChainMap to collections.__all__.
294-
298+
295299
- Issue #8933: distutils' PKG-INFO files and packaging's METADATA files will
296300
now correctly report Metadata-Version: 1.1 instead of 1.0 if a Classifier or
297301
Download-URL field is present.

Python/import.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1892,6 +1892,8 @@ find_module_path_list(PyObject *fullname, PyObject *name,
18921892
}
18931893

18941894
for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
1895+
struct stat statbuf;
1896+
18951897
filemode = fdp->mode;
18961898
if (filemode[0] == 'U')
18971899
filemode = "r" PY_STDIOTEXTMODE;
@@ -1905,6 +1907,13 @@ find_module_path_list(PyObject *fullname, PyObject *name,
19051907
if (Py_VerboseFlag > 1)
19061908
PySys_FormatStderr("# trying %R\n", filename);
19071909

1910+
if (_Py_stat(filename, &statbuf) == 0 && /* it exists */
1911+
S_ISDIR(statbuf.st_mode)) /* it's a directory */
1912+
{
1913+
Py_DECREF(filename);
1914+
continue;
1915+
}
1916+
19081917
fp = _Py_fopen(filename, filemode);
19091918
if (fp == NULL) {
19101919
Py_DECREF(filename);

0 commit comments

Comments
 (0)