blob: c7a7d8fbcae2d371c166f7a050c8e50844d6a3f6 [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 Cannond71bed32010-07-03 22:18:47 +00004from .. import util
Brett Cannon4ee2cda2009-02-01 03:08:31 +00005from . import util as source_util
Brett Cannon23cbd8a2009-01-18 00:24:28 +00006
Antoine Pitroudd21f682012-01-25 03:00:57 +01007import errno
Brett Cannon23cbd8a2009-01-18 00:24:28 +00008import imp
Brett Cannon61b14252010-07-03 21:48:25 +00009import marshal
Brett Cannon23cbd8a2009-01-18 00:24:28 +000010import os
11import py_compile
Brett Cannon186335b2010-08-22 22:11:06 +000012import shutil
Brett Cannone52c9192009-11-07 23:55:05 +000013import stat
Brett Cannon23cbd8a2009-01-18 00:24:28 +000014import sys
15import unittest
16
Barry Warsaw04b56842010-05-18 14:15:20 +000017from test.support import make_legacy_pyc
18
Brett Cannon23cbd8a2009-01-18 00:24:28 +000019
20class SimpleTest(unittest.TestCase):
21
22 """Should have no issue importing a source module [basic]. And if there is
23 a syntax error, it should raise a SyntaxError [syntax error].
24
25 """
26
27 # [basic]
Brett Cannon30b047d2009-02-01 02:05:11 +000028 def test_module(self):
Brett Cannon4ee2cda2009-02-01 03:08:31 +000029 with source_util.create_modules('_temp') as mapping:
Brett Cannon61b14252010-07-03 21:48:25 +000030 loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp'])
Brett Cannon30b047d2009-02-01 02:05:11 +000031 module = loader.load_module('_temp')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000032 self.assertTrue('_temp' in sys.modules)
Brett Cannon30b047d2009-02-01 02:05:11 +000033 check = {'__name__': '_temp', '__file__': mapping['_temp'],
Brett Cannon06c9d962009-02-07 01:52:25 +000034 '__package__': ''}
Brett Cannon30b047d2009-02-01 02:05:11 +000035 for attr, value in check.items():
36 self.assertEqual(getattr(module, attr), value)
37
38 def test_package(self):
Brett Cannon4ee2cda2009-02-01 03:08:31 +000039 with source_util.create_modules('_pkg.__init__') as mapping:
Brett Cannon61b14252010-07-03 21:48:25 +000040 loader = _bootstrap._SourceFileLoader('_pkg',
41 mapping['_pkg.__init__'])
Brett Cannon30b047d2009-02-01 02:05:11 +000042 module = loader.load_module('_pkg')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000043 self.assertTrue('_pkg' in sys.modules)
Brett Cannon30b047d2009-02-01 02:05:11 +000044 check = {'__name__': '_pkg', '__file__': mapping['_pkg.__init__'],
45 '__path__': [os.path.dirname(mapping['_pkg.__init__'])],
46 '__package__': '_pkg'}
47 for attr, value in check.items():
48 self.assertEqual(getattr(module, attr), value)
49
50
51 def test_lacking_parent(self):
Brett Cannon4ee2cda2009-02-01 03:08:31 +000052 with source_util.create_modules('_pkg.__init__', '_pkg.mod')as mapping:
Brett Cannon61b14252010-07-03 21:48:25 +000053 loader = _bootstrap._SourceFileLoader('_pkg.mod',
54 mapping['_pkg.mod'])
Brett Cannon30b047d2009-02-01 02:05:11 +000055 module = loader.load_module('_pkg.mod')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000056 self.assertTrue('_pkg.mod' in sys.modules)
Brett Cannon30b047d2009-02-01 02:05:11 +000057 check = {'__name__': '_pkg.mod', '__file__': mapping['_pkg.mod'],
58 '__package__': '_pkg'}
59 for attr, value in check.items():
60 self.assertEqual(getattr(module, attr), value)
61
62 def fake_mtime(self, fxn):
63 """Fake mtime to always be higher than expected."""
64 return lambda name: fxn(name) + 1
65
66 def test_module_reuse(self):
Brett Cannon4ee2cda2009-02-01 03:08:31 +000067 with source_util.create_modules('_temp') as mapping:
Brett Cannon61b14252010-07-03 21:48:25 +000068 loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp'])
Brett Cannon30b047d2009-02-01 02:05:11 +000069 module = loader.load_module('_temp')
70 module_id = id(module)
71 module_dict_id = id(module.__dict__)
72 with open(mapping['_temp'], 'w') as file:
73 file.write("testing_var = 42\n")
74 # For filesystems where the mtime is only to a second granularity,
75 # everything that has happened above can be too fast;
76 # force an mtime on the source that is guaranteed to be different
77 # than the original mtime.
Brett Cannon61b14252010-07-03 21:48:25 +000078 loader.path_mtime = self.fake_mtime(loader.path_mtime)
Brett Cannon30b047d2009-02-01 02:05:11 +000079 module = loader.load_module('_temp')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000080 self.assertTrue('testing_var' in module.__dict__,
Brett Cannon30b047d2009-02-01 02:05:11 +000081 "'testing_var' not in "
82 "{0}".format(list(module.__dict__.keys())))
83 self.assertEqual(module, sys.modules['_temp'])
84 self.assertEqual(id(module), module_id)
85 self.assertEqual(id(module.__dict__), module_dict_id)
86
87 def test_state_after_failure(self):
88 # A failed reload should leave the original module intact.
89 attributes = ('__file__', '__path__', '__package__')
90 value = '<test>'
91 name = '_temp'
Brett Cannon4ee2cda2009-02-01 03:08:31 +000092 with source_util.create_modules(name) as mapping:
Brett Cannon30b047d2009-02-01 02:05:11 +000093 orig_module = imp.new_module(name)
94 for attr in attributes:
95 setattr(orig_module, attr, value)
96 with open(mapping[name], 'w') as file:
97 file.write('+++ bad syntax +++')
Brett Cannon61b14252010-07-03 21:48:25 +000098 loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp'])
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 Cannon61b14252010-07-03 21:48:25 +0000109 loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp'])
Brett Cannon2153dc02009-08-27 23:49:21 +0000110 with self.assertRaises(SyntaxError):
111 loader.load_module('_temp')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000112 self.assertTrue('_temp' not in sys.modules)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000113
Brett Cannond71bed32010-07-03 22:18:47 +0000114 def test_file_from_empty_string_dir(self):
115 # Loading a module found from an empty string entry on sys.path should
116 # not only work, but keep all attributes relative.
Brett Cannon186335b2010-08-22 22:11:06 +0000117 file_path = '_temp.py'
118 with open(file_path, 'w') as file:
Brett Cannond71bed32010-07-03 22:18:47 +0000119 file.write("# test file for importlib")
120 try:
121 with util.uncache('_temp'):
Brett Cannon186335b2010-08-22 22:11:06 +0000122 loader = _bootstrap._SourceFileLoader('_temp', file_path)
Brett Cannond71bed32010-07-03 22:18:47 +0000123 mod = loader.load_module('_temp')
Brett Cannon186335b2010-08-22 22:11:06 +0000124 self.assertEqual(file_path, mod.__file__)
125 self.assertEqual(imp.cache_from_source(file_path),
Brett Cannond71bed32010-07-03 22:18:47 +0000126 mod.__cached__)
Brett Cannond71bed32010-07-03 22:18:47 +0000127 finally:
Brett Cannon186335b2010-08-22 22:11:06 +0000128 os.unlink(file_path)
129 pycache = os.path.dirname(imp.cache_from_source(file_path))
130 shutil.rmtree(pycache)
Brett Cannond71bed32010-07-03 22:18:47 +0000131
Antoine Pitrou2be60af2012-01-24 17:44:06 +0100132 def test_timestamp_overflow(self):
133 # When a modification timestamp is larger than 2**32, it should be
134 # truncated rather than raise an OverflowError.
135 with source_util.create_modules('_temp') as mapping:
136 source = mapping['_temp']
137 compiled = imp.cache_from_source(source)
138 with open(source, 'w') as f:
139 f.write("x = 5")
Antoine Pitroudd21f682012-01-25 03:00:57 +0100140 try:
Antoine Pitrou33d15f72012-01-25 18:01:45 +0100141 os.utime(source, (2 ** 33 - 5, 2 ** 33 - 5))
Antoine Pitroudd21f682012-01-25 03:00:57 +0100142 except OverflowError:
143 self.skipTest("cannot set modification time to large integer")
144 except OSError as e:
145 if e.errno != getattr(errno, 'EOVERFLOW', None):
146 raise
147 self.skipTest("cannot set modification time to large integer ({})".format(e))
Antoine Pitrou2be60af2012-01-24 17:44:06 +0100148 loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp'])
149 mod = loader.load_module('_temp')
150 # Sanity checks.
151 self.assertEqual(mod.__cached__, compiled)
152 self.assertEqual(mod.x, 5)
153 # The pyc file was created.
154 os.stat(compiled)
155
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000156
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000157class BadBytecodeTest(unittest.TestCase):
158
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000159 def import_(self, file, module_name):
Brett Cannon61b14252010-07-03 21:48:25 +0000160 loader = self.loader(module_name, file)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000161 module = loader.load_module(module_name)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000162 self.assertTrue(module_name in sys.modules)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000163
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000164 def manipulate_bytecode(self, name, mapping, manipulator, *,
165 del_source=False):
166 """Manipulate the bytecode of a module by passing it into a callable
167 that returns what to use as the new bytecode."""
168 try:
169 del sys.modules['_temp']
170 except KeyError:
171 pass
172 py_compile.compile(mapping[name])
Brett Cannon61b14252010-07-03 21:48:25 +0000173 if not del_source:
174 bytecode_path = imp.cache_from_source(mapping[name])
175 else:
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000176 os.unlink(mapping[name])
Brett Cannon61b14252010-07-03 21:48:25 +0000177 bytecode_path = make_legacy_pyc(mapping[name])
178 if manipulator:
179 with open(bytecode_path, 'rb') as file:
180 bc = file.read()
181 new_bc = manipulator(bc)
182 with open(bytecode_path, 'wb') as file:
183 if new_bc is not None:
184 file.write(new_bc)
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000185 return bytecode_path
186
Brett Cannon61b14252010-07-03 21:48:25 +0000187 def _test_empty_file(self, test, *, del_source=False):
188 with source_util.create_modules('_temp') as mapping:
189 bc_path = self.manipulate_bytecode('_temp', mapping,
190 lambda bc: b'',
191 del_source=del_source)
192 test('_temp', mapping, bc_path)
193
194 @source_util.writes_bytecode_files
195 def _test_partial_magic(self, test, *, del_source=False):
196 # When their are less than 4 bytes to a .pyc, regenerate it if
197 # possible, else raise ImportError.
198 with source_util.create_modules('_temp') as mapping:
199 bc_path = self.manipulate_bytecode('_temp', mapping,
200 lambda bc: bc[:3],
201 del_source=del_source)
202 test('_temp', mapping, bc_path)
203
204 def _test_magic_only(self, test, *, del_source=False):
205 with source_util.create_modules('_temp') as mapping:
206 bc_path = self.manipulate_bytecode('_temp', mapping,
207 lambda bc: bc[:4],
208 del_source=del_source)
209 test('_temp', mapping, bc_path)
210
211 def _test_partial_timestamp(self, test, *, del_source=False):
212 with source_util.create_modules('_temp') as mapping:
213 bc_path = self.manipulate_bytecode('_temp', mapping,
214 lambda bc: bc[:7],
215 del_source=del_source)
216 test('_temp', mapping, bc_path)
217
218 def _test_no_marshal(self, *, del_source=False):
219 with source_util.create_modules('_temp') as mapping:
220 bc_path = self.manipulate_bytecode('_temp', mapping,
221 lambda bc: bc[:8],
222 del_source=del_source)
223 file_path = mapping['_temp'] if not del_source else bc_path
224 with self.assertRaises(EOFError):
225 self.import_(file_path, '_temp')
226
227 def _test_non_code_marshal(self, *, del_source=False):
228 with source_util.create_modules('_temp') as mapping:
229 bytecode_path = self.manipulate_bytecode('_temp', mapping,
230 lambda bc: bc[:8] + marshal.dumps(b'abcd'),
231 del_source=del_source)
232 file_path = mapping['_temp'] if not del_source else bytecode_path
233 with self.assertRaises(ImportError):
234 self.import_(file_path, '_temp')
235
236 def _test_bad_marshal(self, *, del_source=False):
237 with source_util.create_modules('_temp') as mapping:
238 bytecode_path = self.manipulate_bytecode('_temp', mapping,
239 lambda bc: bc[:8] + b'<test>',
240 del_source=del_source)
241 file_path = mapping['_temp'] if not del_source else bytecode_path
Vinay Sajip5bdae3b2011-07-02 16:42:47 +0100242 with self.assertRaises(EOFError):
Brett Cannon61b14252010-07-03 21:48:25 +0000243 self.import_(file_path, '_temp')
244
245 def _test_bad_magic(self, test, *, del_source=False):
246 with source_util.create_modules('_temp') as mapping:
247 bc_path = self.manipulate_bytecode('_temp', mapping,
248 lambda bc: b'\x00\x00\x00\x00' + bc[4:])
249 test('_temp', mapping, bc_path)
250
251
252class SourceLoaderBadBytecodeTest(BadBytecodeTest):
253
254 loader = _bootstrap._SourceFileLoader
255
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000256 @source_util.writes_bytecode_files
257 def test_empty_file(self):
258 # When a .pyc is empty, regenerate it if possible, else raise
259 # ImportError.
Brett Cannon61b14252010-07-03 21:48:25 +0000260 def test(name, mapping, bytecode_path):
261 self.import_(mapping[name], name)
262 with open(bytecode_path, 'rb') as file:
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000263 self.assertGreater(len(file.read()), 8)
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000264
Brett Cannon61b14252010-07-03 21:48:25 +0000265 self._test_empty_file(test)
266
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000267 def test_partial_magic(self):
Brett Cannon61b14252010-07-03 21:48:25 +0000268 def test(name, mapping, bytecode_path):
269 self.import_(mapping[name], name)
270 with open(bytecode_path, 'rb') as file:
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000271 self.assertGreater(len(file.read()), 8)
Brett Cannon61b14252010-07-03 21:48:25 +0000272
273 self._test_partial_magic(test)
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000274
275 @source_util.writes_bytecode_files
276 def test_magic_only(self):
277 # When there is only the magic number, regenerate the .pyc if possible,
278 # else raise EOFError.
Brett Cannon61b14252010-07-03 21:48:25 +0000279 def test(name, mapping, bytecode_path):
280 self.import_(mapping[name], name)
281 with open(bytecode_path, 'rb') as file:
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000282 self.assertGreater(len(file.read()), 8)
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000283
Antoine Pitrou7c9907e2011-12-30 21:25:15 +0100284 self._test_magic_only(test)
285
Brett Cannon1262e7c2009-05-11 01:47:11 +0000286 @source_util.writes_bytecode_files
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000287 def test_bad_magic(self):
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000288 # When the magic number is different, the bytecode should be
289 # regenerated.
Brett Cannon61b14252010-07-03 21:48:25 +0000290 def test(name, mapping, bytecode_path):
291 self.import_(mapping[name], name)
292 with open(bytecode_path, 'rb') as bytecode_file:
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000293 self.assertEqual(bytecode_file.read(4), imp.get_magic())
294
Brett Cannon61b14252010-07-03 21:48:25 +0000295 self._test_bad_magic(test)
296
297 @source_util.writes_bytecode_files
298 def test_partial_timestamp(self):
299 # When the timestamp is partial, regenerate the .pyc, else
300 # raise EOFError.
301 def test(name, mapping, bc_path):
302 self.import_(mapping[name], name)
303 with open(bc_path, 'rb') as file:
304 self.assertGreater(len(file.read()), 8)
305
Antoine Pitrou7c9907e2011-12-30 21:25:15 +0100306 self._test_partial_timestamp(test)
307
Brett Cannon61b14252010-07-03 21:48:25 +0000308 @source_util.writes_bytecode_files
309 def test_no_marshal(self):
310 # When there is only the magic number and timestamp, raise EOFError.
311 self._test_no_marshal()
312
313 @source_util.writes_bytecode_files
314 def test_non_code_marshal(self):
315 self._test_non_code_marshal()
316 # XXX ImportError when sourceless
317
318 # [bad marshal]
319 @source_util.writes_bytecode_files
320 def test_bad_marshal(self):
321 # Bad marshal data should raise a ValueError.
322 self._test_bad_marshal()
323
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000324 # [bad timestamp]
Brett Cannon1262e7c2009-05-11 01:47:11 +0000325 @source_util.writes_bytecode_files
Brett Cannon61b14252010-07-03 21:48:25 +0000326 def test_old_timestamp(self):
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000327 # When the timestamp is older than the source, bytecode should be
328 # regenerated.
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000329 zeros = b'\x00\x00\x00\x00'
Brett Cannon4ee2cda2009-02-01 03:08:31 +0000330 with source_util.create_modules('_temp') as mapping:
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000331 py_compile.compile(mapping['_temp'])
Barry Warsaw28a691b2010-04-17 00:19:56 +0000332 bytecode_path = imp.cache_from_source(mapping['_temp'])
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000333 with open(bytecode_path, 'r+b') as bytecode_file:
334 bytecode_file.seek(4)
335 bytecode_file.write(zeros)
336 self.import_(mapping['_temp'], '_temp')
337 source_mtime = os.path.getmtime(mapping['_temp'])
338 source_timestamp = importlib._w_long(source_mtime)
339 with open(bytecode_path, 'rb') as bytecode_file:
340 bytecode_file.seek(4)
341 self.assertEqual(bytecode_file.read(4), source_timestamp)
342
Brett Cannone52c9192009-11-07 23:55:05 +0000343 # [bytecode read-only]
344 @source_util.writes_bytecode_files
345 def test_read_only_bytecode(self):
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000346 # When bytecode is read-only but should be rewritten, fail silently.
Brett Cannone52c9192009-11-07 23:55:05 +0000347 with source_util.create_modules('_temp') as mapping:
348 # Create bytecode that will need to be re-created.
349 py_compile.compile(mapping['_temp'])
Barry Warsaw28a691b2010-04-17 00:19:56 +0000350 bytecode_path = imp.cache_from_source(mapping['_temp'])
Brett Cannone52c9192009-11-07 23:55:05 +0000351 with open(bytecode_path, 'r+b') as bytecode_file:
352 bytecode_file.seek(0)
353 bytecode_file.write(b'\x00\x00\x00\x00')
354 # Make the bytecode read-only.
355 os.chmod(bytecode_path,
356 stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
357 try:
358 # Should not raise IOError!
359 self.import_(mapping['_temp'], '_temp')
360 finally:
361 # Make writable for eventual clean-up.
362 os.chmod(bytecode_path, stat.S_IWUSR)
363
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000364
Brett Cannon61b14252010-07-03 21:48:25 +0000365class SourcelessLoaderBadBytecodeTest(BadBytecodeTest):
366
367 loader = _bootstrap._SourcelessFileLoader
368
369 def test_empty_file(self):
370 def test(name, mapping, bytecode_path):
371 with self.assertRaises(ImportError):
372 self.import_(bytecode_path, name)
373
374 self._test_empty_file(test, del_source=True)
375
376 def test_partial_magic(self):
377 def test(name, mapping, bytecode_path):
378 with self.assertRaises(ImportError):
379 self.import_(bytecode_path, name)
380 self._test_partial_magic(test, del_source=True)
381
382 def test_magic_only(self):
383 def test(name, mapping, bytecode_path):
384 with self.assertRaises(EOFError):
385 self.import_(bytecode_path, name)
386
387 self._test_magic_only(test, del_source=True)
388
389 def test_bad_magic(self):
390 def test(name, mapping, bytecode_path):
391 with self.assertRaises(ImportError):
392 self.import_(bytecode_path, name)
393
394 self._test_bad_magic(test, del_source=True)
395
396 def test_partial_timestamp(self):
397 def test(name, mapping, bytecode_path):
398 with self.assertRaises(EOFError):
399 self.import_(bytecode_path, name)
400
401 self._test_partial_timestamp(test, del_source=True)
402
403 def test_no_marshal(self):
404 self._test_no_marshal(del_source=True)
405
406 def test_non_code_marshal(self):
407 self._test_non_code_marshal(del_source=True)
408
409
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000410def test_main():
411 from test.support import run_unittest
Brett Cannon61b14252010-07-03 21:48:25 +0000412 run_unittest(SimpleTest,
413 SourceLoaderBadBytecodeTest,
414 SourcelessLoaderBadBytecodeTest
Brett Cannon186335b2010-08-22 22:11:06 +0000415 )
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000416
417
418if __name__ == '__main__':
419 test_main()