Commit 3497c0bf authored by Eric Snow's avatar Eric Snow

Issue #21503: Use test_both() consistently in test_importlib.

parent d50cee20
from .. import abc from .. import abc
from .. import util from .. import util
frozen_machinery, source_machinery = util.import_importlib('importlib.machinery') machinery = util.import_importlib('importlib.machinery')
import sys import sys
import unittest import unittest
...@@ -44,8 +44,10 @@ class FindSpecTests(abc.FinderTests): ...@@ -44,8 +44,10 @@ class FindSpecTests(abc.FinderTests):
['pkg']) ['pkg'])
self.assertIsNone(spec) self.assertIsNone(spec)
Frozen_FindSpecTests, Source_FindSpecTests = util.test_both(FindSpecTests,
machinery=[frozen_machinery, source_machinery]) (Frozen_FindSpecTests,
Source_FindSpecTests
) = util.test_both(FindSpecTests, machinery=machinery)
@unittest.skipIf(util.BUILTINS.good_name is None, 'no reasonable builtin module') @unittest.skipIf(util.BUILTINS.good_name is None, 'no reasonable builtin module')
...@@ -78,8 +80,10 @@ class FinderTests(abc.FinderTests): ...@@ -78,8 +80,10 @@ class FinderTests(abc.FinderTests):
['pkg']) ['pkg'])
self.assertIsNone(loader) self.assertIsNone(loader)
Frozen_FinderTests, Source_FinderTests = util.test_both(FinderTests,
machinery=[frozen_machinery, source_machinery]) (Frozen_FinderTests,
Source_FinderTests
) = util.test_both(FinderTests, machinery=machinery)
if __name__ == '__main__': if __name__ == '__main__':
......
from .. import abc from .. import abc
from .. import util from .. import util
frozen_machinery, source_machinery = util.import_importlib('importlib.machinery') machinery = util.import_importlib('importlib.machinery')
import sys import sys
import types import types
...@@ -65,8 +65,9 @@ class LoaderTests(abc.LoaderTests): ...@@ -65,8 +65,9 @@ class LoaderTests(abc.LoaderTests):
self.assertEqual(cm.exception.name, module_name) self.assertEqual(cm.exception.name, module_name)
Frozen_LoaderTests, Source_LoaderTests = util.test_both(LoaderTests, (Frozen_LoaderTests,
machinery=[frozen_machinery, source_machinery]) Source_LoaderTests
) = util.test_both(LoaderTests, machinery=machinery)
@unittest.skipIf(util.BUILTINS.good_name is None, 'no reasonable builtin module') @unittest.skipIf(util.BUILTINS.good_name is None, 'no reasonable builtin module')
...@@ -98,9 +99,10 @@ class InspectLoaderTests: ...@@ -98,9 +99,10 @@ class InspectLoaderTests:
method(util.BUILTINS.bad_name) method(util.BUILTINS.bad_name)
self.assertRaises(util.BUILTINS.bad_name) self.assertRaises(util.BUILTINS.bad_name)
Frozen_InspectLoaderTests, Source_InspectLoaderTests = util.test_both(
InspectLoaderTests, (Frozen_InspectLoaderTests,
machinery=[frozen_machinery, source_machinery]) Source_InspectLoaderTests
) = util.test_both(InspectLoaderTests, machinery=machinery)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -5,7 +5,7 @@ import unittest ...@@ -5,7 +5,7 @@ import unittest
from .. import util from .. import util
frozen_machinery, source_machinery = util.import_importlib('importlib.machinery') machinery = util.import_importlib('importlib.machinery')
# XXX find_spec tests # XXX find_spec tests
...@@ -41,9 +41,10 @@ class ExtensionModuleCaseSensitivityTest: ...@@ -41,9 +41,10 @@ class ExtensionModuleCaseSensitivityTest:
loader = self.find_module() loader = self.find_module()
self.assertTrue(hasattr(loader, 'load_module')) self.assertTrue(hasattr(loader, 'load_module'))
Frozen_ExtensionCaseSensitivity, Source_ExtensionCaseSensitivity = util.test_both(
ExtensionModuleCaseSensitivityTest, (Frozen_ExtensionCaseSensitivity,
machinery=[frozen_machinery, source_machinery]) Source_ExtensionCaseSensitivity
) = util.test_both(ExtensionModuleCaseSensitivityTest, machinery=machinery)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -35,8 +35,10 @@ class FinderTests(abc.FinderTests): ...@@ -35,8 +35,10 @@ class FinderTests(abc.FinderTests):
def test_failure(self): def test_failure(self):
self.assertIsNone(self.find_module('asdfjkl;')) self.assertIsNone(self.find_module('asdfjkl;'))
Frozen_FinderTests, Source_FinderTests = util.test_both(
FinderTests, machinery=machinery) (Frozen_FinderTests,
Source_FinderTests
) = util.test_both(FinderTests, machinery=machinery)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -76,8 +76,9 @@ class LoaderTests(abc.LoaderTests): ...@@ -76,8 +76,9 @@ class LoaderTests(abc.LoaderTests):
loader = self.machinery.ExtensionFileLoader('pkg', path) loader = self.machinery.ExtensionFileLoader('pkg', path)
self.assertTrue(loader.is_package('pkg')) self.assertTrue(loader.is_package('pkg'))
Frozen_LoaderTests, Source_LoaderTests = util.test_both( (Frozen_LoaderTests,
LoaderTests, machinery=machinery) Source_LoaderTests
) = util.test_both(LoaderTests, machinery=machinery)
......
...@@ -23,8 +23,10 @@ class PathHookTests: ...@@ -23,8 +23,10 @@ class PathHookTests:
# exists. # exists.
self.assertTrue(hasattr(self.hook(util.EXTENSIONS.path), 'find_module')) self.assertTrue(hasattr(self.hook(util.EXTENSIONS.path), 'find_module'))
Frozen_PathHooksTests, Source_PathHooksTests = util.test_both(
PathHookTests, machinery=machinery) (Frozen_PathHooksTests,
Source_PathHooksTests
) = util.test_both(PathHookTests, machinery=machinery)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -37,8 +37,10 @@ class FindSpecTests(abc.FinderTests): ...@@ -37,8 +37,10 @@ class FindSpecTests(abc.FinderTests):
spec = self.find('<not real>') spec = self.find('<not real>')
self.assertIsNone(spec) self.assertIsNone(spec)
Frozen_FindSpecTests, Source_FindSpecTests = util.test_both(FindSpecTests,
machinery=machinery) (Frozen_FindSpecTests,
Source_FindSpecTests
) = util.test_both(FindSpecTests, machinery=machinery)
class FinderTests(abc.FinderTests): class FinderTests(abc.FinderTests):
...@@ -72,8 +74,10 @@ class FinderTests(abc.FinderTests): ...@@ -72,8 +74,10 @@ class FinderTests(abc.FinderTests):
loader = self.find('<not real>') loader = self.find('<not real>')
self.assertIsNone(loader) self.assertIsNone(loader)
Frozen_FinderTests, Source_FinderTests = util.test_both(FinderTests,
machinery=machinery) (Frozen_FinderTests,
Source_FinderTests
) = util.test_both(FinderTests, machinery=machinery)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -85,8 +85,10 @@ class ExecModuleTests(abc.LoaderTests): ...@@ -85,8 +85,10 @@ class ExecModuleTests(abc.LoaderTests):
self.exec_module('_not_real') self.exec_module('_not_real')
self.assertEqual(cm.exception.name, '_not_real') self.assertEqual(cm.exception.name, '_not_real')
Frozen_ExecModuleTests, Source_ExecModuleTests = util.test_both(ExecModuleTests,
machinery=machinery) (Frozen_ExecModuleTests,
Source_ExecModuleTests
) = util.test_both(ExecModuleTests, machinery=machinery)
class LoaderTests(abc.LoaderTests): class LoaderTests(abc.LoaderTests):
...@@ -175,8 +177,10 @@ class LoaderTests(abc.LoaderTests): ...@@ -175,8 +177,10 @@ class LoaderTests(abc.LoaderTests):
self.machinery.FrozenImporter.load_module('_not_real') self.machinery.FrozenImporter.load_module('_not_real')
self.assertEqual(cm.exception.name, '_not_real') self.assertEqual(cm.exception.name, '_not_real')
Frozen_LoaderTests, Source_LoaderTests = util.test_both(LoaderTests,
machinery=machinery) (Frozen_LoaderTests,
Source_LoaderTests
) = util.test_both(LoaderTests, machinery=machinery)
class InspectLoaderTests: class InspectLoaderTests:
...@@ -214,8 +218,9 @@ class InspectLoaderTests: ...@@ -214,8 +218,9 @@ class InspectLoaderTests:
method('importlib') method('importlib')
self.assertEqual(cm.exception.name, 'importlib') self.assertEqual(cm.exception.name, 'importlib')
Frozen_ILTests, Source_ILTests = util.test_both(InspectLoaderTests, (Frozen_ILTests,
machinery=machinery) Source_ILTests
) = util.test_both(InspectLoaderTests, machinery=machinery)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -23,8 +23,10 @@ class SpecLoaderAttributeTests: ...@@ -23,8 +23,10 @@ class SpecLoaderAttributeTests:
module = self.__import__('blah') module = self.__import__('blah')
self.assertEqual(loader, module.__loader__) self.assertEqual(loader, module.__loader__)
Frozen_SpecTests, Source_SpecTests = util.test_both(
SpecLoaderAttributeTests, __import__=util.__import__) (Frozen_SpecTests,
Source_SpecTests
) = util.test_both(SpecLoaderAttributeTests, __import__=util.__import__)
class LoaderMock: class LoaderMock:
...@@ -61,8 +63,9 @@ class LoaderAttributeTests: ...@@ -61,8 +63,9 @@ class LoaderAttributeTests:
self.assertEqual(loader, module.__loader__) self.assertEqual(loader, module.__loader__)
Frozen_Tests, Source_Tests = util.test_both(LoaderAttributeTests, (Frozen_Tests,
__import__=util.__import__) Source_Tests
) = util.test_both(LoaderAttributeTests, __import__=util.__import__)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -69,17 +69,23 @@ class Using__package__: ...@@ -69,17 +69,23 @@ class Using__package__:
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
self.__import__('', globals, {}, ['relimport'], 1) self.__import__('', globals, {}, ['relimport'], 1)
class Using__package__PEP302(Using__package__): class Using__package__PEP302(Using__package__):
mock_modules = util.mock_modules mock_modules = util.mock_modules
Frozen_UsingPackagePEP302, Source_UsingPackagePEP302 = util.test_both(
Using__package__PEP302, __import__=util.__import__) (Frozen_UsingPackagePEP302,
Source_UsingPackagePEP302
) = util.test_both(Using__package__PEP302, __import__=util.__import__)
class Using__package__PEP451(Using__package__): class Using__package__PEP451(Using__package__):
mock_modules = util.mock_spec mock_modules = util.mock_spec
Frozen_UsingPackagePEP451, Source_UsingPackagePEP451 = util.test_both(
Using__package__PEP451, __import__=util.__import__) (Frozen_UsingPackagePEP451,
Source_UsingPackagePEP451
) = util.test_both(Using__package__PEP451, __import__=util.__import__)
class Setting__package__: class Setting__package__:
...@@ -94,7 +100,7 @@ class Setting__package__: ...@@ -94,7 +100,7 @@ class Setting__package__:
""" """
__import__ = util.__import__[1] __import__ = util.__import__['Source']
# [top-level] # [top-level]
def test_top_level(self): def test_top_level(self):
......
...@@ -78,15 +78,19 @@ class APITest: ...@@ -78,15 +78,19 @@ class APITest:
class OldAPITests(APITest): class OldAPITests(APITest):
bad_finder_loader = BadLoaderFinder bad_finder_loader = BadLoaderFinder
Frozen_OldAPITests, Source_OldAPITests = util.test_both(
OldAPITests, __import__=util.__import__) (Frozen_OldAPITests,
Source_OldAPITests
) = util.test_both(OldAPITests, __import__=util.__import__)
class SpecAPITests(APITest): class SpecAPITests(APITest):
bad_finder_loader = BadSpecFinderLoader bad_finder_loader = BadSpecFinderLoader
Frozen_SpecAPITests, Source_SpecAPITests = util.test_both(
SpecAPITests, __import__=util.__import__) (Frozen_SpecAPITests,
Source_SpecAPITests
) = util.test_both(SpecAPITests, __import__=util.__import__)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -38,15 +38,17 @@ class UseCache: ...@@ -38,15 +38,17 @@ class UseCache:
self.__import__(name) self.__import__(name)
self.assertEqual(cm.exception.name, name) self.assertEqual(cm.exception.name, name)
Frozen_UseCache, Source_UseCache = util.test_both(
UseCache, __import__=util.__import__) (Frozen_UseCache,
Source_UseCache
) = util.test_both(UseCache, __import__=util.__import__)
class ImportlibUseCache(UseCache, unittest.TestCase): class ImportlibUseCache(UseCache, unittest.TestCase):
# Pertinent only to PEP 302; exec_module() doesn't return a module. # Pertinent only to PEP 302; exec_module() doesn't return a module.
__import__ = util.__import__[1] __import__ = util.__import__['Source']
def create_mock(self, *names, return_=None): def create_mock(self, *names, return_=None):
mock = util.mock_modules(*names) mock = util.mock_modules(*names)
......
...@@ -28,8 +28,10 @@ class ReturnValue: ...@@ -28,8 +28,10 @@ class ReturnValue:
module = self.__import__('pkg.module', fromlist=['attr']) module = self.__import__('pkg.module', fromlist=['attr'])
self.assertEqual(module.__name__, 'pkg.module') self.assertEqual(module.__name__, 'pkg.module')
Frozen_ReturnValue, Source_ReturnValue = util.test_both(
ReturnValue, __import__=util.__import__) (Frozen_ReturnValue,
Source_ReturnValue
) = util.test_both(ReturnValue, __import__=util.__import__)
class HandlingFromlist: class HandlingFromlist:
...@@ -120,8 +122,10 @@ class HandlingFromlist: ...@@ -120,8 +122,10 @@ class HandlingFromlist:
self.assertEqual(module.module1.__name__, 'pkg.module1') self.assertEqual(module.module1.__name__, 'pkg.module1')
self.assertEqual(module.module2.__name__, 'pkg.module2') self.assertEqual(module.module2.__name__, 'pkg.module2')
Frozen_FromList, Source_FromList = util.test_both(
HandlingFromlist, __import__=util.__import__) (Frozen_FromList,
Source_FromList
) = util.test_both(HandlingFromlist, __import__=util.__import__)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -45,8 +45,10 @@ class CallingOrder: ...@@ -45,8 +45,10 @@ class CallingOrder:
self.assertEqual(len(w), 1) self.assertEqual(len(w), 1)
self.assertTrue(issubclass(w[-1].category, ImportWarning)) self.assertTrue(issubclass(w[-1].category, ImportWarning))
Frozen_CallingOrder, Source_CallingOrder = util.test_both(
CallingOrder, __import__=util.__import__) (Frozen_CallingOrder,
Source_CallingOrder
) = util.test_both(CallingOrder, __import__=util.__import__)
class CallSignature: class CallSignature:
...@@ -99,19 +101,25 @@ class CallSignature: ...@@ -99,19 +101,25 @@ class CallSignature:
self.assertEqual(args[0], mod_name) self.assertEqual(args[0], mod_name)
self.assertIs(args[1], path) self.assertIs(args[1], path)
class CallSignaturePEP302(CallSignature): class CallSignaturePEP302(CallSignature):
mock_modules = util.mock_modules mock_modules = util.mock_modules
finder_name = 'find_module' finder_name = 'find_module'
Frozen_CallSignaturePEP302, Source_CallSignaturePEP302 = util.test_both(
CallSignaturePEP302, __import__=util.__import__) (Frozen_CallSignaturePEP302,
Source_CallSignaturePEP302
) = util.test_both(CallSignaturePEP302, __import__=util.__import__)
class CallSignaturePEP451(CallSignature): class CallSignaturePEP451(CallSignature):
mock_modules = util.mock_spec mock_modules = util.mock_spec
finder_name = 'find_spec' finder_name = 'find_spec'
Frozen_CallSignaturePEP451, Source_CallSignaturePEP451 = util.test_both(
CallSignaturePEP451, __import__=util.__import__) (Frozen_CallSignaturePEP451,
Source_CallSignaturePEP451
) = util.test_both(CallSignaturePEP451, __import__=util.__import__)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -101,8 +101,10 @@ class ParentModuleTests: ...@@ -101,8 +101,10 @@ class ParentModuleTests:
finally: finally:
support.unload(subname) support.unload(subname)
Frozen_ParentTests, Source_ParentTests = util.test_both(
ParentModuleTests, __import__=util.__import__) (Frozen_ParentTests,
Source_ParentTests
) = util.test_both(ParentModuleTests, __import__=util.__import__)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -158,8 +158,10 @@ class FinderTests: ...@@ -158,8 +158,10 @@ class FinderTests:
got = self.machinery.PathFinder.find_spec('whatever', [path]) got = self.machinery.PathFinder.find_spec('whatever', [path])
self.assertEqual(got, success_finder.spec) self.assertEqual(got, success_finder.spec)
Frozen_FinderTests, Source_FinderTests = util.test_both(
FinderTests, importlib=importlib, machinery=machinery) (Frozen_FinderTests,
Source_FinderTests
) = util.test_both(FinderTests, importlib=importlib, machinery=machinery)
class PathEntryFinderTests: class PathEntryFinderTests:
...@@ -182,8 +184,10 @@ class PathEntryFinderTests: ...@@ -182,8 +184,10 @@ class PathEntryFinderTests:
path_hooks=[Finder]): path_hooks=[Finder]):
self.machinery.PathFinder.find_spec('importlib') self.machinery.PathFinder.find_spec('importlib')
Frozen_PEFTests, Source_PEFTests = util.test_both(
PathEntryFinderTests, machinery=machinery) (Frozen_PEFTests,
Source_PEFTests
) = util.test_both(PathEntryFinderTests, machinery=machinery)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -207,8 +207,10 @@ class RelativeImports: ...@@ -207,8 +207,10 @@ class RelativeImports:
with self.assertRaises(KeyError): with self.assertRaises(KeyError):
self.__import__('sys', level=1) self.__import__('sys', level=1)
Frozen_RelativeImports, Source_RelativeImports = util.test_both(
RelativeImports, __import__=util.__import__) (Frozen_RelativeImports,
Source_RelativeImports
) = util.test_both(RelativeImports, __import__=util.__import__)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -62,20 +62,28 @@ class CaseSensitivityTest: ...@@ -62,20 +62,28 @@ class CaseSensitivityTest:
self.assertIsNotNone(insensitive) self.assertIsNotNone(insensitive)
self.assertIn(self.name, insensitive.get_filename(self.name)) self.assertIn(self.name, insensitive.get_filename(self.name))
class CaseSensitivityTestPEP302(CaseSensitivityTest): class CaseSensitivityTestPEP302(CaseSensitivityTest):
def find(self, finder): def find(self, finder):
return finder.find_module(self.name) return finder.find_module(self.name)
Frozen_CaseSensitivityTestPEP302, Source_CaseSensitivityTestPEP302 = util.test_both(
CaseSensitivityTestPEP302, importlib=importlib, machinery=machinery) (Frozen_CaseSensitivityTestPEP302,
Source_CaseSensitivityTestPEP302
) = util.test_both(CaseSensitivityTestPEP302, importlib=importlib,
machinery=machinery)
class CaseSensitivityTestPEP451(CaseSensitivityTest): class CaseSensitivityTestPEP451(CaseSensitivityTest):
def find(self, finder): def find(self, finder):
found = finder.find_spec(self.name) found = finder.find_spec(self.name)
return found.loader if found is not None else found return found.loader if found is not None else found
Frozen_CaseSensitivityTestPEP451, Source_CaseSensitivityTestPEP451 = util.test_both(
CaseSensitivityTestPEP451, importlib=importlib, machinery=machinery) (Frozen_CaseSensitivityTestPEP451,
Source_CaseSensitivityTestPEP451
) = util.test_both(CaseSensitivityTestPEP451, importlib=importlib,
machinery=machinery)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -235,9 +235,11 @@ class SimpleTest(abc.LoaderTests): ...@@ -235,9 +235,11 @@ class SimpleTest(abc.LoaderTests):
warnings.simplefilter('ignore', DeprecationWarning) warnings.simplefilter('ignore', DeprecationWarning)
loader.load_module('bad name') loader.load_module('bad name')
Frozen_SimpleTest, Source_SimpleTest = util.test_both(
SimpleTest, importlib=importlib, machinery=machinery, abc=importlib_abc, (Frozen_SimpleTest,
util=importlib_util) Source_SimpleTest
) = util.test_both(SimpleTest, importlib=importlib, machinery=machinery,
abc=importlib_abc, util=importlib_util)
class BadBytecodeTest: class BadBytecodeTest:
...@@ -346,6 +348,7 @@ class BadBytecodeTest: ...@@ -346,6 +348,7 @@ class BadBytecodeTest:
lambda bc: b'\x00\x00\x00\x00' + bc[4:]) lambda bc: b'\x00\x00\x00\x00' + bc[4:])
test('_temp', mapping, bc_path) test('_temp', mapping, bc_path)
class BadBytecodeTestPEP451(BadBytecodeTest): class BadBytecodeTestPEP451(BadBytecodeTest):
def import_(self, file, module_name): def import_(self, file, module_name):
...@@ -354,6 +357,7 @@ class BadBytecodeTestPEP451(BadBytecodeTest): ...@@ -354,6 +357,7 @@ class BadBytecodeTestPEP451(BadBytecodeTest):
module.__spec__ = self.util.spec_from_loader(module_name, loader) module.__spec__ = self.util.spec_from_loader(module_name, loader)
loader.exec_module(module) loader.exec_module(module)
class BadBytecodeTestPEP302(BadBytecodeTest): class BadBytecodeTestPEP302(BadBytecodeTest):
def import_(self, file, module_name): def import_(self, file, module_name):
...@@ -490,21 +494,29 @@ class SourceLoaderBadBytecodeTest: ...@@ -490,21 +494,29 @@ class SourceLoaderBadBytecodeTest:
# Make writable for eventual clean-up. # Make writable for eventual clean-up.
os.chmod(bytecode_path, stat.S_IWUSR) os.chmod(bytecode_path, stat.S_IWUSR)
class SourceLoaderBadBytecodeTestPEP451( class SourceLoaderBadBytecodeTestPEP451(
SourceLoaderBadBytecodeTest, BadBytecodeTestPEP451): SourceLoaderBadBytecodeTest, BadBytecodeTestPEP451):
pass pass
Frozen_SourceBadBytecodePEP451, Source_SourceBadBytecodePEP451 = util.test_both(
SourceLoaderBadBytecodeTestPEP451, importlib=importlib, machinery=machinery, (Frozen_SourceBadBytecodePEP451,
abc=importlib_abc, util=importlib_util) Source_SourceBadBytecodePEP451
) = util.test_both(SourceLoaderBadBytecodeTestPEP451, importlib=importlib,
machinery=machinery, abc=importlib_abc,
util=importlib_util)
class SourceLoaderBadBytecodeTestPEP302( class SourceLoaderBadBytecodeTestPEP302(
SourceLoaderBadBytecodeTest, BadBytecodeTestPEP302): SourceLoaderBadBytecodeTest, BadBytecodeTestPEP302):
pass pass
Frozen_SourceBadBytecodePEP302, Source_SourceBadBytecodePEP302 = util.test_both(
SourceLoaderBadBytecodeTestPEP302, importlib=importlib, machinery=machinery, (Frozen_SourceBadBytecodePEP302,
abc=importlib_abc, util=importlib_util) Source_SourceBadBytecodePEP302
) = util.test_both(SourceLoaderBadBytecodeTestPEP302, importlib=importlib,
machinery=machinery, abc=importlib_abc,
util=importlib_util)
class SourcelessLoaderBadBytecodeTest: class SourcelessLoaderBadBytecodeTest:
...@@ -566,21 +578,29 @@ class SourcelessLoaderBadBytecodeTest: ...@@ -566,21 +578,29 @@ class SourcelessLoaderBadBytecodeTest:
def test_non_code_marshal(self): def test_non_code_marshal(self):
self._test_non_code_marshal(del_source=True) self._test_non_code_marshal(del_source=True)
class SourcelessLoaderBadBytecodeTestPEP451(SourcelessLoaderBadBytecodeTest, class SourcelessLoaderBadBytecodeTestPEP451(SourcelessLoaderBadBytecodeTest,
BadBytecodeTestPEP451): BadBytecodeTestPEP451):
pass pass
Frozen_SourcelessBadBytecodePEP451, Source_SourcelessBadBytecodePEP451 = util.test_both(
SourcelessLoaderBadBytecodeTestPEP451, importlib=importlib, (Frozen_SourcelessBadBytecodePEP451,
machinery=machinery, abc=importlib_abc, util=importlib_util) Source_SourcelessBadBytecodePEP451
) = util.test_both(SourcelessLoaderBadBytecodeTestPEP451, importlib=importlib,
machinery=machinery, abc=importlib_abc,
util=importlib_util)
class SourcelessLoaderBadBytecodeTestPEP302(SourcelessLoaderBadBytecodeTest, class SourcelessLoaderBadBytecodeTestPEP302(SourcelessLoaderBadBytecodeTest,
BadBytecodeTestPEP302): BadBytecodeTestPEP302):
pass pass
Frozen_SourcelessBadBytecodePEP302, Source_SourcelessBadBytecodePEP302 = util.test_both(
SourcelessLoaderBadBytecodeTestPEP302, importlib=importlib, (Frozen_SourcelessBadBytecodePEP302,
machinery=machinery, abc=importlib_abc, util=importlib_util) Source_SourcelessBadBytecodePEP302
) = util.test_both(SourcelessLoaderBadBytecodeTestPEP302, importlib=importlib,
machinery=machinery, abc=importlib_abc,
util=importlib_util)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -195,8 +195,10 @@ class FinderTestsPEP451(FinderTests): ...@@ -195,8 +195,10 @@ class FinderTestsPEP451(FinderTests):
spec = finder.find_spec(name) spec = finder.find_spec(name)
return spec.loader if spec is not None else spec return spec.loader if spec is not None else spec
Frozen_FinderTestsPEP451, Source_FinderTestsPEP451 = util.test_both(
FinderTestsPEP451, machinery=machinery) (Frozen_FinderTestsPEP451,
Source_FinderTestsPEP451
) = util.test_both(FinderTestsPEP451, machinery=machinery)
class FinderTestsPEP420(FinderTests): class FinderTestsPEP420(FinderTests):
...@@ -209,8 +211,10 @@ class FinderTestsPEP420(FinderTests): ...@@ -209,8 +211,10 @@ class FinderTestsPEP420(FinderTests):
loader_portions = finder.find_loader(name) loader_portions = finder.find_loader(name)
return loader_portions[0] if loader_only else loader_portions return loader_portions[0] if loader_only else loader_portions
Frozen_FinderTestsPEP420, Source_FinderTestsPEP420 = util.test_both(
FinderTestsPEP420, machinery=machinery) (Frozen_FinderTestsPEP420,
Source_FinderTestsPEP420
) = util.test_both(FinderTestsPEP420, machinery=machinery)
class FinderTestsPEP302(FinderTests): class FinderTestsPEP302(FinderTests):
...@@ -222,9 +226,10 @@ class FinderTestsPEP302(FinderTests): ...@@ -222,9 +226,10 @@ class FinderTestsPEP302(FinderTests):
warnings.simplefilter("ignore", DeprecationWarning) warnings.simplefilter("ignore", DeprecationWarning)
return finder.find_module(name) return finder.find_module(name)
Frozen_FinderTestsPEP302, Source_FinderTestsPEP302 = util.test_both(
FinderTestsPEP302, machinery=machinery)
(Frozen_FinderTestsPEP302,
Source_FinderTestsPEP302
) = util.test_both(FinderTestsPEP302, machinery=machinery)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -22,7 +22,10 @@ class PathHookTest: ...@@ -22,7 +22,10 @@ class PathHookTest:
# The empty string represents the cwd. # The empty string represents the cwd.
self.assertTrue(hasattr(self.path_hook()(''), 'find_module')) self.assertTrue(hasattr(self.path_hook()(''), 'find_module'))
Frozen_PathHookTest, Source_PathHooktest = util.test_both(PathHookTest, machinery=machinery)
(Frozen_PathHookTest,
Source_PathHooktest
) = util.test_both(PathHookTest, machinery=machinery)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -88,6 +88,7 @@ class EncodingTest: ...@@ -88,6 +88,7 @@ class EncodingTest:
with self.assertRaises(SyntaxError): with self.assertRaises(SyntaxError):
self.run_test(source) self.run_test(source)
class EncodingTestPEP451(EncodingTest): class EncodingTestPEP451(EncodingTest):
def load(self, loader): def load(self, loader):
...@@ -96,8 +97,11 @@ class EncodingTestPEP451(EncodingTest): ...@@ -96,8 +97,11 @@ class EncodingTestPEP451(EncodingTest):
loader.exec_module(module) loader.exec_module(module)
return module return module
Frozen_EncodingTestPEP451, Source_EncodingTestPEP451 = util.test_both(
EncodingTestPEP451, machinery=machinery) (Frozen_EncodingTestPEP451,
Source_EncodingTestPEP451
) = util.test_both(EncodingTestPEP451, machinery=machinery)
class EncodingTestPEP302(EncodingTest): class EncodingTestPEP302(EncodingTest):
...@@ -106,8 +110,10 @@ class EncodingTestPEP302(EncodingTest): ...@@ -106,8 +110,10 @@ class EncodingTestPEP302(EncodingTest):
warnings.simplefilter('ignore', DeprecationWarning) warnings.simplefilter('ignore', DeprecationWarning)
return loader.load_module(self.module_name) return loader.load_module(self.module_name)
Frozen_EncodingTestPEP302, Source_EncodingTestPEP302 = util.test_both(
EncodingTestPEP302, machinery=machinery) (Frozen_EncodingTestPEP302,
Source_EncodingTestPEP302
) = util.test_both(EncodingTestPEP302, machinery=machinery)
class LineEndingTest: class LineEndingTest:
...@@ -138,6 +144,7 @@ class LineEndingTest: ...@@ -138,6 +144,7 @@ class LineEndingTest:
def test_lf(self): def test_lf(self):
self.run_test(b'\n') self.run_test(b'\n')
class LineEndingTestPEP451(LineEndingTest): class LineEndingTestPEP451(LineEndingTest):
def load(self, loader, module_name): def load(self, loader, module_name):
...@@ -146,8 +153,11 @@ class LineEndingTestPEP451(LineEndingTest): ...@@ -146,8 +153,11 @@ class LineEndingTestPEP451(LineEndingTest):
loader.exec_module(module) loader.exec_module(module)
return module return module
Frozen_LineEndingTestPEP451, Source_LineEndingTestPEP451 = util.test_both(
LineEndingTestPEP451, machinery=machinery) (Frozen_LineEndingTestPEP451,
Source_LineEndingTestPEP451
) = util.test_both(LineEndingTestPEP451, machinery=machinery)
class LineEndingTestPEP302(LineEndingTest): class LineEndingTestPEP302(LineEndingTest):
...@@ -156,8 +166,10 @@ class LineEndingTestPEP302(LineEndingTest): ...@@ -156,8 +166,10 @@ class LineEndingTestPEP302(LineEndingTest):
warnings.simplefilter('ignore', DeprecationWarning) warnings.simplefilter('ignore', DeprecationWarning)
return loader.load_module(module_name) return loader.load_module(module_name)
Frozen_LineEndingTestPEP302, Source_LineEndingTestPEP302 = util.test_both(
LineEndingTestPEP302, machinery=machinery) (Frozen_LineEndingTestPEP302,
Source_LineEndingTestPEP302
) = util.test_both(LineEndingTestPEP302, machinery=machinery)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -10,12 +10,13 @@ import unittest ...@@ -10,12 +10,13 @@ import unittest
from unittest import mock from unittest import mock
import warnings import warnings
from . import util from . import util as test_util
init = test_util.import_importlib('importlib')
abc = test_util.import_importlib('importlib.abc')
machinery = test_util.import_importlib('importlib.machinery')
util = test_util.import_importlib('importlib.util')
frozen_init, source_init = util.import_importlib('importlib')
frozen_abc, source_abc = util.import_importlib('importlib.abc')
machinery = util.import_importlib('importlib.machinery')
frozen_util, source_util = util.import_importlib('importlib.util')
##### Inheritance ############################################################## ##### Inheritance ##############################################################
class InheritanceTests: class InheritanceTests:
...@@ -26,8 +27,7 @@ class InheritanceTests: ...@@ -26,8 +27,7 @@ class InheritanceTests:
subclasses = [] subclasses = []
superclasses = [] superclasses = []
def __init__(self, *args, **kwargs): def setUp(self):
super().__init__(*args, **kwargs)
self.superclasses = [getattr(self.abc, class_name) self.superclasses = [getattr(self.abc, class_name)
for class_name in self.superclass_names] for class_name in self.superclass_names]
if hasattr(self, 'subclass_names'): if hasattr(self, 'subclass_names'):
...@@ -36,11 +36,11 @@ class InheritanceTests: ...@@ -36,11 +36,11 @@ class InheritanceTests:
# checking across module boundaries (i.e. the _bootstrap in abc is # checking across module boundaries (i.e. the _bootstrap in abc is
# not the same as the one in machinery). That means stealing one of # not the same as the one in machinery). That means stealing one of
# the modules from the other to make sure the same instance is used. # the modules from the other to make sure the same instance is used.
self.subclasses = [getattr(self.abc.machinery, class_name) machinery = self.abc.machinery
self.subclasses = [getattr(machinery, class_name)
for class_name in self.subclass_names] for class_name in self.subclass_names]
assert self.subclasses or self.superclasses, self.__class__ assert self.subclasses or self.superclasses, self.__class__
testing = self.__class__.__name__.partition('_')[2] self.__test = getattr(self.abc, self._NAME)
self.__test = getattr(self.abc, testing)
def test_subclasses(self): def test_subclasses(self):
# Test that the expected subclasses inherit. # Test that the expected subclasses inherit.
...@@ -54,94 +54,97 @@ class InheritanceTests: ...@@ -54,94 +54,97 @@ class InheritanceTests:
self.assertTrue(issubclass(self.__test, superclass), self.assertTrue(issubclass(self.__test, superclass),
"{0} is not a superclass of {1}".format(superclass, self.__test)) "{0} is not a superclass of {1}".format(superclass, self.__test))
def create_inheritance_tests(base_class):
def set_frozen(ns):
ns['abc'] = frozen_abc
def set_source(ns):
ns['abc'] = source_abc
classes = []
for prefix, ns_set in [('Frozen', set_frozen), ('Source', set_source)]:
classes.append(types.new_class('_'.join([prefix, base_class.__name__]),
(base_class, unittest.TestCase),
exec_body=ns_set))
return classes
class MetaPathFinder(InheritanceTests): class MetaPathFinder(InheritanceTests):
superclass_names = ['Finder'] superclass_names = ['Finder']
subclass_names = ['BuiltinImporter', 'FrozenImporter', 'PathFinder', subclass_names = ['BuiltinImporter', 'FrozenImporter', 'PathFinder',
'WindowsRegistryFinder'] 'WindowsRegistryFinder']
tests = create_inheritance_tests(MetaPathFinder)
Frozen_MetaPathFinderInheritanceTests, Source_MetaPathFinderInheritanceTests = tests (Frozen_MetaPathFinderInheritanceTests,
Source_MetaPathFinderInheritanceTests
) = test_util.test_both(MetaPathFinder, abc=abc)
class PathEntryFinder(InheritanceTests): class PathEntryFinder(InheritanceTests):
superclass_names = ['Finder'] superclass_names = ['Finder']
subclass_names = ['FileFinder'] subclass_names = ['FileFinder']
tests = create_inheritance_tests(PathEntryFinder)
Frozen_PathEntryFinderInheritanceTests, Source_PathEntryFinderInheritanceTests = tests (Frozen_PathEntryFinderInheritanceTests,
Source_PathEntryFinderInheritanceTests
) = test_util.test_both(PathEntryFinder, abc=abc)
class ResourceLoader(InheritanceTests): class ResourceLoader(InheritanceTests):
superclass_names = ['Loader'] superclass_names = ['Loader']
tests = create_inheritance_tests(ResourceLoader)
Frozen_ResourceLoaderInheritanceTests, Source_ResourceLoaderInheritanceTests = tests (Frozen_ResourceLoaderInheritanceTests,
Source_ResourceLoaderInheritanceTests
) = test_util.test_both(ResourceLoader, abc=abc)
class InspectLoader(InheritanceTests): class InspectLoader(InheritanceTests):
superclass_names = ['Loader'] superclass_names = ['Loader']
subclass_names = ['BuiltinImporter', 'FrozenImporter', 'ExtensionFileLoader'] subclass_names = ['BuiltinImporter', 'FrozenImporter', 'ExtensionFileLoader']
tests = create_inheritance_tests(InspectLoader)
Frozen_InspectLoaderInheritanceTests, Source_InspectLoaderInheritanceTests = tests (Frozen_InspectLoaderInheritanceTests,
Source_InspectLoaderInheritanceTests
) = test_util.test_both(InspectLoader, abc=abc)
class ExecutionLoader(InheritanceTests): class ExecutionLoader(InheritanceTests):
superclass_names = ['InspectLoader'] superclass_names = ['InspectLoader']
subclass_names = ['ExtensionFileLoader'] subclass_names = ['ExtensionFileLoader']
tests = create_inheritance_tests(ExecutionLoader)
Frozen_ExecutionLoaderInheritanceTests, Source_ExecutionLoaderInheritanceTests = tests (Frozen_ExecutionLoaderInheritanceTests,
Source_ExecutionLoaderInheritanceTests
) = test_util.test_both(ExecutionLoader, abc=abc)
class FileLoader(InheritanceTests): class FileLoader(InheritanceTests):
superclass_names = ['ResourceLoader', 'ExecutionLoader'] superclass_names = ['ResourceLoader', 'ExecutionLoader']
subclass_names = ['SourceFileLoader', 'SourcelessFileLoader'] subclass_names = ['SourceFileLoader', 'SourcelessFileLoader']
tests = create_inheritance_tests(FileLoader)
Frozen_FileLoaderInheritanceTests, Source_FileLoaderInheritanceTests = tests (Frozen_FileLoaderInheritanceTests,
Source_FileLoaderInheritanceTests
) = test_util.test_both(FileLoader, abc=abc)
class SourceLoader(InheritanceTests): class SourceLoader(InheritanceTests):
superclass_names = ['ResourceLoader', 'ExecutionLoader'] superclass_names = ['ResourceLoader', 'ExecutionLoader']
subclass_names = ['SourceFileLoader'] subclass_names = ['SourceFileLoader']
tests = create_inheritance_tests(SourceLoader)
Frozen_SourceLoaderInheritanceTests, Source_SourceLoaderInheritanceTests = tests (Frozen_SourceLoaderInheritanceTests,
Source_SourceLoaderInheritanceTests
) = test_util.test_both(SourceLoader, abc=abc)
##### Default return values #################################################### ##### Default return values ####################################################
def make_abc_subclasses(base_class):
classes = [] def make_abc_subclasses(base_class, name=None, inst=False, **kwargs):
for kind, abc in [('Frozen', frozen_abc), ('Source', source_abc)]: if name is None:
name = '_'.join([kind, base_class.__name__]) name = base_class.__name__
base_classes = base_class, getattr(abc, base_class.__name__) base = {kind: getattr(splitabc, name)
classes.append(types.new_class(name, base_classes)) for kind, splitabc in abc.items()}
return classes return {cls._KIND: cls() if inst else cls
for cls in test_util.split_frozen(base_class, base, **kwargs)}
def make_return_value_tests(base_class, test_class):
frozen_class, source_class = make_abc_subclasses(base_class)
tests = [] class ABCTestHarness:
for prefix, class_in_test in [('Frozen', frozen_class), ('Source', source_class)]:
def set_ns(ns): @property
ns['ins'] = class_in_test() def ins(self):
tests.append(types.new_class('_'.join([prefix, test_class.__name__]), # Lazily set ins on the class.
(test_class, unittest.TestCase), cls = self.SPLIT[self._KIND]
exec_body=set_ns)) ins = cls()
return tests self.__class__.ins = ins
return ins
class MetaPathFinder: class MetaPathFinder:
...@@ -149,10 +152,10 @@ class MetaPathFinder: ...@@ -149,10 +152,10 @@ class MetaPathFinder:
def find_module(self, fullname, path): def find_module(self, fullname, path):
return super().find_module(fullname, path) return super().find_module(fullname, path)
Frozen_MPF, Source_MPF = make_abc_subclasses(MetaPathFinder)
class MetaPathFinderDefaultsTests(ABCTestHarness):
class MetaPathFinderDefaultsTests: SPLIT = make_abc_subclasses(MetaPathFinder)
def test_find_module(self): def test_find_module(self):
# Default should return None. # Default should return None.
...@@ -163,8 +166,9 @@ class MetaPathFinderDefaultsTests: ...@@ -163,8 +166,9 @@ class MetaPathFinderDefaultsTests:
self.ins.invalidate_caches() self.ins.invalidate_caches()
tests = make_return_value_tests(MetaPathFinder, MetaPathFinderDefaultsTests) (Frozen_MPFDefaultTests,
Frozen_MPFDefaultTests, Source_MPFDefaultTests = tests Source_MPFDefaultTests
) = test_util.test_both(MetaPathFinderDefaultsTests)
class PathEntryFinder: class PathEntryFinder:
...@@ -172,10 +176,10 @@ class PathEntryFinder: ...@@ -172,10 +176,10 @@ class PathEntryFinder:
def find_loader(self, fullname): def find_loader(self, fullname):
return super().find_loader(fullname) return super().find_loader(fullname)
Frozen_PEF, Source_PEF = make_abc_subclasses(PathEntryFinder)
class PathEntryFinderDefaultsTests(ABCTestHarness):
class PathEntryFinderDefaultsTests: SPLIT = make_abc_subclasses(PathEntryFinder)
def test_find_loader(self): def test_find_loader(self):
self.assertEqual((None, []), self.ins.find_loader('something')) self.assertEqual((None, []), self.ins.find_loader('something'))
...@@ -188,8 +192,9 @@ class PathEntryFinderDefaultsTests: ...@@ -188,8 +192,9 @@ class PathEntryFinderDefaultsTests:
self.ins.invalidate_caches() self.ins.invalidate_caches()
tests = make_return_value_tests(PathEntryFinder, PathEntryFinderDefaultsTests) (Frozen_PEFDefaultTests,
Frozen_PEFDefaultTests, Source_PEFDefaultTests = tests Source_PEFDefaultTests
) = test_util.test_both(PathEntryFinderDefaultsTests)
class Loader: class Loader:
...@@ -198,10 +203,9 @@ class Loader: ...@@ -198,10 +203,9 @@ class Loader:
return super().load_module(fullname) return super().load_module(fullname)
Frozen_L, Source_L = make_abc_subclasses(Loader) class LoaderDefaultsTests(ABCTestHarness):
SPLIT = make_abc_subclasses(Loader)
class LoaderDefaultsTests:
def test_load_module(self): def test_load_module(self):
with self.assertRaises(ImportError): with self.assertRaises(ImportError):
...@@ -217,8 +221,9 @@ class LoaderDefaultsTests: ...@@ -217,8 +221,9 @@ class LoaderDefaultsTests:
self.assertTrue(repr(mod)) self.assertTrue(repr(mod))
tests = make_return_value_tests(Loader, LoaderDefaultsTests) (Frozen_LDefaultTests,
Frozen_LDefaultTests, SourceLDefaultTests = tests SourceLDefaultTests
) = test_util.test_both(LoaderDefaultsTests)
class ResourceLoader(Loader): class ResourceLoader(Loader):
...@@ -227,18 +232,18 @@ class ResourceLoader(Loader): ...@@ -227,18 +232,18 @@ class ResourceLoader(Loader):
return super().get_data(path) return super().get_data(path)
Frozen_RL, Source_RL = make_abc_subclasses(ResourceLoader) class ResourceLoaderDefaultsTests(ABCTestHarness):
class ResourceLoaderDefaultsTests: SPLIT = make_abc_subclasses(ResourceLoader)
def test_get_data(self): def test_get_data(self):
with self.assertRaises(IOError): with self.assertRaises(IOError):
self.ins.get_data('/some/path') self.ins.get_data('/some/path')
tests = make_return_value_tests(ResourceLoader, ResourceLoaderDefaultsTests) (Frozen_RLDefaultTests,
Frozen_RLDefaultTests, Source_RLDefaultTests = tests Source_RLDefaultTests
) = test_util.test_both(ResourceLoaderDefaultsTests)
class InspectLoader(Loader): class InspectLoader(Loader):
...@@ -250,10 +255,12 @@ class InspectLoader(Loader): ...@@ -250,10 +255,12 @@ class InspectLoader(Loader):
return super().get_source(fullname) return super().get_source(fullname)
Frozen_IL, Source_IL = make_abc_subclasses(InspectLoader) SPLIT_IL = make_abc_subclasses(InspectLoader)
class InspectLoaderDefaultsTests: class InspectLoaderDefaultsTests(ABCTestHarness):
SPLIT = SPLIT_IL
def test_is_package(self): def test_is_package(self):
with self.assertRaises(ImportError): with self.assertRaises(ImportError):
...@@ -264,8 +271,9 @@ class InspectLoaderDefaultsTests: ...@@ -264,8 +271,9 @@ class InspectLoaderDefaultsTests:
self.ins.get_source('blah') self.ins.get_source('blah')
tests = make_return_value_tests(InspectLoader, InspectLoaderDefaultsTests) (Frozen_ILDefaultTests,
Frozen_ILDefaultTests, Source_ILDefaultTests = tests Source_ILDefaultTests
) = test_util.test_both(InspectLoaderDefaultsTests)
class ExecutionLoader(InspectLoader): class ExecutionLoader(InspectLoader):
...@@ -273,21 +281,25 @@ class ExecutionLoader(InspectLoader): ...@@ -273,21 +281,25 @@ class ExecutionLoader(InspectLoader):
def get_filename(self, fullname): def get_filename(self, fullname):
return super().get_filename(fullname) return super().get_filename(fullname)
Frozen_EL, Source_EL = make_abc_subclasses(ExecutionLoader)
SPLIT_EL = make_abc_subclasses(ExecutionLoader)
class ExecutionLoaderDefaultsTests: class ExecutionLoaderDefaultsTests(ABCTestHarness):
SPLIT = SPLIT_EL
def test_get_filename(self): def test_get_filename(self):
with self.assertRaises(ImportError): with self.assertRaises(ImportError):
self.ins.get_filename('blah') self.ins.get_filename('blah')
tests = make_return_value_tests(ExecutionLoader, InspectLoaderDefaultsTests) (Frozen_ELDefaultTests,
Frozen_ELDefaultTests, Source_ELDefaultsTests = tests Source_ELDefaultsTests
) = test_util.test_both(InspectLoaderDefaultsTests)
##### MetaPathFinder concrete methods ##########################################
##### MetaPathFinder concrete methods ##########################################
class MetaPathFinderFindModuleTests: class MetaPathFinderFindModuleTests:
@classmethod @classmethod
...@@ -317,13 +329,12 @@ class MetaPathFinderFindModuleTests: ...@@ -317,13 +329,12 @@ class MetaPathFinderFindModuleTests:
self.assertIs(found, spec.loader) self.assertIs(found, spec.loader)
Frozen_MPFFindModuleTests, Source_MPFFindModuleTests = util.test_both( (Frozen_MPFFindModuleTests,
MetaPathFinderFindModuleTests, Source_MPFFindModuleTests
abc=(frozen_abc, source_abc), ) = test_util.test_both(MetaPathFinderFindModuleTests, abc=abc, util=util)
util=(frozen_util, source_util))
##### PathEntryFinder concrete methods #########################################
##### PathEntryFinder concrete methods #########################################
class PathEntryFinderFindLoaderTests: class PathEntryFinderFindLoaderTests:
@classmethod @classmethod
...@@ -361,11 +372,10 @@ class PathEntryFinderFindLoaderTests: ...@@ -361,11 +372,10 @@ class PathEntryFinderFindLoaderTests:
self.assertEqual(paths, found[1]) self.assertEqual(paths, found[1])
Frozen_PEFFindLoaderTests, Source_PEFFindLoaderTests = util.test_both( (Frozen_PEFFindLoaderTests,
PathEntryFinderFindLoaderTests, Source_PEFFindLoaderTests
abc=(frozen_abc, source_abc), ) = test_util.test_both(PathEntryFinderFindLoaderTests, abc=abc, util=util,
machinery=machinery, machinery=machinery)
util=(frozen_util, source_util))
##### Loader concrete methods ################################################## ##### Loader concrete methods ##################################################
...@@ -386,7 +396,7 @@ class LoaderLoadModuleTests: ...@@ -386,7 +396,7 @@ class LoaderLoadModuleTests:
def test_fresh(self): def test_fresh(self):
loader = self.loader() loader = self.loader()
name = 'blah' name = 'blah'
with util.uncache(name): with test_util.uncache(name):
loader.load_module(name) loader.load_module(name)
module = loader.found module = loader.found
self.assertIs(sys.modules[name], module) self.assertIs(sys.modules[name], module)
...@@ -404,7 +414,7 @@ class LoaderLoadModuleTests: ...@@ -404,7 +414,7 @@ class LoaderLoadModuleTests:
module = types.ModuleType(name) module = types.ModuleType(name)
module.__spec__ = self.util.spec_from_loader(name, loader) module.__spec__ = self.util.spec_from_loader(name, loader)
module.__loader__ = loader module.__loader__ = loader
with util.uncache(name): with test_util.uncache(name):
sys.modules[name] = module sys.modules[name] = module
loader.load_module(name) loader.load_module(name)
found = loader.found found = loader.found
...@@ -412,10 +422,9 @@ class LoaderLoadModuleTests: ...@@ -412,10 +422,9 @@ class LoaderLoadModuleTests:
self.assertIs(module, sys.modules[name]) self.assertIs(module, sys.modules[name])
Frozen_LoaderLoadModuleTests, Source_LoaderLoadModuleTests = util.test_both( (Frozen_LoaderLoadModuleTests,
LoaderLoadModuleTests, Source_LoaderLoadModuleTests
abc=(frozen_abc, source_abc), ) = test_util.test_both(LoaderLoadModuleTests, abc=abc, util=util)
util=(frozen_util, source_util))
##### InspectLoader concrete methods ########################################### ##### InspectLoader concrete methods ###########################################
...@@ -461,11 +470,10 @@ class InspectLoaderSourceToCodeTests: ...@@ -461,11 +470,10 @@ class InspectLoaderSourceToCodeTests:
self.assertEqual(code.co_filename, '<string>') self.assertEqual(code.co_filename, '<string>')
class Frozen_ILSourceToCodeTests(InspectLoaderSourceToCodeTests, unittest.TestCase): (Frozen_ILSourceToCodeTests,
InspectLoaderSubclass = Frozen_IL Source_ILSourceToCodeTests
) = test_util.test_both(InspectLoaderSourceToCodeTests,
class Source_ILSourceToCodeTests(InspectLoaderSourceToCodeTests, unittest.TestCase): InspectLoaderSubclass=SPLIT_IL)
InspectLoaderSubclass = Source_IL
class InspectLoaderGetCodeTests: class InspectLoaderGetCodeTests:
...@@ -495,11 +503,10 @@ class InspectLoaderGetCodeTests: ...@@ -495,11 +503,10 @@ class InspectLoaderGetCodeTests:
loader.get_code('blah') loader.get_code('blah')
class Frozen_ILGetCodeTests(InspectLoaderGetCodeTests, unittest.TestCase): (Frozen_ILGetCodeTests,
InspectLoaderSubclass = Frozen_IL Source_ILGetCodeTests
) = test_util.test_both(InspectLoaderGetCodeTests,
class Source_ILGetCodeTests(InspectLoaderGetCodeTests, unittest.TestCase): InspectLoaderSubclass=SPLIT_IL)
InspectLoaderSubclass = Source_IL
class InspectLoaderLoadModuleTests: class InspectLoaderLoadModuleTests:
...@@ -543,11 +550,10 @@ class InspectLoaderLoadModuleTests: ...@@ -543,11 +550,10 @@ class InspectLoaderLoadModuleTests:
self.assertEqual(module, sys.modules[self.module_name]) self.assertEqual(module, sys.modules[self.module_name])
class Frozen_ILLoadModuleTests(InspectLoaderLoadModuleTests, unittest.TestCase): (Frozen_ILLoadModuleTests,
InspectLoaderSubclass = Frozen_IL Source_ILLoadModuleTests
) = test_util.test_both(InspectLoaderLoadModuleTests,
class Source_ILLoadModuleTests(InspectLoaderLoadModuleTests, unittest.TestCase): InspectLoaderSubclass=SPLIT_IL)
InspectLoaderSubclass = Source_IL
##### ExecutionLoader concrete methods ######################################### ##### ExecutionLoader concrete methods #########################################
...@@ -608,15 +614,14 @@ class ExecutionLoaderGetCodeTests: ...@@ -608,15 +614,14 @@ class ExecutionLoaderGetCodeTests:
self.assertEqual(module.attr, 42) self.assertEqual(module.attr, 42)
class Frozen_ELGetCodeTests(ExecutionLoaderGetCodeTests, unittest.TestCase): (Frozen_ELGetCodeTests,
ExecutionLoaderSubclass = Frozen_EL Source_ELGetCodeTests
) = test_util.test_both(ExecutionLoaderGetCodeTests,
class Source_ELGetCodeTests(ExecutionLoaderGetCodeTests, unittest.TestCase): ExecutionLoaderSubclass=SPLIT_EL)
ExecutionLoaderSubclass = Source_EL
##### SourceLoader concrete methods ############################################ ##### SourceLoader concrete methods ############################################
class SourceLoader: class SourceOnlyLoader:
# Globals that should be defined for all modules. # Globals that should be defined for all modules.
source = (b"_ = '::'.join([__name__, __file__, __cached__, __package__, " source = (b"_ = '::'.join([__name__, __file__, __cached__, __package__, "
...@@ -637,10 +642,10 @@ class SourceLoader: ...@@ -637,10 +642,10 @@ class SourceLoader:
return '<module>' return '<module>'
Frozen_SourceOnlyL, Source_SourceOnlyL = make_abc_subclasses(SourceLoader) SPLIT_SOL = make_abc_subclasses(SourceOnlyLoader, 'SourceLoader')
class SourceLoader(SourceLoader): class SourceLoader(SourceOnlyLoader):
source_mtime = 1 source_mtime = 1
...@@ -677,11 +682,7 @@ class SourceLoader(SourceLoader): ...@@ -677,11 +682,7 @@ class SourceLoader(SourceLoader):
return path == self.bytecode_path return path == self.bytecode_path
Frozen_SL, Source_SL = make_abc_subclasses(SourceLoader) SPLIT_SL = make_abc_subclasses(SourceLoader, util=util, init=init)
Frozen_SL.util = frozen_util
Source_SL.util = source_util
Frozen_SL.init = frozen_init
Source_SL.init = source_init
class SourceLoaderTestHarness: class SourceLoaderTestHarness:
...@@ -765,7 +766,7 @@ class SourceOnlyLoaderTests(SourceLoaderTestHarness): ...@@ -765,7 +766,7 @@ class SourceOnlyLoaderTests(SourceLoaderTestHarness):
# Loading a module should set __name__, __loader__, __package__, # Loading a module should set __name__, __loader__, __package__,
# __path__ (for packages), __file__, and __cached__. # __path__ (for packages), __file__, and __cached__.
# The module should also be put into sys.modules. # The module should also be put into sys.modules.
with util.uncache(self.name): with test_util.uncache(self.name):
with warnings.catch_warnings(): with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning) warnings.simplefilter('ignore', DeprecationWarning)
module = self.loader.load_module(self.name) module = self.loader.load_module(self.name)
...@@ -778,7 +779,7 @@ class SourceOnlyLoaderTests(SourceLoaderTestHarness): ...@@ -778,7 +779,7 @@ class SourceOnlyLoaderTests(SourceLoaderTestHarness):
# is a package. # is a package.
# Testing the values for a package are covered by test_load_module. # Testing the values for a package are covered by test_load_module.
self.setUp(is_package=False) self.setUp(is_package=False)
with util.uncache(self.name): with test_util.uncache(self.name):
with warnings.catch_warnings(): with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning) warnings.simplefilter('ignore', DeprecationWarning)
module = self.loader.load_module(self.name) module = self.loader.load_module(self.name)
...@@ -798,13 +799,10 @@ class SourceOnlyLoaderTests(SourceLoaderTestHarness): ...@@ -798,13 +799,10 @@ class SourceOnlyLoaderTests(SourceLoaderTestHarness):
self.assertEqual(returned_source, source) self.assertEqual(returned_source, source)
class Frozen_SourceOnlyLTests(SourceOnlyLoaderTests, unittest.TestCase): (Frozen_SourceOnlyLoaderTests,
loader_mock = Frozen_SourceOnlyL Source_SourceOnlyLoaderTests
util = frozen_util ) = test_util.test_both(SourceOnlyLoaderTests, util=util,
loader_mock=SPLIT_SOL)
class Source_SourceOnlyLTests(SourceOnlyLoaderTests, unittest.TestCase):
loader_mock = Source_SourceOnlyL
util = source_util
@unittest.skipIf(sys.dont_write_bytecode, "sys.dont_write_bytecode is true") @unittest.skipIf(sys.dont_write_bytecode, "sys.dont_write_bytecode is true")
...@@ -896,15 +894,10 @@ class SourceLoaderBytecodeTests(SourceLoaderTestHarness): ...@@ -896,15 +894,10 @@ class SourceLoaderBytecodeTests(SourceLoaderTestHarness):
self.verify_code(code_object) self.verify_code(code_object)
class Frozen_SLBytecodeTests(SourceLoaderBytecodeTests, unittest.TestCase): (Frozen_SLBytecodeTests,
loader_mock = Frozen_SL SourceSLBytecodeTests
init = frozen_init ) = test_util.test_both(SourceLoaderBytecodeTests, init=init, util=util,
util = frozen_util loader_mock=SPLIT_SL)
class SourceSLBytecodeTests(SourceLoaderBytecodeTests, unittest.TestCase):
loader_mock = Source_SL
init = source_init
util = source_util
class SourceLoaderGetSourceTests: class SourceLoaderGetSourceTests:
...@@ -940,11 +933,10 @@ class SourceLoaderGetSourceTests: ...@@ -940,11 +933,10 @@ class SourceLoaderGetSourceTests:
self.assertEqual(mock.get_source(name), expect) self.assertEqual(mock.get_source(name), expect)
class Frozen_SourceOnlyLGetSourceTests(SourceLoaderGetSourceTests, unittest.TestCase): (Frozen_SourceOnlyLoaderGetSourceTests,
SourceOnlyLoaderMock = Frozen_SourceOnlyL Source_SourceOnlyLoaderGetSourceTests
) = test_util.test_both(SourceLoaderGetSourceTests,
class Source_SourceOnlyLGetSourceTests(SourceLoaderGetSourceTests, unittest.TestCase): SourceOnlyLoaderMock=SPLIT_SOL)
SourceOnlyLoaderMock = Source_SourceOnlyL
if __name__ == '__main__': if __name__ == '__main__':
......
from . import util from . import util as test_util
frozen_init, source_init = util.import_importlib('importlib') init = test_util.import_importlib('importlib')
frozen_util, source_util = util.import_importlib('importlib.util') util = test_util.import_importlib('importlib.util')
frozen_machinery, source_machinery = util.import_importlib('importlib.machinery') machinery = test_util.import_importlib('importlib.machinery')
import os.path import os.path
import sys import sys
...@@ -18,8 +18,8 @@ class ImportModuleTests: ...@@ -18,8 +18,8 @@ class ImportModuleTests:
def test_module_import(self): def test_module_import(self):
# Test importing a top-level module. # Test importing a top-level module.
with util.mock_modules('top_level') as mock: with test_util.mock_modules('top_level') as mock:
with util.import_state(meta_path=[mock]): with test_util.import_state(meta_path=[mock]):
module = self.init.import_module('top_level') module = self.init.import_module('top_level')
self.assertEqual(module.__name__, 'top_level') self.assertEqual(module.__name__, 'top_level')
...@@ -28,8 +28,8 @@ class ImportModuleTests: ...@@ -28,8 +28,8 @@ class ImportModuleTests:
pkg_name = 'pkg' pkg_name = 'pkg'
pkg_long_name = '{0}.__init__'.format(pkg_name) pkg_long_name = '{0}.__init__'.format(pkg_name)
name = '{0}.mod'.format(pkg_name) name = '{0}.mod'.format(pkg_name)
with util.mock_modules(pkg_long_name, name) as mock: with test_util.mock_modules(pkg_long_name, name) as mock:
with util.import_state(meta_path=[mock]): with test_util.import_state(meta_path=[mock]):
module = self.init.import_module(name) module = self.init.import_module(name)
self.assertEqual(module.__name__, name) self.assertEqual(module.__name__, name)
...@@ -40,16 +40,16 @@ class ImportModuleTests: ...@@ -40,16 +40,16 @@ class ImportModuleTests:
module_name = 'mod' module_name = 'mod'
absolute_name = '{0}.{1}'.format(pkg_name, module_name) absolute_name = '{0}.{1}'.format(pkg_name, module_name)
relative_name = '.{0}'.format(module_name) relative_name = '.{0}'.format(module_name)
with util.mock_modules(pkg_long_name, absolute_name) as mock: with test_util.mock_modules(pkg_long_name, absolute_name) as mock:
with util.import_state(meta_path=[mock]): with test_util.import_state(meta_path=[mock]):
self.init.import_module(pkg_name) self.init.import_module(pkg_name)
module = self.init.import_module(relative_name, pkg_name) module = self.init.import_module(relative_name, pkg_name)
self.assertEqual(module.__name__, absolute_name) self.assertEqual(module.__name__, absolute_name)
def test_deep_relative_package_import(self): def test_deep_relative_package_import(self):
modules = ['a.__init__', 'a.b.__init__', 'a.c'] modules = ['a.__init__', 'a.b.__init__', 'a.c']
with util.mock_modules(*modules) as mock: with test_util.mock_modules(*modules) as mock:
with util.import_state(meta_path=[mock]): with test_util.import_state(meta_path=[mock]):
self.init.import_module('a') self.init.import_module('a')
self.init.import_module('a.b') self.init.import_module('a.b')
module = self.init.import_module('..c', 'a.b') module = self.init.import_module('..c', 'a.b')
...@@ -61,8 +61,8 @@ class ImportModuleTests: ...@@ -61,8 +61,8 @@ class ImportModuleTests:
pkg_name = 'pkg' pkg_name = 'pkg'
pkg_long_name = '{0}.__init__'.format(pkg_name) pkg_long_name = '{0}.__init__'.format(pkg_name)
name = '{0}.mod'.format(pkg_name) name = '{0}.mod'.format(pkg_name)
with util.mock_modules(pkg_long_name, name) as mock: with test_util.mock_modules(pkg_long_name, name) as mock:
with util.import_state(meta_path=[mock]): with test_util.import_state(meta_path=[mock]):
self.init.import_module(pkg_name) self.init.import_module(pkg_name)
module = self.init.import_module(name, pkg_name) module = self.init.import_module(name, pkg_name)
self.assertEqual(module.__name__, name) self.assertEqual(module.__name__, name)
...@@ -86,16 +86,15 @@ class ImportModuleTests: ...@@ -86,16 +86,15 @@ class ImportModuleTests:
b_load_count += 1 b_load_count += 1
code = {'a': load_a, 'a.b': load_b} code = {'a': load_a, 'a.b': load_b}
modules = ['a.__init__', 'a.b'] modules = ['a.__init__', 'a.b']
with util.mock_modules(*modules, module_code=code) as mock: with test_util.mock_modules(*modules, module_code=code) as mock:
with util.import_state(meta_path=[mock]): with test_util.import_state(meta_path=[mock]):
self.init.import_module('a.b') self.init.import_module('a.b')
self.assertEqual(b_load_count, 1) self.assertEqual(b_load_count, 1)
class Frozen_ImportModuleTests(ImportModuleTests, unittest.TestCase):
init = frozen_init
class Source_ImportModuleTests(ImportModuleTests, unittest.TestCase): (Frozen_ImportModuleTests,
init = source_init Source_ImportModuleTests
) = test_util.test_both(ImportModuleTests, init=init)
class FindLoaderTests: class FindLoaderTests:
...@@ -107,7 +106,7 @@ class FindLoaderTests: ...@@ -107,7 +106,7 @@ class FindLoaderTests:
def test_sys_modules(self): def test_sys_modules(self):
# If a module with __loader__ is in sys.modules, then return it. # If a module with __loader__ is in sys.modules, then return it.
name = 'some_mod' name = 'some_mod'
with util.uncache(name): with test_util.uncache(name):
module = types.ModuleType(name) module = types.ModuleType(name)
loader = 'a loader!' loader = 'a loader!'
module.__loader__ = loader module.__loader__ = loader
...@@ -120,7 +119,7 @@ class FindLoaderTests: ...@@ -120,7 +119,7 @@ class FindLoaderTests:
def test_sys_modules_loader_is_None(self): def test_sys_modules_loader_is_None(self):
# If sys.modules[name].__loader__ is None, raise ValueError. # If sys.modules[name].__loader__ is None, raise ValueError.
name = 'some_mod' name = 'some_mod'
with util.uncache(name): with test_util.uncache(name):
module = types.ModuleType(name) module = types.ModuleType(name)
module.__loader__ = None module.__loader__ = None
sys.modules[name] = module sys.modules[name] = module
...@@ -133,7 +132,7 @@ class FindLoaderTests: ...@@ -133,7 +132,7 @@ class FindLoaderTests:
# Should raise ValueError # Should raise ValueError
# Issue #17099 # Issue #17099
name = 'some_mod' name = 'some_mod'
with util.uncache(name): with test_util.uncache(name):
module = types.ModuleType(name) module = types.ModuleType(name)
try: try:
del module.__loader__ del module.__loader__
...@@ -148,8 +147,8 @@ class FindLoaderTests: ...@@ -148,8 +147,8 @@ class FindLoaderTests:
def test_success(self): def test_success(self):
# Return the loader found on sys.meta_path. # Return the loader found on sys.meta_path.
name = 'some_mod' name = 'some_mod'
with util.uncache(name): with test_util.uncache(name):
with util.import_state(meta_path=[self.FakeMetaFinder]): with test_util.import_state(meta_path=[self.FakeMetaFinder]):
with warnings.catch_warnings(): with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning) warnings.simplefilter('ignore', DeprecationWarning)
self.assertEqual((name, None), self.init.find_loader(name)) self.assertEqual((name, None), self.init.find_loader(name))
...@@ -158,8 +157,8 @@ class FindLoaderTests: ...@@ -158,8 +157,8 @@ class FindLoaderTests:
# Searching on a path should work. # Searching on a path should work.
name = 'some_mod' name = 'some_mod'
path = 'path to some place' path = 'path to some place'
with util.uncache(name): with test_util.uncache(name):
with util.import_state(meta_path=[self.FakeMetaFinder]): with test_util.import_state(meta_path=[self.FakeMetaFinder]):
with warnings.catch_warnings(): with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning) warnings.simplefilter('ignore', DeprecationWarning)
self.assertEqual((name, path), self.assertEqual((name, path),
...@@ -171,11 +170,10 @@ class FindLoaderTests: ...@@ -171,11 +170,10 @@ class FindLoaderTests:
warnings.simplefilter('ignore', DeprecationWarning) warnings.simplefilter('ignore', DeprecationWarning)
self.assertIsNone(self.init.find_loader('nevergoingtofindthismodule')) self.assertIsNone(self.init.find_loader('nevergoingtofindthismodule'))
class Frozen_FindLoaderTests(FindLoaderTests, unittest.TestCase):
init = frozen_init
class Source_FindLoaderTests(FindLoaderTests, unittest.TestCase): (Frozen_FindLoaderTests,
init = source_init Source_FindLoaderTests
) = test_util.test_both(FindLoaderTests, init=init)
class ReloadTests: class ReloadTests:
...@@ -195,10 +193,10 @@ class ReloadTests: ...@@ -195,10 +193,10 @@ class ReloadTests:
module = type(sys)('top_level') module = type(sys)('top_level')
module.spam = 3 module.spam = 3
sys.modules['top_level'] = module sys.modules['top_level'] = module
mock = util.mock_modules('top_level', mock = test_util.mock_modules('top_level',
module_code={'top_level': code}) module_code={'top_level': code})
with mock: with mock:
with util.import_state(meta_path=[mock]): with test_util.import_state(meta_path=[mock]):
module = self.init.import_module('top_level') module = self.init.import_module('top_level')
reloaded = self.init.reload(module) reloaded = self.init.reload(module)
actual = sys.modules['top_level'] actual = sys.modules['top_level']
...@@ -230,7 +228,7 @@ class ReloadTests: ...@@ -230,7 +228,7 @@ class ReloadTests:
def test_reload_location_changed(self): def test_reload_location_changed(self):
name = 'spam' name = 'spam'
with support.temp_cwd(None) as cwd: with support.temp_cwd(None) as cwd:
with util.uncache('spam'): with test_util.uncache('spam'):
with support.DirsOnSysPath(cwd): with support.DirsOnSysPath(cwd):
# Start as a plain module. # Start as a plain module.
self.init.invalidate_caches() self.init.invalidate_caches()
...@@ -281,7 +279,7 @@ class ReloadTests: ...@@ -281,7 +279,7 @@ class ReloadTests:
def test_reload_namespace_changed(self): def test_reload_namespace_changed(self):
name = 'spam' name = 'spam'
with support.temp_cwd(None) as cwd: with support.temp_cwd(None) as cwd:
with util.uncache('spam'): with test_util.uncache('spam'):
with support.DirsOnSysPath(cwd): with support.DirsOnSysPath(cwd):
# Start as a namespace package. # Start as a namespace package.
self.init.invalidate_caches() self.init.invalidate_caches()
...@@ -338,20 +336,16 @@ class ReloadTests: ...@@ -338,20 +336,16 @@ class ReloadTests:
# See #19851. # See #19851.
name = 'spam' name = 'spam'
subname = 'ham' subname = 'ham'
with util.temp_module(name, pkg=True) as pkg_dir: with test_util.temp_module(name, pkg=True) as pkg_dir:
fullname, _ = util.submodule(name, subname, pkg_dir) fullname, _ = test_util.submodule(name, subname, pkg_dir)
ham = self.init.import_module(fullname) ham = self.init.import_module(fullname)
reloaded = self.init.reload(ham) reloaded = self.init.reload(ham)
self.assertIs(reloaded, ham) self.assertIs(reloaded, ham)
class Frozen_ReloadTests(ReloadTests, unittest.TestCase): (Frozen_ReloadTests,
init = frozen_init Source_ReloadTests
util = frozen_util ) = test_util.test_both(ReloadTests, init=init, util=util)
class Source_ReloadTests(ReloadTests, unittest.TestCase):
init = source_init
util = source_util
class InvalidateCacheTests: class InvalidateCacheTests:
...@@ -384,11 +378,10 @@ class InvalidateCacheTests: ...@@ -384,11 +378,10 @@ class InvalidateCacheTests:
self.addCleanup(lambda: sys.path_importer_cache.__delitem__(key)) self.addCleanup(lambda: sys.path_importer_cache.__delitem__(key))
self.init.invalidate_caches() # Shouldn't trigger an exception. self.init.invalidate_caches() # Shouldn't trigger an exception.
class Frozen_InvalidateCacheTests(InvalidateCacheTests, unittest.TestCase):
init = frozen_init
class Source_InvalidateCacheTests(InvalidateCacheTests, unittest.TestCase): (Frozen_InvalidateCacheTests,
init = source_init Source_InvalidateCacheTests
) = test_util.test_both(InvalidateCacheTests, init=init)
class FrozenImportlibTests(unittest.TestCase): class FrozenImportlibTests(unittest.TestCase):
...@@ -398,6 +391,7 @@ class FrozenImportlibTests(unittest.TestCase): ...@@ -398,6 +391,7 @@ class FrozenImportlibTests(unittest.TestCase):
# Can't do an isinstance() check since separate copies of importlib # Can't do an isinstance() check since separate copies of importlib
# may have been used for import, so just check the name is not for the # may have been used for import, so just check the name is not for the
# frozen loader. # frozen loader.
source_init = init['Source']
self.assertNotEqual(source_init.__loader__.__class__.__name__, self.assertNotEqual(source_init.__loader__.__class__.__name__,
'FrozenImporter') 'FrozenImporter')
...@@ -426,11 +420,10 @@ class StartupTests: ...@@ -426,11 +420,10 @@ class StartupTests:
elif self.machinery.FrozenImporter.find_module(name): elif self.machinery.FrozenImporter.find_module(name):
self.assertIsNot(module.__spec__, None) self.assertIsNot(module.__spec__, None)
class Frozen_StartupTests(StartupTests, unittest.TestCase):
machinery = frozen_machinery
class Source_StartupTests(StartupTests, unittest.TestCase): (Frozen_StartupTests,
machinery = source_machinery Source_StartupTests
) = test_util.test_both(StartupTests, machinery=machinery)
if __name__ == '__main__': if __name__ == '__main__':
......
from . import util from . import util as test_util
frozen_init, source_init = util.import_importlib('importlib')
frozen_bootstrap = frozen_init._bootstrap init = test_util.import_importlib('importlib')
source_bootstrap = source_init._bootstrap
import sys import sys
import time import time
...@@ -33,13 +32,16 @@ if threading is not None: ...@@ -33,13 +32,16 @@ if threading is not None:
# _release_save() unsupported # _release_save() unsupported
test_release_save_unacquired = None test_release_save_unacquired = None
class Frozen_ModuleLockAsRLockTests(ModuleLockAsRLockTests, lock_tests.RLockTests): LOCK_TYPES = {kind: splitinit._bootstrap._ModuleLock
LockType = frozen_bootstrap._ModuleLock for kind, splitinit in init.items()}
class Source_ModuleLockAsRLockTests(ModuleLockAsRLockTests, lock_tests.RLockTests):
LockType = source_bootstrap._ModuleLock
(Frozen_ModuleLockAsRLockTests,
Source_ModuleLockAsRLockTests
) = test_util.test_both(ModuleLockAsRLockTests, lock_tests.RLockTests,
LockType=LOCK_TYPES)
else: else:
LOCK_TYPES = {}
class Frozen_ModuleLockAsRLockTests(unittest.TestCase): class Frozen_ModuleLockAsRLockTests(unittest.TestCase):
pass pass
...@@ -47,6 +49,7 @@ else: ...@@ -47,6 +49,7 @@ else:
pass pass
@unittest.skipUnless(threading, "threads needed for this test")
class DeadlockAvoidanceTests: class DeadlockAvoidanceTests:
def setUp(self): def setUp(self):
...@@ -106,19 +109,22 @@ class DeadlockAvoidanceTests: ...@@ -106,19 +109,22 @@ class DeadlockAvoidanceTests:
self.assertEqual(results.count((True, False)), 0) self.assertEqual(results.count((True, False)), 0)
self.assertEqual(results.count((True, True)), len(results)) self.assertEqual(results.count((True, True)), len(results))
@unittest.skipUnless(threading, "threads needed for this test")
class Frozen_DeadlockAvoidanceTests(DeadlockAvoidanceTests, unittest.TestCase):
LockType = frozen_bootstrap._ModuleLock
DeadlockError = frozen_bootstrap._DeadlockError
@unittest.skipUnless(threading, "threads needed for this test") DEADLOCK_ERRORS = {kind: splitinit._bootstrap._DeadlockError
class Source_DeadlockAvoidanceTests(DeadlockAvoidanceTests, unittest.TestCase): for kind, splitinit in init.items()}
LockType = source_bootstrap._ModuleLock
DeadlockError = source_bootstrap._DeadlockError (Frozen_DeadlockAvoidanceTests,
Source_DeadlockAvoidanceTests
) = test_util.test_both(DeadlockAvoidanceTests,
LockType=LOCK_TYPES, DeadlockError=DEADLOCK_ERRORS)
class LifetimeTests: class LifetimeTests:
@property
def bootstrap(self):
return self.init._bootstrap
def test_lock_lifetime(self): def test_lock_lifetime(self):
name = "xyzzy" name = "xyzzy"
self.assertNotIn(name, self.bootstrap._module_locks) self.assertNotIn(name, self.bootstrap._module_locks)
...@@ -135,11 +141,10 @@ class LifetimeTests: ...@@ -135,11 +141,10 @@ class LifetimeTests:
self.assertEqual(0, len(self.bootstrap._module_locks), self.assertEqual(0, len(self.bootstrap._module_locks),
self.bootstrap._module_locks) self.bootstrap._module_locks)
class Frozen_LifetimeTests(LifetimeTests, unittest.TestCase):
bootstrap = frozen_bootstrap
class Source_LifetimeTests(LifetimeTests, unittest.TestCase): (Frozen_LifetimeTests,
bootstrap = source_bootstrap Source_LifetimeTests
) = test_util.test_both(LifetimeTests, init=init)
@support.reap_threads @support.reap_threads
......
from . import util from . import util as test_util
frozen_init, source_init = util.import_importlib('importlib') init = test_util.import_importlib('importlib')
frozen_bootstrap = frozen_init._bootstrap machinery = test_util.import_importlib('importlib.machinery')
source_bootstrap = source_init._bootstrap util = test_util.import_importlib('importlib.util')
frozen_machinery, source_machinery = util.import_importlib('importlib.machinery')
frozen_util, source_util = util.import_importlib('importlib.util')
import os.path import os.path
from test.support import CleanImport from test.support import CleanImport
...@@ -52,6 +50,8 @@ class LegacyLoader(TestLoader): ...@@ -52,6 +50,8 @@ class LegacyLoader(TestLoader):
with warnings.catch_warnings(): with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning) warnings.simplefilter("ignore", DeprecationWarning)
frozen_util = util['Frozen']
@frozen_util.module_for_loader @frozen_util.module_for_loader
def load_module(self, module): def load_module(self, module):
module.ham = self.HAM module.ham = self.HAM
...@@ -221,18 +221,17 @@ class ModuleSpecTests: ...@@ -221,18 +221,17 @@ class ModuleSpecTests:
self.assertEqual(self.loc_spec.cached, 'spam.pyc') self.assertEqual(self.loc_spec.cached, 'spam.pyc')
class Frozen_ModuleSpecTests(ModuleSpecTests, unittest.TestCase): (Frozen_ModuleSpecTests,
util = frozen_util Source_ModuleSpecTests
machinery = frozen_machinery ) = test_util.test_both(ModuleSpecTests, util=util, machinery=machinery)
class Source_ModuleSpecTests(ModuleSpecTests, unittest.TestCase):
util = source_util
machinery = source_machinery
class ModuleSpecMethodsTests: class ModuleSpecMethodsTests:
@property
def bootstrap(self):
return self.init._bootstrap
def setUp(self): def setUp(self):
self.name = 'spam' self.name = 'spam'
self.path = 'spam.py' self.path = 'spam.py'
...@@ -528,20 +527,18 @@ class ModuleSpecMethodsTests: ...@@ -528,20 +527,18 @@ class ModuleSpecMethodsTests:
self.assertIs(installed, loaded) self.assertIs(installed, loaded)
class Frozen_ModuleSpecMethodsTests(ModuleSpecMethodsTests, unittest.TestCase): (Frozen_ModuleSpecMethodsTests,
bootstrap = frozen_bootstrap Source_ModuleSpecMethodsTests
machinery = frozen_machinery ) = test_util.test_both(ModuleSpecMethodsTests, init=init, util=util,
util = frozen_util machinery=machinery)
class Source_ModuleSpecMethodsTests(ModuleSpecMethodsTests, unittest.TestCase):
bootstrap = source_bootstrap
machinery = source_machinery
util = source_util
class ModuleReprTests: class ModuleReprTests:
@property
def bootstrap(self):
return self.init._bootstrap
def setUp(self): def setUp(self):
self.module = type(os)('spam') self.module = type(os)('spam')
self.spec = self.machinery.ModuleSpec('spam', TestLoader()) self.spec = self.machinery.ModuleSpec('spam', TestLoader())
...@@ -625,16 +622,10 @@ class ModuleReprTests: ...@@ -625,16 +622,10 @@ class ModuleReprTests:
self.assertEqual(modrepr, '<module {!r}>'.format('spam')) self.assertEqual(modrepr, '<module {!r}>'.format('spam'))
class Frozen_ModuleReprTests(ModuleReprTests, unittest.TestCase): (Frozen_ModuleReprTests,
bootstrap = frozen_bootstrap Source_ModuleReprTests
machinery = frozen_machinery ) = test_util.test_both(ModuleReprTests, init=init, util=util,
util = frozen_util machinery=machinery)
class Source_ModuleReprTests(ModuleReprTests, unittest.TestCase):
bootstrap = source_bootstrap
machinery = source_machinery
util = source_util
class FactoryTests: class FactoryTests:
...@@ -787,7 +778,7 @@ class FactoryTests: ...@@ -787,7 +778,7 @@ class FactoryTests:
# spec_from_file_location() # spec_from_file_location()
def test_spec_from_file_location_default(self): def test_spec_from_file_location_default(self):
if self.machinery is source_machinery: if self.machinery is machinery['Source']:
raise unittest.SkipTest('not sure why this is breaking...') raise unittest.SkipTest('not sure why this is breaking...')
spec = self.util.spec_from_file_location(self.name, self.path) spec = self.util.spec_from_file_location(self.name, self.path)
...@@ -947,11 +938,6 @@ class FactoryTests: ...@@ -947,11 +938,6 @@ class FactoryTests:
self.assertTrue(spec.has_location) self.assertTrue(spec.has_location)
class Frozen_FactoryTests(FactoryTests, unittest.TestCase): (Frozen_FactoryTests,
util = frozen_util Source_FactoryTests
machinery = frozen_machinery ) = test_util.test_both(FactoryTests, util=util, machinery=machinery)
class Source_FactoryTests(FactoryTests, unittest.TestCase):
util = source_util
machinery = source_machinery
from importlib import util import importlib.util
from . import util as test_util from . import util as test_util
frozen_init, source_init = test_util.import_importlib('importlib') init = test_util.import_importlib('importlib')
frozen_machinery, source_machinery = test_util.import_importlib('importlib.machinery') machinery = test_util.import_importlib('importlib.machinery')
frozen_util, source_util = test_util.import_importlib('importlib.util') util = test_util.import_importlib('importlib.util')
import os import os
import sys import sys
...@@ -32,8 +32,10 @@ class DecodeSourceBytesTests: ...@@ -32,8 +32,10 @@ class DecodeSourceBytesTests:
self.assertEqual(self.util.decode_source(source_bytes), self.assertEqual(self.util.decode_source(source_bytes),
'\n'.join([self.source, self.source])) '\n'.join([self.source, self.source]))
Frozen_DecodeSourceBytesTests, Source_DecodeSourceBytesTests = test_util.test_both(
DecodeSourceBytesTests, util=[frozen_util, source_util]) (Frozen_DecodeSourceBytesTests,
Source_DecodeSourceBytesTests
) = test_util.test_both(DecodeSourceBytesTests, util=util)
class ModuleForLoaderTests: class ModuleForLoaderTests:
...@@ -161,8 +163,10 @@ class ModuleForLoaderTests: ...@@ -161,8 +163,10 @@ class ModuleForLoaderTests:
self.assertIs(module.__loader__, loader) self.assertIs(module.__loader__, loader)
self.assertEqual(module.__package__, name) self.assertEqual(module.__package__, name)
Frozen_ModuleForLoaderTests, Source_ModuleForLoaderTests = test_util.test_both(
ModuleForLoaderTests, util=[frozen_util, source_util]) (Frozen_ModuleForLoaderTests,
Source_ModuleForLoaderTests
) = test_util.test_both(ModuleForLoaderTests, util=util)
class SetPackageTests: class SetPackageTests:
...@@ -222,18 +226,25 @@ class SetPackageTests: ...@@ -222,18 +226,25 @@ class SetPackageTests:
self.assertEqual(wrapped.__name__, fxn.__name__) self.assertEqual(wrapped.__name__, fxn.__name__)
self.assertEqual(wrapped.__qualname__, fxn.__qualname__) self.assertEqual(wrapped.__qualname__, fxn.__qualname__)
Frozen_SetPackageTests, Source_SetPackageTests = test_util.test_both(
SetPackageTests, util=[frozen_util, source_util]) (Frozen_SetPackageTests,
Source_SetPackageTests
) = test_util.test_both(SetPackageTests, util=util)
class SetLoaderTests: class SetLoaderTests:
"""Tests importlib.util.set_loader().""" """Tests importlib.util.set_loader()."""
@property
def DummyLoader(self):
# Set DummyLoader on the class lazily.
class DummyLoader: class DummyLoader:
@util.set_loader @self.util.set_loader
def load_module(self, module): def load_module(self, module):
return self.module return self.module
self.__class__.DummyLoader = DummyLoader
return DummyLoader
def test_no_attribute(self): def test_no_attribute(self):
loader = self.DummyLoader() loader = self.DummyLoader()
...@@ -262,17 +273,10 @@ class SetLoaderTests: ...@@ -262,17 +273,10 @@ class SetLoaderTests:
warnings.simplefilter('ignore', DeprecationWarning) warnings.simplefilter('ignore', DeprecationWarning)
self.assertEqual(42, loader.load_module('blah').__loader__) self.assertEqual(42, loader.load_module('blah').__loader__)
class Frozen_SetLoaderTests(SetLoaderTests, unittest.TestCase):
class DummyLoader:
@frozen_util.set_loader
def load_module(self, module):
return self.module
class Source_SetLoaderTests(SetLoaderTests, unittest.TestCase): (Frozen_SetLoaderTests,
class DummyLoader: Source_SetLoaderTests
@source_util.set_loader ) = test_util.test_both(SetLoaderTests, util=util)
def load_module(self, module):
return self.module
class ResolveNameTests: class ResolveNameTests:
...@@ -307,9 +311,10 @@ class ResolveNameTests: ...@@ -307,9 +311,10 @@ class ResolveNameTests:
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
self.util.resolve_name('..bacon', 'spam') self.util.resolve_name('..bacon', 'spam')
Frozen_ResolveNameTests, Source_ResolveNameTests = test_util.test_both(
ResolveNameTests, (Frozen_ResolveNameTests,
util=[frozen_util, source_util]) Source_ResolveNameTests
) = test_util.test_both(ResolveNameTests, util=util)
class FindSpecTests: class FindSpecTests:
...@@ -446,15 +451,10 @@ class FindSpecTests: ...@@ -446,15 +451,10 @@ class FindSpecTests:
self.assertNotIn(fullname, sorted(sys.modules)) self.assertNotIn(fullname, sorted(sys.modules))
class Frozen_FindSpecTests(FindSpecTests, unittest.TestCase): (Frozen_FindSpecTests,
init = frozen_init Source_FindSpecTests
machinery = frozen_machinery ) = test_util.test_both(FindSpecTests, init=init, util=util,
util = frozen_util machinery=machinery)
class Source_FindSpecTests(FindSpecTests, unittest.TestCase):
init = source_init
machinery = source_machinery
util = source_util
class MagicNumberTests: class MagicNumberTests:
...@@ -467,8 +467,10 @@ class MagicNumberTests: ...@@ -467,8 +467,10 @@ class MagicNumberTests:
# The magic number uses \r\n to come out wrong when splitting on lines. # The magic number uses \r\n to come out wrong when splitting on lines.
self.assertTrue(self.util.MAGIC_NUMBER.endswith(b'\r\n')) self.assertTrue(self.util.MAGIC_NUMBER.endswith(b'\r\n'))
Frozen_MagicNumberTests, Source_MagicNumberTests = test_util.test_both(
MagicNumberTests, util=[frozen_util, source_util]) (Frozen_MagicNumberTests,
Source_MagicNumberTests
) = test_util.test_both(MagicNumberTests, util=util)
class PEP3147Tests: class PEP3147Tests:
...@@ -583,9 +585,10 @@ class PEP3147Tests: ...@@ -583,9 +585,10 @@ class PEP3147Tests:
ValueError, self.util.source_from_cache, ValueError, self.util.source_from_cache,
'/foo/bar/foo.cpython-32.foo.pyc') '/foo/bar/foo.cpython-32.foo.pyc')
Frozen_PEP3147Tests, Source_PEP3147Tests = test_util.test_both(
PEP3147Tests, (Frozen_PEP3147Tests,
util=[frozen_util, source_util]) Source_PEP3147Tests
) = test_util.test_both(PEP3147Tests, util=util)
if __name__ == '__main__': if __name__ == '__main__':
......
from . import util from . import util as test_util
frozen_machinery, source_machinery = util.import_importlib('importlib.machinery') machinery = test_util.import_importlib('importlib.machinery')
import sys import sys
import unittest import unittest
...@@ -19,11 +19,6 @@ class WindowsRegistryFinderTests: ...@@ -19,11 +19,6 @@ class WindowsRegistryFinderTests:
self.assertIs(loader, None) self.assertIs(loader, None)
class Frozen_WindowsRegistryFinderTests(WindowsRegistryFinderTests, (Frozen_WindowsRegistryFinderTests,
unittest.TestCase): Source_WindowsRegistryFinderTests
machinery = frozen_machinery ) = test_util.test_both(WindowsRegistryFinderTests, machinery=machinery)
class Source_WindowsRegistryFinderTests(WindowsRegistryFinderTests,
unittest.TestCase):
machinery = source_machinery
...@@ -50,19 +50,36 @@ def import_importlib(module_name): ...@@ -50,19 +50,36 @@ def import_importlib(module_name):
frozen = support.import_fresh_module(module_name) frozen = support.import_fresh_module(module_name)
source = support.import_fresh_module(module_name, fresh=fresh, source = support.import_fresh_module(module_name, fresh=fresh,
blocked=('_frozen_importlib',)) blocked=('_frozen_importlib',))
return {'Frozen': frozen, 'Source': source}
def specialize_class(cls, kind, base=None, **kwargs):
# XXX Support passing in submodule names--load (and cache) them?
# That would clean up the test modules a bit more.
if base is None:
base = unittest.TestCase
elif not isinstance(base, type):
base = base[kind]
name = '{}_{}'.format(kind, cls.__name__)
bases = (cls, base)
specialized = types.new_class(name, bases)
specialized.__module__ = cls.__module__
specialized._NAME = cls.__name__
specialized._KIND = kind
for attr, values in kwargs.items():
value = values[kind]
setattr(specialized, attr, value)
return specialized
def split_frozen(cls, base=None, **kwargs):
frozen = specialize_class(cls, 'Frozen', base, **kwargs)
source = specialize_class(cls, 'Source', base, **kwargs)
return frozen, source return frozen, source
def test_both(test_class, **kwargs): def test_both(test_class, base=None, **kwargs):
frozen_tests = types.new_class('Frozen_'+test_class.__name__, return split_frozen(test_class, base, **kwargs)
(test_class, unittest.TestCase))
source_tests = types.new_class('Source_'+test_class.__name__,
(test_class, unittest.TestCase))
frozen_tests.__module__ = source_tests.__module__ = test_class.__module__
for attr, (frozen_value, source_value) in kwargs.items():
setattr(frozen_tests, attr, frozen_value)
setattr(source_tests, attr, source_value)
return frozen_tests, source_tests
CASE_INSENSITIVE_FS = True CASE_INSENSITIVE_FS = True
...@@ -75,8 +92,9 @@ if sys.platform not in ('win32', 'cygwin'): ...@@ -75,8 +92,9 @@ if sys.platform not in ('win32', 'cygwin'):
if not os.path.exists(changed_name): if not os.path.exists(changed_name):
CASE_INSENSITIVE_FS = False CASE_INSENSITIVE_FS = False
_, source_importlib = import_importlib('importlib') source_importlib = import_importlib('importlib')['Source']
__import__ = staticmethod(builtins.__import__), staticmethod(source_importlib.__import__) __import__ = {'Frozen': staticmethod(builtins.__import__),
'Source': staticmethod(source_importlib.__import__)}
def case_insensitive_tests(test): def case_insensitive_tests(test):
......
...@@ -496,6 +496,8 @@ Tests ...@@ -496,6 +496,8 @@ Tests
- Issue #21097: Move test_namespace_pkgs into test_importlib. - Issue #21097: Move test_namespace_pkgs into test_importlib.
- Issue #21503: Use test_both() consistently in test_importlib.
- Issue #20939: Avoid various network test failures due to new - Issue #20939: Avoid various network test failures due to new
redirect of http://www.python.org/ to https://www.python.org: redirect of http://www.python.org/ to https://www.python.org:
use http://www.example.com instead. use http://www.example.com instead.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment