Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 1 | from .. import abc |
Brett Cannon | d71bed3 | 2010-07-03 22:18:47 +0000 | [diff] [blame] | 2 | from .. import util |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 3 | from . import util as source_util |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 4 | |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame^] | 5 | importlib = util.import_importlib('importlib') |
| 6 | importlib_abc = util.import_importlib('importlib.abc') |
| 7 | machinery = util.import_importlib('importlib.machinery') |
| 8 | importlib_util = util.import_importlib('importlib.util') |
| 9 | |
Antoine Pitrou | dd21f68 | 2012-01-25 03:00:57 +0100 | [diff] [blame] | 10 | import errno |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 11 | import marshal |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 12 | import os |
| 13 | import py_compile |
Brett Cannon | 186335b | 2010-08-22 22:11:06 +0000 | [diff] [blame] | 14 | import shutil |
Brett Cannon | e52c919 | 2009-11-07 23:55:05 +0000 | [diff] [blame] | 15 | import stat |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 16 | import sys |
Brett Cannon | ef88802 | 2013-06-15 18:39:21 -0400 | [diff] [blame] | 17 | import types |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 18 | import unittest |
| 19 | |
Brett Cannon | 0dbb4c7 | 2013-05-31 18:56:47 -0400 | [diff] [blame] | 20 | from test.support import make_legacy_pyc, unload |
Barry Warsaw | 04b5684 | 2010-05-18 14:15:20 +0000 | [diff] [blame] | 21 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 22 | |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame^] | 23 | class SimpleTest(abc.LoaderTests): |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 24 | |
| 25 | """Should have no issue importing a source module [basic]. And if there is |
| 26 | a syntax error, it should raise a SyntaxError [syntax error]. |
| 27 | |
| 28 | """ |
| 29 | |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 30 | def test_load_module_API(self): |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame^] | 31 | class Tester(self.abc.FileLoader): |
Brett Cannon | 0dbb4c7 | 2013-05-31 18:56:47 -0400 | [diff] [blame] | 32 | def get_source(self, _): return 'attr = 42' |
| 33 | def is_package(self, _): return False |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 34 | |
Brett Cannon | 0dbb4c7 | 2013-05-31 18:56:47 -0400 | [diff] [blame] | 35 | loader = Tester('blah', 'blah.py') |
| 36 | self.addCleanup(unload, 'blah') |
| 37 | module = loader.load_module() # Should not raise an exception. |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 38 | |
| 39 | def test_get_filename_API(self): |
| 40 | # If fullname is not set then assume self.path is desired. |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame^] | 41 | class Tester(self.abc.FileLoader): |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 42 | def get_code(self, _): pass |
| 43 | def get_source(self, _): pass |
| 44 | def is_package(self, _): pass |
Barry Warsaw | 0efcf99 | 2012-07-31 17:52:32 -0400 | [diff] [blame] | 45 | def module_repr(self, _): pass |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 46 | |
| 47 | path = 'some_path' |
| 48 | name = 'some_name' |
| 49 | loader = Tester(name, path) |
| 50 | self.assertEqual(path, loader.get_filename(name)) |
| 51 | self.assertEqual(path, loader.get_filename()) |
| 52 | self.assertEqual(path, loader.get_filename(None)) |
| 53 | with self.assertRaises(ImportError): |
| 54 | loader.get_filename(name + 'XXX') |
| 55 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 56 | # [basic] |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 57 | def test_module(self): |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 58 | with source_util.create_modules('_temp') as mapping: |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame^] | 59 | loader = self.machinery.SourceFileLoader('_temp', mapping['_temp']) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 60 | module = loader.load_module('_temp') |
Eric V. Smith | faae3ad | 2012-06-27 15:26:26 -0400 | [diff] [blame] | 61 | self.assertIn('_temp', sys.modules) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 62 | check = {'__name__': '_temp', '__file__': mapping['_temp'], |
Brett Cannon | 06c9d96 | 2009-02-07 01:52:25 +0000 | [diff] [blame] | 63 | '__package__': ''} |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 64 | for attr, value in check.items(): |
| 65 | self.assertEqual(getattr(module, attr), value) |
| 66 | |
| 67 | def test_package(self): |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 68 | with source_util.create_modules('_pkg.__init__') as mapping: |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame^] | 69 | loader = self.machinery.SourceFileLoader('_pkg', |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 70 | mapping['_pkg.__init__']) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 71 | module = loader.load_module('_pkg') |
Eric V. Smith | faae3ad | 2012-06-27 15:26:26 -0400 | [diff] [blame] | 72 | self.assertIn('_pkg', sys.modules) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 73 | check = {'__name__': '_pkg', '__file__': mapping['_pkg.__init__'], |
| 74 | '__path__': [os.path.dirname(mapping['_pkg.__init__'])], |
| 75 | '__package__': '_pkg'} |
| 76 | for attr, value in check.items(): |
| 77 | self.assertEqual(getattr(module, attr), value) |
| 78 | |
| 79 | |
| 80 | def test_lacking_parent(self): |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 81 | with source_util.create_modules('_pkg.__init__', '_pkg.mod')as mapping: |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame^] | 82 | loader = self.machinery.SourceFileLoader('_pkg.mod', |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 83 | mapping['_pkg.mod']) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 84 | module = loader.load_module('_pkg.mod') |
Eric V. Smith | faae3ad | 2012-06-27 15:26:26 -0400 | [diff] [blame] | 85 | self.assertIn('_pkg.mod', sys.modules) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 86 | check = {'__name__': '_pkg.mod', '__file__': mapping['_pkg.mod'], |
| 87 | '__package__': '_pkg'} |
| 88 | for attr, value in check.items(): |
| 89 | self.assertEqual(getattr(module, attr), value) |
| 90 | |
| 91 | def fake_mtime(self, fxn): |
| 92 | """Fake mtime to always be higher than expected.""" |
| 93 | return lambda name: fxn(name) + 1 |
| 94 | |
| 95 | def test_module_reuse(self): |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 96 | with source_util.create_modules('_temp') as mapping: |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame^] | 97 | loader = self.machinery.SourceFileLoader('_temp', mapping['_temp']) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 98 | module = loader.load_module('_temp') |
| 99 | module_id = id(module) |
| 100 | module_dict_id = id(module.__dict__) |
| 101 | with open(mapping['_temp'], 'w') as file: |
| 102 | file.write("testing_var = 42\n") |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 103 | module = loader.load_module('_temp') |
Eric V. Smith | faae3ad | 2012-06-27 15:26:26 -0400 | [diff] [blame] | 104 | self.assertIn('testing_var', module.__dict__, |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 105 | "'testing_var' not in " |
| 106 | "{0}".format(list(module.__dict__.keys()))) |
| 107 | self.assertEqual(module, sys.modules['_temp']) |
| 108 | self.assertEqual(id(module), module_id) |
| 109 | self.assertEqual(id(module.__dict__), module_dict_id) |
| 110 | |
| 111 | def test_state_after_failure(self): |
| 112 | # A failed reload should leave the original module intact. |
| 113 | attributes = ('__file__', '__path__', '__package__') |
| 114 | value = '<test>' |
| 115 | name = '_temp' |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 116 | with source_util.create_modules(name) as mapping: |
Brett Cannon | ef88802 | 2013-06-15 18:39:21 -0400 | [diff] [blame] | 117 | orig_module = types.ModuleType(name) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 118 | for attr in attributes: |
| 119 | setattr(orig_module, attr, value) |
| 120 | with open(mapping[name], 'w') as file: |
| 121 | file.write('+++ bad syntax +++') |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame^] | 122 | loader = self.machinery.SourceFileLoader('_temp', mapping['_temp']) |
Brett Cannon | 2153dc0 | 2009-08-27 23:49:21 +0000 | [diff] [blame] | 123 | with self.assertRaises(SyntaxError): |
| 124 | loader.load_module(name) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 125 | for attr in attributes: |
| 126 | self.assertEqual(getattr(orig_module, attr), value) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 127 | |
| 128 | # [syntax error] |
| 129 | def test_bad_syntax(self): |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 130 | with source_util.create_modules('_temp') as mapping: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 131 | with open(mapping['_temp'], 'w') as file: |
| 132 | file.write('=') |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame^] | 133 | loader = self.machinery.SourceFileLoader('_temp', mapping['_temp']) |
Brett Cannon | 2153dc0 | 2009-08-27 23:49:21 +0000 | [diff] [blame] | 134 | with self.assertRaises(SyntaxError): |
| 135 | loader.load_module('_temp') |
Eric V. Smith | faae3ad | 2012-06-27 15:26:26 -0400 | [diff] [blame] | 136 | self.assertNotIn('_temp', sys.modules) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 137 | |
Brett Cannon | d71bed3 | 2010-07-03 22:18:47 +0000 | [diff] [blame] | 138 | def test_file_from_empty_string_dir(self): |
| 139 | # Loading a module found from an empty string entry on sys.path should |
| 140 | # not only work, but keep all attributes relative. |
Brett Cannon | 186335b | 2010-08-22 22:11:06 +0000 | [diff] [blame] | 141 | file_path = '_temp.py' |
| 142 | with open(file_path, 'w') as file: |
Brett Cannon | d71bed3 | 2010-07-03 22:18:47 +0000 | [diff] [blame] | 143 | file.write("# test file for importlib") |
| 144 | try: |
| 145 | with util.uncache('_temp'): |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame^] | 146 | loader = self.machinery.SourceFileLoader('_temp', file_path) |
Brett Cannon | d71bed3 | 2010-07-03 22:18:47 +0000 | [diff] [blame] | 147 | mod = loader.load_module('_temp') |
Brett Cannon | 186335b | 2010-08-22 22:11:06 +0000 | [diff] [blame] | 148 | self.assertEqual(file_path, mod.__file__) |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame^] | 149 | self.assertEqual(self.util.cache_from_source(file_path), |
Brett Cannon | d71bed3 | 2010-07-03 22:18:47 +0000 | [diff] [blame] | 150 | mod.__cached__) |
Brett Cannon | d71bed3 | 2010-07-03 22:18:47 +0000 | [diff] [blame] | 151 | finally: |
Brett Cannon | 186335b | 2010-08-22 22:11:06 +0000 | [diff] [blame] | 152 | os.unlink(file_path) |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame^] | 153 | pycache = os.path.dirname(self.util.cache_from_source(file_path)) |
Ezio Melotti | c28f6fa | 2013-03-16 19:48:51 +0200 | [diff] [blame] | 154 | if os.path.exists(pycache): |
| 155 | shutil.rmtree(pycache) |
Brett Cannon | d71bed3 | 2010-07-03 22:18:47 +0000 | [diff] [blame] | 156 | |
Antoine Pitrou | 2be60af | 2012-01-24 17:44:06 +0100 | [diff] [blame] | 157 | def test_timestamp_overflow(self): |
| 158 | # When a modification timestamp is larger than 2**32, it should be |
| 159 | # truncated rather than raise an OverflowError. |
| 160 | with source_util.create_modules('_temp') as mapping: |
| 161 | source = mapping['_temp'] |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame^] | 162 | compiled = self.util.cache_from_source(source) |
Antoine Pitrou | 2be60af | 2012-01-24 17:44:06 +0100 | [diff] [blame] | 163 | with open(source, 'w') as f: |
| 164 | f.write("x = 5") |
Antoine Pitrou | dd21f68 | 2012-01-25 03:00:57 +0100 | [diff] [blame] | 165 | try: |
Antoine Pitrou | 33d15f7 | 2012-01-25 18:01:45 +0100 | [diff] [blame] | 166 | os.utime(source, (2 ** 33 - 5, 2 ** 33 - 5)) |
Antoine Pitrou | dd21f68 | 2012-01-25 03:00:57 +0100 | [diff] [blame] | 167 | except OverflowError: |
| 168 | self.skipTest("cannot set modification time to large integer") |
| 169 | except OSError as e: |
| 170 | if e.errno != getattr(errno, 'EOVERFLOW', None): |
| 171 | raise |
| 172 | self.skipTest("cannot set modification time to large integer ({})".format(e)) |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame^] | 173 | loader = self.machinery.SourceFileLoader('_temp', mapping['_temp']) |
Antoine Pitrou | 2be60af | 2012-01-24 17:44:06 +0100 | [diff] [blame] | 174 | mod = loader.load_module('_temp') |
| 175 | # Sanity checks. |
| 176 | self.assertEqual(mod.__cached__, compiled) |
| 177 | self.assertEqual(mod.x, 5) |
| 178 | # The pyc file was created. |
| 179 | os.stat(compiled) |
| 180 | |
Brett Cannon | b3d6aff | 2013-10-25 12:33:59 -0400 | [diff] [blame] | 181 | def test_unloadable(self): |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame^] | 182 | loader = self.machinery.SourceFileLoader('good name', {}) |
Brett Cannon | b3d6aff | 2013-10-25 12:33:59 -0400 | [diff] [blame] | 183 | with self.assertRaises(ImportError): |
| 184 | loader.load_module('bad name') |
| 185 | |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame^] | 186 | Frozen_SimpleTest, Source_SimpleTest = util.test_both( |
| 187 | SimpleTest, importlib=importlib, machinery=machinery, abc=importlib_abc, |
| 188 | util=importlib_util) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 189 | |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame^] | 190 | |
| 191 | class BadBytecodeTest: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 192 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 193 | def import_(self, file, module_name): |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 194 | loader = self.loader(module_name, file) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 195 | module = loader.load_module(module_name) |
Eric V. Smith | faae3ad | 2012-06-27 15:26:26 -0400 | [diff] [blame] | 196 | self.assertIn(module_name, sys.modules) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 197 | |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 198 | def manipulate_bytecode(self, name, mapping, manipulator, *, |
| 199 | del_source=False): |
| 200 | """Manipulate the bytecode of a module by passing it into a callable |
| 201 | that returns what to use as the new bytecode.""" |
| 202 | try: |
| 203 | del sys.modules['_temp'] |
| 204 | except KeyError: |
| 205 | pass |
| 206 | py_compile.compile(mapping[name]) |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 207 | if not del_source: |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame^] | 208 | bytecode_path = self.util.cache_from_source(mapping[name]) |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 209 | else: |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 210 | os.unlink(mapping[name]) |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 211 | bytecode_path = make_legacy_pyc(mapping[name]) |
| 212 | if manipulator: |
| 213 | with open(bytecode_path, 'rb') as file: |
| 214 | bc = file.read() |
| 215 | new_bc = manipulator(bc) |
| 216 | with open(bytecode_path, 'wb') as file: |
| 217 | if new_bc is not None: |
| 218 | file.write(new_bc) |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 219 | return bytecode_path |
| 220 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 221 | def _test_empty_file(self, test, *, del_source=False): |
| 222 | with source_util.create_modules('_temp') as mapping: |
| 223 | bc_path = self.manipulate_bytecode('_temp', mapping, |
| 224 | lambda bc: b'', |
| 225 | del_source=del_source) |
| 226 | test('_temp', mapping, bc_path) |
| 227 | |
| 228 | @source_util.writes_bytecode_files |
| 229 | def _test_partial_magic(self, test, *, del_source=False): |
| 230 | # When their are less than 4 bytes to a .pyc, regenerate it if |
| 231 | # possible, else raise ImportError. |
| 232 | with source_util.create_modules('_temp') as mapping: |
| 233 | bc_path = self.manipulate_bytecode('_temp', mapping, |
| 234 | lambda bc: bc[:3], |
| 235 | del_source=del_source) |
| 236 | test('_temp', mapping, bc_path) |
| 237 | |
| 238 | def _test_magic_only(self, test, *, del_source=False): |
| 239 | with source_util.create_modules('_temp') as mapping: |
| 240 | bc_path = self.manipulate_bytecode('_temp', mapping, |
| 241 | lambda bc: bc[:4], |
| 242 | del_source=del_source) |
| 243 | test('_temp', mapping, bc_path) |
| 244 | |
| 245 | def _test_partial_timestamp(self, test, *, del_source=False): |
| 246 | with source_util.create_modules('_temp') as mapping: |
| 247 | bc_path = self.manipulate_bytecode('_temp', mapping, |
| 248 | lambda bc: bc[:7], |
| 249 | del_source=del_source) |
| 250 | test('_temp', mapping, bc_path) |
| 251 | |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 252 | def _test_partial_size(self, test, *, del_source=False): |
| 253 | with source_util.create_modules('_temp') as mapping: |
| 254 | bc_path = self.manipulate_bytecode('_temp', mapping, |
| 255 | lambda bc: bc[:11], |
| 256 | del_source=del_source) |
| 257 | test('_temp', mapping, bc_path) |
| 258 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 259 | def _test_no_marshal(self, *, del_source=False): |
| 260 | with source_util.create_modules('_temp') as mapping: |
| 261 | bc_path = self.manipulate_bytecode('_temp', mapping, |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 262 | lambda bc: bc[:12], |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 263 | del_source=del_source) |
| 264 | file_path = mapping['_temp'] if not del_source else bc_path |
| 265 | with self.assertRaises(EOFError): |
| 266 | self.import_(file_path, '_temp') |
| 267 | |
| 268 | def _test_non_code_marshal(self, *, del_source=False): |
| 269 | with source_util.create_modules('_temp') as mapping: |
| 270 | bytecode_path = self.manipulate_bytecode('_temp', mapping, |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 271 | lambda bc: bc[:12] + marshal.dumps(b'abcd'), |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 272 | del_source=del_source) |
| 273 | file_path = mapping['_temp'] if not del_source else bytecode_path |
Brett Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 274 | with self.assertRaises(ImportError) as cm: |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 275 | self.import_(file_path, '_temp') |
Brett Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 276 | self.assertEqual(cm.exception.name, '_temp') |
| 277 | self.assertEqual(cm.exception.path, bytecode_path) |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 278 | |
| 279 | def _test_bad_marshal(self, *, del_source=False): |
| 280 | with source_util.create_modules('_temp') as mapping: |
| 281 | bytecode_path = self.manipulate_bytecode('_temp', mapping, |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 282 | lambda bc: bc[:12] + b'<test>', |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 283 | del_source=del_source) |
| 284 | file_path = mapping['_temp'] if not del_source else bytecode_path |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 285 | with self.assertRaises(EOFError): |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 286 | self.import_(file_path, '_temp') |
| 287 | |
| 288 | def _test_bad_magic(self, test, *, del_source=False): |
| 289 | with source_util.create_modules('_temp') as mapping: |
| 290 | bc_path = self.manipulate_bytecode('_temp', mapping, |
| 291 | lambda bc: b'\x00\x00\x00\x00' + bc[4:]) |
| 292 | test('_temp', mapping, bc_path) |
| 293 | |
| 294 | |
| 295 | class SourceLoaderBadBytecodeTest(BadBytecodeTest): |
| 296 | |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame^] | 297 | @classmethod |
| 298 | def setUpClass(cls): |
| 299 | cls.loader = cls.machinery.SourceFileLoader |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 300 | |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 301 | @source_util.writes_bytecode_files |
| 302 | def test_empty_file(self): |
| 303 | # When a .pyc is empty, regenerate it if possible, else raise |
| 304 | # ImportError. |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 305 | def test(name, mapping, bytecode_path): |
| 306 | self.import_(mapping[name], name) |
| 307 | with open(bytecode_path, 'rb') as file: |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 308 | self.assertGreater(len(file.read()), 12) |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 309 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 310 | self._test_empty_file(test) |
| 311 | |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 312 | def test_partial_magic(self): |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 313 | def test(name, mapping, bytecode_path): |
| 314 | self.import_(mapping[name], name) |
| 315 | with open(bytecode_path, 'rb') as file: |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 316 | self.assertGreater(len(file.read()), 12) |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 317 | |
| 318 | self._test_partial_magic(test) |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 319 | |
| 320 | @source_util.writes_bytecode_files |
| 321 | def test_magic_only(self): |
| 322 | # When there is only the magic number, regenerate the .pyc if possible, |
| 323 | # else raise EOFError. |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 324 | def test(name, mapping, bytecode_path): |
| 325 | self.import_(mapping[name], name) |
| 326 | with open(bytecode_path, 'rb') as file: |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 327 | self.assertGreater(len(file.read()), 12) |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 328 | |
Antoine Pitrou | 7c9907e | 2011-12-30 21:25:15 +0100 | [diff] [blame] | 329 | self._test_magic_only(test) |
| 330 | |
Brett Cannon | 1262e7c | 2009-05-11 01:47:11 +0000 | [diff] [blame] | 331 | @source_util.writes_bytecode_files |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 332 | def test_bad_magic(self): |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 333 | # When the magic number is different, the bytecode should be |
| 334 | # regenerated. |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 335 | def test(name, mapping, bytecode_path): |
| 336 | self.import_(mapping[name], name) |
| 337 | with open(bytecode_path, 'rb') as bytecode_file: |
Brett Cannon | ef88802 | 2013-06-15 18:39:21 -0400 | [diff] [blame] | 338 | self.assertEqual(bytecode_file.read(4), |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame^] | 339 | self.util.MAGIC_NUMBER) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 340 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 341 | self._test_bad_magic(test) |
| 342 | |
| 343 | @source_util.writes_bytecode_files |
| 344 | def test_partial_timestamp(self): |
| 345 | # When the timestamp is partial, regenerate the .pyc, else |
| 346 | # raise EOFError. |
| 347 | def test(name, mapping, bc_path): |
| 348 | self.import_(mapping[name], name) |
| 349 | with open(bc_path, 'rb') as file: |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 350 | self.assertGreater(len(file.read()), 12) |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 351 | |
Antoine Pitrou | 7c9907e | 2011-12-30 21:25:15 +0100 | [diff] [blame] | 352 | self._test_partial_timestamp(test) |
| 353 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 354 | @source_util.writes_bytecode_files |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 355 | def test_partial_size(self): |
| 356 | # When the size is partial, regenerate the .pyc, else |
| 357 | # raise EOFError. |
| 358 | def test(name, mapping, bc_path): |
| 359 | self.import_(mapping[name], name) |
| 360 | with open(bc_path, 'rb') as file: |
| 361 | self.assertGreater(len(file.read()), 12) |
| 362 | |
| 363 | self._test_partial_size(test) |
| 364 | |
| 365 | @source_util.writes_bytecode_files |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 366 | def test_no_marshal(self): |
| 367 | # When there is only the magic number and timestamp, raise EOFError. |
| 368 | self._test_no_marshal() |
| 369 | |
| 370 | @source_util.writes_bytecode_files |
| 371 | def test_non_code_marshal(self): |
| 372 | self._test_non_code_marshal() |
| 373 | # XXX ImportError when sourceless |
| 374 | |
| 375 | # [bad marshal] |
| 376 | @source_util.writes_bytecode_files |
| 377 | def test_bad_marshal(self): |
| 378 | # Bad marshal data should raise a ValueError. |
| 379 | self._test_bad_marshal() |
| 380 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 381 | # [bad timestamp] |
Brett Cannon | 1262e7c | 2009-05-11 01:47:11 +0000 | [diff] [blame] | 382 | @source_util.writes_bytecode_files |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 383 | def test_old_timestamp(self): |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 384 | # When the timestamp is older than the source, bytecode should be |
| 385 | # regenerated. |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 386 | zeros = b'\x00\x00\x00\x00' |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 387 | with source_util.create_modules('_temp') as mapping: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 388 | py_compile.compile(mapping['_temp']) |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame^] | 389 | bytecode_path = self.util.cache_from_source(mapping['_temp']) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 390 | with open(bytecode_path, 'r+b') as bytecode_file: |
| 391 | bytecode_file.seek(4) |
| 392 | bytecode_file.write(zeros) |
| 393 | self.import_(mapping['_temp'], '_temp') |
| 394 | source_mtime = os.path.getmtime(mapping['_temp']) |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame^] | 395 | source_timestamp = self.importlib._w_long(source_mtime) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 396 | with open(bytecode_path, 'rb') as bytecode_file: |
| 397 | bytecode_file.seek(4) |
| 398 | self.assertEqual(bytecode_file.read(4), source_timestamp) |
| 399 | |
Brett Cannon | e52c919 | 2009-11-07 23:55:05 +0000 | [diff] [blame] | 400 | # [bytecode read-only] |
| 401 | @source_util.writes_bytecode_files |
| 402 | def test_read_only_bytecode(self): |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 403 | # When bytecode is read-only but should be rewritten, fail silently. |
Brett Cannon | e52c919 | 2009-11-07 23:55:05 +0000 | [diff] [blame] | 404 | with source_util.create_modules('_temp') as mapping: |
| 405 | # Create bytecode that will need to be re-created. |
| 406 | py_compile.compile(mapping['_temp']) |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame^] | 407 | bytecode_path = self.util.cache_from_source(mapping['_temp']) |
Brett Cannon | e52c919 | 2009-11-07 23:55:05 +0000 | [diff] [blame] | 408 | with open(bytecode_path, 'r+b') as bytecode_file: |
| 409 | bytecode_file.seek(0) |
| 410 | bytecode_file.write(b'\x00\x00\x00\x00') |
| 411 | # Make the bytecode read-only. |
| 412 | os.chmod(bytecode_path, |
| 413 | stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH) |
| 414 | try: |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 415 | # Should not raise OSError! |
Brett Cannon | e52c919 | 2009-11-07 23:55:05 +0000 | [diff] [blame] | 416 | self.import_(mapping['_temp'], '_temp') |
| 417 | finally: |
| 418 | # Make writable for eventual clean-up. |
| 419 | os.chmod(bytecode_path, stat.S_IWUSR) |
| 420 | |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame^] | 421 | Frozen_SourceBadBytecode, Source_SourceBadBytecode = util.test_both( |
| 422 | SourceLoaderBadBytecodeTest, importlib=importlib, machinery=machinery, |
| 423 | abc=importlib_abc, util=importlib_util) |
| 424 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 425 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 426 | class SourcelessLoaderBadBytecodeTest(BadBytecodeTest): |
| 427 | |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame^] | 428 | @classmethod |
| 429 | def setUpClass(cls): |
| 430 | cls.loader = cls.machinery.SourcelessFileLoader |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 431 | |
| 432 | def test_empty_file(self): |
| 433 | def test(name, mapping, bytecode_path): |
Brett Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 434 | with self.assertRaises(ImportError) as cm: |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 435 | self.import_(bytecode_path, name) |
Brett Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 436 | self.assertEqual(cm.exception.name, name) |
| 437 | self.assertEqual(cm.exception.path, bytecode_path) |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 438 | |
| 439 | self._test_empty_file(test, del_source=True) |
| 440 | |
| 441 | def test_partial_magic(self): |
| 442 | def test(name, mapping, bytecode_path): |
Brett Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 443 | with self.assertRaises(ImportError) as cm: |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 444 | self.import_(bytecode_path, name) |
Brett Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 445 | self.assertEqual(cm.exception.name, name) |
| 446 | self.assertEqual(cm.exception.path, bytecode_path) |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 447 | self._test_partial_magic(test, del_source=True) |
| 448 | |
| 449 | def test_magic_only(self): |
| 450 | def test(name, mapping, bytecode_path): |
| 451 | with self.assertRaises(EOFError): |
| 452 | self.import_(bytecode_path, name) |
| 453 | |
| 454 | self._test_magic_only(test, del_source=True) |
| 455 | |
| 456 | def test_bad_magic(self): |
| 457 | def test(name, mapping, bytecode_path): |
Brett Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 458 | with self.assertRaises(ImportError) as cm: |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 459 | self.import_(bytecode_path, name) |
Brett Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 460 | self.assertEqual(cm.exception.name, name) |
| 461 | self.assertEqual(cm.exception.path, bytecode_path) |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 462 | |
| 463 | self._test_bad_magic(test, del_source=True) |
| 464 | |
| 465 | def test_partial_timestamp(self): |
| 466 | def test(name, mapping, bytecode_path): |
| 467 | with self.assertRaises(EOFError): |
| 468 | self.import_(bytecode_path, name) |
| 469 | |
| 470 | self._test_partial_timestamp(test, del_source=True) |
| 471 | |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 472 | def test_partial_size(self): |
| 473 | def test(name, mapping, bytecode_path): |
| 474 | with self.assertRaises(EOFError): |
| 475 | self.import_(bytecode_path, name) |
| 476 | |
| 477 | self._test_partial_size(test, del_source=True) |
| 478 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 479 | def test_no_marshal(self): |
| 480 | self._test_no_marshal(del_source=True) |
| 481 | |
| 482 | def test_non_code_marshal(self): |
| 483 | self._test_non_code_marshal(del_source=True) |
| 484 | |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame^] | 485 | Frozen_SourcelessBadBytecode, Source_SourcelessBadBytecode = util.test_both( |
| 486 | SourcelessLoaderBadBytecodeTest, importlib=importlib, |
| 487 | machinery=machinery, abc=importlib_abc, util=importlib_util) |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 488 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 489 | |
| 490 | if __name__ == '__main__': |
Brett Cannon | 0dbb4c7 | 2013-05-31 18:56:47 -0400 | [diff] [blame] | 491 | unittest.main() |