blob: f1e2713a971ad24e83ce985abba8d4d96a61be82 [file] [log] [blame]
Brett Cannon30b047d2009-02-01 02:05:11 +00001from .. import abc
Brett Cannond71bed32010-07-03 22:18:47 +00002from .. import util
Brett Cannon4ee2cda2009-02-01 03:08:31 +00003from . import util as source_util
Brett Cannon23cbd8a2009-01-18 00:24:28 +00004
Brett Cannon9ea8eda2013-11-08 14:25:37 -05005importlib = util.import_importlib('importlib')
6importlib_abc = util.import_importlib('importlib.abc')
7machinery = util.import_importlib('importlib.machinery')
8importlib_util = util.import_importlib('importlib.util')
9
Antoine Pitroudd21f682012-01-25 03:00:57 +010010import errno
Brett Cannon61b14252010-07-03 21:48:25 +000011import marshal
Brett Cannon23cbd8a2009-01-18 00:24:28 +000012import os
13import py_compile
Brett Cannon186335b2010-08-22 22:11:06 +000014import shutil
Brett Cannone52c9192009-11-07 23:55:05 +000015import stat
Brett Cannon23cbd8a2009-01-18 00:24:28 +000016import sys
Brett Cannonef888022013-06-15 18:39:21 -040017import types
Brett Cannon23cbd8a2009-01-18 00:24:28 +000018import unittest
19
Brett Cannon0dbb4c72013-05-31 18:56:47 -040020from test.support import make_legacy_pyc, unload
Barry Warsaw04b56842010-05-18 14:15:20 +000021
Brett Cannon23cbd8a2009-01-18 00:24:28 +000022
Brett Cannon9ea8eda2013-11-08 14:25:37 -050023class SimpleTest(abc.LoaderTests):
Brett Cannon23cbd8a2009-01-18 00:24:28 +000024
25 """Should have no issue importing a source module [basic]. And if there is
26 a syntax error, it should raise a SyntaxError [syntax error].
27
28 """
29
Brett Cannonc0499522012-05-11 14:48:41 -040030 def test_load_module_API(self):
Brett Cannon9ea8eda2013-11-08 14:25:37 -050031 class Tester(self.abc.FileLoader):
Brett Cannon0dbb4c72013-05-31 18:56:47 -040032 def get_source(self, _): return 'attr = 42'
33 def is_package(self, _): return False
Brett Cannonc0499522012-05-11 14:48:41 -040034
Brett Cannon0dbb4c72013-05-31 18:56:47 -040035 loader = Tester('blah', 'blah.py')
36 self.addCleanup(unload, 'blah')
37 module = loader.load_module() # Should not raise an exception.
Brett Cannonc0499522012-05-11 14:48:41 -040038
39 def test_get_filename_API(self):
40 # If fullname is not set then assume self.path is desired.
Brett Cannon9ea8eda2013-11-08 14:25:37 -050041 class Tester(self.abc.FileLoader):
Brett Cannonc0499522012-05-11 14:48:41 -040042 def get_code(self, _): pass
43 def get_source(self, _): pass
44 def is_package(self, _): pass
Barry Warsaw0efcf992012-07-31 17:52:32 -040045 def module_repr(self, _): pass
Brett Cannonc0499522012-05-11 14:48:41 -040046
47 path = 'some_path'
48 name = 'some_name'
49 loader = Tester(name, path)
50 self.assertEqual(path, loader.get_filename(name))
51 self.assertEqual(path, loader.get_filename())
52 self.assertEqual(path, loader.get_filename(None))
53 with self.assertRaises(ImportError):
54 loader.get_filename(name + 'XXX')
55
Brett Cannon23cbd8a2009-01-18 00:24:28 +000056 # [basic]
Brett Cannon30b047d2009-02-01 02:05:11 +000057 def test_module(self):
Brett Cannon4ee2cda2009-02-01 03:08:31 +000058 with source_util.create_modules('_temp') as mapping:
Brett Cannon9ea8eda2013-11-08 14:25:37 -050059 loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
Brett Cannon30b047d2009-02-01 02:05:11 +000060 module = loader.load_module('_temp')
Eric V. Smithfaae3ad2012-06-27 15:26:26 -040061 self.assertIn('_temp', sys.modules)
Brett Cannon30b047d2009-02-01 02:05:11 +000062 check = {'__name__': '_temp', '__file__': mapping['_temp'],
Brett Cannon06c9d962009-02-07 01:52:25 +000063 '__package__': ''}
Brett Cannon30b047d2009-02-01 02:05:11 +000064 for attr, value in check.items():
65 self.assertEqual(getattr(module, attr), value)
66
67 def test_package(self):
Brett Cannon4ee2cda2009-02-01 03:08:31 +000068 with source_util.create_modules('_pkg.__init__') as mapping:
Brett Cannon9ea8eda2013-11-08 14:25:37 -050069 loader = self.machinery.SourceFileLoader('_pkg',
Brett Cannon61b14252010-07-03 21:48:25 +000070 mapping['_pkg.__init__'])
Brett Cannon30b047d2009-02-01 02:05:11 +000071 module = loader.load_module('_pkg')
Eric V. Smithfaae3ad2012-06-27 15:26:26 -040072 self.assertIn('_pkg', sys.modules)
Brett Cannon30b047d2009-02-01 02:05:11 +000073 check = {'__name__': '_pkg', '__file__': mapping['_pkg.__init__'],
74 '__path__': [os.path.dirname(mapping['_pkg.__init__'])],
75 '__package__': '_pkg'}
76 for attr, value in check.items():
77 self.assertEqual(getattr(module, attr), value)
78
79
80 def test_lacking_parent(self):
Brett Cannon4ee2cda2009-02-01 03:08:31 +000081 with source_util.create_modules('_pkg.__init__', '_pkg.mod')as mapping:
Brett Cannon9ea8eda2013-11-08 14:25:37 -050082 loader = self.machinery.SourceFileLoader('_pkg.mod',
Brett Cannon61b14252010-07-03 21:48:25 +000083 mapping['_pkg.mod'])
Brett Cannon30b047d2009-02-01 02:05:11 +000084 module = loader.load_module('_pkg.mod')
Eric V. Smithfaae3ad2012-06-27 15:26:26 -040085 self.assertIn('_pkg.mod', sys.modules)
Brett Cannon30b047d2009-02-01 02:05:11 +000086 check = {'__name__': '_pkg.mod', '__file__': mapping['_pkg.mod'],
87 '__package__': '_pkg'}
88 for attr, value in check.items():
89 self.assertEqual(getattr(module, attr), value)
90
91 def fake_mtime(self, fxn):
92 """Fake mtime to always be higher than expected."""
93 return lambda name: fxn(name) + 1
94
95 def test_module_reuse(self):
Brett Cannon4ee2cda2009-02-01 03:08:31 +000096 with source_util.create_modules('_temp') as mapping:
Brett Cannon9ea8eda2013-11-08 14:25:37 -050097 loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
Brett Cannon30b047d2009-02-01 02:05:11 +000098 module = loader.load_module('_temp')
99 module_id = id(module)
100 module_dict_id = id(module.__dict__)
101 with open(mapping['_temp'], 'w') as file:
102 file.write("testing_var = 42\n")
Brett Cannon30b047d2009-02-01 02:05:11 +0000103 module = loader.load_module('_temp')
Eric V. Smithfaae3ad2012-06-27 15:26:26 -0400104 self.assertIn('testing_var', module.__dict__,
Brett Cannon30b047d2009-02-01 02:05:11 +0000105 "'testing_var' not in "
106 "{0}".format(list(module.__dict__.keys())))
107 self.assertEqual(module, sys.modules['_temp'])
108 self.assertEqual(id(module), module_id)
109 self.assertEqual(id(module.__dict__), module_dict_id)
110
111 def test_state_after_failure(self):
112 # A failed reload should leave the original module intact.
113 attributes = ('__file__', '__path__', '__package__')
114 value = '<test>'
115 name = '_temp'
Brett Cannon4ee2cda2009-02-01 03:08:31 +0000116 with source_util.create_modules(name) as mapping:
Brett Cannonef888022013-06-15 18:39:21 -0400117 orig_module = types.ModuleType(name)
Brett Cannon30b047d2009-02-01 02:05:11 +0000118 for attr in attributes:
119 setattr(orig_module, attr, value)
120 with open(mapping[name], 'w') as file:
121 file.write('+++ bad syntax +++')
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500122 loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
Brett Cannon2153dc02009-08-27 23:49:21 +0000123 with self.assertRaises(SyntaxError):
124 loader.load_module(name)
Brett Cannon30b047d2009-02-01 02:05:11 +0000125 for attr in attributes:
126 self.assertEqual(getattr(orig_module, attr), value)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000127
128 # [syntax error]
129 def test_bad_syntax(self):
Brett Cannon4ee2cda2009-02-01 03:08:31 +0000130 with source_util.create_modules('_temp') as mapping:
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000131 with open(mapping['_temp'], 'w') as file:
132 file.write('=')
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500133 loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
Brett Cannon2153dc02009-08-27 23:49:21 +0000134 with self.assertRaises(SyntaxError):
135 loader.load_module('_temp')
Eric V. Smithfaae3ad2012-06-27 15:26:26 -0400136 self.assertNotIn('_temp', sys.modules)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000137
Brett Cannond71bed32010-07-03 22:18:47 +0000138 def test_file_from_empty_string_dir(self):
139 # Loading a module found from an empty string entry on sys.path should
140 # not only work, but keep all attributes relative.
Brett Cannon186335b2010-08-22 22:11:06 +0000141 file_path = '_temp.py'
142 with open(file_path, 'w') as file:
Brett Cannond71bed32010-07-03 22:18:47 +0000143 file.write("# test file for importlib")
144 try:
145 with util.uncache('_temp'):
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500146 loader = self.machinery.SourceFileLoader('_temp', file_path)
Brett Cannond71bed32010-07-03 22:18:47 +0000147 mod = loader.load_module('_temp')
Brett Cannon186335b2010-08-22 22:11:06 +0000148 self.assertEqual(file_path, mod.__file__)
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500149 self.assertEqual(self.util.cache_from_source(file_path),
Brett Cannond71bed32010-07-03 22:18:47 +0000150 mod.__cached__)
Brett Cannond71bed32010-07-03 22:18:47 +0000151 finally:
Brett Cannon186335b2010-08-22 22:11:06 +0000152 os.unlink(file_path)
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500153 pycache = os.path.dirname(self.util.cache_from_source(file_path))
Ezio Melottic28f6fa2013-03-16 19:48:51 +0200154 if os.path.exists(pycache):
155 shutil.rmtree(pycache)
Brett Cannond71bed32010-07-03 22:18:47 +0000156
Antoine Pitrou2be60af2012-01-24 17:44:06 +0100157 def test_timestamp_overflow(self):
158 # When a modification timestamp is larger than 2**32, it should be
159 # truncated rather than raise an OverflowError.
160 with source_util.create_modules('_temp') as mapping:
161 source = mapping['_temp']
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500162 compiled = self.util.cache_from_source(source)
Antoine Pitrou2be60af2012-01-24 17:44:06 +0100163 with open(source, 'w') as f:
164 f.write("x = 5")
Antoine Pitroudd21f682012-01-25 03:00:57 +0100165 try:
Antoine Pitrou33d15f72012-01-25 18:01:45 +0100166 os.utime(source, (2 ** 33 - 5, 2 ** 33 - 5))
Antoine Pitroudd21f682012-01-25 03:00:57 +0100167 except OverflowError:
168 self.skipTest("cannot set modification time to large integer")
169 except OSError as e:
170 if e.errno != getattr(errno, 'EOVERFLOW', None):
171 raise
172 self.skipTest("cannot set modification time to large integer ({})".format(e))
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500173 loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
Antoine Pitrou2be60af2012-01-24 17:44:06 +0100174 mod = loader.load_module('_temp')
175 # Sanity checks.
176 self.assertEqual(mod.__cached__, compiled)
177 self.assertEqual(mod.x, 5)
178 # The pyc file was created.
179 os.stat(compiled)
180
Brett Cannonb3d6aff2013-10-25 12:33:59 -0400181 def test_unloadable(self):
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500182 loader = self.machinery.SourceFileLoader('good name', {})
Brett Cannonb3d6aff2013-10-25 12:33:59 -0400183 with self.assertRaises(ImportError):
184 loader.load_module('bad name')
185
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500186Frozen_SimpleTest, Source_SimpleTest = util.test_both(
187 SimpleTest, importlib=importlib, machinery=machinery, abc=importlib_abc,
188 util=importlib_util)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000189
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500190
191class BadBytecodeTest:
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000192
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000193 def import_(self, file, module_name):
Brett Cannon61b14252010-07-03 21:48:25 +0000194 loader = self.loader(module_name, file)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000195 module = loader.load_module(module_name)
Eric V. Smithfaae3ad2012-06-27 15:26:26 -0400196 self.assertIn(module_name, sys.modules)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000197
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000198 def manipulate_bytecode(self, name, mapping, manipulator, *,
199 del_source=False):
200 """Manipulate the bytecode of a module by passing it into a callable
201 that returns what to use as the new bytecode."""
202 try:
203 del sys.modules['_temp']
204 except KeyError:
205 pass
206 py_compile.compile(mapping[name])
Brett Cannon61b14252010-07-03 21:48:25 +0000207 if not del_source:
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500208 bytecode_path = self.util.cache_from_source(mapping[name])
Brett Cannon61b14252010-07-03 21:48:25 +0000209 else:
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000210 os.unlink(mapping[name])
Brett Cannon61b14252010-07-03 21:48:25 +0000211 bytecode_path = make_legacy_pyc(mapping[name])
212 if manipulator:
213 with open(bytecode_path, 'rb') as file:
214 bc = file.read()
215 new_bc = manipulator(bc)
216 with open(bytecode_path, 'wb') as file:
217 if new_bc is not None:
218 file.write(new_bc)
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000219 return bytecode_path
220
Brett Cannon61b14252010-07-03 21:48:25 +0000221 def _test_empty_file(self, test, *, del_source=False):
222 with source_util.create_modules('_temp') as mapping:
223 bc_path = self.manipulate_bytecode('_temp', mapping,
224 lambda bc: b'',
225 del_source=del_source)
226 test('_temp', mapping, bc_path)
227
228 @source_util.writes_bytecode_files
229 def _test_partial_magic(self, test, *, del_source=False):
230 # When their are less than 4 bytes to a .pyc, regenerate it if
231 # possible, else raise ImportError.
232 with source_util.create_modules('_temp') as mapping:
233 bc_path = self.manipulate_bytecode('_temp', mapping,
234 lambda bc: bc[:3],
235 del_source=del_source)
236 test('_temp', mapping, bc_path)
237
238 def _test_magic_only(self, test, *, del_source=False):
239 with source_util.create_modules('_temp') as mapping:
240 bc_path = self.manipulate_bytecode('_temp', mapping,
241 lambda bc: bc[:4],
242 del_source=del_source)
243 test('_temp', mapping, bc_path)
244
245 def _test_partial_timestamp(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: bc[:7],
249 del_source=del_source)
250 test('_temp', mapping, bc_path)
251
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100252 def _test_partial_size(self, test, *, del_source=False):
253 with source_util.create_modules('_temp') as mapping:
254 bc_path = self.manipulate_bytecode('_temp', mapping,
255 lambda bc: bc[:11],
256 del_source=del_source)
257 test('_temp', mapping, bc_path)
258
Brett Cannon61b14252010-07-03 21:48:25 +0000259 def _test_no_marshal(self, *, del_source=False):
260 with source_util.create_modules('_temp') as mapping:
261 bc_path = self.manipulate_bytecode('_temp', mapping,
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100262 lambda bc: bc[:12],
Brett Cannon61b14252010-07-03 21:48:25 +0000263 del_source=del_source)
264 file_path = mapping['_temp'] if not del_source else bc_path
265 with self.assertRaises(EOFError):
266 self.import_(file_path, '_temp')
267
268 def _test_non_code_marshal(self, *, del_source=False):
269 with source_util.create_modules('_temp') as mapping:
270 bytecode_path = self.manipulate_bytecode('_temp', mapping,
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100271 lambda bc: bc[:12] + marshal.dumps(b'abcd'),
Brett Cannon61b14252010-07-03 21:48:25 +0000272 del_source=del_source)
273 file_path = mapping['_temp'] if not del_source else bytecode_path
Brett Cannonbbb66802012-04-12 21:09:01 -0400274 with self.assertRaises(ImportError) as cm:
Brett Cannon61b14252010-07-03 21:48:25 +0000275 self.import_(file_path, '_temp')
Brett Cannonbbb66802012-04-12 21:09:01 -0400276 self.assertEqual(cm.exception.name, '_temp')
277 self.assertEqual(cm.exception.path, bytecode_path)
Brett Cannon61b14252010-07-03 21:48:25 +0000278
279 def _test_bad_marshal(self, *, del_source=False):
280 with source_util.create_modules('_temp') as mapping:
281 bytecode_path = self.manipulate_bytecode('_temp', mapping,
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100282 lambda bc: bc[:12] + b'<test>',
Brett Cannon61b14252010-07-03 21:48:25 +0000283 del_source=del_source)
284 file_path = mapping['_temp'] if not del_source else bytecode_path
Vinay Sajip5bdae3b2011-07-02 16:42:47 +0100285 with self.assertRaises(EOFError):
Brett Cannon61b14252010-07-03 21:48:25 +0000286 self.import_(file_path, '_temp')
287
288 def _test_bad_magic(self, test, *, del_source=False):
289 with source_util.create_modules('_temp') as mapping:
290 bc_path = self.manipulate_bytecode('_temp', mapping,
291 lambda bc: b'\x00\x00\x00\x00' + bc[4:])
292 test('_temp', mapping, bc_path)
293
294
295class SourceLoaderBadBytecodeTest(BadBytecodeTest):
296
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500297 @classmethod
298 def setUpClass(cls):
299 cls.loader = cls.machinery.SourceFileLoader
Brett Cannon61b14252010-07-03 21:48:25 +0000300
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000301 @source_util.writes_bytecode_files
302 def test_empty_file(self):
303 # When a .pyc is empty, regenerate it if possible, else raise
304 # ImportError.
Brett Cannon61b14252010-07-03 21:48:25 +0000305 def test(name, mapping, bytecode_path):
306 self.import_(mapping[name], name)
307 with open(bytecode_path, 'rb') as file:
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100308 self.assertGreater(len(file.read()), 12)
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000309
Brett Cannon61b14252010-07-03 21:48:25 +0000310 self._test_empty_file(test)
311
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000312 def test_partial_magic(self):
Brett Cannon61b14252010-07-03 21:48:25 +0000313 def test(name, mapping, bytecode_path):
314 self.import_(mapping[name], name)
315 with open(bytecode_path, 'rb') as file:
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100316 self.assertGreater(len(file.read()), 12)
Brett Cannon61b14252010-07-03 21:48:25 +0000317
318 self._test_partial_magic(test)
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000319
320 @source_util.writes_bytecode_files
321 def test_magic_only(self):
322 # When there is only the magic number, regenerate the .pyc if possible,
323 # else raise EOFError.
Brett Cannon61b14252010-07-03 21:48:25 +0000324 def test(name, mapping, bytecode_path):
325 self.import_(mapping[name], name)
326 with open(bytecode_path, 'rb') as file:
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100327 self.assertGreater(len(file.read()), 12)
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000328
Antoine Pitrou7c9907e2011-12-30 21:25:15 +0100329 self._test_magic_only(test)
330
Brett Cannon1262e7c2009-05-11 01:47:11 +0000331 @source_util.writes_bytecode_files
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000332 def test_bad_magic(self):
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000333 # When the magic number is different, the bytecode should be
334 # regenerated.
Brett Cannon61b14252010-07-03 21:48:25 +0000335 def test(name, mapping, bytecode_path):
336 self.import_(mapping[name], name)
337 with open(bytecode_path, 'rb') as bytecode_file:
Brett Cannonef888022013-06-15 18:39:21 -0400338 self.assertEqual(bytecode_file.read(4),
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500339 self.util.MAGIC_NUMBER)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000340
Brett Cannon61b14252010-07-03 21:48:25 +0000341 self._test_bad_magic(test)
342
343 @source_util.writes_bytecode_files
344 def test_partial_timestamp(self):
345 # When the timestamp is partial, regenerate the .pyc, else
346 # raise EOFError.
347 def test(name, mapping, bc_path):
348 self.import_(mapping[name], name)
349 with open(bc_path, 'rb') as file:
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100350 self.assertGreater(len(file.read()), 12)
Brett Cannon61b14252010-07-03 21:48:25 +0000351
Antoine Pitrou7c9907e2011-12-30 21:25:15 +0100352 self._test_partial_timestamp(test)
353
Brett Cannon61b14252010-07-03 21:48:25 +0000354 @source_util.writes_bytecode_files
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100355 def test_partial_size(self):
356 # When the size is partial, regenerate the .pyc, else
357 # raise EOFError.
358 def test(name, mapping, bc_path):
359 self.import_(mapping[name], name)
360 with open(bc_path, 'rb') as file:
361 self.assertGreater(len(file.read()), 12)
362
363 self._test_partial_size(test)
364
365 @source_util.writes_bytecode_files
Brett Cannon61b14252010-07-03 21:48:25 +0000366 def test_no_marshal(self):
367 # When there is only the magic number and timestamp, raise EOFError.
368 self._test_no_marshal()
369
370 @source_util.writes_bytecode_files
371 def test_non_code_marshal(self):
372 self._test_non_code_marshal()
373 # XXX ImportError when sourceless
374
375 # [bad marshal]
376 @source_util.writes_bytecode_files
377 def test_bad_marshal(self):
378 # Bad marshal data should raise a ValueError.
379 self._test_bad_marshal()
380
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000381 # [bad timestamp]
Brett Cannon1262e7c2009-05-11 01:47:11 +0000382 @source_util.writes_bytecode_files
Brett Cannon61b14252010-07-03 21:48:25 +0000383 def test_old_timestamp(self):
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000384 # When the timestamp is older than the source, bytecode should be
385 # regenerated.
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000386 zeros = b'\x00\x00\x00\x00'
Brett Cannon4ee2cda2009-02-01 03:08:31 +0000387 with source_util.create_modules('_temp') as mapping:
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000388 py_compile.compile(mapping['_temp'])
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500389 bytecode_path = self.util.cache_from_source(mapping['_temp'])
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000390 with open(bytecode_path, 'r+b') as bytecode_file:
391 bytecode_file.seek(4)
392 bytecode_file.write(zeros)
393 self.import_(mapping['_temp'], '_temp')
394 source_mtime = os.path.getmtime(mapping['_temp'])
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500395 source_timestamp = self.importlib._w_long(source_mtime)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000396 with open(bytecode_path, 'rb') as bytecode_file:
397 bytecode_file.seek(4)
398 self.assertEqual(bytecode_file.read(4), source_timestamp)
399
Brett Cannone52c9192009-11-07 23:55:05 +0000400 # [bytecode read-only]
401 @source_util.writes_bytecode_files
402 def test_read_only_bytecode(self):
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000403 # When bytecode is read-only but should be rewritten, fail silently.
Brett Cannone52c9192009-11-07 23:55:05 +0000404 with source_util.create_modules('_temp') as mapping:
405 # Create bytecode that will need to be re-created.
406 py_compile.compile(mapping['_temp'])
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500407 bytecode_path = self.util.cache_from_source(mapping['_temp'])
Brett Cannone52c9192009-11-07 23:55:05 +0000408 with open(bytecode_path, 'r+b') as bytecode_file:
409 bytecode_file.seek(0)
410 bytecode_file.write(b'\x00\x00\x00\x00')
411 # Make the bytecode read-only.
412 os.chmod(bytecode_path,
413 stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
414 try:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200415 # Should not raise OSError!
Brett Cannone52c9192009-11-07 23:55:05 +0000416 self.import_(mapping['_temp'], '_temp')
417 finally:
418 # Make writable for eventual clean-up.
419 os.chmod(bytecode_path, stat.S_IWUSR)
420
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500421Frozen_SourceBadBytecode, Source_SourceBadBytecode = util.test_both(
422 SourceLoaderBadBytecodeTest, importlib=importlib, machinery=machinery,
423 abc=importlib_abc, util=importlib_util)
424
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000425
Brett Cannon61b14252010-07-03 21:48:25 +0000426class SourcelessLoaderBadBytecodeTest(BadBytecodeTest):
427
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500428 @classmethod
429 def setUpClass(cls):
430 cls.loader = cls.machinery.SourcelessFileLoader
Brett Cannon61b14252010-07-03 21:48:25 +0000431
432 def test_empty_file(self):
433 def test(name, mapping, bytecode_path):
Brett Cannonbbb66802012-04-12 21:09:01 -0400434 with self.assertRaises(ImportError) as cm:
Brett Cannon61b14252010-07-03 21:48:25 +0000435 self.import_(bytecode_path, name)
Brett Cannonbbb66802012-04-12 21:09:01 -0400436 self.assertEqual(cm.exception.name, name)
437 self.assertEqual(cm.exception.path, bytecode_path)
Brett Cannon61b14252010-07-03 21:48:25 +0000438
439 self._test_empty_file(test, del_source=True)
440
441 def test_partial_magic(self):
442 def test(name, mapping, bytecode_path):
Brett Cannonbbb66802012-04-12 21:09:01 -0400443 with self.assertRaises(ImportError) as cm:
Brett Cannon61b14252010-07-03 21:48:25 +0000444 self.import_(bytecode_path, name)
Brett Cannonbbb66802012-04-12 21:09:01 -0400445 self.assertEqual(cm.exception.name, name)
446 self.assertEqual(cm.exception.path, bytecode_path)
Brett Cannon61b14252010-07-03 21:48:25 +0000447 self._test_partial_magic(test, del_source=True)
448
449 def test_magic_only(self):
450 def test(name, mapping, bytecode_path):
451 with self.assertRaises(EOFError):
452 self.import_(bytecode_path, name)
453
454 self._test_magic_only(test, del_source=True)
455
456 def test_bad_magic(self):
457 def test(name, mapping, bytecode_path):
Brett Cannonbbb66802012-04-12 21:09:01 -0400458 with self.assertRaises(ImportError) as cm:
Brett Cannon61b14252010-07-03 21:48:25 +0000459 self.import_(bytecode_path, name)
Brett Cannonbbb66802012-04-12 21:09:01 -0400460 self.assertEqual(cm.exception.name, name)
461 self.assertEqual(cm.exception.path, bytecode_path)
Brett Cannon61b14252010-07-03 21:48:25 +0000462
463 self._test_bad_magic(test, del_source=True)
464
465 def test_partial_timestamp(self):
466 def test(name, mapping, bytecode_path):
467 with self.assertRaises(EOFError):
468 self.import_(bytecode_path, name)
469
470 self._test_partial_timestamp(test, del_source=True)
471
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100472 def test_partial_size(self):
473 def test(name, mapping, bytecode_path):
474 with self.assertRaises(EOFError):
475 self.import_(bytecode_path, name)
476
477 self._test_partial_size(test, del_source=True)
478
Brett Cannon61b14252010-07-03 21:48:25 +0000479 def test_no_marshal(self):
480 self._test_no_marshal(del_source=True)
481
482 def test_non_code_marshal(self):
483 self._test_non_code_marshal(del_source=True)
484
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500485Frozen_SourcelessBadBytecode, Source_SourcelessBadBytecode = util.test_both(
486 SourcelessLoaderBadBytecodeTest, importlib=importlib,
487 machinery=machinery, abc=importlib_abc, util=importlib_util)
Brett Cannon61b14252010-07-03 21:48:25 +0000488
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000489
490if __name__ == '__main__':
Brett Cannon0dbb4c72013-05-31 18:56:47 -0400491 unittest.main()