Update importlib.invalidate_caches() to be more general.

This commit is contained in:
Brett Cannon 2012-02-27 18:15:42 -05:00
parent 9a4d7ddb6c
commit b46a1793a7
5 changed files with 62 additions and 38 deletions

View file

@ -143,6 +143,13 @@ class FinderTests(abc.FinderTests):
finally:
os.unlink('mod.py')
def test_invalidate_caches(self):
# invalidate_caches() should reset the mtime.
finder = _bootstrap._FileFinder('', _bootstrap._SourceFinderDetails())
finder._path_mtime = 42
finder.invalidate_caches()
self.assertEqual(finder._path_mtime, -1)
def test_main():
from test.support import run_unittest

View file

@ -84,6 +84,34 @@ class ImportModuleTests(unittest.TestCase):
importlib.import_module('a.b')
self.assertEqual(b_load_count, 1)
class InvalidateCacheTests(unittest.TestCase):
def test_method_called(self):
# If defined the method should be called.
class InvalidatingNullFinder:
def __init__(self, *ignored):
self.called = False
def find_module(self, *args):
return None
def invalidate_caches(self):
self.called = True
key = 'gobledeegook'
ins = InvalidatingNullFinder()
sys.path_importer_cache[key] = ins
self.addCleanup(lambda: sys.path_importer_cache.__delitem__(key))
importlib.invalidate_caches()
self.assertTrue(ins.called)
def test_method_lacking(self):
# There should be no issues if the method is not defined.
key = 'gobbledeegook'
sys.path_importer_cache[key] = imp.NullImporter('abc')
self.addCleanup(lambda: sys.path_importer_cache.__delitem__(key))
importlib.invalidate_caches() # Shouldn't trigger an exception.
def test_main():
from test.support import run_unittest
run_unittest(ImportModuleTests)