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