diff options
author | Brett Cannon <bcannon@gmail.com> | 2009-07-20 04:23:48 +0000 |
---|---|---|
committer | Brett Cannon <bcannon@gmail.com> | 2009-07-20 04:23:48 +0000 |
commit | 6919427e9462d05f402faa5f846f43e08347cebe (patch) | |
tree | 862ce874c7e299b7eb5078a7a5b668e4b17b9918 /Lib/importlib/test | |
parent | 64ef00fa605463e1da84e43ea8a5d722843174b6 (diff) | |
download | cpython-6919427e9462d05f402faa5f846f43e08347cebe.tar.gz cpython-6919427e9462d05f402faa5f846f43e08347cebe.zip |
Implement the PEP 302 protocol for get_filename() as
importlib.abc.ExecutionLoader. PyLoader now inherits from this ABC instead of
InspectLoader directly. Both PyLoader and PyPycLoader provide concrete
implementations of get_filename in terms of source_path and bytecode_path.
Diffstat (limited to 'Lib/importlib/test')
-rw-r--r-- | Lib/importlib/test/source/test_abc_loader.py | 53 | ||||
-rw-r--r-- | Lib/importlib/test/test_abc.py | 8 |
2 files changed, 57 insertions, 4 deletions
diff --git a/Lib/importlib/test/source/test_abc_loader.py b/Lib/importlib/test/source/test_abc_loader.py index 6465d261b7d..8c69cfd5371 100644 --- a/Lib/importlib/test/source/test_abc_loader.py +++ b/Lib/importlib/test/source/test_abc_loader.py @@ -218,6 +218,21 @@ class PyLoaderInterfaceTests(unittest.TestCase): with util.uncache(name), self.assertRaises(ImportError): mock.load_module(name) + def test_get_filename_with_source_path(self): + # get_filename() should return what source_path() returns. + name = 'mod' + path = os.path.join('path', 'to', 'source') + mock = PyLoaderMock({name: path}) + with util.uncache(name): + self.assertEqual(mock.get_filename(name), path) + + def test_get_filename_no_source_path(self): + # get_filename() should raise ImportError if source_path returns None. + name = 'mod' + mock = PyLoaderMock({name: None}) + with util.uncache(name), self.assertRaises(ImportError): + mock.get_filename(name) + class PyLoaderGetSourceTests(unittest.TestCase): @@ -283,6 +298,38 @@ class PyPycLoaderTests(PyLoaderTests): super().test_unloadable() +class PyPycLoaderInterfaceTests(unittest.TestCase): + + """Test for the interface of importlib.abc.PyPycLoader.""" + + def get_filename_check(self, src_path, bc_path, expect): + name = 'mod' + mock = PyPycLoaderMock({name: src_path}, {name: {'path': bc_path}}) + with util.uncache(name): + assert mock.source_path(name) == src_path + assert mock.bytecode_path(name) == bc_path + self.assertEqual(mock.get_filename(name), expect) + + def test_filename_with_source_bc(self): + # When source and bytecode paths present, return the source path. + self.get_filename_check('source_path', 'bc_path', 'source_path') + + def test_filename_with_source_no_bc(self): + # With source but no bc, return source path. + self.get_filename_check('source_path', None, 'source_path') + + def test_filename_with_no_source_bc(self): + # With not source but bc, return the bc path. + self.get_filename_check(None, 'bc_path', 'bc_path') + + def test_filename_with_no_source_or_bc(self): + # With no source or bc, raise ImportError. + name = 'mod' + mock = PyPycLoaderMock({name: None}, {name: {'path': None}}) + with util.uncache(name), self.assertRaises(ImportError): + mock.get_filename(name) + + class SkipWritingBytecodeTests(unittest.TestCase): """Test that bytecode is properly handled based on @@ -421,9 +468,9 @@ class MissingPathsTests(unittest.TestCase): def test_main(): from test.support import run_unittest run_unittest(PyLoaderTests, PyLoaderInterfaceTests, PyLoaderGetSourceTests, - PyPycLoaderTests, SkipWritingBytecodeTests, - RegeneratedBytecodeTests, BadBytecodeFailureTests, - MissingPathsTests) + PyPycLoaderTests, PyPycLoaderInterfaceTests, + SkipWritingBytecodeTests, RegeneratedBytecodeTests, + BadBytecodeFailureTests, MissingPathsTests) if __name__ == '__main__': diff --git a/Lib/importlib/test/test_abc.py b/Lib/importlib/test/test_abc.py index 6e09534d8c3..5229ba4309a 100644 --- a/Lib/importlib/test/test_abc.py +++ b/Lib/importlib/test/test_abc.py @@ -53,9 +53,15 @@ class InspectLoader(InheritanceTests, unittest.TestCase): machinery.FrozenImporter] +class ExecutionLoader(InheritanceTests, unittest.TestCase): + + superclasses = [abc.InspectLoader] + subclasses = [abc.PyLoader] + + class PyLoader(InheritanceTests, unittest.TestCase): - superclasses = [abc.Loader, abc.ResourceLoader, abc.InspectLoader] + superclasses = [abc.Loader, abc.ResourceLoader, abc.ExecutionLoader] class PyPycLoader(InheritanceTests, unittest.TestCase): |