1- import importlib
2- from importlib import machinery
31from .. import abc
42from .. import util
53from . import util as builtin_util
64
5+ frozen_machinery , source_machinery = util .import_importlib ('importlib.machinery' )
6+
77import sys
88import types
99import unittest
1010
1111
12- class LoaderTests (unittest . TestCase , abc .LoaderTests ):
12+ class LoaderTests (abc .LoaderTests ):
1313
1414 """Test load_module() for built-in modules."""
1515
16- verification = {'__name__' : 'errno' , '__package__' : '' ,
17- '__loader__' : machinery .BuiltinImporter }
16+ def setUp (self ):
17+ self .verification = {'__name__' : 'errno' , '__package__' : '' ,
18+ '__loader__' : self .machinery .BuiltinImporter }
1819
1920 def verify (self , module ):
2021 """Verify that the module matches against what it should have."""
@@ -23,8 +24,8 @@ def verify(self, module):
2324 self .assertEqual (getattr (module , attr ), value )
2425 self .assertIn (module .__name__ , sys .modules )
2526
26- load_module = staticmethod ( lambda name :
27- machinery .BuiltinImporter .load_module (name ) )
27+ def load_module ( self , name ) :
28+ return self . machinery .BuiltinImporter .load_module (name )
2829
2930 def test_module (self ):
3031 # Common case.
@@ -61,45 +62,47 @@ def test_unloadable(self):
6162 def test_already_imported (self ):
6263 # Using the name of a module already imported but not a built-in should
6364 # still fail.
64- assert hasattr (importlib , '__file__' ) # Not a built-in.
65+ assert hasattr (unittest , '__file__' ) # Not a built-in.
6566 with self .assertRaises (ImportError ) as cm :
66- self .load_module ('importlib' )
67- self .assertEqual (cm .exception .name , 'importlib' )
67+ self .load_module ('unittest' )
68+ self .assertEqual (cm .exception .name , 'unittest' )
69+
70+
71+ Frozen_LoaderTests , Source_LoaderTests = util .test_both (LoaderTests ,
72+ machinery = [frozen_machinery , source_machinery ])
6873
6974
70- class InspectLoaderTests ( unittest . TestCase ) :
75+ class InspectLoaderTests :
7176
7277 """Tests for InspectLoader methods for BuiltinImporter."""
7378
7479 def test_get_code (self ):
7580 # There is no code object.
76- result = machinery .BuiltinImporter .get_code (builtin_util .NAME )
81+ result = self . machinery .BuiltinImporter .get_code (builtin_util .NAME )
7782 self .assertIsNone (result )
7883
7984 def test_get_source (self ):
8085 # There is no source.
81- result = machinery .BuiltinImporter .get_source (builtin_util .NAME )
86+ result = self . machinery .BuiltinImporter .get_source (builtin_util .NAME )
8287 self .assertIsNone (result )
8388
8489 def test_is_package (self ):
8590 # Cannot be a package.
86- result = machinery .BuiltinImporter .is_package (builtin_util .NAME )
91+ result = self . machinery .BuiltinImporter .is_package (builtin_util .NAME )
8792 self .assertTrue (not result )
8893
8994 def test_not_builtin (self ):
9095 # Modules not built-in should raise ImportError.
9196 for meth_name in ('get_code' , 'get_source' , 'is_package' ):
92- method = getattr (machinery .BuiltinImporter , meth_name )
97+ method = getattr (self . machinery .BuiltinImporter , meth_name )
9398 with self .assertRaises (ImportError ) as cm :
9499 method (builtin_util .BAD_NAME )
95100 self .assertRaises (builtin_util .BAD_NAME )
96101
97-
98-
99- def test_main ():
100- from test .support import run_unittest
101- run_unittest (LoaderTests , InspectLoaderTests )
102+ Frozen_InspectLoaderTests , Source_InspectLoaderTests = util .test_both (
103+ InspectLoaderTests ,
104+ machinery = [frozen_machinery , source_machinery ])
102105
103106
104107if __name__ == '__main__' :
105- test_main ()
108+ unittest . main ()
0 commit comments