diff options
author | Brett Cannon <brett@python.org> | 2013-11-29 21:17:05 (GMT) |
---|---|---|
committer | Brett Cannon <brett@python.org> | 2013-11-29 21:17:05 (GMT) |
commit | ed05b8a1a1faeee605943f2b25ebea0df79bda53 (patch) | |
tree | 63a234ab842972415e759ca93215846fd4bcbf07 /Lib/test/test_importlib | |
parent | a951f3d1acb4e12236eb78a1961143ea689d51e6 (diff) | |
download | cpython-ed05b8a1a1faeee605943f2b25ebea0df79bda53.zip cpython-ed05b8a1a1faeee605943f2b25ebea0df79bda53.tar.gz cpython-ed05b8a1a1faeee605943f2b25ebea0df79bda53.tar.bz2 |
Issue #19712: Port test.test_importlib.import_ tests to use PEP 451
that don't require changing test.test_importlib.util.mock_modules().
Diffstat (limited to 'Lib/test/test_importlib')
-rw-r--r-- | Lib/test/test_importlib/import_/test___loader__.py | 22 | ||||
-rw-r--r-- | Lib/test/test_importlib/import_/test_api.py | 60 | ||||
-rw-r--r-- | Lib/test/test_importlib/import_/test_path.py | 2 |
3 files changed, 68 insertions, 16 deletions
diff --git a/Lib/test/test_importlib/import_/test___loader__.py b/Lib/test/test_importlib/import_/test___loader__.py index 9c18d19..6df8010 100644 --- a/Lib/test/test_importlib/import_/test___loader__.py +++ b/Lib/test/test_importlib/import_/test___loader__.py @@ -1,3 +1,4 @@ +from importlib import machinery import sys import types import unittest @@ -6,6 +7,27 @@ from .. import util from . import util as import_util +class SpecLoaderMock: + + def find_spec(self, fullname, path=None, target=None): + return machinery.ModuleSpec(fullname, self) + + def exec_module(self, module): + pass + + +class SpecLoaderAttributeTests: + + def test___loader__(self): + loader = SpecLoaderMock() + with util.uncache('blah'), util.import_state(meta_path=[loader]): + module = self.__import__('blah') + self.assertEqual(loader, module.__loader__) + +Frozen_SpecTests, Source_SpecTests = util.test_both( + SpecLoaderAttributeTests, __import__=import_util.__import__) + + class LoaderMock: def find_module(self, fullname, path=None): diff --git a/Lib/test/test_importlib/import_/test_api.py b/Lib/test/test_importlib/import_/test_api.py index dc8b8a8..439c105 100644 --- a/Lib/test/test_importlib/import_/test_api.py +++ b/Lib/test/test_importlib/import_/test_api.py @@ -1,19 +1,37 @@ from .. import util from . import util as import_util + +from importlib import machinery import sys import types import unittest +PKG_NAME = 'fine' +SUBMOD_NAME = 'fine.bogus' + + +class BadSpecFinderLoader: + @classmethod + def find_spec(cls, fullname, path=None, target=None): + if fullname == SUBMOD_NAME: + spec = machinery.ModuleSpec(fullname, cls) + return spec + + @staticmethod + def exec_module(module): + if module.__name__ == SUBMOD_NAME: + raise ImportError('I cannot be loaded!') + class BadLoaderFinder: - bad = 'fine.bogus' @classmethod def find_module(cls, fullname, path): - if fullname == cls.bad: + if fullname == SUBMOD_NAME: return cls + @classmethod def load_module(cls, fullname): - if fullname == cls.bad: + if fullname == SUBMOD_NAME: raise ImportError('I cannot be loaded!') @@ -37,27 +55,39 @@ class APITest: def test_nonexistent_fromlist_entry(self): # If something in fromlist doesn't exist, that's okay. # issue15715 - mod = types.ModuleType('fine') + mod = types.ModuleType(PKG_NAME) mod.__path__ = ['XXX'] - with util.import_state(meta_path=[BadLoaderFinder]): - with util.uncache('fine'): - sys.modules['fine'] = mod - self.__import__('fine', fromlist=['not here']) + with util.import_state(meta_path=[self.bad_finder_loader]): + with util.uncache(PKG_NAME): + sys.modules[PKG_NAME] = mod + self.__import__(PKG_NAME, fromlist=['not here']) def test_fromlist_load_error_propagates(self): # If something in fromlist triggers an exception not related to not # existing, let that exception propagate. # issue15316 - mod = types.ModuleType('fine') + mod = types.ModuleType(PKG_NAME) mod.__path__ = ['XXX'] - with util.import_state(meta_path=[BadLoaderFinder]): - with util.uncache('fine'): - sys.modules['fine'] = mod + with util.import_state(meta_path=[self.bad_finder_loader]): + with util.uncache(PKG_NAME): + sys.modules[PKG_NAME] = mod with self.assertRaises(ImportError): - self.__import__('fine', fromlist=['bogus']) + self.__import__(PKG_NAME, + fromlist=[SUBMOD_NAME.rpartition('.')[-1]]) + + +class OldAPITests(APITest): + bad_finder_loader = BadLoaderFinder + +Frozen_OldAPITests, Source_OldAPITests = util.test_both( + OldAPITests, __import__=import_util.__import__) + + +class SpecAPITests(APITest): + bad_finder_loader = BadSpecFinderLoader -Frozen_APITests, Source_APITests = util.test_both( - APITest, __import__=import_util.__import__) +Frozen_SpecAPITests, Source_SpecAPITests = util.test_both( + SpecAPITests, __import__=import_util.__import__) if __name__ == '__main__': diff --git a/Lib/test/test_importlib/import_/test_path.py b/Lib/test/test_importlib/import_/test_path.py index 14fdaa3..2e5033e 100644 --- a/Lib/test/test_importlib/import_/test_path.py +++ b/Lib/test/test_importlib/import_/test_path.py @@ -17,7 +17,7 @@ class FinderTests: """Tests for PathFinder.""" def test_failure(self): - # Test None returned upon not finding a suitable finder. + # Test None returned upon not finding a suitable loader. module = '<test module>' with util.import_state(): self.assertIsNone(self.machinery.PathFinder.find_module(module)) |