diff options
author | Brett Cannon <brett@python.org> | 2012-02-27 23:15:42 (GMT) |
---|---|---|
committer | Brett Cannon <brett@python.org> | 2012-02-27 23:15:42 (GMT) |
commit | b46a1793a787747d59e735e12471b3a309aa51da (patch) | |
tree | 61eacabf46212791ef3fa96f44c95c9593c8607d /Lib/importlib/test | |
parent | 9a4d7ddb6c09af03953840ff8a2c1215fc6742a7 (diff) | |
download | cpython-b46a1793a787747d59e735e12471b3a309aa51da.zip cpython-b46a1793a787747d59e735e12471b3a309aa51da.tar.gz cpython-b46a1793a787747d59e735e12471b3a309aa51da.tar.bz2 |
Update importlib.invalidate_caches() to be more general.
Diffstat (limited to 'Lib/importlib/test')
-rw-r--r-- | Lib/importlib/test/source/test_finder.py | 7 | ||||
-rw-r--r-- | Lib/importlib/test/test_api.py | 28 |
2 files changed, 35 insertions, 0 deletions
diff --git a/Lib/importlib/test/source/test_finder.py b/Lib/importlib/test/source/test_finder.py index 7b9088d..68e9ae7 100644 --- a/Lib/importlib/test/source/test_finder.py +++ b/Lib/importlib/test/source/test_finder.py @@ -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 diff --git a/Lib/importlib/test/test_api.py b/Lib/importlib/test/test_api.py index a151626..cc147c2 100644 --- a/Lib/importlib/test/test_api.py +++ b/Lib/importlib/test/test_api.py @@ -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) |