1from .. import abc 2from .. import util 3 4machinery = util.import_importlib('importlib.machinery') 5 6from test.support import captured_stdout 7import types 8import unittest 9import warnings 10 11 12class ExecModuleTests(abc.LoaderTests): 13 14 def exec_module(self, name): 15 with util.uncache(name), captured_stdout() as stdout: 16 spec = self.machinery.ModuleSpec( 17 name, self.machinery.FrozenImporter, origin='frozen', 18 is_package=self.machinery.FrozenImporter.is_package(name)) 19 module = types.ModuleType(name) 20 module.__spec__ = spec 21 assert not hasattr(module, 'initialized') 22 self.machinery.FrozenImporter.exec_module(module) 23 self.assertTrue(module.initialized) 24 self.assertTrue(hasattr(module, '__spec__')) 25 self.assertEqual(module.__spec__.origin, 'frozen') 26 return module, stdout.getvalue() 27 28 def test_module(self): 29 name = '__hello__' 30 module, output = self.exec_module(name) 31 check = {'__name__': name} 32 for attr, value in check.items(): 33 self.assertEqual(getattr(module, attr), value) 34 self.assertEqual(output, 'Hello world!\n') 35 self.assertTrue(hasattr(module, '__spec__')) 36 37 def test_package(self): 38 name = '__phello__' 39 module, output = self.exec_module(name) 40 check = {'__name__': name} 41 for attr, value in check.items(): 42 attr_value = getattr(module, attr) 43 self.assertEqual(attr_value, value, 44 'for {name}.{attr}, {given!r} != {expected!r}'.format( 45 name=name, attr=attr, given=attr_value, 46 expected=value)) 47 self.assertEqual(output, 'Hello world!\n') 48 49 def test_lacking_parent(self): 50 name = '__phello__.spam' 51 with util.uncache('__phello__'): 52 module, output = self.exec_module(name) 53 check = {'__name__': name} 54 for attr, value in check.items(): 55 attr_value = getattr(module, attr) 56 self.assertEqual(attr_value, value, 57 'for {name}.{attr}, {given} != {expected!r}'.format( 58 name=name, attr=attr, given=attr_value, 59 expected=value)) 60 self.assertEqual(output, 'Hello world!\n') 61 62 def test_module_repr(self): 63 name = '__hello__' 64 module, output = self.exec_module(name) 65 with warnings.catch_warnings(): 66 warnings.simplefilter('ignore', DeprecationWarning) 67 repr_str = self.machinery.FrozenImporter.module_repr(module) 68 self.assertEqual(repr_str, 69 "<module '__hello__' (frozen)>") 70 71 def test_module_repr_indirect(self): 72 name = '__hello__' 73 module, output = self.exec_module(name) 74 self.assertEqual(repr(module), 75 "<module '__hello__' (frozen)>") 76 77 # No way to trigger an error in a frozen module. 78 test_state_after_failure = None 79 80 def test_unloadable(self): 81 assert self.machinery.FrozenImporter.find_spec('_not_real') is None 82 with self.assertRaises(ImportError) as cm: 83 self.exec_module('_not_real') 84 self.assertEqual(cm.exception.name, '_not_real') 85 86 87(Frozen_ExecModuleTests, 88 Source_ExecModuleTests 89 ) = util.test_both(ExecModuleTests, machinery=machinery) 90 91 92class LoaderTests(abc.LoaderTests): 93 94 def test_module(self): 95 with util.uncache('__hello__'), captured_stdout() as stdout: 96 with warnings.catch_warnings(): 97 warnings.simplefilter('ignore', DeprecationWarning) 98 module = self.machinery.FrozenImporter.load_module('__hello__') 99 check = {'__name__': '__hello__', 100 '__package__': '', 101 '__loader__': self.machinery.FrozenImporter, 102 } 103 for attr, value in check.items(): 104 self.assertEqual(getattr(module, attr), value) 105 self.assertEqual(stdout.getvalue(), 'Hello world!\n') 106 self.assertFalse(hasattr(module, '__file__')) 107 108 def test_package(self): 109 with util.uncache('__phello__'), captured_stdout() as stdout: 110 with warnings.catch_warnings(): 111 warnings.simplefilter('ignore', DeprecationWarning) 112 module = self.machinery.FrozenImporter.load_module('__phello__') 113 check = {'__name__': '__phello__', 114 '__package__': '__phello__', 115 '__path__': [], 116 '__loader__': self.machinery.FrozenImporter, 117 } 118 for attr, value in check.items(): 119 attr_value = getattr(module, attr) 120 self.assertEqual(attr_value, value, 121 "for __phello__.%s, %r != %r" % 122 (attr, attr_value, value)) 123 self.assertEqual(stdout.getvalue(), 'Hello world!\n') 124 self.assertFalse(hasattr(module, '__file__')) 125 126 def test_lacking_parent(self): 127 with util.uncache('__phello__', '__phello__.spam'), \ 128 captured_stdout() as stdout: 129 with warnings.catch_warnings(): 130 warnings.simplefilter('ignore', DeprecationWarning) 131 module = self.machinery.FrozenImporter.load_module('__phello__.spam') 132 check = {'__name__': '__phello__.spam', 133 '__package__': '__phello__', 134 '__loader__': self.machinery.FrozenImporter, 135 } 136 for attr, value in check.items(): 137 attr_value = getattr(module, attr) 138 self.assertEqual(attr_value, value, 139 "for __phello__.spam.%s, %r != %r" % 140 (attr, attr_value, value)) 141 self.assertEqual(stdout.getvalue(), 'Hello world!\n') 142 self.assertFalse(hasattr(module, '__file__')) 143 144 def test_module_reuse(self): 145 with util.uncache('__hello__'), captured_stdout() as stdout: 146 with warnings.catch_warnings(): 147 warnings.simplefilter('ignore', DeprecationWarning) 148 module1 = self.machinery.FrozenImporter.load_module('__hello__') 149 module2 = self.machinery.FrozenImporter.load_module('__hello__') 150 self.assertIs(module1, module2) 151 self.assertEqual(stdout.getvalue(), 152 'Hello world!\nHello world!\n') 153 154 def test_module_repr(self): 155 with util.uncache('__hello__'), captured_stdout(): 156 with warnings.catch_warnings(): 157 warnings.simplefilter('ignore', DeprecationWarning) 158 module = self.machinery.FrozenImporter.load_module('__hello__') 159 repr_str = self.machinery.FrozenImporter.module_repr(module) 160 self.assertEqual(repr_str, 161 "<module '__hello__' (frozen)>") 162 163 # No way to trigger an error in a frozen module. 164 test_state_after_failure = None 165 166 def test_unloadable(self): 167 with warnings.catch_warnings(): 168 warnings.simplefilter("ignore", DeprecationWarning) 169 assert self.machinery.FrozenImporter.find_module('_not_real') is None 170 with self.assertRaises(ImportError) as cm: 171 self.machinery.FrozenImporter.load_module('_not_real') 172 self.assertEqual(cm.exception.name, '_not_real') 173 174 175(Frozen_LoaderTests, 176 Source_LoaderTests 177 ) = util.test_both(LoaderTests, machinery=machinery) 178 179 180class InspectLoaderTests: 181 182 """Tests for the InspectLoader methods for FrozenImporter.""" 183 184 def test_get_code(self): 185 # Make sure that the code object is good. 186 name = '__hello__' 187 with captured_stdout() as stdout: 188 code = self.machinery.FrozenImporter.get_code(name) 189 mod = types.ModuleType(name) 190 exec(code, mod.__dict__) 191 self.assertTrue(hasattr(mod, 'initialized')) 192 self.assertEqual(stdout.getvalue(), 'Hello world!\n') 193 194 def test_get_source(self): 195 # Should always return None. 196 result = self.machinery.FrozenImporter.get_source('__hello__') 197 self.assertIsNone(result) 198 199 def test_is_package(self): 200 # Should be able to tell what is a package. 201 test_for = (('__hello__', False), ('__phello__', True), 202 ('__phello__.spam', False)) 203 for name, is_package in test_for: 204 result = self.machinery.FrozenImporter.is_package(name) 205 self.assertEqual(bool(result), is_package) 206 207 def test_failure(self): 208 # Raise ImportError for modules that are not frozen. 209 for meth_name in ('get_code', 'get_source', 'is_package'): 210 method = getattr(self.machinery.FrozenImporter, meth_name) 211 with self.assertRaises(ImportError) as cm: 212 method('importlib') 213 self.assertEqual(cm.exception.name, 'importlib') 214 215(Frozen_ILTests, 216 Source_ILTests 217 ) = util.test_both(InspectLoaderTests, machinery=machinery) 218 219 220if __name__ == '__main__': 221 unittest.main() 222