blob: 343e120e4c9986cad8d88193ac16c16f6864a2f6 [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
Barry Warsaw04b56842010-05-18 14:15:20 +000013from test.support import make_legacy_pyc
14
Brett Cannon23cbd8a2009-01-18 00:24:28 +000015
16class 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 Cannon30b047d2009-02-01 02:05:11 +000024 def test_module(self):
Brett Cannon4ee2cda2009-02-01 03:08:31 +000025 with source_util.create_modules('_temp') as mapping:
Brett Cannonf87e04d2009-03-12 22:47:53 +000026 loader = _bootstrap._PyPycFileLoader('_temp', mapping['_temp'],
27 False)
Brett Cannon30b047d2009-02-01 02:05:11 +000028 module = loader.load_module('_temp')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000029 self.assertTrue('_temp' in sys.modules)
Brett Cannon30b047d2009-02-01 02:05:11 +000030 check = {'__name__': '_temp', '__file__': mapping['_temp'],
Brett Cannon06c9d962009-02-07 01:52:25 +000031 '__package__': ''}
Brett Cannon30b047d2009-02-01 02:05:11 +000032 for attr, value in check.items():
33 self.assertEqual(getattr(module, attr), value)
34
35 def test_package(self):
Brett Cannon4ee2cda2009-02-01 03:08:31 +000036 with source_util.create_modules('_pkg.__init__') as mapping:
Brett Cannonf87e04d2009-03-12 22:47:53 +000037 loader = _bootstrap._PyPycFileLoader('_pkg',
38 mapping['_pkg.__init__'],
39 True)
Brett Cannon30b047d2009-02-01 02:05:11 +000040 module = loader.load_module('_pkg')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000041 self.assertTrue('_pkg' in sys.modules)
Brett Cannon30b047d2009-02-01 02:05:11 +000042 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 Cannon4ee2cda2009-02-01 03:08:31 +000050 with source_util.create_modules('_pkg.__init__', '_pkg.mod')as mapping:
Brett Cannonf87e04d2009-03-12 22:47:53 +000051 loader = _bootstrap._PyPycFileLoader('_pkg.mod',
52 mapping['_pkg.mod'], False)
Brett Cannon30b047d2009-02-01 02:05:11 +000053 module = loader.load_module('_pkg.mod')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000054 self.assertTrue('_pkg.mod' in sys.modules)
Brett Cannon30b047d2009-02-01 02:05:11 +000055 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 Cannon4ee2cda2009-02-01 03:08:31 +000065 with source_util.create_modules('_temp') as mapping:
Brett Cannonf87e04d2009-03-12 22:47:53 +000066 loader = _bootstrap._PyPycFileLoader('_temp', mapping['_temp'],
67 False)
Brett Cannon30b047d2009-02-01 02:05:11 +000068 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 Petersonc9c0f202009-06-30 23:06:06 +000079 self.assertTrue('testing_var' in module.__dict__,
Brett Cannon30b047d2009-02-01 02:05:11 +000080 "'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 Cannon4ee2cda2009-02-01 03:08:31 +000091 with source_util.create_modules(name) as mapping:
Brett Cannon30b047d2009-02-01 02:05:11 +000092 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 Cannonf87e04d2009-03-12 22:47:53 +000097 loader = _bootstrap._PyPycFileLoader('_temp', mapping['_temp'],
98 False)
Brett Cannon2153dc02009-08-27 23:49:21 +000099 with self.assertRaises(SyntaxError):
100 loader.load_module(name)
Brett Cannon30b047d2009-02-01 02:05:11 +0000101 for attr in attributes:
102 self.assertEqual(getattr(orig_module, attr), value)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000103
104 # [syntax error]
105 def test_bad_syntax(self):
Brett Cannon4ee2cda2009-02-01 03:08:31 +0000106 with source_util.create_modules('_temp') as mapping:
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000107 with open(mapping['_temp'], 'w') as file:
108 file.write('=')
Brett Cannonf87e04d2009-03-12 22:47:53 +0000109 loader = _bootstrap._PyPycFileLoader('_temp', mapping['_temp'],
110 False)
Brett Cannon2153dc02009-08-27 23:49:21 +0000111 with self.assertRaises(SyntaxError):
112 loader.load_module('_temp')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000113 self.assertTrue('_temp' not in sys.modules)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000114
115
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000116class BadBytecodeTest(unittest.TestCase):
117
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000118 def import_(self, file, module_name):
Brett Cannonf87e04d2009-03-12 22:47:53 +0000119 loader = _bootstrap._PyPycFileLoader(module_name, file, False)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000120 module = loader.load_module(module_name)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000121 self.assertTrue(module_name in sys.modules)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000122
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000123 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 Warsaw28a691b2010-04-17 00:19:56 +0000132 bytecode_path = imp.cache_from_source(mapping[name])
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000133 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 Warsaw04b56842010-05-18 14:15:20 +0000141 make_legacy_pyc(mapping[name])
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000142 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 Cannon1262e7c2009-05-11 01:47:11 +0000213 @source_util.writes_bytecode_files
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000214 def test_bad_magic(self):
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000215 # When the magic number is different, the bytecode should be
216 # regenerated.
Brett Cannon4ee2cda2009-02-01 03:08:31 +0000217 with source_util.create_modules('_temp') as mapping:
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000218 bc_path = self.manipulate_bytecode('_temp', mapping,
219 lambda bc: b'\x00\x00\x00\x00' + bc[4:])
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000220 self.import_(mapping['_temp'], '_temp')
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000221 with open(bc_path, 'rb') as bytecode_file:
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000222 self.assertEqual(bytecode_file.read(4), imp.get_magic())
223
224 # [bad timestamp]
Brett Cannon1262e7c2009-05-11 01:47:11 +0000225 @source_util.writes_bytecode_files
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000226 def test_bad_bytecode(self):
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000227 # When the timestamp is older than the source, bytecode should be
228 # regenerated.
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000229 zeros = b'\x00\x00\x00\x00'
Brett Cannon4ee2cda2009-02-01 03:08:31 +0000230 with source_util.create_modules('_temp') as mapping:
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000231 py_compile.compile(mapping['_temp'])
Barry Warsaw28a691b2010-04-17 00:19:56 +0000232 bytecode_path = imp.cache_from_source(mapping['_temp'])
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000233 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 Cannone52c9192009-11-07 23:55:05 +0000244 @source_util.writes_bytecode_files
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000245 def test_bad_marshal(self):
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000246 # Bad marshal data should raise a ValueError.
Brett Cannon4ee2cda2009-02-01 03:08:31 +0000247 with source_util.create_modules('_temp') as mapping:
Barry Warsaw28a691b2010-04-17 00:19:56 +0000248 bytecode_path = imp.cache_from_source(mapping['_temp'])
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000249 source_mtime = os.path.getmtime(mapping['_temp'])
250 source_timestamp = importlib._w_long(source_mtime)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000251 source_util.ensure_bytecode_path(bytecode_path)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000252 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 Cannon2153dc02009-08-27 23:49:21 +0000256 with self.assertRaises(ValueError):
257 self.import_(mapping['_temp'], '_temp')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000258 self.assertTrue('_temp' not in sys.modules)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000259
Brett Cannone52c9192009-11-07 23:55:05 +0000260 # [bytecode read-only]
261 @source_util.writes_bytecode_files
262 def test_read_only_bytecode(self):
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000263 # When bytecode is read-only but should be rewritten, fail silently.
Brett Cannone52c9192009-11-07 23:55:05 +0000264 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 Warsaw28a691b2010-04-17 00:19:56 +0000267 bytecode_path = imp.cache_from_source(mapping['_temp'])
Brett Cannone52c9192009-11-07 23:55:05 +0000268 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 Cannon23cbd8a2009-01-18 00:24:28 +0000281
282def test_main():
283 from test.support import run_unittest
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000284 run_unittest(SimpleTest, BadBytecodeTest)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000285
286
287if __name__ == '__main__':
288 test_main()