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

Skip to content

Commit 53ffdc5

Browse files
author
Victor Stinner
committed
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.
1 parent da6eb53 commit 53ffdc5

3 files changed

Lines changed: 22 additions & 2 deletions

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.2.3?
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

@@ -77,7 +81,7 @@ Tests
7781

7882
Extension Modules
7983
-----------------
80-
84+
8185
- Issue #13022: Fix: _multiprocessing.recvfd() doesn't check that
8286
file descriptor was actually received.
8387

Python/import.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1763,6 +1763,7 @@ find_module(char *fullname, char *subname, PyObject *path, char *buf,
17631763
saved_namelen = namelen;
17641764
#endif /* PYOS_OS2 */
17651765
for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
1766+
struct stat statbuf;
17661767
#if defined(PYOS_OS2) && defined(HAVE_DYNAMIC_LOADING)
17671768
/* OS/2 limits DLLs to 8 character names (w/o
17681769
extension)
@@ -1791,10 +1792,16 @@ find_module(char *fullname, char *subname, PyObject *path, char *buf,
17911792
strcpy(buf+len, fdp->suffix);
17921793
if (Py_VerboseFlag > 1)
17931794
PySys_WriteStderr("# trying %s\n", buf);
1795+
17941796
filemode = fdp->mode;
17951797
if (filemode[0] == 'U')
17961798
filemode = "r" PY_STDIOTEXTMODE;
1797-
fp = fopen(buf, filemode);
1799+
1800+
if (stat(buf, &statbuf) == 0 && S_ISDIR(statbuf.st_mode))
1801+
/* it's a directory */
1802+
fp = NULL;
1803+
else
1804+
fp = fopen(buf, filemode);
17981805
if (fp != NULL) {
17991806
if (case_ok(buf, len, namelen, name))
18001807
break;

0 commit comments

Comments
 (0)