Commit a1fe1f8d authored by Victor Stinner's avatar Victor Stinner

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.
parents 92c144ee 53ffdc53
...@@ -139,6 +139,15 @@ class ImportTests(unittest.TestCase): ...@@ -139,6 +139,15 @@ class ImportTests(unittest.TestCase):
self.assertIs(orig_path, new_os.path) self.assertIs(orig_path, new_os.path)
self.assertIsNot(orig_getenv, new_os.getenv) 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'): def test_module_with_large_stack(self, module='longlist'):
# Regression test for http://bugs.python.org/issue561858. # Regression test for http://bugs.python.org/issue561858.
filename = module + '.py' filename = module + '.py'
......
...@@ -10,6 +10,10 @@ What's New in Python 3.3 Alpha 1? ...@@ -10,6 +10,10 @@ What's New in Python 3.3 Alpha 1?
Core and Builtins 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 - Issue #13021: Missing decref on an error path. Thanks to Suman Saha for
finding the bug and providing a patch. finding the bug and providing a patch.
......
...@@ -1892,6 +1892,8 @@ find_module_path_list(PyObject *fullname, PyObject *name, ...@@ -1892,6 +1892,8 @@ find_module_path_list(PyObject *fullname, PyObject *name,
} }
for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) { for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
struct stat statbuf;
filemode = fdp->mode; filemode = fdp->mode;
if (filemode[0] == 'U') if (filemode[0] == 'U')
filemode = "r" PY_STDIOTEXTMODE; filemode = "r" PY_STDIOTEXTMODE;
...@@ -1905,6 +1907,13 @@ find_module_path_list(PyObject *fullname, PyObject *name, ...@@ -1905,6 +1907,13 @@ find_module_path_list(PyObject *fullname, PyObject *name,
if (Py_VerboseFlag > 1) if (Py_VerboseFlag > 1)
PySys_FormatStderr("# trying %R\n", filename); 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); fp = _Py_fopen(filename, filemode);
if (fp == NULL) { if (fp == NULL) {
Py_DECREF(filename); Py_DECREF(filename);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment