summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2012-08-20 03:18:15 (GMT)
committerNick Coghlan <ncoghlan@gmail.com>2012-08-20 03:18:15 (GMT)
commit48fec0539157db38a993ededb32a8f312ec09fb9 (patch)
tree4cea5afa658f50a6f4c5c46458241d02794bdd91 /Lib
parentdb7920b97874bf3988984fae57f4ca80fced558f (diff)
downloadcpython-48fec0539157db38a993ededb32a8f312ec09fb9.zip
cpython-48fec0539157db38a993ededb32a8f312ec09fb9.tar.gz
cpython-48fec0539157db38a993ededb32a8f312ec09fb9.tar.bz2
Close #14846: Handle a sys.path entry going away
Diffstat (limited to 'Lib')
-rw-r--r--Lib/importlib/_bootstrap.py6
-rw-r--r--Lib/test/test_importlib/source/test_finder.py15
2 files changed, 17 insertions, 4 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
index 471b380..4c1f2f9 100644
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -1367,7 +1367,11 @@ class FileFinder:
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
index 40905eb..8bc16a5 100644
--- a/Lib/test/test_importlib/source/test_finder.py
+++ b/Lib/test/test_importlib/source/test_finder.py
@@ -35,13 +35,15 @@ class FinderTests(abc.FinderTests):
"""
- 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 @@ class FinderTests(abc.FinderTests):
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