| 1 | n/a | from .. import abc | 
|---|
| 2 | n/a | from .. import util | 
|---|
| 3 | n/a |  | 
|---|
| 4 | n/a | importlib = util.import_importlib('importlib') | 
|---|
| 5 | n/a | importlib_abc = util.import_importlib('importlib.abc') | 
|---|
| 6 | n/a | machinery = util.import_importlib('importlib.machinery') | 
|---|
| 7 | n/a | importlib_util = util.import_importlib('importlib.util') | 
|---|
| 8 | n/a |  | 
|---|
| 9 | n/a | import errno | 
|---|
| 10 | n/a | import marshal | 
|---|
| 11 | n/a | import os | 
|---|
| 12 | n/a | import py_compile | 
|---|
| 13 | n/a | import shutil | 
|---|
| 14 | n/a | import stat | 
|---|
| 15 | n/a | import sys | 
|---|
| 16 | n/a | import types | 
|---|
| 17 | n/a | import unittest | 
|---|
| 18 | n/a | import warnings | 
|---|
| 19 | n/a |  | 
|---|
| 20 | n/a | from test.support import make_legacy_pyc, unload | 
|---|
| 21 | n/a |  | 
|---|
| 22 | n/a |  | 
|---|
| 23 | n/a | class SimpleTest(abc.LoaderTests): | 
|---|
| 24 | n/a |  | 
|---|
| 25 | n/a | """Should have no issue importing a source module [basic]. And if there is | 
|---|
| 26 | n/a | a syntax error, it should raise a SyntaxError [syntax error]. | 
|---|
| 27 | n/a |  | 
|---|
| 28 | n/a | """ | 
|---|
| 29 | n/a |  | 
|---|
| 30 | n/a | def setUp(self): | 
|---|
| 31 | n/a | self.name = 'spam' | 
|---|
| 32 | n/a | self.filepath = os.path.join('ham', self.name + '.py') | 
|---|
| 33 | n/a | self.loader = self.machinery.SourceFileLoader(self.name, self.filepath) | 
|---|
| 34 | n/a |  | 
|---|
| 35 | n/a | def test_load_module_API(self): | 
|---|
| 36 | n/a | class Tester(self.abc.FileLoader): | 
|---|
| 37 | n/a | def get_source(self, _): return 'attr = 42' | 
|---|
| 38 | n/a | def is_package(self, _): return False | 
|---|
| 39 | n/a |  | 
|---|
| 40 | n/a | loader = Tester('blah', 'blah.py') | 
|---|
| 41 | n/a | self.addCleanup(unload, 'blah') | 
|---|
| 42 | n/a | with warnings.catch_warnings(): | 
|---|
| 43 | n/a | warnings.simplefilter('ignore', DeprecationWarning) | 
|---|
| 44 | n/a | module = loader.load_module()  # Should not raise an exception. | 
|---|
| 45 | n/a |  | 
|---|
| 46 | n/a | def test_get_filename_API(self): | 
|---|
| 47 | n/a | # If fullname is not set then assume self.path is desired. | 
|---|
| 48 | n/a | class Tester(self.abc.FileLoader): | 
|---|
| 49 | n/a | def get_code(self, _): pass | 
|---|
| 50 | n/a | def get_source(self, _): pass | 
|---|
| 51 | n/a | def is_package(self, _): pass | 
|---|
| 52 | n/a | def module_repr(self, _): pass | 
|---|
| 53 | n/a |  | 
|---|
| 54 | n/a | path = 'some_path' | 
|---|
| 55 | n/a | name = 'some_name' | 
|---|
| 56 | n/a | loader = Tester(name, path) | 
|---|
| 57 | n/a | self.assertEqual(path, loader.get_filename(name)) | 
|---|
| 58 | n/a | self.assertEqual(path, loader.get_filename()) | 
|---|
| 59 | n/a | self.assertEqual(path, loader.get_filename(None)) | 
|---|
| 60 | n/a | with self.assertRaises(ImportError): | 
|---|
| 61 | n/a | loader.get_filename(name + 'XXX') | 
|---|
| 62 | n/a |  | 
|---|
| 63 | n/a | def test_equality(self): | 
|---|
| 64 | n/a | other = self.machinery.SourceFileLoader(self.name, self.filepath) | 
|---|
| 65 | n/a | self.assertEqual(self.loader, other) | 
|---|
| 66 | n/a |  | 
|---|
| 67 | n/a | def test_inequality(self): | 
|---|
| 68 | n/a | other = self.machinery.SourceFileLoader('_' + self.name, self.filepath) | 
|---|
| 69 | n/a | self.assertNotEqual(self.loader, other) | 
|---|
| 70 | n/a |  | 
|---|
| 71 | n/a | # [basic] | 
|---|
| 72 | n/a | def test_module(self): | 
|---|
| 73 | n/a | with util.create_modules('_temp') as mapping: | 
|---|
| 74 | n/a | loader = self.machinery.SourceFileLoader('_temp', mapping['_temp']) | 
|---|
| 75 | n/a | with warnings.catch_warnings(): | 
|---|
| 76 | n/a | warnings.simplefilter('ignore', DeprecationWarning) | 
|---|
| 77 | n/a | module = loader.load_module('_temp') | 
|---|
| 78 | n/a | self.assertIn('_temp', sys.modules) | 
|---|
| 79 | n/a | check = {'__name__': '_temp', '__file__': mapping['_temp'], | 
|---|
| 80 | n/a | '__package__': ''} | 
|---|
| 81 | n/a | for attr, value in check.items(): | 
|---|
| 82 | n/a | self.assertEqual(getattr(module, attr), value) | 
|---|
| 83 | n/a |  | 
|---|
| 84 | n/a | def test_package(self): | 
|---|
| 85 | n/a | with util.create_modules('_pkg.__init__') as mapping: | 
|---|
| 86 | n/a | loader = self.machinery.SourceFileLoader('_pkg', | 
|---|
| 87 | n/a | mapping['_pkg.__init__']) | 
|---|
| 88 | n/a | with warnings.catch_warnings(): | 
|---|
| 89 | n/a | warnings.simplefilter('ignore', DeprecationWarning) | 
|---|
| 90 | n/a | module = loader.load_module('_pkg') | 
|---|
| 91 | n/a | self.assertIn('_pkg', sys.modules) | 
|---|
| 92 | n/a | check = {'__name__': '_pkg', '__file__': mapping['_pkg.__init__'], | 
|---|
| 93 | n/a | '__path__': [os.path.dirname(mapping['_pkg.__init__'])], | 
|---|
| 94 | n/a | '__package__': '_pkg'} | 
|---|
| 95 | n/a | for attr, value in check.items(): | 
|---|
| 96 | n/a | self.assertEqual(getattr(module, attr), value) | 
|---|
| 97 | n/a |  | 
|---|
| 98 | n/a |  | 
|---|
| 99 | n/a | def test_lacking_parent(self): | 
|---|
| 100 | n/a | with util.create_modules('_pkg.__init__', '_pkg.mod')as mapping: | 
|---|
| 101 | n/a | loader = self.machinery.SourceFileLoader('_pkg.mod', | 
|---|
| 102 | n/a | mapping['_pkg.mod']) | 
|---|
| 103 | n/a | with warnings.catch_warnings(): | 
|---|
| 104 | n/a | warnings.simplefilter('ignore', DeprecationWarning) | 
|---|
| 105 | n/a | module = loader.load_module('_pkg.mod') | 
|---|
| 106 | n/a | self.assertIn('_pkg.mod', sys.modules) | 
|---|
| 107 | n/a | check = {'__name__': '_pkg.mod', '__file__': mapping['_pkg.mod'], | 
|---|
| 108 | n/a | '__package__': '_pkg'} | 
|---|
| 109 | n/a | for attr, value in check.items(): | 
|---|
| 110 | n/a | self.assertEqual(getattr(module, attr), value) | 
|---|
| 111 | n/a |  | 
|---|
| 112 | n/a | def fake_mtime(self, fxn): | 
|---|
| 113 | n/a | """Fake mtime to always be higher than expected.""" | 
|---|
| 114 | n/a | return lambda name: fxn(name) + 1 | 
|---|
| 115 | n/a |  | 
|---|
| 116 | n/a | def test_module_reuse(self): | 
|---|
| 117 | n/a | with util.create_modules('_temp') as mapping: | 
|---|
| 118 | n/a | loader = self.machinery.SourceFileLoader('_temp', mapping['_temp']) | 
|---|
| 119 | n/a | with warnings.catch_warnings(): | 
|---|
| 120 | n/a | warnings.simplefilter('ignore', DeprecationWarning) | 
|---|
| 121 | n/a | module = loader.load_module('_temp') | 
|---|
| 122 | n/a | module_id = id(module) | 
|---|
| 123 | n/a | module_dict_id = id(module.__dict__) | 
|---|
| 124 | n/a | with open(mapping['_temp'], 'w') as file: | 
|---|
| 125 | n/a | file.write("testing_var = 42\n") | 
|---|
| 126 | n/a | with warnings.catch_warnings(): | 
|---|
| 127 | n/a | warnings.simplefilter('ignore', DeprecationWarning) | 
|---|
| 128 | n/a | module = loader.load_module('_temp') | 
|---|
| 129 | n/a | self.assertIn('testing_var', module.__dict__, | 
|---|
| 130 | n/a | "'testing_var' not in " | 
|---|
| 131 | n/a | "{0}".format(list(module.__dict__.keys()))) | 
|---|
| 132 | n/a | self.assertEqual(module, sys.modules['_temp']) | 
|---|
| 133 | n/a | self.assertEqual(id(module), module_id) | 
|---|
| 134 | n/a | self.assertEqual(id(module.__dict__), module_dict_id) | 
|---|
| 135 | n/a |  | 
|---|
| 136 | n/a | def test_state_after_failure(self): | 
|---|
| 137 | n/a | # A failed reload should leave the original module intact. | 
|---|
| 138 | n/a | attributes = ('__file__', '__path__', '__package__') | 
|---|
| 139 | n/a | value = '<test>' | 
|---|
| 140 | n/a | name = '_temp' | 
|---|
| 141 | n/a | with util.create_modules(name) as mapping: | 
|---|
| 142 | n/a | orig_module = types.ModuleType(name) | 
|---|
| 143 | n/a | for attr in attributes: | 
|---|
| 144 | n/a | setattr(orig_module, attr, value) | 
|---|
| 145 | n/a | with open(mapping[name], 'w') as file: | 
|---|
| 146 | n/a | file.write('+++ bad syntax +++') | 
|---|
| 147 | n/a | loader = self.machinery.SourceFileLoader('_temp', mapping['_temp']) | 
|---|
| 148 | n/a | with self.assertRaises(SyntaxError): | 
|---|
| 149 | n/a | loader.exec_module(orig_module) | 
|---|
| 150 | n/a | for attr in attributes: | 
|---|
| 151 | n/a | self.assertEqual(getattr(orig_module, attr), value) | 
|---|
| 152 | n/a | with self.assertRaises(SyntaxError): | 
|---|
| 153 | n/a | with warnings.catch_warnings(): | 
|---|
| 154 | n/a | warnings.simplefilter('ignore', DeprecationWarning) | 
|---|
| 155 | n/a | loader.load_module(name) | 
|---|
| 156 | n/a | for attr in attributes: | 
|---|
| 157 | n/a | self.assertEqual(getattr(orig_module, attr), value) | 
|---|
| 158 | n/a |  | 
|---|
| 159 | n/a | # [syntax error] | 
|---|
| 160 | n/a | def test_bad_syntax(self): | 
|---|
| 161 | n/a | with util.create_modules('_temp') as mapping: | 
|---|
| 162 | n/a | with open(mapping['_temp'], 'w') as file: | 
|---|
| 163 | n/a | file.write('=') | 
|---|
| 164 | n/a | loader = self.machinery.SourceFileLoader('_temp', mapping['_temp']) | 
|---|
| 165 | n/a | with self.assertRaises(SyntaxError): | 
|---|
| 166 | n/a | with warnings.catch_warnings(): | 
|---|
| 167 | n/a | warnings.simplefilter('ignore', DeprecationWarning) | 
|---|
| 168 | n/a | loader.load_module('_temp') | 
|---|
| 169 | n/a | self.assertNotIn('_temp', sys.modules) | 
|---|
| 170 | n/a |  | 
|---|
| 171 | n/a | def test_file_from_empty_string_dir(self): | 
|---|
| 172 | n/a | # Loading a module found from an empty string entry on sys.path should | 
|---|
| 173 | n/a | # not only work, but keep all attributes relative. | 
|---|
| 174 | n/a | file_path = '_temp.py' | 
|---|
| 175 | n/a | with open(file_path, 'w') as file: | 
|---|
| 176 | n/a | file.write("# test file for importlib") | 
|---|
| 177 | n/a | try: | 
|---|
| 178 | n/a | with util.uncache('_temp'): | 
|---|
| 179 | n/a | loader = self.machinery.SourceFileLoader('_temp', file_path) | 
|---|
| 180 | n/a | with warnings.catch_warnings(): | 
|---|
| 181 | n/a | warnings.simplefilter('ignore', DeprecationWarning) | 
|---|
| 182 | n/a | mod = loader.load_module('_temp') | 
|---|
| 183 | n/a | self.assertEqual(file_path, mod.__file__) | 
|---|
| 184 | n/a | self.assertEqual(self.util.cache_from_source(file_path), | 
|---|
| 185 | n/a | mod.__cached__) | 
|---|
| 186 | n/a | finally: | 
|---|
| 187 | n/a | os.unlink(file_path) | 
|---|
| 188 | n/a | pycache = os.path.dirname(self.util.cache_from_source(file_path)) | 
|---|
| 189 | n/a | if os.path.exists(pycache): | 
|---|
| 190 | n/a | shutil.rmtree(pycache) | 
|---|
| 191 | n/a |  | 
|---|
| 192 | n/a | @util.writes_bytecode_files | 
|---|
| 193 | n/a | def test_timestamp_overflow(self): | 
|---|
| 194 | n/a | # When a modification timestamp is larger than 2**32, it should be | 
|---|
| 195 | n/a | # truncated rather than raise an OverflowError. | 
|---|
| 196 | n/a | with util.create_modules('_temp') as mapping: | 
|---|
| 197 | n/a | source = mapping['_temp'] | 
|---|
| 198 | n/a | compiled = self.util.cache_from_source(source) | 
|---|
| 199 | n/a | with open(source, 'w') as f: | 
|---|
| 200 | n/a | f.write("x = 5") | 
|---|
| 201 | n/a | try: | 
|---|
| 202 | n/a | os.utime(source, (2 ** 33 - 5, 2 ** 33 - 5)) | 
|---|
| 203 | n/a | except OverflowError: | 
|---|
| 204 | n/a | self.skipTest("cannot set modification time to large integer") | 
|---|
| 205 | n/a | except OSError as e: | 
|---|
| 206 | n/a | if e.errno != getattr(errno, 'EOVERFLOW', None): | 
|---|
| 207 | n/a | raise | 
|---|
| 208 | n/a | self.skipTest("cannot set modification time to large integer ({})".format(e)) | 
|---|
| 209 | n/a | loader = self.machinery.SourceFileLoader('_temp', mapping['_temp']) | 
|---|
| 210 | n/a | # PEP 451 | 
|---|
| 211 | n/a | module = types.ModuleType('_temp') | 
|---|
| 212 | n/a | module.__spec__ = self.util.spec_from_loader('_temp', loader) | 
|---|
| 213 | n/a | loader.exec_module(module) | 
|---|
| 214 | n/a | self.assertEqual(module.x, 5) | 
|---|
| 215 | n/a | self.assertTrue(os.path.exists(compiled)) | 
|---|
| 216 | n/a | os.unlink(compiled) | 
|---|
| 217 | n/a | # PEP 302 | 
|---|
| 218 | n/a | with warnings.catch_warnings(): | 
|---|
| 219 | n/a | warnings.simplefilter('ignore', DeprecationWarning) | 
|---|
| 220 | n/a | mod = loader.load_module('_temp') | 
|---|
| 221 | n/a | # Sanity checks. | 
|---|
| 222 | n/a | self.assertEqual(mod.__cached__, compiled) | 
|---|
| 223 | n/a | self.assertEqual(mod.x, 5) | 
|---|
| 224 | n/a | # The pyc file was created. | 
|---|
| 225 | n/a | self.assertTrue(os.path.exists(compiled)) | 
|---|
| 226 | n/a |  | 
|---|
| 227 | n/a | def test_unloadable(self): | 
|---|
| 228 | n/a | loader = self.machinery.SourceFileLoader('good name', {}) | 
|---|
| 229 | n/a | module = types.ModuleType('bad name') | 
|---|
| 230 | n/a | module.__spec__ = self.machinery.ModuleSpec('bad name', loader) | 
|---|
| 231 | n/a | with self.assertRaises(ImportError): | 
|---|
| 232 | n/a | loader.exec_module(module) | 
|---|
| 233 | n/a | with self.assertRaises(ImportError): | 
|---|
| 234 | n/a | with warnings.catch_warnings(): | 
|---|
| 235 | n/a | warnings.simplefilter('ignore', DeprecationWarning) | 
|---|
| 236 | n/a | loader.load_module('bad name') | 
|---|
| 237 | n/a |  | 
|---|
| 238 | n/a |  | 
|---|
| 239 | n/a | (Frozen_SimpleTest, | 
|---|
| 240 | n/a | Source_SimpleTest | 
|---|
| 241 | n/a | ) = util.test_both(SimpleTest, importlib=importlib, machinery=machinery, | 
|---|
| 242 | n/a | abc=importlib_abc, util=importlib_util) | 
|---|
| 243 | n/a |  | 
|---|
| 244 | n/a |  | 
|---|
| 245 | n/a | class BadBytecodeTest: | 
|---|
| 246 | n/a |  | 
|---|
| 247 | n/a | def import_(self, file, module_name): | 
|---|
| 248 | n/a | raise NotImplementedError | 
|---|
| 249 | n/a |  | 
|---|
| 250 | n/a | def manipulate_bytecode(self, name, mapping, manipulator, *, | 
|---|
| 251 | n/a | del_source=False): | 
|---|
| 252 | n/a | """Manipulate the bytecode of a module by passing it into a callable | 
|---|
| 253 | n/a | that returns what to use as the new bytecode.""" | 
|---|
| 254 | n/a | try: | 
|---|
| 255 | n/a | del sys.modules['_temp'] | 
|---|
| 256 | n/a | except KeyError: | 
|---|
| 257 | n/a | pass | 
|---|
| 258 | n/a | py_compile.compile(mapping[name]) | 
|---|
| 259 | n/a | if not del_source: | 
|---|
| 260 | n/a | bytecode_path = self.util.cache_from_source(mapping[name]) | 
|---|
| 261 | n/a | else: | 
|---|
| 262 | n/a | os.unlink(mapping[name]) | 
|---|
| 263 | n/a | bytecode_path = make_legacy_pyc(mapping[name]) | 
|---|
| 264 | n/a | if manipulator: | 
|---|
| 265 | n/a | with open(bytecode_path, 'rb') as file: | 
|---|
| 266 | n/a | bc = file.read() | 
|---|
| 267 | n/a | new_bc = manipulator(bc) | 
|---|
| 268 | n/a | with open(bytecode_path, 'wb') as file: | 
|---|
| 269 | n/a | if new_bc is not None: | 
|---|
| 270 | n/a | file.write(new_bc) | 
|---|
| 271 | n/a | return bytecode_path | 
|---|
| 272 | n/a |  | 
|---|
| 273 | n/a | def _test_empty_file(self, test, *, del_source=False): | 
|---|
| 274 | n/a | with util.create_modules('_temp') as mapping: | 
|---|
| 275 | n/a | bc_path = self.manipulate_bytecode('_temp', mapping, | 
|---|
| 276 | n/a | lambda bc: b'', | 
|---|
| 277 | n/a | del_source=del_source) | 
|---|
| 278 | n/a | test('_temp', mapping, bc_path) | 
|---|
| 279 | n/a |  | 
|---|
| 280 | n/a | @util.writes_bytecode_files | 
|---|
| 281 | n/a | def _test_partial_magic(self, test, *, del_source=False): | 
|---|
| 282 | n/a | # When their are less than 4 bytes to a .pyc, regenerate it if | 
|---|
| 283 | n/a | # possible, else raise ImportError. | 
|---|
| 284 | n/a | with util.create_modules('_temp') as mapping: | 
|---|
| 285 | n/a | bc_path = self.manipulate_bytecode('_temp', mapping, | 
|---|
| 286 | n/a | lambda bc: bc[:3], | 
|---|
| 287 | n/a | del_source=del_source) | 
|---|
| 288 | n/a | test('_temp', mapping, bc_path) | 
|---|
| 289 | n/a |  | 
|---|
| 290 | n/a | def _test_magic_only(self, test, *, del_source=False): | 
|---|
| 291 | n/a | with util.create_modules('_temp') as mapping: | 
|---|
| 292 | n/a | bc_path = self.manipulate_bytecode('_temp', mapping, | 
|---|
| 293 | n/a | lambda bc: bc[:4], | 
|---|
| 294 | n/a | del_source=del_source) | 
|---|
| 295 | n/a | test('_temp', mapping, bc_path) | 
|---|
| 296 | n/a |  | 
|---|
| 297 | n/a | def _test_partial_timestamp(self, test, *, del_source=False): | 
|---|
| 298 | n/a | with util.create_modules('_temp') as mapping: | 
|---|
| 299 | n/a | bc_path = self.manipulate_bytecode('_temp', mapping, | 
|---|
| 300 | n/a | lambda bc: bc[:7], | 
|---|
| 301 | n/a | del_source=del_source) | 
|---|
| 302 | n/a | test('_temp', mapping, bc_path) | 
|---|
| 303 | n/a |  | 
|---|
| 304 | n/a | def _test_partial_size(self, test, *, del_source=False): | 
|---|
| 305 | n/a | with util.create_modules('_temp') as mapping: | 
|---|
| 306 | n/a | bc_path = self.manipulate_bytecode('_temp', mapping, | 
|---|
| 307 | n/a | lambda bc: bc[:11], | 
|---|
| 308 | n/a | del_source=del_source) | 
|---|
| 309 | n/a | test('_temp', mapping, bc_path) | 
|---|
| 310 | n/a |  | 
|---|
| 311 | n/a | def _test_no_marshal(self, *, del_source=False): | 
|---|
| 312 | n/a | with util.create_modules('_temp') as mapping: | 
|---|
| 313 | n/a | bc_path = self.manipulate_bytecode('_temp', mapping, | 
|---|
| 314 | n/a | lambda bc: bc[:12], | 
|---|
| 315 | n/a | del_source=del_source) | 
|---|
| 316 | n/a | file_path = mapping['_temp'] if not del_source else bc_path | 
|---|
| 317 | n/a | with self.assertRaises(EOFError): | 
|---|
| 318 | n/a | self.import_(file_path, '_temp') | 
|---|
| 319 | n/a |  | 
|---|
| 320 | n/a | def _test_non_code_marshal(self, *, del_source=False): | 
|---|
| 321 | n/a | with util.create_modules('_temp') as mapping: | 
|---|
| 322 | n/a | bytecode_path = self.manipulate_bytecode('_temp', mapping, | 
|---|
| 323 | n/a | lambda bc: bc[:12] + marshal.dumps(b'abcd'), | 
|---|
| 324 | n/a | del_source=del_source) | 
|---|
| 325 | n/a | file_path = mapping['_temp'] if not del_source else bytecode_path | 
|---|
| 326 | n/a | with self.assertRaises(ImportError) as cm: | 
|---|
| 327 | n/a | self.import_(file_path, '_temp') | 
|---|
| 328 | n/a | self.assertEqual(cm.exception.name, '_temp') | 
|---|
| 329 | n/a | self.assertEqual(cm.exception.path, bytecode_path) | 
|---|
| 330 | n/a |  | 
|---|
| 331 | n/a | def _test_bad_marshal(self, *, del_source=False): | 
|---|
| 332 | n/a | with util.create_modules('_temp') as mapping: | 
|---|
| 333 | n/a | bytecode_path = self.manipulate_bytecode('_temp', mapping, | 
|---|
| 334 | n/a | lambda bc: bc[:12] + b'<test>', | 
|---|
| 335 | n/a | del_source=del_source) | 
|---|
| 336 | n/a | file_path = mapping['_temp'] if not del_source else bytecode_path | 
|---|
| 337 | n/a | with self.assertRaises(EOFError): | 
|---|
| 338 | n/a | self.import_(file_path, '_temp') | 
|---|
| 339 | n/a |  | 
|---|
| 340 | n/a | def _test_bad_magic(self, test, *, del_source=False): | 
|---|
| 341 | n/a | with util.create_modules('_temp') as mapping: | 
|---|
| 342 | n/a | bc_path = self.manipulate_bytecode('_temp', mapping, | 
|---|
| 343 | n/a | lambda bc: b'\x00\x00\x00\x00' + bc[4:]) | 
|---|
| 344 | n/a | test('_temp', mapping, bc_path) | 
|---|
| 345 | n/a |  | 
|---|
| 346 | n/a |  | 
|---|
| 347 | n/a | class BadBytecodeTestPEP451(BadBytecodeTest): | 
|---|
| 348 | n/a |  | 
|---|
| 349 | n/a | def import_(self, file, module_name): | 
|---|
| 350 | n/a | loader = self.loader(module_name, file) | 
|---|
| 351 | n/a | module = types.ModuleType(module_name) | 
|---|
| 352 | n/a | module.__spec__ = self.util.spec_from_loader(module_name, loader) | 
|---|
| 353 | n/a | loader.exec_module(module) | 
|---|
| 354 | n/a |  | 
|---|
| 355 | n/a |  | 
|---|
| 356 | n/a | class BadBytecodeTestPEP302(BadBytecodeTest): | 
|---|
| 357 | n/a |  | 
|---|
| 358 | n/a | def import_(self, file, module_name): | 
|---|
| 359 | n/a | loader = self.loader(module_name, file) | 
|---|
| 360 | n/a | with warnings.catch_warnings(): | 
|---|
| 361 | n/a | warnings.simplefilter('ignore', DeprecationWarning) | 
|---|
| 362 | n/a | module = loader.load_module(module_name) | 
|---|
| 363 | n/a | self.assertIn(module_name, sys.modules) | 
|---|
| 364 | n/a |  | 
|---|
| 365 | n/a |  | 
|---|
| 366 | n/a | class SourceLoaderBadBytecodeTest: | 
|---|
| 367 | n/a |  | 
|---|
| 368 | n/a | @classmethod | 
|---|
| 369 | n/a | def setUpClass(cls): | 
|---|
| 370 | n/a | cls.loader = cls.machinery.SourceFileLoader | 
|---|
| 371 | n/a |  | 
|---|
| 372 | n/a | @util.writes_bytecode_files | 
|---|
| 373 | n/a | def test_empty_file(self): | 
|---|
| 374 | n/a | # When a .pyc is empty, regenerate it if possible, else raise | 
|---|
| 375 | n/a | # ImportError. | 
|---|
| 376 | n/a | def test(name, mapping, bytecode_path): | 
|---|
| 377 | n/a | self.import_(mapping[name], name) | 
|---|
| 378 | n/a | with open(bytecode_path, 'rb') as file: | 
|---|
| 379 | n/a | self.assertGreater(len(file.read()), 12) | 
|---|
| 380 | n/a |  | 
|---|
| 381 | n/a | self._test_empty_file(test) | 
|---|
| 382 | n/a |  | 
|---|
| 383 | n/a | def test_partial_magic(self): | 
|---|
| 384 | n/a | def test(name, mapping, bytecode_path): | 
|---|
| 385 | n/a | self.import_(mapping[name], name) | 
|---|
| 386 | n/a | with open(bytecode_path, 'rb') as file: | 
|---|
| 387 | n/a | self.assertGreater(len(file.read()), 12) | 
|---|
| 388 | n/a |  | 
|---|
| 389 | n/a | self._test_partial_magic(test) | 
|---|
| 390 | n/a |  | 
|---|
| 391 | n/a | @util.writes_bytecode_files | 
|---|
| 392 | n/a | def test_magic_only(self): | 
|---|
| 393 | n/a | # When there is only the magic number, regenerate the .pyc if possible, | 
|---|
| 394 | n/a | # else raise EOFError. | 
|---|
| 395 | n/a | def test(name, mapping, bytecode_path): | 
|---|
| 396 | n/a | self.import_(mapping[name], name) | 
|---|
| 397 | n/a | with open(bytecode_path, 'rb') as file: | 
|---|
| 398 | n/a | self.assertGreater(len(file.read()), 12) | 
|---|
| 399 | n/a |  | 
|---|
| 400 | n/a | self._test_magic_only(test) | 
|---|
| 401 | n/a |  | 
|---|
| 402 | n/a | @util.writes_bytecode_files | 
|---|
| 403 | n/a | def test_bad_magic(self): | 
|---|
| 404 | n/a | # When the magic number is different, the bytecode should be | 
|---|
| 405 | n/a | # regenerated. | 
|---|
| 406 | n/a | def test(name, mapping, bytecode_path): | 
|---|
| 407 | n/a | self.import_(mapping[name], name) | 
|---|
| 408 | n/a | with open(bytecode_path, 'rb') as bytecode_file: | 
|---|
| 409 | n/a | self.assertEqual(bytecode_file.read(4), | 
|---|
| 410 | n/a | self.util.MAGIC_NUMBER) | 
|---|
| 411 | n/a |  | 
|---|
| 412 | n/a | self._test_bad_magic(test) | 
|---|
| 413 | n/a |  | 
|---|
| 414 | n/a | @util.writes_bytecode_files | 
|---|
| 415 | n/a | def test_partial_timestamp(self): | 
|---|
| 416 | n/a | # When the timestamp is partial, regenerate the .pyc, else | 
|---|
| 417 | n/a | # raise EOFError. | 
|---|
| 418 | n/a | def test(name, mapping, bc_path): | 
|---|
| 419 | n/a | self.import_(mapping[name], name) | 
|---|
| 420 | n/a | with open(bc_path, 'rb') as file: | 
|---|
| 421 | n/a | self.assertGreater(len(file.read()), 12) | 
|---|
| 422 | n/a |  | 
|---|
| 423 | n/a | self._test_partial_timestamp(test) | 
|---|
| 424 | n/a |  | 
|---|
| 425 | n/a | @util.writes_bytecode_files | 
|---|
| 426 | n/a | def test_partial_size(self): | 
|---|
| 427 | n/a | # When the size is partial, regenerate the .pyc, else | 
|---|
| 428 | n/a | # raise EOFError. | 
|---|
| 429 | n/a | def test(name, mapping, bc_path): | 
|---|
| 430 | n/a | self.import_(mapping[name], name) | 
|---|
| 431 | n/a | with open(bc_path, 'rb') as file: | 
|---|
| 432 | n/a | self.assertGreater(len(file.read()), 12) | 
|---|
| 433 | n/a |  | 
|---|
| 434 | n/a | self._test_partial_size(test) | 
|---|
| 435 | n/a |  | 
|---|
| 436 | n/a | @util.writes_bytecode_files | 
|---|
| 437 | n/a | def test_no_marshal(self): | 
|---|
| 438 | n/a | # When there is only the magic number and timestamp, raise EOFError. | 
|---|
| 439 | n/a | self._test_no_marshal() | 
|---|
| 440 | n/a |  | 
|---|
| 441 | n/a | @util.writes_bytecode_files | 
|---|
| 442 | n/a | def test_non_code_marshal(self): | 
|---|
| 443 | n/a | self._test_non_code_marshal() | 
|---|
| 444 | n/a | # XXX ImportError when sourceless | 
|---|
| 445 | n/a |  | 
|---|
| 446 | n/a | # [bad marshal] | 
|---|
| 447 | n/a | @util.writes_bytecode_files | 
|---|
| 448 | n/a | def test_bad_marshal(self): | 
|---|
| 449 | n/a | # Bad marshal data should raise a ValueError. | 
|---|
| 450 | n/a | self._test_bad_marshal() | 
|---|
| 451 | n/a |  | 
|---|
| 452 | n/a | # [bad timestamp] | 
|---|
| 453 | n/a | @util.writes_bytecode_files | 
|---|
| 454 | n/a | def test_old_timestamp(self): | 
|---|
| 455 | n/a | # When the timestamp is older than the source, bytecode should be | 
|---|
| 456 | n/a | # regenerated. | 
|---|
| 457 | n/a | zeros = b'\x00\x00\x00\x00' | 
|---|
| 458 | n/a | with util.create_modules('_temp') as mapping: | 
|---|
| 459 | n/a | py_compile.compile(mapping['_temp']) | 
|---|
| 460 | n/a | bytecode_path = self.util.cache_from_source(mapping['_temp']) | 
|---|
| 461 | n/a | with open(bytecode_path, 'r+b') as bytecode_file: | 
|---|
| 462 | n/a | bytecode_file.seek(4) | 
|---|
| 463 | n/a | bytecode_file.write(zeros) | 
|---|
| 464 | n/a | self.import_(mapping['_temp'], '_temp') | 
|---|
| 465 | n/a | source_mtime = os.path.getmtime(mapping['_temp']) | 
|---|
| 466 | n/a | source_timestamp = self.importlib._w_long(source_mtime) | 
|---|
| 467 | n/a | with open(bytecode_path, 'rb') as bytecode_file: | 
|---|
| 468 | n/a | bytecode_file.seek(4) | 
|---|
| 469 | n/a | self.assertEqual(bytecode_file.read(4), source_timestamp) | 
|---|
| 470 | n/a |  | 
|---|
| 471 | n/a | # [bytecode read-only] | 
|---|
| 472 | n/a | @util.writes_bytecode_files | 
|---|
| 473 | n/a | def test_read_only_bytecode(self): | 
|---|
| 474 | n/a | # When bytecode is read-only but should be rewritten, fail silently. | 
|---|
| 475 | n/a | with util.create_modules('_temp') as mapping: | 
|---|
| 476 | n/a | # Create bytecode that will need to be re-created. | 
|---|
| 477 | n/a | py_compile.compile(mapping['_temp']) | 
|---|
| 478 | n/a | bytecode_path = self.util.cache_from_source(mapping['_temp']) | 
|---|
| 479 | n/a | with open(bytecode_path, 'r+b') as bytecode_file: | 
|---|
| 480 | n/a | bytecode_file.seek(0) | 
|---|
| 481 | n/a | bytecode_file.write(b'\x00\x00\x00\x00') | 
|---|
| 482 | n/a | # Make the bytecode read-only. | 
|---|
| 483 | n/a | os.chmod(bytecode_path, | 
|---|
| 484 | n/a | stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH) | 
|---|
| 485 | n/a | try: | 
|---|
| 486 | n/a | # Should not raise OSError! | 
|---|
| 487 | n/a | self.import_(mapping['_temp'], '_temp') | 
|---|
| 488 | n/a | finally: | 
|---|
| 489 | n/a | # Make writable for eventual clean-up. | 
|---|
| 490 | n/a | os.chmod(bytecode_path, stat.S_IWUSR) | 
|---|
| 491 | n/a |  | 
|---|
| 492 | n/a |  | 
|---|
| 493 | n/a | class SourceLoaderBadBytecodeTestPEP451( | 
|---|
| 494 | n/a | SourceLoaderBadBytecodeTest, BadBytecodeTestPEP451): | 
|---|
| 495 | n/a | pass | 
|---|
| 496 | n/a |  | 
|---|
| 497 | n/a |  | 
|---|
| 498 | n/a | (Frozen_SourceBadBytecodePEP451, | 
|---|
| 499 | n/a | Source_SourceBadBytecodePEP451 | 
|---|
| 500 | n/a | ) = util.test_both(SourceLoaderBadBytecodeTestPEP451, importlib=importlib, | 
|---|
| 501 | n/a | machinery=machinery, abc=importlib_abc, | 
|---|
| 502 | n/a | util=importlib_util) | 
|---|
| 503 | n/a |  | 
|---|
| 504 | n/a |  | 
|---|
| 505 | n/a | class SourceLoaderBadBytecodeTestPEP302( | 
|---|
| 506 | n/a | SourceLoaderBadBytecodeTest, BadBytecodeTestPEP302): | 
|---|
| 507 | n/a | pass | 
|---|
| 508 | n/a |  | 
|---|
| 509 | n/a |  | 
|---|
| 510 | n/a | (Frozen_SourceBadBytecodePEP302, | 
|---|
| 511 | n/a | Source_SourceBadBytecodePEP302 | 
|---|
| 512 | n/a | ) = util.test_both(SourceLoaderBadBytecodeTestPEP302, importlib=importlib, | 
|---|
| 513 | n/a | machinery=machinery, abc=importlib_abc, | 
|---|
| 514 | n/a | util=importlib_util) | 
|---|
| 515 | n/a |  | 
|---|
| 516 | n/a |  | 
|---|
| 517 | n/a | class SourcelessLoaderBadBytecodeTest: | 
|---|
| 518 | n/a |  | 
|---|
| 519 | n/a | @classmethod | 
|---|
| 520 | n/a | def setUpClass(cls): | 
|---|
| 521 | n/a | cls.loader = cls.machinery.SourcelessFileLoader | 
|---|
| 522 | n/a |  | 
|---|
| 523 | n/a | def test_empty_file(self): | 
|---|
| 524 | n/a | def test(name, mapping, bytecode_path): | 
|---|
| 525 | n/a | with self.assertRaises(ImportError) as cm: | 
|---|
| 526 | n/a | self.import_(bytecode_path, name) | 
|---|
| 527 | n/a | self.assertEqual(cm.exception.name, name) | 
|---|
| 528 | n/a | self.assertEqual(cm.exception.path, bytecode_path) | 
|---|
| 529 | n/a |  | 
|---|
| 530 | n/a | self._test_empty_file(test, del_source=True) | 
|---|
| 531 | n/a |  | 
|---|
| 532 | n/a | def test_partial_magic(self): | 
|---|
| 533 | n/a | def test(name, mapping, bytecode_path): | 
|---|
| 534 | n/a | with self.assertRaises(ImportError) as cm: | 
|---|
| 535 | n/a | self.import_(bytecode_path, name) | 
|---|
| 536 | n/a | self.assertEqual(cm.exception.name, name) | 
|---|
| 537 | n/a | self.assertEqual(cm.exception.path, bytecode_path) | 
|---|
| 538 | n/a | self._test_partial_magic(test, del_source=True) | 
|---|
| 539 | n/a |  | 
|---|
| 540 | n/a | def test_magic_only(self): | 
|---|
| 541 | n/a | def test(name, mapping, bytecode_path): | 
|---|
| 542 | n/a | with self.assertRaises(EOFError): | 
|---|
| 543 | n/a | self.import_(bytecode_path, name) | 
|---|
| 544 | n/a |  | 
|---|
| 545 | n/a | self._test_magic_only(test, del_source=True) | 
|---|
| 546 | n/a |  | 
|---|
| 547 | n/a | def test_bad_magic(self): | 
|---|
| 548 | n/a | def test(name, mapping, bytecode_path): | 
|---|
| 549 | n/a | with self.assertRaises(ImportError) as cm: | 
|---|
| 550 | n/a | self.import_(bytecode_path, name) | 
|---|
| 551 | n/a | self.assertEqual(cm.exception.name, name) | 
|---|
| 552 | n/a | self.assertEqual(cm.exception.path, bytecode_path) | 
|---|
| 553 | n/a |  | 
|---|
| 554 | n/a | self._test_bad_magic(test, del_source=True) | 
|---|
| 555 | n/a |  | 
|---|
| 556 | n/a | def test_partial_timestamp(self): | 
|---|
| 557 | n/a | def test(name, mapping, bytecode_path): | 
|---|
| 558 | n/a | with self.assertRaises(EOFError): | 
|---|
| 559 | n/a | self.import_(bytecode_path, name) | 
|---|
| 560 | n/a |  | 
|---|
| 561 | n/a | self._test_partial_timestamp(test, del_source=True) | 
|---|
| 562 | n/a |  | 
|---|
| 563 | n/a | def test_partial_size(self): | 
|---|
| 564 | n/a | def test(name, mapping, bytecode_path): | 
|---|
| 565 | n/a | with self.assertRaises(EOFError): | 
|---|
| 566 | n/a | self.import_(bytecode_path, name) | 
|---|
| 567 | n/a |  | 
|---|
| 568 | n/a | self._test_partial_size(test, del_source=True) | 
|---|
| 569 | n/a |  | 
|---|
| 570 | n/a | def test_no_marshal(self): | 
|---|
| 571 | n/a | self._test_no_marshal(del_source=True) | 
|---|
| 572 | n/a |  | 
|---|
| 573 | n/a | def test_non_code_marshal(self): | 
|---|
| 574 | n/a | self._test_non_code_marshal(del_source=True) | 
|---|
| 575 | n/a |  | 
|---|
| 576 | n/a |  | 
|---|
| 577 | n/a | class SourcelessLoaderBadBytecodeTestPEP451(SourcelessLoaderBadBytecodeTest, | 
|---|
| 578 | n/a | BadBytecodeTestPEP451): | 
|---|
| 579 | n/a | pass | 
|---|
| 580 | n/a |  | 
|---|
| 581 | n/a |  | 
|---|
| 582 | n/a | (Frozen_SourcelessBadBytecodePEP451, | 
|---|
| 583 | n/a | Source_SourcelessBadBytecodePEP451 | 
|---|
| 584 | n/a | ) = util.test_both(SourcelessLoaderBadBytecodeTestPEP451, importlib=importlib, | 
|---|
| 585 | n/a | machinery=machinery, abc=importlib_abc, | 
|---|
| 586 | n/a | util=importlib_util) | 
|---|
| 587 | n/a |  | 
|---|
| 588 | n/a |  | 
|---|
| 589 | n/a | class SourcelessLoaderBadBytecodeTestPEP302(SourcelessLoaderBadBytecodeTest, | 
|---|
| 590 | n/a | BadBytecodeTestPEP302): | 
|---|
| 591 | n/a | pass | 
|---|
| 592 | n/a |  | 
|---|
| 593 | n/a |  | 
|---|
| 594 | n/a | (Frozen_SourcelessBadBytecodePEP302, | 
|---|
| 595 | n/a | Source_SourcelessBadBytecodePEP302 | 
|---|
| 596 | n/a | ) = util.test_both(SourcelessLoaderBadBytecodeTestPEP302, importlib=importlib, | 
|---|
| 597 | n/a | machinery=machinery, abc=importlib_abc, | 
|---|
| 598 | n/a | util=importlib_util) | 
|---|
| 599 | n/a |  | 
|---|
| 600 | n/a |  | 
|---|
| 601 | n/a | if __name__ == '__main__': | 
|---|
| 602 | n/a | unittest.main() | 
|---|