aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/test
diff options
context:
space:
mode:
authorBrett Cannon <brettcannon@users.noreply.github.com>2018-04-06 16:10:18 -0700
committerGitHub <noreply@github.com>2018-04-06 16:10:18 -0700
commit9e2be60634914f23db2ae5624e4acc9335bf5fea (patch)
tree9b1613b2c8bdad0fd942222c07a72e90e8dd509e /Lib/test
parent3a9ccee0e5dbf7d67f5ab79f6095755969db117c (diff)
downloadcpython-9e2be60634914f23db2ae5624e4acc9335bf5fea.tar.gz
cpython-9e2be60634914f23db2ae5624e4acc9335bf5fea.zip
bpo-33169: Remove values of `None` from sys.path_importer_cache when invalidating caches (GH-6402)
An entry of None in sys.path_importer_cache represents a negative/missing finder for a path, so clearing it out makes sense.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_importlib/import_/test_path.py21
-rw-r--r--Lib/test/test_importlib/test_api.py2
2 files changed, 22 insertions, 1 deletions
diff --git a/Lib/test/test_importlib/import_/test_path.py b/Lib/test/test_importlib/import_/test_path.py
index 7aa26b0eee7..18c81dda45c 100644
--- a/Lib/test/test_importlib/import_/test_path.py
+++ b/Lib/test/test_importlib/import_/test_path.py
@@ -184,6 +184,27 @@ class FinderTests:
# Do not want FileNotFoundError raised.
self.assertIsNone(self.machinery.PathFinder.find_spec('whatever'))
+ def test_invalidate_caches_finders(self):
+ # Finders with an invalidate_caches() method have it called.
+ class FakeFinder:
+ def __init__(self):
+ self.called = False
+
+ def invalidate_caches(self):
+ self.called = True
+
+ cache = {'leave_alone': object(), 'finder_to_invalidate': FakeFinder()}
+ with util.import_state(path_importer_cache=cache):
+ self.machinery.PathFinder.invalidate_caches()
+ self.assertTrue(cache['finder_to_invalidate'].called)
+
+ def test_invalidate_caches_clear_out_None(self):
+ # Clear out None in sys.path_importer_cache() when invalidating caches.
+ cache = {'clear_out': None}
+ with util.import_state(path_importer_cache=cache):
+ self.machinery.PathFinder.invalidate_caches()
+ self.assertEqual(len(cache), 0)
+
class FindModuleTests(FinderTests):
def find(self, *args, **kwargs):
diff --git a/Lib/test/test_importlib/test_api.py b/Lib/test/test_importlib/test_api.py
index 8beb4244eac..edb745c2cd4 100644
--- a/Lib/test/test_importlib/test_api.py
+++ b/Lib/test/test_importlib/test_api.py
@@ -406,7 +406,7 @@ class InvalidateCacheTests:
# There should be no issues if the method is not defined.
key = 'gobbledeegook'
sys.path_importer_cache[key] = None
- self.addCleanup(lambda: sys.path_importer_cache.__delitem__(key))
+ self.addCleanup(lambda: sys.path_importer_cache.pop(key, None))
self.init.invalidate_caches() # Shouldn't trigger an exception.