Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 48fec05

Browse files
committed
Close #14846: Handle a sys.path entry going away
1 parent db7920b commit 48fec05

4 files changed

Lines changed: 953 additions & 933 deletions

File tree

Lib/importlib/_bootstrap.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1367,7 +1367,11 @@ def find_loader(self, fullname):
13671367
def _fill_cache(self):
13681368
"""Fill the cache of potential modules and packages for this directory."""
13691369
path = self.path
1370-
contents = _os.listdir(path)
1370+
try:
1371+
contents = _os.listdir(path)
1372+
except FileNotFoundError:
1373+
# Directory has been removed since last import
1374+
contents = []
13711375
# We store two cached versions, to handle runtime changes of the
13721376
# PYTHONCASEOK environment variable.
13731377
if not sys.platform.startswith('win'):

Lib/test/test_importlib/source/test_finder.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@ class FinderTests(abc.FinderTests):
3535
3636
"""
3737

38-
def import_(self, root, module):
38+
def get_finder(self, root):
3939
loader_details = [(machinery.SourceFileLoader,
4040
machinery.SOURCE_SUFFIXES),
4141
(machinery.SourcelessFileLoader,
4242
machinery.BYTECODE_SUFFIXES)]
43-
finder = machinery.FileFinder(root, *loader_details)
44-
return finder.find_module(module)
43+
return machinery.FileFinder(root, *loader_details)
44+
45+
def import_(self, root, module):
46+
return self.get_finder(root).find_module(module)
4547

4648
def run_test(self, test, create=None, *, compile_=None, unlink=None):
4749
"""Test the finding of 'test' with the creation of modules listed in
@@ -137,6 +139,13 @@ def test_invalidate_caches(self):
137139
finder.invalidate_caches()
138140
self.assertEqual(finder._path_mtime, -1)
139141

142+
# Regression test for http://bugs.python.org/issue14846
143+
def test_dir_removal_handling(self):
144+
mod = 'mod'
145+
with source_util.create_modules(mod) as mapping:
146+
finder = self.get_finder(mapping['.root'])
147+
self.assertIsNotNone(finder.find_module(mod))
148+
self.assertIsNone(finder.find_module(mod))
140149

141150
def test_main():
142151
from test.support import run_unittest

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Core and Builtins
1313
- Issue #15604: Update uses of PyObject_IsTrue() to check for and handle
1414
errors correctly. Patch by Serhiy Storchaka.
1515

16+
- Issue #14846: importlib.FileFinder now handles the case where the
17+
directory being searched is removed after a previous import attempt
18+
1619
Library
1720
-------
1821

0 commit comments

Comments
 (0)