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