[Python-checkins] cpython (3.3): Issue #16730: Don't raise an exception in

brett.cannon python-checkins at python.org
Fri Jan 11 21:45:13 CET 2013


http://hg.python.org/cpython/rev/159967aa24a5
changeset:   81427:159967aa24a5
branch:      3.3
parent:      81416:d3374d393975
user:        Brett Cannon <brett at python.org>
date:        Fri Jan 11 15:40:12 2013 -0500
summary:
  Issue #16730: Don't raise an exception in
importlib.machinery.FileFinder when the directory has become
unreadable or a file. This brings semantics in line with Python 3.2
import.

Reported and diagnosed by David Pritchard.

files:
  Lib/importlib/_bootstrap.py                   |     5 +-
  Lib/test/test_importlib/source/test_finder.py |    35 +
  Misc/NEWS                                     |     5 +
  Python/importlib.h                            |  1901 +++++----
  4 files changed, 995 insertions(+), 951 deletions(-)


diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -1395,8 +1395,9 @@
         path = self.path
         try:
             contents = _os.listdir(path)
-        except FileNotFoundError:
-            # Directory has been removed since last import
+        except (FileNotFoundError, PermissionError, NotADirectoryError):
+            # Directory has either been removed, turned into a file, or made
+            # unreadable.
             contents = []
         # We store two cached versions, to handle runtime changes of the
         # PYTHONCASEOK environment variable.
diff --git a/Lib/test/test_importlib/source/test_finder.py b/Lib/test/test_importlib/source/test_finder.py
--- a/Lib/test/test_importlib/source/test_finder.py
+++ b/Lib/test/test_importlib/source/test_finder.py
@@ -6,6 +6,9 @@
 import imp
 import os
 import py_compile
+import stat
+import sys
+import tempfile
 from test.support import make_legacy_pyc
 import unittest
 import warnings
@@ -147,6 +150,38 @@
             self.assertIsNotNone(finder.find_module(mod))
         self.assertIsNone(finder.find_module(mod))
 
+    @unittest.skipUnless(sys.platform != 'win32',
+            'os.chmod() does not support the needed arguments under Windows')
+    def test_no_read_directory(self):
+        # Issue #16730
+        tempdir = tempfile.TemporaryDirectory()
+        original_mode = os.stat(tempdir.name).st_mode
+        def cleanup(tempdir):
+            """Cleanup function for the temporary directory.
+
+            Since we muck with the permissions, we want to set them back to
+            their original values to make sure the directory can be properly
+            cleaned up.
+
+            """
+            os.chmod(tempdir.name, original_mode)
+            # If this is not explicitly called then the __del__ method is used,
+            # but since already mucking around might as well explicitly clean
+            # up.
+            tempdir.__exit__(None, None, None)
+        self.addCleanup(cleanup, tempdir)
+        os.chmod(tempdir.name, stat.S_IWUSR | stat.S_IXUSR)
+        finder = self.get_finder(tempdir.name)
+        self.assertEqual((None, []), finder.find_loader('doesnotexist'))
+
+    def test_ignore_file(self):
+        # If a directory got changed to a file from underneath us, then don't
+        # worry about looking for submodules.
+        with tempfile.NamedTemporaryFile() as file_obj:
+            finder = self.get_finder(file_obj.name)
+            self.assertEqual((None, []), finder.find_loader('doesnotexist'))
+
+
 def test_main():
     from test.support import run_unittest
     run_unittest(FinderTests)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,11 @@
 Core and Builtins
 -----------------
 
+- Issue #16730: importlib.machinery.FileFinder now no longers raises an
+  exception when trying to populate its cache and it finds out the directory is
+  unreadable or has turned into a file. Reported and diagnosed by
+  David Pritchard.
+
 - Issue #16906: Fix a logic error that prevented most static strings from being
   cleared.
 
diff --git a/Python/importlib.h b/Python/importlib.h
--- a/Python/importlib.h
+++ b/Python/importlib.h
[stripped]

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list