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 |
| 9 | import sys |
| 10 | import unittest |
| 11 | |
| 12 | |
| 13 | class SimpleTest(unittest.TestCase): |
| 14 | |
| 15 | """Should have no issue importing a source module [basic]. And if there is |
| 16 | a syntax error, it should raise a SyntaxError [syntax error]. |
| 17 | |
| 18 | """ |
| 19 | |
| 20 | # [basic] |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 21 | def test_module(self): |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 22 | with source_util.create_modules('_temp') as mapping: |
Brett Cannon | f87e04d | 2009-03-12 22:47:53 +0000 | [diff] [blame] | 23 | loader = _bootstrap._PyPycFileLoader('_temp', mapping['_temp'], |
| 24 | False) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 25 | module = loader.load_module('_temp') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 26 | self.assertTrue('_temp' in sys.modules) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 27 | check = {'__name__': '_temp', '__file__': mapping['_temp'], |
Brett Cannon | 06c9d96 | 2009-02-07 01:52:25 +0000 | [diff] [blame] | 28 | '__package__': ''} |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 29 | for attr, value in check.items(): |
| 30 | self.assertEqual(getattr(module, attr), value) |
| 31 | |
| 32 | def test_package(self): |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 33 | with source_util.create_modules('_pkg.__init__') as mapping: |
Brett Cannon | f87e04d | 2009-03-12 22:47:53 +0000 | [diff] [blame] | 34 | loader = _bootstrap._PyPycFileLoader('_pkg', |
| 35 | mapping['_pkg.__init__'], |
| 36 | True) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 37 | module = loader.load_module('_pkg') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 38 | self.assertTrue('_pkg' in sys.modules) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 39 | check = {'__name__': '_pkg', '__file__': mapping['_pkg.__init__'], |
| 40 | '__path__': [os.path.dirname(mapping['_pkg.__init__'])], |
| 41 | '__package__': '_pkg'} |
| 42 | for attr, value in check.items(): |
| 43 | self.assertEqual(getattr(module, attr), value) |
| 44 | |
| 45 | |
| 46 | def test_lacking_parent(self): |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 47 | with source_util.create_modules('_pkg.__init__', '_pkg.mod')as mapping: |
Brett Cannon | f87e04d | 2009-03-12 22:47:53 +0000 | [diff] [blame] | 48 | loader = _bootstrap._PyPycFileLoader('_pkg.mod', |
| 49 | mapping['_pkg.mod'], False) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 50 | module = loader.load_module('_pkg.mod') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 51 | self.assertTrue('_pkg.mod' in sys.modules) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 52 | check = {'__name__': '_pkg.mod', '__file__': mapping['_pkg.mod'], |
| 53 | '__package__': '_pkg'} |
| 54 | for attr, value in check.items(): |
| 55 | self.assertEqual(getattr(module, attr), value) |
| 56 | |
| 57 | def fake_mtime(self, fxn): |
| 58 | """Fake mtime to always be higher than expected.""" |
| 59 | return lambda name: fxn(name) + 1 |
| 60 | |
| 61 | def test_module_reuse(self): |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 62 | with source_util.create_modules('_temp') as mapping: |
Brett Cannon | f87e04d | 2009-03-12 22:47:53 +0000 | [diff] [blame] | 63 | loader = _bootstrap._PyPycFileLoader('_temp', mapping['_temp'], |
| 64 | False) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 65 | module = loader.load_module('_temp') |
| 66 | module_id = id(module) |
| 67 | module_dict_id = id(module.__dict__) |
| 68 | with open(mapping['_temp'], 'w') as file: |
| 69 | file.write("testing_var = 42\n") |
| 70 | # For filesystems where the mtime is only to a second granularity, |
| 71 | # everything that has happened above can be too fast; |
| 72 | # force an mtime on the source that is guaranteed to be different |
| 73 | # than the original mtime. |
| 74 | loader.source_mtime = self.fake_mtime(loader.source_mtime) |
| 75 | module = loader.load_module('_temp') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 76 | self.assertTrue('testing_var' in module.__dict__, |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 77 | "'testing_var' not in " |
| 78 | "{0}".format(list(module.__dict__.keys()))) |
| 79 | self.assertEqual(module, sys.modules['_temp']) |
| 80 | self.assertEqual(id(module), module_id) |
| 81 | self.assertEqual(id(module.__dict__), module_dict_id) |
| 82 | |
| 83 | def test_state_after_failure(self): |
| 84 | # A failed reload should leave the original module intact. |
| 85 | attributes = ('__file__', '__path__', '__package__') |
| 86 | value = '<test>' |
| 87 | name = '_temp' |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 88 | with source_util.create_modules(name) as mapping: |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 89 | orig_module = imp.new_module(name) |
| 90 | for attr in attributes: |
| 91 | setattr(orig_module, attr, value) |
| 92 | with open(mapping[name], 'w') as file: |
| 93 | file.write('+++ bad syntax +++') |
Brett Cannon | f87e04d | 2009-03-12 22:47:53 +0000 | [diff] [blame] | 94 | loader = _bootstrap._PyPycFileLoader('_temp', mapping['_temp'], |
| 95 | False) |
Brett Cannon | 2153dc0 | 2009-08-27 23:49:21 +0000 | [diff] [blame^] | 96 | with self.assertRaises(SyntaxError): |
| 97 | loader.load_module(name) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 98 | for attr in attributes: |
| 99 | self.assertEqual(getattr(orig_module, attr), value) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 100 | |
| 101 | # [syntax error] |
| 102 | def test_bad_syntax(self): |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 103 | with source_util.create_modules('_temp') as mapping: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 104 | with open(mapping['_temp'], 'w') as file: |
| 105 | file.write('=') |
Brett Cannon | f87e04d | 2009-03-12 22:47:53 +0000 | [diff] [blame] | 106 | loader = _bootstrap._PyPycFileLoader('_temp', mapping['_temp'], |
| 107 | False) |
Brett Cannon | 2153dc0 | 2009-08-27 23:49:21 +0000 | [diff] [blame^] | 108 | with self.assertRaises(SyntaxError): |
| 109 | loader.load_module('_temp') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 110 | self.assertTrue('_temp' not in sys.modules) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 111 | |
| 112 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 113 | class BadBytecodeTest(unittest.TestCase): |
| 114 | |
| 115 | """But there are several things about the bytecode which might lead to the |
| 116 | source being preferred. If the magic number differs from what the |
| 117 | interpreter uses, then the source is used with the bytecode regenerated. |
| 118 | If the timestamp is older than the modification time for the source then |
| 119 | the bytecode is not used [bad timestamp]. |
| 120 | |
| 121 | But if the marshal data is bad, even if the magic number and timestamp |
| 122 | work, a ValueError is raised and the source is not used [bad marshal]. |
| 123 | |
| 124 | """ |
| 125 | |
| 126 | def import_(self, file, module_name): |
Brett Cannon | f87e04d | 2009-03-12 22:47:53 +0000 | [diff] [blame] | 127 | loader = _bootstrap._PyPycFileLoader(module_name, file, False) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 128 | module = loader.load_module(module_name) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 129 | self.assertTrue(module_name in sys.modules) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 130 | |
| 131 | # [bad magic] |
Brett Cannon | 1262e7c | 2009-05-11 01:47:11 +0000 | [diff] [blame] | 132 | @source_util.writes_bytecode_files |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 133 | def test_bad_magic(self): |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 134 | with source_util.create_modules('_temp') as mapping: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 135 | py_compile.compile(mapping['_temp']) |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 136 | bytecode_path = source_util.bytecode_path(mapping['_temp']) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 137 | with open(bytecode_path, 'r+b') as bytecode_file: |
| 138 | bytecode_file.seek(0) |
| 139 | bytecode_file.write(b'\x00\x00\x00\x00') |
| 140 | self.import_(mapping['_temp'], '_temp') |
| 141 | with open(bytecode_path, 'rb') as bytecode_file: |
| 142 | self.assertEqual(bytecode_file.read(4), imp.get_magic()) |
| 143 | |
| 144 | # [bad timestamp] |
Brett Cannon | 1262e7c | 2009-05-11 01:47:11 +0000 | [diff] [blame] | 145 | @source_util.writes_bytecode_files |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 146 | def test_bad_bytecode(self): |
| 147 | zeros = b'\x00\x00\x00\x00' |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 148 | with source_util.create_modules('_temp') as mapping: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 149 | py_compile.compile(mapping['_temp']) |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 150 | bytecode_path = source_util.bytecode_path(mapping['_temp']) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 151 | with open(bytecode_path, 'r+b') as bytecode_file: |
| 152 | bytecode_file.seek(4) |
| 153 | bytecode_file.write(zeros) |
| 154 | self.import_(mapping['_temp'], '_temp') |
| 155 | source_mtime = os.path.getmtime(mapping['_temp']) |
| 156 | source_timestamp = importlib._w_long(source_mtime) |
| 157 | with open(bytecode_path, 'rb') as bytecode_file: |
| 158 | bytecode_file.seek(4) |
| 159 | self.assertEqual(bytecode_file.read(4), source_timestamp) |
| 160 | |
| 161 | # [bad marshal] |
| 162 | def test_bad_marshal(self): |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 163 | with source_util.create_modules('_temp') as mapping: |
| 164 | bytecode_path = source_util.bytecode_path(mapping['_temp']) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 165 | source_mtime = os.path.getmtime(mapping['_temp']) |
| 166 | source_timestamp = importlib._w_long(source_mtime) |
| 167 | with open(bytecode_path, 'wb') as bytecode_file: |
| 168 | bytecode_file.write(imp.get_magic()) |
| 169 | bytecode_file.write(source_timestamp) |
| 170 | bytecode_file.write(b'AAAA') |
Brett Cannon | 2153dc0 | 2009-08-27 23:49:21 +0000 | [diff] [blame^] | 171 | with self.assertRaises(ValueError): |
| 172 | self.import_(mapping['_temp'], '_temp') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 173 | self.assertTrue('_temp' not in sys.modules) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 174 | |
| 175 | |
| 176 | def test_main(): |
| 177 | from test.support import run_unittest |
| 178 | run_unittest(SimpleTest, DontWriteBytecodeTest, BadDataTest, |
| 179 | SourceBytecodeInteraction, BadBytecodeTest) |
| 180 | |
| 181 | |
| 182 | if __name__ == '__main__': |
| 183 | test_main() |