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