blob: 905940501c6d594091dde1d855190787913d9473 [file] [log] [blame]
Brett Cannon23cbd8a2009-01-18 00:24:28 +00001import importlib
Brett Cannonf87e04d2009-03-12 22:47:53 +00002from importlib import _bootstrap
Brett Cannon30b047d2009-02-01 02:05:11 +00003from .. import abc
Brett Cannon4ee2cda2009-02-01 03:08:31 +00004from . import util as source_util
Brett Cannon23cbd8a2009-01-18 00:24:28 +00005
6import imp
7import os
8import py_compile
Brett Cannone52c9192009-11-07 23:55:05 +00009import stat
Brett Cannon23cbd8a2009-01-18 00:24:28 +000010import sys
11import unittest
12
13
14class 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 Cannon30b047d2009-02-01 02:05:11 +000022 def test_module(self):
Brett Cannon4ee2cda2009-02-01 03:08:31 +000023 with source_util.create_modules('_temp') as mapping:
Brett Cannonf87e04d2009-03-12 22:47:53 +000024 loader = _bootstrap._PyPycFileLoader('_temp', mapping['_temp'],
25 False)
Brett Cannon30b047d2009-02-01 02:05:11 +000026 module = loader.load_module('_temp')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000027 self.assertTrue('_temp' in sys.modules)
Brett Cannon30b047d2009-02-01 02:05:11 +000028 check = {'__name__': '_temp', '__file__': mapping['_temp'],
Brett Cannon06c9d962009-02-07 01:52:25 +000029 '__package__': ''}
Brett Cannon30b047d2009-02-01 02:05:11 +000030 for attr, value in check.items():
31 self.assertEqual(getattr(module, attr), value)
32
33 def test_package(self):
Brett Cannon4ee2cda2009-02-01 03:08:31 +000034 with source_util.create_modules('_pkg.__init__') as mapping:
Brett Cannonf87e04d2009-03-12 22:47:53 +000035 loader = _bootstrap._PyPycFileLoader('_pkg',
36 mapping['_pkg.__init__'],
37 True)
Brett Cannon30b047d2009-02-01 02:05:11 +000038 module = loader.load_module('_pkg')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000039 self.assertTrue('_pkg' in sys.modules)
Brett Cannon30b047d2009-02-01 02:05:11 +000040 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 Cannon4ee2cda2009-02-01 03:08:31 +000048 with source_util.create_modules('_pkg.__init__', '_pkg.mod')as mapping:
Brett Cannonf87e04d2009-03-12 22:47:53 +000049 loader = _bootstrap._PyPycFileLoader('_pkg.mod',
50 mapping['_pkg.mod'], False)
Brett Cannon30b047d2009-02-01 02:05:11 +000051 module = loader.load_module('_pkg.mod')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000052 self.assertTrue('_pkg.mod' in sys.modules)
Brett Cannon30b047d2009-02-01 02:05:11 +000053 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 Cannon4ee2cda2009-02-01 03:08:31 +000063 with source_util.create_modules('_temp') as mapping:
Brett Cannonf87e04d2009-03-12 22:47:53 +000064 loader = _bootstrap._PyPycFileLoader('_temp', mapping['_temp'],
65 False)
Brett Cannon30b047d2009-02-01 02:05:11 +000066 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 Petersonc9c0f202009-06-30 23:06:06 +000077 self.assertTrue('testing_var' in module.__dict__,
Brett Cannon30b047d2009-02-01 02:05:11 +000078 "'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 Cannon4ee2cda2009-02-01 03:08:31 +000089 with source_util.create_modules(name) as mapping:
Brett Cannon30b047d2009-02-01 02:05:11 +000090 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 Cannonf87e04d2009-03-12 22:47:53 +000095 loader = _bootstrap._PyPycFileLoader('_temp', mapping['_temp'],
96 False)
Brett Cannon2153dc02009-08-27 23:49:21 +000097 with self.assertRaises(SyntaxError):
98 loader.load_module(name)
Brett Cannon30b047d2009-02-01 02:05:11 +000099 for attr in attributes:
100 self.assertEqual(getattr(orig_module, attr), value)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000101
102 # [syntax error]
103 def test_bad_syntax(self):
Brett Cannon4ee2cda2009-02-01 03:08:31 +0000104 with source_util.create_modules('_temp') as mapping:
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000105 with open(mapping['_temp'], 'w') as file:
106 file.write('=')
Brett Cannonf87e04d2009-03-12 22:47:53 +0000107 loader = _bootstrap._PyPycFileLoader('_temp', mapping['_temp'],
108 False)
Brett Cannon2153dc02009-08-27 23:49:21 +0000109 with self.assertRaises(SyntaxError):
110 loader.load_module('_temp')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000111 self.assertTrue('_temp' not in sys.modules)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000112
113
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000114class BadBytecodeTest(unittest.TestCase):
115
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000116 def import_(self, file, module_name):
Brett Cannonf87e04d2009-03-12 22:47:53 +0000117 loader = _bootstrap._PyPycFileLoader(module_name, file, False)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000118 module = loader.load_module(module_name)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000119 self.assertTrue(module_name in sys.modules)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000120
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000121 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 Warsaw28a691b2010-04-17 00:19:56 +0000130 bytecode_path = imp.cache_from_source(mapping[name])
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000131 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 Cannon1262e7c2009-05-11 01:47:11 +0000210 @source_util.writes_bytecode_files
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000211 def test_bad_magic(self):
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000212 # When the magic number is different, the bytecode should be
213 # regenerated.
Brett Cannon4ee2cda2009-02-01 03:08:31 +0000214 with source_util.create_modules('_temp') as mapping:
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000215 bc_path = self.manipulate_bytecode('_temp', mapping,
216 lambda bc: b'\x00\x00\x00\x00' + bc[4:])
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000217 self.import_(mapping['_temp'], '_temp')
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000218 with open(bc_path, 'rb') as bytecode_file:
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000219 self.assertEqual(bytecode_file.read(4), imp.get_magic())
220
221 # [bad timestamp]
Brett Cannon1262e7c2009-05-11 01:47:11 +0000222 @source_util.writes_bytecode_files
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000223 def test_bad_bytecode(self):
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000224 # When the timestamp is older than the source, bytecode should be
225 # regenerated.
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000226 zeros = b'\x00\x00\x00\x00'
Brett Cannon4ee2cda2009-02-01 03:08:31 +0000227 with source_util.create_modules('_temp') as mapping:
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000228 py_compile.compile(mapping['_temp'])
Barry Warsaw28a691b2010-04-17 00:19:56 +0000229 bytecode_path = imp.cache_from_source(mapping['_temp'])
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000230 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 Cannone52c9192009-11-07 23:55:05 +0000241 @source_util.writes_bytecode_files
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000242 def test_bad_marshal(self):
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000243 # Bad marshal data should raise a ValueError.
Brett Cannon4ee2cda2009-02-01 03:08:31 +0000244 with source_util.create_modules('_temp') as mapping:
Barry Warsaw28a691b2010-04-17 00:19:56 +0000245 bytecode_path = imp.cache_from_source(mapping['_temp'])
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000246 source_mtime = os.path.getmtime(mapping['_temp'])
247 source_timestamp = importlib._w_long(source_mtime)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000248 source_util.ensure_bytecode_path(bytecode_path)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000249 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 Cannon2153dc02009-08-27 23:49:21 +0000253 with self.assertRaises(ValueError):
254 self.import_(mapping['_temp'], '_temp')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000255 self.assertTrue('_temp' not in sys.modules)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000256
Brett Cannone52c9192009-11-07 23:55:05 +0000257 # [bytecode read-only]
258 @source_util.writes_bytecode_files
259 def test_read_only_bytecode(self):
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000260 # When bytecode is read-only but should be rewritten, fail silently.
Brett Cannone52c9192009-11-07 23:55:05 +0000261 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 Warsaw28a691b2010-04-17 00:19:56 +0000264 bytecode_path = imp.cache_from_source(mapping['_temp'])
Brett Cannone52c9192009-11-07 23:55:05 +0000265 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 Cannon23cbd8a2009-01-18 00:24:28 +0000278
279def test_main():
280 from test.support import run_unittest
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000281 run_unittest(SimpleTest, BadBytecodeTest)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000282
283
284if __name__ == '__main__':
285 test_main()