1
- import importlib
2
- from importlib import machinery
3
1
from .. import abc
4
2
from .. import util
5
3
from . import util as builtin_util
6
4
5
+ frozen_machinery , source_machinery = util .import_importlib ('importlib.machinery' )
6
+
7
7
import sys
8
8
import types
9
9
import unittest
10
10
11
11
12
- class LoaderTests (unittest . TestCase , abc .LoaderTests ):
12
+ class LoaderTests (abc .LoaderTests ):
13
13
14
14
"""Test load_module() for built-in modules."""
15
15
16
- verification = {'__name__' : 'errno' , '__package__' : '' ,
17
- '__loader__' : machinery .BuiltinImporter }
16
+ def setUp (self ):
17
+ self .verification = {'__name__' : 'errno' , '__package__' : '' ,
18
+ '__loader__' : self .machinery .BuiltinImporter }
18
19
19
20
def verify (self , module ):
20
21
"""Verify that the module matches against what it should have."""
@@ -23,8 +24,8 @@ def verify(self, module):
23
24
self .assertEqual (getattr (module , attr ), value )
24
25
self .assertIn (module .__name__ , sys .modules )
25
26
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 )
28
29
29
30
def test_module (self ):
30
31
# Common case.
@@ -61,45 +62,47 @@ def test_unloadable(self):
61
62
def test_already_imported (self ):
62
63
# Using the name of a module already imported but not a built-in should
63
64
# still fail.
64
- assert hasattr (importlib , '__file__' ) # Not a built-in.
65
+ assert hasattr (unittest , '__file__' ) # Not a built-in.
65
66
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 ])
68
73
69
74
70
- class InspectLoaderTests ( unittest . TestCase ) :
75
+ class InspectLoaderTests :
71
76
72
77
"""Tests for InspectLoader methods for BuiltinImporter."""
73
78
74
79
def test_get_code (self ):
75
80
# There is no code object.
76
- result = machinery .BuiltinImporter .get_code (builtin_util .NAME )
81
+ result = self . machinery .BuiltinImporter .get_code (builtin_util .NAME )
77
82
self .assertIsNone (result )
78
83
79
84
def test_get_source (self ):
80
85
# There is no source.
81
- result = machinery .BuiltinImporter .get_source (builtin_util .NAME )
86
+ result = self . machinery .BuiltinImporter .get_source (builtin_util .NAME )
82
87
self .assertIsNone (result )
83
88
84
89
def test_is_package (self ):
85
90
# Cannot be a package.
86
- result = machinery .BuiltinImporter .is_package (builtin_util .NAME )
91
+ result = self . machinery .BuiltinImporter .is_package (builtin_util .NAME )
87
92
self .assertTrue (not result )
88
93
89
94
def test_not_builtin (self ):
90
95
# Modules not built-in should raise ImportError.
91
96
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 )
93
98
with self .assertRaises (ImportError ) as cm :
94
99
method (builtin_util .BAD_NAME )
95
100
self .assertRaises (builtin_util .BAD_NAME )
96
101
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 ])
102
105
103
106
104
107
if __name__ == '__main__' :
105
- test_main ()
108
+ unittest . main ()
0 commit comments