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