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