cpython: 543c76769c14 (original) (raw)
Mercurial > cpython
changeset 87794:543c76769c14
Issue #19712: Update test.test_importlib.import_ to test/use PEP 451 where appropriate. [#19712]
line wrap: on
line diff
--- a/Lib/test/test_importlib/import_/test___package__.py +++ b/Lib/test/test_importlib/import_/test___package__.py @@ -36,7 +36,7 @@ class Using__package__: def test_using___package__(self): # [package]
with util.mock_modules('pkg.__init__', 'pkg.fake') as importer:[](#l1.7)
with self.mock_modules('pkg.__init__', 'pkg.fake') as importer:[](#l1.8) with util.import_state(meta_path=[importer]):[](#l1.9) self.__import__('pkg.fake')[](#l1.10) module = self.__import__('',[](#l1.11)
@@ -49,7 +49,7 @@ class Using__package__: globals_ = {'name': 'pkg.fake', 'path': []} if package_as_None: globals_['package'] = None
with util.mock_modules('pkg.__init__', 'pkg.fake') as importer:[](#l1.16)
with self.mock_modules('pkg.__init__', 'pkg.fake') as importer:[](#l1.17) with util.import_state(meta_path=[importer]):[](#l1.18) self.__import__('pkg.fake')[](#l1.19) module = self.__import__('', globals= globals_,[](#l1.20)
@@ -70,11 +70,20 @@ class Using__package__: with self.assertRaises(TypeError): self.import('', globals, {}, ['relimport'], 1) -Frozen_UsingPackage, Source_UsingPackage = util.test_both(
Using__package__, __import__=import_util.__import__)[](#l1.26)
+class Using__package__PEP302(Using__package__):
+ +Frozen_UsingPackagePEP302, Source_UsingPackagePEP302 = util.test_both(
Using__package__PEP302, __import__=import_util.__import__)[](#l1.31)
+ +class Using__package__PEP302(Using__package__):
+ +Frozen_UsingPackagePEP451, Source_UsingPackagePEP451 = util.test_both(
Using__package__PEP302, __import__=import_util.__import__)[](#l1.37)
-class Setting__package__(unittest.TestCase): +class Setting__package__: """Because package is a new feature, it is not always set by a loader. Import will set it as needed to help with the transition to relying on @@ -90,7 +99,7 @@ class Setting__package__(unittest.TestCa # [top-level] def test_top_level(self):
with util.mock_modules('top_level') as mock:[](#l1.49)
with self.mock_modules('top_level') as mock:[](#l1.50) with util.import_state(meta_path=[mock]):[](#l1.51) del mock['top_level'].__package__[](#l1.52) module = self.__import__('top_level')[](#l1.53)
@@ -98,7 +107,7 @@ class Setting__package__(unittest.TestCa # [package] def test_package(self):
with util.mock_modules('pkg.__init__') as mock:[](#l1.58)
with self.mock_modules('pkg.__init__') as mock:[](#l1.59) with util.import_state(meta_path=[mock]):[](#l1.60) del mock['pkg'].__package__[](#l1.61) module = self.__import__('pkg')[](#l1.62)
@@ -106,13 +115,19 @@ class Setting__package__(unittest.TestCa # [submodule] def test_submodule(self):
with util.mock_modules('pkg.__init__', 'pkg.mod') as mock:[](#l1.67)
with self.mock_modules('pkg.__init__', 'pkg.mod') as mock:[](#l1.68) with util.import_state(meta_path=[mock]):[](#l1.69) del mock['pkg.mod'].__package__[](#l1.70) pkg = self.__import__('pkg.mod')[](#l1.71) module = getattr(pkg, 'mod')[](#l1.72) self.assertEqual(module.__package__, 'pkg')[](#l1.73)
+class Setting__package__PEP302(Setting__package__, unittest.TestCase):
+ +class Setting__package__PEP451(Setting__package__, unittest.TestCase):
+ if name == 'main': unittest.main()
--- a/Lib/test/test_importlib/import_/test_caching.py +++ b/Lib/test/test_importlib/import_/test_caching.py @@ -39,6 +39,16 @@ class UseCache: self.import(name) self.assertEqual(cm.exception.name, name) +Frozen_UseCache, Source_UseCache = util.test_both(
UseCache, __import__=import_util.__import__)[](#l2.8)
+ + +class ImportlibUseCache(UseCache, unittest.TestCase): +
+ def create_mock(self, *names, return_=None): mock = util.mock_modules(*names) original_load = mock.load_module @@ -48,14 +58,6 @@ class UseCache: mock.load_module = MethodType(load_module, mock) return mock -Frozen_UseCache, Source_UseCache = util.test_both(
UseCache, __import__=import_util.__import__)[](#l2.25)
- - -class ImportlibUseCache(UseCache, unittest.TestCase): -
- # import inconsistent between loaders and built-in import when it comes # to when to use the module in sys.modules and when not to. def test_using_cache_after_loader(self):
--- a/Lib/test/test_importlib/import_/test_fromlist.py +++ b/Lib/test/test_importlib/import_/test_fromlist.py @@ -17,7 +17,7 @@ class ReturnValue: def test_return_from_import(self): # [import return]
with util.mock_modules('pkg.__init__', 'pkg.module') as importer:[](#l3.7)
with util.mock_spec('pkg.__init__', 'pkg.module') as importer:[](#l3.8) with util.import_state(meta_path=[importer]):[](#l3.9) module = self.__import__('pkg.module')[](#l3.10) self.assertEqual(module.__name__, 'pkg')[](#l3.11)
--- a/Lib/test/test_importlib/import_/test_meta_path.py +++ b/Lib/test/test_importlib/import_/test_meta_path.py @@ -18,23 +18,18 @@ class CallingOrder: def test_first_called(self): # [first called] mod = 'top_level'
first = util.mock_modules(mod)[](#l4.7)
second = util.mock_modules(mod)[](#l4.8)
with util.mock_modules(mod) as first, util.mock_modules(mod) as second:[](#l4.9)
first.modules[mod] = 42[](#l4.10)
second.modules[mod] = -13[](#l4.11)
with util.mock_spec(mod) as first, util.mock_spec(mod) as second:[](#l4.12) with util.import_state(meta_path=[first, second]):[](#l4.13)
self.assertEqual(self.__import__(mod), 42)[](#l4.14)
self.assertIs(self.__import__(mod), first.modules[mod])[](#l4.15)
def test_continuing(self): # [continuing] mod_name = 'for_real'
with util.mock_modules('nonexistent') as first, \[](#l4.20)
util.mock_modules(mod_name) as second:[](#l4.21)
first.find_module = lambda self, fullname, path=None: None[](#l4.22)
second.modules[mod_name] = 42[](#l4.23)
with util.mock_spec('nonexistent') as first, \[](#l4.24)
util.mock_spec(mod_name) as second:[](#l4.25)
first.find_spec = lambda self, fullname, path=None, parent=None: None[](#l4.26) with util.import_state(meta_path=[first, second]):[](#l4.27)
self.assertEqual(self.__import__(mod_name), 42)[](#l4.28)
self.assertIs(self.__import__(mod_name), second.modules[mod_name])[](#l4.29)
def test_empty(self): # Raise an ImportWarning if sys.meta_path is empty. @@ -61,29 +56,27 @@ class CallSignature: [no path]. Otherwise, the value for path is passed in for the 'path' argument [path set]."""
- def log_finder(self, importer):
fxn = getattr(importer, self.finder_name)[](#l4.39) log = [][](#l4.40) def wrapper(self, *args, **kwargs):[](#l4.41) log.append([args, kwargs])[](#l4.42) return fxn(*args, **kwargs)[](#l4.43) return log, wrapper[](#l4.44)
- def test_no_path(self): # [no path] mod_name = 'top_level' assert '.' not in mod_name
with util.mock_modules(mod_name) as importer:[](#l4.51)
log, wrapped_call = self.log(importer.find_module)[](#l4.52)
importer.find_module = MethodType(wrapped_call, importer)[](#l4.53)
with self.mock_modules(mod_name) as importer:[](#l4.54)
log, wrapped_call = self.log_finder(importer)[](#l4.55)
setattr(importer, self.finder_name, MethodType(wrapped_call, importer))[](#l4.56) with util.import_state(meta_path=[importer]):[](#l4.57) self.__import__(mod_name)[](#l4.58) assert len(log) == 1[](#l4.59) args = log[0][0][](#l4.60) kwargs = log[0][1][](#l4.61) # Assuming all arguments are positional.[](#l4.62)
self.assertEqual(len(args), 2)[](#l4.63)
self.assertEqual(len(kwargs), 0)[](#l4.64) self.assertEqual(args[0], mod_name)[](#l4.65) self.assertIsNone(args[1])[](#l4.66)
@@ -93,10 +86,10 @@ class CallSignature: mod_name = pkg_name + '.module' path = [42] assert '.' in mod_name
with util.mock_modules(pkg_name+'.__init__', mod_name) as importer:[](#l4.72)
with self.mock_modules(pkg_name+'.__init__', mod_name) as importer:[](#l4.73) importer.modules[pkg_name].__path__ = path[](#l4.74)
log, wrapped_call = self.log(importer.find_module)[](#l4.75)
importer.find_module = MethodType(wrapped_call, importer)[](#l4.76)
log, wrapped_call = self.log_finder(importer)[](#l4.77)
setattr(importer, self.finder_name, MethodType(wrapped_call, importer))[](#l4.78) with util.import_state(meta_path=[importer]):[](#l4.79) self.__import__(mod_name)[](#l4.80) assert len(log) == 2[](#l4.81)
@@ -107,8 +100,19 @@ class CallSignature: self.assertEqual(args[0], mod_name) self.assertIs(args[1], path) -Frozen_CallSignature, Source_CallSignature = util.test_both(
CallSignature, __import__=import_util.__import__)[](#l4.87)
+class CallSignaturePEP302(CallSignature):
+ +Frozen_CallSignaturePEP302, Source_CallSignaturePEP302 = util.test_both(
CallSignaturePEP302, __import__=import_util.__import__)[](#l4.93)
+ +class CallSignaturePEP451(CallSignature):
+ +Frozen_CallSignaturePEP451, Source_CallSignaturePEP451 = util.test_both(
CallSignaturePEP451, __import__=import_util.__import__)[](#l4.100)
--- a/Lib/test/test_importlib/import_/test_packages.py +++ b/Lib/test/test_importlib/import_/test_packages.py @@ -11,13 +11,13 @@ class ParentModuleTests: """Importing a submodule should import the parent modules.""" def test_import_parent(self):
with util.mock_modules('pkg.__init__', 'pkg.module') as mock:[](#l5.7)
with util.mock_spec('pkg.__init__', 'pkg.module') as mock:[](#l5.8) with util.import_state(meta_path=[mock]):[](#l5.9) module = self.__import__('pkg.module')[](#l5.10) self.assertIn('pkg', sys.modules)[](#l5.11)
with util.mock_modules('pkg.module') as mock:[](#l5.14)
with util.mock_spec('pkg.module') as mock:[](#l5.15) with util.import_state(meta_path=[mock]):[](#l5.16) with self.assertRaises(ImportError) as cm:[](#l5.17) self.__import__('pkg.module')[](#l5.18)
@@ -27,7 +27,7 @@ class ParentModuleTests: def init(): import pkg.module 1/0
mock = util.mock_modules('pkg.__init__', 'pkg.module',[](#l5.23)
mock = util.mock_spec('pkg.__init__', 'pkg.module',[](#l5.24) module_code={'pkg': __init__})[](#l5.25) with mock:[](#l5.26) with util.import_state(meta_path=[mock]):[](#l5.27)
@@ -44,7 +44,7 @@ class ParentModuleTests: def init(): from . import module 1/0
mock = util.mock_modules('pkg.__init__', 'pkg.module',[](#l5.32)
mock = util.mock_spec('pkg.__init__', 'pkg.module',[](#l5.33) module_code={'pkg': __init__})[](#l5.34) with mock:[](#l5.35) with util.import_state(meta_path=[mock]):[](#l5.36)
@@ -63,7 +63,7 @@ class ParentModuleTests: def init(): from ..subpkg import module 1/0
mock = util.mock_modules('pkg.__init__', 'pkg.subpkg.__init__',[](#l5.41)
mock = util.mock_spec('pkg.__init__', 'pkg.subpkg.__init__',[](#l5.42) 'pkg.subpkg.module',[](#l5.43) module_code={'pkg.subpkg': __init__})[](#l5.44) with mock:[](#l5.45)
@@ -93,9 +93,9 @@ class ParentModuleTests: subname = name + '.b' def module_injection(): sys.modules[subname] = 'total bunk'
mock_modules = util.mock_modules('mod',[](#l5.50)
mock_spec = util.mock_spec('mod',[](#l5.51) module_code={'mod': module_injection})[](#l5.52)
with mock_modules as mock:[](#l5.53)
with mock_spec as mock:[](#l5.54) with util.import_state(meta_path=[mock]):[](#l5.55) try:[](#l5.56) submodule = self.__import__(subname)[](#l5.57)
--- a/Lib/test/test_importlib/import_/test_path.py +++ b/Lib/test/test_importlib/import_/test_path.py @@ -27,7 +27,7 @@ class FinderTests: # Implicitly tests that sys.path_importer_cache is used. module = '' path = ''
importer = util.mock_modules(module)[](#l6.7)
importer = util.mock_spec(module)[](#l6.8) with util.import_state(path_importer_cache={path: importer},[](#l6.9) path=[path]):[](#l6.10) loader = self.machinery.PathFinder.find_module(module)[](#l6.11)
@@ -38,7 +38,7 @@ class FinderTests: # Implicitly tests that sys.path_importer_cache is used. module = '' path = ''
importer = util.mock_modules(module)[](#l6.16)
importer = util.mock_spec(module)[](#l6.17) with util.import_state(path_importer_cache={path: importer}):[](#l6.18) loader = self.machinery.PathFinder.find_module(module, [path])[](#l6.19) self.assertIs(loader, importer)[](#l6.20)
@@ -47,7 +47,7 @@ class FinderTests: # An empty list should not count as asking for sys.path. module = 'module' path = ''
importer = util.mock_modules(module)[](#l6.25)
importer = util.mock_spec(module)[](#l6.26) with util.import_state(path_importer_cache={path: importer},[](#l6.27) path=[path]):[](#l6.28) self.assertIsNone(self.machinery.PathFinder.find_module('module', []))[](#l6.29)
@@ -57,7 +57,7 @@ class FinderTests: # Test that sys.path_importer_cache is set. module = '' path = ''
importer = util.mock_modules(module)[](#l6.34)
importer = util.mock_spec(module)[](#l6.35) hook = import_util.mock_path_hook(path, importer=importer)[](#l6.36) with util.import_state(path_hooks=[hook]):[](#l6.37) loader = self.machinery.PathFinder.find_module(module, [path])[](#l6.38)
@@ -82,7 +82,7 @@ class FinderTests: # The empty string should create a finder using the cwd. path = '' module = ''
importer = util.mock_modules(module)[](#l6.43)
importer = util.mock_spec(module)[](#l6.44) hook = import_util.mock_path_hook(os.getcwd(), importer=importer)[](#l6.45) with util.import_state(path=[path], path_hooks=[hook]):[](#l6.46) loader = self.machinery.PathFinder.find_module(module)[](#l6.47)
--- a/Lib/test/test_importlib/import_/test_relative_imports.py +++ b/Lib/test/test_importlib/import_/test_relative_imports.py @@ -64,7 +64,7 @@ class RelativeImports: uncache_names.append(name) else: uncache_names.append(name[:-len('.init')])
with util.mock_modules(*create) as importer:[](#l7.7)
with util.mock_spec(*create) as importer:[](#l7.8) with util.import_state(meta_path=[importer]):[](#l7.9) for global_ in globals_:[](#l7.10) with util.uncache(*uncache_names):[](#l7.11)
--- a/Lib/test/test_importlib/util.py +++ b/Lib/test/test_importlib/util.py @@ -1,4 +1,5 @@ from contextlib import contextmanager +from importlib import util import os.path from test import support import unittest @@ -101,9 +102,9 @@ def import_state(**kwargs): setattr(sys, attr, value) -class mock_modules: +class _ImporterMock:
def init(self, *names, module_code={}): self.modules = {} @@ -133,6 +134,19 @@ class mock_modules: def getitem(self, name): return self.modules[name]
- def enter(self):
self._uncache = uncache(*self.modules.keys())[](#l8.26)
self._uncache.__enter__()[](#l8.27)
return self[](#l8.28)
+ + +class mock_modules(_ImporterMock): +
+ def find_module(self, fullname, path=None): if fullname not in self.modules: return None @@ -152,10 +166,28 @@ class mock_modules: raise return self.modules[fullname]
- def enter(self):
self._uncache = uncache(*self.modules.keys())[](#l8.46)
self._uncache.__enter__()[](#l8.47)
return self[](#l8.48)
+class mock_spec(_ImporterMock): +
- def find_spec(self, fullname, path=None, parent=None):
try:[](#l8.56)
module = self.modules[fullname][](#l8.57)
except KeyError:[](#l8.58)
return None[](#l8.59)
is_package = hasattr(module, '__path__')[](#l8.60)
spec = util.spec_from_file_location([](#l8.61)
fullname, module.__file__, loader=self,[](#l8.62)
submodule_search_locations=getattr(module, '__path__', None))[](#l8.63)
return spec[](#l8.64)