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