[pypy-commit] pypy stdlib-2.7.3: 'import foo' should not try to open a *directory* named foo.py...

amauryfa noreply at buildbot.pypy.org
Sat Jul 7 23:01:42 CEST 2012


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: stdlib-2.7.3
Changeset: r55982:f44a6f8d0d5e
Date: 2012-07-07 22:45 +0200
http://bitbucket.org/pypy/pypy/changeset/f44a6f8d0d5e/

Log:	'import foo' should not try to open a *directory* named foo.py...

diff --git a/pypy/module/imp/importing.py b/pypy/module/imp/importing.py
--- a/pypy/module/imp/importing.py
+++ b/pypy/module/imp/importing.py
@@ -49,6 +49,10 @@
 
     return '.' + soabi + SO
 
+def file_exists(path):
+    """Tests whether the given path is an existing regular file."""
+    return os.path.isfile(path) and case_ok(path)
+
 def find_modtype(space, filepart):
     """Check which kind of module to import for the given filepart,
     which is a path without extension.  Returns PY_SOURCE, PY_COMPILED or
@@ -56,13 +60,13 @@
     """
     # check the .py file
     pyfile = filepart + ".py"
-    if os.path.exists(pyfile) and case_ok(pyfile):
+    if file_exists(pyfile):
         return PY_SOURCE, ".py", "U"
 
     # on Windows, also check for a .pyw file
     if CHECK_FOR_PYW:
         pyfile = filepart + ".pyw"
-        if os.path.exists(pyfile) and case_ok(pyfile):
+        if file_exists(pyfile):
             return PY_SOURCE, ".pyw", "U"
 
     # The .py file does not exist.  By default on PyPy, lonepycfiles
@@ -73,14 +77,14 @@
     # check the .pyc file
     if space.config.objspace.usepycfiles and space.config.objspace.lonepycfiles:
         pycfile = filepart + ".pyc"
-        if os.path.exists(pycfile) and case_ok(pycfile):
+        if file_exists(pycfile):
             # existing .pyc file
             return PY_COMPILED, ".pyc", "rb"
 
     if space.config.objspace.usemodules.cpyext:
         so_extension = get_so_extension(space)
         pydfile = filepart + so_extension
-        if os.path.exists(pydfile) and case_ok(pydfile):
+        if file_exists(pydfile):
             return C_EXTENSION, so_extension, "rb"
 
     return SEARCH_ERROR, None, None
diff --git a/pypy/module/imp/test/test_import.py b/pypy/module/imp/test/test_import.py
--- a/pypy/module/imp/test/test_import.py
+++ b/pypy/module/imp/test/test_import.py
@@ -370,6 +370,15 @@
     def test__import__empty_string(self):
         raises(ValueError, __import__, "")
 
+    def test_py_directory(self):
+        import imp, os, sys
+        source = os.path.join(sys.path[0], 'foo.py')
+        os.mkdir(source)
+        try:
+            raises(ImportError, imp.find_module, 'foo')
+        finally:
+            os.rmdir(source)
+
     def test_invalid__name__(self):
         glob = {}
         exec "__name__ = None; import sys" in glob


More information about the pypy-commit mailing list