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