Commits

Amaury Forgeot d'Arc  committed f44a6f8 Draft

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

  • Participants
  • Parent commits 9b4cc19
  • Branches stdlib-2.7.3

Comments (0)

Files changed (2)

File pypy/module/imp/importing.py

 
     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
     """
     # 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
     # 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

File pypy/module/imp/test/test_import.py

     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