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 | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 4 | from . import util as source_util |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 5 | |
| 6 | import imp |
| 7 | import os |
| 8 | import py_compile |
Brett Cannon | e52c919 | 2009-11-07 23:55:05 +0000 | [diff] [blame] | 9 | import stat |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 10 | import sys |
| 11 | import unittest |
| 12 | |
Barry Warsaw | 04b5684 | 2010-05-18 14:15:20 +0000 | [diff] [blame^] | 13 | from test.support import make_legacy_pyc |
| 14 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 15 | |
| 16 | class SimpleTest(unittest.TestCase): |
| 17 | |
| 18 | """Should have no issue importing a source module [basic]. And if there is |
| 19 | a syntax error, it should raise a SyntaxError [syntax error]. |
| 20 | |
| 21 | """ |
| 22 | |
| 23 | # [basic] |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 24 | def test_module(self): |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 25 | with source_util.create_modules('_temp') as mapping: |
Brett Cannon | f87e04d | 2009-03-12 22:47:53 +0000 | [diff] [blame] | 26 | loader = _bootstrap._PyPycFileLoader('_temp', mapping['_temp'], |
| 27 | False) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 28 | module = loader.load_module('_temp') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 29 | self.assertTrue('_temp' in sys.modules) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 30 | check = {'__name__': '_temp', '__file__': mapping['_temp'], |
Brett Cannon | 06c9d96 | 2009-02-07 01:52:25 +0000 | [diff] [blame] | 31 | '__package__': ''} |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 32 | for attr, value in check.items(): |
| 33 | self.assertEqual(getattr(module, attr), value) |
| 34 | |
| 35 | def test_package(self): |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 36 | with source_util.create_modules('_pkg.__init__') as mapping: |
Brett Cannon | f87e04d | 2009-03-12 22:47:53 +0000 | [diff] [blame] | 37 | loader = _bootstrap._PyPycFileLoader('_pkg', |
| 38 | mapping['_pkg.__init__'], |
| 39 | True) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 40 | module = loader.load_module('_pkg') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 41 | self.assertTrue('_pkg' in sys.modules) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 42 | check = {'__name__': '_pkg', '__file__': mapping['_pkg.__init__'], |
| 43 | '__path__': [os.path.dirname(mapping['_pkg.__init__'])], |
| 44 | '__package__': '_pkg'} |
| 45 | for attr, value in check.items(): |
| 46 | self.assertEqual(getattr(module, attr), value) |
| 47 | |
| 48 | |
| 49 | def test_lacking_parent(self): |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 50 | with source_util.create_modules('_pkg.__init__', '_pkg.mod')as mapping: |
Brett Cannon | f87e04d | 2009-03-12 22:47:53 +0000 | [diff] [blame] | 51 | loader = _bootstrap._PyPycFileLoader('_pkg.mod', |
| 52 | mapping['_pkg.mod'], False) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 53 | module = loader.load_module('_pkg.mod') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 54 | self.assertTrue('_pkg.mod' in sys.modules) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 55 | check = {'__name__': '_pkg.mod', '__file__': mapping['_pkg.mod'], |
| 56 | '__package__': '_pkg'} |
| 57 | for attr, value in check.items(): |
| 58 | self.assertEqual(getattr(module, attr), value) |
| 59 | |
| 60 | def fake_mtime(self, fxn): |
| 61 | """Fake mtime to always be higher than expected.""" |
| 62 | return lambda name: fxn(name) + 1 |
| 63 | |
| 64 | def test_module_reuse(self): |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 65 | with source_util.create_modules('_temp') as mapping: |
Brett Cannon | f87e04d | 2009-03-12 22:47:53 +0000 | [diff] [blame] | 66 | loader = _bootstrap._PyPycFileLoader('_temp', mapping['_temp'], |
| 67 | False) |
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. |
| 77 | loader.source_mtime = self.fake_mtime(loader.source_mtime) |
| 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 | f87e04d | 2009-03-12 22:47:53 +0000 | [diff] [blame] | 97 | loader = _bootstrap._PyPycFileLoader('_temp', mapping['_temp'], |
| 98 | False) |
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 | f87e04d | 2009-03-12 22:47:53 +0000 | [diff] [blame] | 109 | loader = _bootstrap._PyPycFileLoader('_temp', mapping['_temp'], |
| 110 | False) |
Brett Cannon | 2153dc0 | 2009-08-27 23:49:21 +0000 | [diff] [blame] | 111 | with self.assertRaises(SyntaxError): |
| 112 | loader.load_module('_temp') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 113 | self.assertTrue('_temp' not in sys.modules) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 114 | |
| 115 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 116 | class BadBytecodeTest(unittest.TestCase): |
| 117 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 118 | def import_(self, file, module_name): |
Brett Cannon | f87e04d | 2009-03-12 22:47:53 +0000 | [diff] [blame] | 119 | loader = _bootstrap._PyPycFileLoader(module_name, file, False) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 120 | module = loader.load_module(module_name) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 121 | self.assertTrue(module_name in sys.modules) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 122 | |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 123 | def manipulate_bytecode(self, name, mapping, manipulator, *, |
| 124 | del_source=False): |
| 125 | """Manipulate the bytecode of a module by passing it into a callable |
| 126 | that returns what to use as the new bytecode.""" |
| 127 | try: |
| 128 | del sys.modules['_temp'] |
| 129 | except KeyError: |
| 130 | pass |
| 131 | py_compile.compile(mapping[name]) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 132 | bytecode_path = imp.cache_from_source(mapping[name]) |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 133 | with open(bytecode_path, 'rb') as file: |
| 134 | bc = file.read() |
| 135 | new_bc = manipulator(bc) |
| 136 | with open(bytecode_path, 'wb') as file: |
| 137 | if new_bc: |
| 138 | file.write(new_bc) |
| 139 | if del_source: |
| 140 | os.unlink(mapping[name]) |
Barry Warsaw | 04b5684 | 2010-05-18 14:15:20 +0000 | [diff] [blame^] | 141 | make_legacy_pyc(mapping[name]) |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 142 | return bytecode_path |
| 143 | |
| 144 | @source_util.writes_bytecode_files |
| 145 | def test_empty_file(self): |
| 146 | # When a .pyc is empty, regenerate it if possible, else raise |
| 147 | # ImportError. |
| 148 | with source_util.create_modules('_temp') as mapping: |
| 149 | bc_path = self.manipulate_bytecode('_temp', mapping, |
| 150 | lambda bc: None) |
| 151 | self.import_(mapping['_temp'], '_temp') |
| 152 | with open(bc_path, 'rb') as file: |
| 153 | self.assertGreater(len(file.read()), 8) |
| 154 | self.manipulate_bytecode('_temp', mapping, lambda bc: None, |
| 155 | del_source=True) |
| 156 | with self.assertRaises(ImportError): |
| 157 | self.import_(mapping['_temp'], '_temp') |
| 158 | |
| 159 | @source_util.writes_bytecode_files |
| 160 | def test_partial_magic(self): |
| 161 | # When their are less than 4 bytes to a .pyc, regenerate it if |
| 162 | # possible, else raise ImportError. |
| 163 | with source_util.create_modules('_temp') as mapping: |
| 164 | bc_path = self.manipulate_bytecode('_temp', mapping, |
| 165 | lambda bc: bc[:3]) |
| 166 | self.import_(mapping['_temp'], '_temp') |
| 167 | with open(bc_path, 'rb') as file: |
| 168 | self.assertGreater(len(file.read()), 8) |
| 169 | self.manipulate_bytecode('_temp', mapping, lambda bc: bc[:3], |
| 170 | del_source=True) |
| 171 | with self.assertRaises(ImportError): |
| 172 | self.import_(mapping['_temp'], '_temp') |
| 173 | |
| 174 | @source_util.writes_bytecode_files |
| 175 | def test_magic_only(self): |
| 176 | # When there is only the magic number, regenerate the .pyc if possible, |
| 177 | # else raise EOFError. |
| 178 | with source_util.create_modules('_temp') as mapping: |
| 179 | bc_path = self.manipulate_bytecode('_temp', mapping, |
| 180 | lambda bc: bc[:4]) |
| 181 | self.import_(mapping['_temp'], '_temp') |
| 182 | with open(bc_path, 'rb') as file: |
| 183 | self.assertGreater(len(file.read()), 8) |
| 184 | self.manipulate_bytecode('_temp', mapping, lambda bc: bc[:4], |
| 185 | del_source=True) |
| 186 | with self.assertRaises(EOFError): |
| 187 | self.import_(mapping['_temp'], '_temp') |
| 188 | |
| 189 | @source_util.writes_bytecode_files |
| 190 | def test_partial_timestamp(self): |
| 191 | # When the timestamp is partial, regenerate the .pyc, else |
| 192 | # raise EOFError. |
| 193 | with source_util.create_modules('_temp') as mapping: |
| 194 | bc_path = self.manipulate_bytecode('_temp', mapping, |
| 195 | lambda bc: bc[:7]) |
| 196 | self.import_(mapping['_temp'], '_temp') |
| 197 | with open(bc_path, 'rb') as file: |
| 198 | self.assertGreater(len(file.read()), 8) |
| 199 | self.manipulate_bytecode('_temp', mapping, lambda bc: bc[:7], |
| 200 | del_source=True) |
| 201 | with self.assertRaises(EOFError): |
| 202 | self.import_(mapping['_temp'], '_temp') |
| 203 | |
| 204 | @source_util.writes_bytecode_files |
| 205 | def test_no_marshal(self): |
| 206 | # When there is only the magic number and timestamp, raise EOFError. |
| 207 | with source_util.create_modules('_temp') as mapping: |
| 208 | bc_path = self.manipulate_bytecode('_temp', mapping, |
| 209 | lambda bc: bc[:8]) |
| 210 | with self.assertRaises(EOFError): |
| 211 | self.import_(mapping['_temp'], '_temp') |
| 212 | |
Brett Cannon | 1262e7c | 2009-05-11 01:47:11 +0000 | [diff] [blame] | 213 | @source_util.writes_bytecode_files |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 214 | def test_bad_magic(self): |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 215 | # When the magic number is different, the bytecode should be |
| 216 | # regenerated. |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 217 | with source_util.create_modules('_temp') as mapping: |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 218 | bc_path = self.manipulate_bytecode('_temp', mapping, |
| 219 | lambda bc: b'\x00\x00\x00\x00' + bc[4:]) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 220 | self.import_(mapping['_temp'], '_temp') |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 221 | with open(bc_path, 'rb') as bytecode_file: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 222 | self.assertEqual(bytecode_file.read(4), imp.get_magic()) |
| 223 | |
| 224 | # [bad timestamp] |
Brett Cannon | 1262e7c | 2009-05-11 01:47:11 +0000 | [diff] [blame] | 225 | @source_util.writes_bytecode_files |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 226 | def test_bad_bytecode(self): |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 227 | # When the timestamp is older than the source, bytecode should be |
| 228 | # regenerated. |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 229 | zeros = b'\x00\x00\x00\x00' |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 230 | with source_util.create_modules('_temp') as mapping: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 231 | py_compile.compile(mapping['_temp']) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 232 | bytecode_path = imp.cache_from_source(mapping['_temp']) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 233 | with open(bytecode_path, 'r+b') as bytecode_file: |
| 234 | bytecode_file.seek(4) |
| 235 | bytecode_file.write(zeros) |
| 236 | self.import_(mapping['_temp'], '_temp') |
| 237 | source_mtime = os.path.getmtime(mapping['_temp']) |
| 238 | source_timestamp = importlib._w_long(source_mtime) |
| 239 | with open(bytecode_path, 'rb') as bytecode_file: |
| 240 | bytecode_file.seek(4) |
| 241 | self.assertEqual(bytecode_file.read(4), source_timestamp) |
| 242 | |
| 243 | # [bad marshal] |
Brett Cannon | e52c919 | 2009-11-07 23:55:05 +0000 | [diff] [blame] | 244 | @source_util.writes_bytecode_files |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 245 | def test_bad_marshal(self): |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 246 | # Bad marshal data should raise a ValueError. |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 247 | with source_util.create_modules('_temp') as mapping: |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 248 | bytecode_path = imp.cache_from_source(mapping['_temp']) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 249 | source_mtime = os.path.getmtime(mapping['_temp']) |
| 250 | source_timestamp = importlib._w_long(source_mtime) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 251 | source_util.ensure_bytecode_path(bytecode_path) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 252 | with open(bytecode_path, 'wb') as bytecode_file: |
| 253 | bytecode_file.write(imp.get_magic()) |
| 254 | bytecode_file.write(source_timestamp) |
| 255 | bytecode_file.write(b'AAAA') |
Brett Cannon | 2153dc0 | 2009-08-27 23:49:21 +0000 | [diff] [blame] | 256 | with self.assertRaises(ValueError): |
| 257 | self.import_(mapping['_temp'], '_temp') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 258 | self.assertTrue('_temp' not in sys.modules) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 259 | |
Brett Cannon | e52c919 | 2009-11-07 23:55:05 +0000 | [diff] [blame] | 260 | # [bytecode read-only] |
| 261 | @source_util.writes_bytecode_files |
| 262 | def test_read_only_bytecode(self): |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 263 | # When bytecode is read-only but should be rewritten, fail silently. |
Brett Cannon | e52c919 | 2009-11-07 23:55:05 +0000 | [diff] [blame] | 264 | with source_util.create_modules('_temp') as mapping: |
| 265 | # Create bytecode that will need to be re-created. |
| 266 | py_compile.compile(mapping['_temp']) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 267 | bytecode_path = imp.cache_from_source(mapping['_temp']) |
Brett Cannon | e52c919 | 2009-11-07 23:55:05 +0000 | [diff] [blame] | 268 | with open(bytecode_path, 'r+b') as bytecode_file: |
| 269 | bytecode_file.seek(0) |
| 270 | bytecode_file.write(b'\x00\x00\x00\x00') |
| 271 | # Make the bytecode read-only. |
| 272 | os.chmod(bytecode_path, |
| 273 | stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH) |
| 274 | try: |
| 275 | # Should not raise IOError! |
| 276 | self.import_(mapping['_temp'], '_temp') |
| 277 | finally: |
| 278 | # Make writable for eventual clean-up. |
| 279 | os.chmod(bytecode_path, stat.S_IWUSR) |
| 280 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 281 | |
| 282 | def test_main(): |
| 283 | from test.support import run_unittest |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 284 | run_unittest(SimpleTest, BadBytecodeTest) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 285 | |
| 286 | |
| 287 | if __name__ == '__main__': |
| 288 | test_main() |