[Python-checkins] cpython: Close #14846: Handle a sys.path entry going away

nick.coghlan python-checkins at python.org
Mon Aug 20 05:18:38 CEST 2012


http://hg.python.org/cpython/rev/bfd04bfb55c5
changeset:   78663:bfd04bfb55c5
user:        Nick Coghlan <ncoghlan at gmail.com>
date:        Mon Aug 20 13:18:15 2012 +1000
summary:
  Close #14846: Handle a sys.path entry going away

files:
  Lib/importlib/_bootstrap.py                   |     6 +-
  Lib/test/test_importlib/source/test_finder.py |    15 +-
  Misc/NEWS                                     |     3 +
  Python/importlib.h                            |  1866 +++++----
  4 files changed, 955 insertions(+), 935 deletions(-)


diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -1367,7 +1367,11 @@
     def _fill_cache(self):
         """Fill the cache of potential modules and packages for this directory."""
         path = self.path
-        contents = _os.listdir(path)
+        try:
+            contents = _os.listdir(path)
+        except FileNotFoundError:
+            # Directory has been removed since last import
+            contents = []
         # We store two cached versions, to handle runtime changes of the
         # PYTHONCASEOK environment variable.
         if not sys.platform.startswith('win'):
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
@@ -35,13 +35,15 @@
 
     """
 
-    def import_(self, root, module):
+    def get_finder(self, root):
         loader_details = [(machinery.SourceFileLoader,
                             machinery.SOURCE_SUFFIXES),
                           (machinery.SourcelessFileLoader,
                             machinery.BYTECODE_SUFFIXES)]
-        finder = machinery.FileFinder(root, *loader_details)
-        return finder.find_module(module)
+        return machinery.FileFinder(root, *loader_details)
+
+    def import_(self, root, module):
+        return self.get_finder(root).find_module(module)
 
     def run_test(self, test, create=None, *, compile_=None, unlink=None):
         """Test the finding of 'test' with the creation of modules listed in
@@ -137,6 +139,13 @@
         finder.invalidate_caches()
         self.assertEqual(finder._path_mtime, -1)
 
+    # Regression test for http://bugs.python.org/issue14846
+    def test_dir_removal_handling(self):
+        mod = 'mod'
+        with source_util.create_modules(mod) as mapping:
+            finder = self.get_finder(mapping['.root'])
+            self.assertIsNotNone(finder.find_module(mod))
+        self.assertIsNone(finder.find_module(mod))
 
 def test_main():
     from test.support import run_unittest
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -13,6 +13,9 @@
 - Issue #15604: Update uses of PyObject_IsTrue() to check for and handle
   errors correctly.  Patch by Serhiy Storchaka.
 
+- Issue #14846: importlib.FileFinder now handles the case where the
+  directory being searched is removed after a previous import attempt
+
 Library
 -------
 
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