blob: 16e4df2c1a7171c681d5856a2652a31dbd9d6674 [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
Eric Snowd749c7a2014-01-04 15:06:49 -070030 def setUp(self):
31 self.name = 'spam'
32 self.filepath = os.path.join('ham', self.name + '.py')
33 self.loader = self.machinery.SourceFileLoader(self.name, self.filepath)
34
Brett Cannonc0499522012-05-11 14:48:41 -040035 def test_load_module_API(self):
Brett Cannon9ea8eda2013-11-08 14:25:37 -050036 class Tester(self.abc.FileLoader):
Brett Cannon0dbb4c72013-05-31 18:56:47 -040037 def get_source(self, _): return 'attr = 42'
38 def is_package(self, _): return False
Brett Cannonc0499522012-05-11 14:48:41 -040039
Brett Cannon0dbb4c72013-05-31 18:56:47 -040040 loader = Tester('blah', 'blah.py')
41 self.addCleanup(unload, 'blah')
42 module = loader.load_module() # Should not raise an exception.
Brett Cannonc0499522012-05-11 14:48:41 -040043
44 def test_get_filename_API(self):
45 # If fullname is not set then assume self.path is desired.
Brett Cannon9ea8eda2013-11-08 14:25:37 -050046 class Tester(self.abc.FileLoader):
Brett Cannonc0499522012-05-11 14:48:41 -040047 def get_code(self, _): pass
48 def get_source(self, _): pass
49 def is_package(self, _): pass
Barry Warsaw0efcf992012-07-31 17:52:32 -040050 def module_repr(self, _): pass
Brett Cannonc0499522012-05-11 14:48:41 -040051
52 path = 'some_path'
53 name = 'some_name'
54 loader = Tester(name, path)
55 self.assertEqual(path, loader.get_filename(name))
56 self.assertEqual(path, loader.get_filename())
57 self.assertEqual(path, loader.get_filename(None))
58 with self.assertRaises(ImportError):
59 loader.get_filename(name + 'XXX')
60
Eric Snowd749c7a2014-01-04 15:06:49 -070061 def test_equality(self):
62 other = self.machinery.SourceFileLoader(self.name, self.filepath)
63 self.assertEqual(self.loader, other)
64
65 def test_inequality(self):
66 other = self.machinery.SourceFileLoader('_' + self.name, self.filepath)
67 self.assertNotEqual(self.loader, other)
68
Brett Cannon23cbd8a2009-01-18 00:24:28 +000069 # [basic]
Brett Cannon30b047d2009-02-01 02:05:11 +000070 def test_module(self):
Brett Cannon4ee2cda2009-02-01 03:08:31 +000071 with source_util.create_modules('_temp') as mapping:
Brett Cannon9ea8eda2013-11-08 14:25:37 -050072 loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
Brett Cannon30b047d2009-02-01 02:05:11 +000073 module = loader.load_module('_temp')
Eric V. Smithfaae3ad2012-06-27 15:26:26 -040074 self.assertIn('_temp', sys.modules)
Brett Cannon30b047d2009-02-01 02:05:11 +000075 check = {'__name__': '_temp', '__file__': mapping['_temp'],
Brett Cannon06c9d962009-02-07 01:52:25 +000076 '__package__': ''}
Brett Cannon30b047d2009-02-01 02:05:11 +000077 for attr, value in check.items():
78 self.assertEqual(getattr(module, attr), value)
79
80 def test_package(self):
Brett Cannon4ee2cda2009-02-01 03:08:31 +000081 with source_util.create_modules('_pkg.__init__') as mapping:
Brett Cannon9ea8eda2013-11-08 14:25:37 -050082 loader = self.machinery.SourceFileLoader('_pkg',
Brett Cannon61b14252010-07-03 21:48:25 +000083 mapping['_pkg.__init__'])
Brett Cannon30b047d2009-02-01 02:05:11 +000084 module = loader.load_module('_pkg')
Eric V. Smithfaae3ad2012-06-27 15:26:26 -040085 self.assertIn('_pkg', sys.modules)
Brett Cannon30b047d2009-02-01 02:05:11 +000086 check = {'__name__': '_pkg', '__file__': mapping['_pkg.__init__'],
87 '__path__': [os.path.dirname(mapping['_pkg.__init__'])],
88 '__package__': '_pkg'}
89 for attr, value in check.items():
90 self.assertEqual(getattr(module, attr), value)
91
92
93 def test_lacking_parent(self):
Brett Cannon4ee2cda2009-02-01 03:08:31 +000094 with source_util.create_modules('_pkg.__init__', '_pkg.mod')as mapping:
Brett Cannon9ea8eda2013-11-08 14:25:37 -050095 loader = self.machinery.SourceFileLoader('_pkg.mod',
Brett Cannon61b14252010-07-03 21:48:25 +000096 mapping['_pkg.mod'])
Brett Cannon30b047d2009-02-01 02:05:11 +000097 module = loader.load_module('_pkg.mod')
Eric V. Smithfaae3ad2012-06-27 15:26:26 -040098 self.assertIn('_pkg.mod', sys.modules)
Brett Cannon30b047d2009-02-01 02:05:11 +000099 check = {'__name__': '_pkg.mod', '__file__': mapping['_pkg.mod'],
100 '__package__': '_pkg'}
101 for attr, value in check.items():
102 self.assertEqual(getattr(module, attr), value)
103
104 def fake_mtime(self, fxn):
105 """Fake mtime to always be higher than expected."""
106 return lambda name: fxn(name) + 1
107
108 def test_module_reuse(self):
Brett Cannon4ee2cda2009-02-01 03:08:31 +0000109 with source_util.create_modules('_temp') as mapping:
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500110 loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
Brett Cannon30b047d2009-02-01 02:05:11 +0000111 module = loader.load_module('_temp')
112 module_id = id(module)
113 module_dict_id = id(module.__dict__)
114 with open(mapping['_temp'], 'w') as file:
115 file.write("testing_var = 42\n")
Brett Cannon30b047d2009-02-01 02:05:11 +0000116 module = loader.load_module('_temp')
Eric V. Smithfaae3ad2012-06-27 15:26:26 -0400117 self.assertIn('testing_var', module.__dict__,
Brett Cannon30b047d2009-02-01 02:05:11 +0000118 "'testing_var' not in "
119 "{0}".format(list(module.__dict__.keys())))
120 self.assertEqual(module, sys.modules['_temp'])
121 self.assertEqual(id(module), module_id)
122 self.assertEqual(id(module.__dict__), module_dict_id)
123
124 def test_state_after_failure(self):
125 # A failed reload should leave the original module intact.
126 attributes = ('__file__', '__path__', '__package__')
127 value = '<test>'
128 name = '_temp'
Brett Cannon4ee2cda2009-02-01 03:08:31 +0000129 with source_util.create_modules(name) as mapping:
Brett Cannonef888022013-06-15 18:39:21 -0400130 orig_module = types.ModuleType(name)
Brett Cannon30b047d2009-02-01 02:05:11 +0000131 for attr in attributes:
132 setattr(orig_module, attr, value)
133 with open(mapping[name], 'w') as file:
134 file.write('+++ bad syntax +++')
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500135 loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
Brett Cannon2153dc02009-08-27 23:49:21 +0000136 with self.assertRaises(SyntaxError):
Brett Cannon2f8c8352013-12-06 14:25:01 -0500137 loader.exec_module(orig_module)
138 for attr in attributes:
139 self.assertEqual(getattr(orig_module, attr), value)
140 with self.assertRaises(SyntaxError):
Brett Cannon2153dc02009-08-27 23:49:21 +0000141 loader.load_module(name)
Brett Cannon30b047d2009-02-01 02:05:11 +0000142 for attr in attributes:
143 self.assertEqual(getattr(orig_module, attr), value)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000144
145 # [syntax error]
146 def test_bad_syntax(self):
Brett Cannon4ee2cda2009-02-01 03:08:31 +0000147 with source_util.create_modules('_temp') as mapping:
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000148 with open(mapping['_temp'], 'w') as file:
149 file.write('=')
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500150 loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
Brett Cannon2153dc02009-08-27 23:49:21 +0000151 with self.assertRaises(SyntaxError):
152 loader.load_module('_temp')
Eric V. Smithfaae3ad2012-06-27 15:26:26 -0400153 self.assertNotIn('_temp', sys.modules)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000154
Brett Cannond71bed32010-07-03 22:18:47 +0000155 def test_file_from_empty_string_dir(self):
156 # Loading a module found from an empty string entry on sys.path should
157 # not only work, but keep all attributes relative.
Brett Cannon186335b2010-08-22 22:11:06 +0000158 file_path = '_temp.py'
159 with open(file_path, 'w') as file:
Brett Cannond71bed32010-07-03 22:18:47 +0000160 file.write("# test file for importlib")
161 try:
162 with util.uncache('_temp'):
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500163 loader = self.machinery.SourceFileLoader('_temp', file_path)
Brett Cannond71bed32010-07-03 22:18:47 +0000164 mod = loader.load_module('_temp')
Brett Cannon186335b2010-08-22 22:11:06 +0000165 self.assertEqual(file_path, mod.__file__)
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500166 self.assertEqual(self.util.cache_from_source(file_path),
Brett Cannond71bed32010-07-03 22:18:47 +0000167 mod.__cached__)
Brett Cannond71bed32010-07-03 22:18:47 +0000168 finally:
Brett Cannon186335b2010-08-22 22:11:06 +0000169 os.unlink(file_path)
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500170 pycache = os.path.dirname(self.util.cache_from_source(file_path))
Ezio Melottic28f6fa2013-03-16 19:48:51 +0200171 if os.path.exists(pycache):
172 shutil.rmtree(pycache)
Brett Cannond71bed32010-07-03 22:18:47 +0000173
Antoine Pitrou2be60af2012-01-24 17:44:06 +0100174 def test_timestamp_overflow(self):
175 # When a modification timestamp is larger than 2**32, it should be
176 # truncated rather than raise an OverflowError.
177 with source_util.create_modules('_temp') as mapping:
178 source = mapping['_temp']
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500179 compiled = self.util.cache_from_source(source)
Antoine Pitrou2be60af2012-01-24 17:44:06 +0100180 with open(source, 'w') as f:
181 f.write("x = 5")
Antoine Pitroudd21f682012-01-25 03:00:57 +0100182 try:
Antoine Pitrou33d15f72012-01-25 18:01:45 +0100183 os.utime(source, (2 ** 33 - 5, 2 ** 33 - 5))
Antoine Pitroudd21f682012-01-25 03:00:57 +0100184 except OverflowError:
185 self.skipTest("cannot set modification time to large integer")
186 except OSError as e:
187 if e.errno != getattr(errno, 'EOVERFLOW', None):
188 raise
189 self.skipTest("cannot set modification time to large integer ({})".format(e))
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500190 loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
Brett Cannon2f8c8352013-12-06 14:25:01 -0500191 # PEP 451
192 module = types.ModuleType('_temp')
193 module.__spec__ = self.util.spec_from_loader('_temp', loader)
194 loader.exec_module(module)
195 self.assertEqual(module.x, 5)
196 self.assertTrue(os.path.exists(compiled))
197 os.unlink(compiled)
198 # PEP 302
199 mod = loader.load_module('_temp') # XXX
Antoine Pitrou2be60af2012-01-24 17:44:06 +0100200 # Sanity checks.
201 self.assertEqual(mod.__cached__, compiled)
202 self.assertEqual(mod.x, 5)
203 # The pyc file was created.
Brett Cannon2f8c8352013-12-06 14:25:01 -0500204 self.assertTrue(os.path.exists(compiled))
Antoine Pitrou2be60af2012-01-24 17:44:06 +0100205
Brett Cannonb3d6aff2013-10-25 12:33:59 -0400206 def test_unloadable(self):
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500207 loader = self.machinery.SourceFileLoader('good name', {})
Brett Cannon2f8c8352013-12-06 14:25:01 -0500208 module = types.ModuleType('bad name')
209 module.__spec__ = self.machinery.ModuleSpec('bad name', loader)
210 with self.assertRaises(ImportError):
211 loader.exec_module(module)
Brett Cannonb3d6aff2013-10-25 12:33:59 -0400212 with self.assertRaises(ImportError):
213 loader.load_module('bad name')
214
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500215Frozen_SimpleTest, Source_SimpleTest = util.test_both(
216 SimpleTest, importlib=importlib, machinery=machinery, abc=importlib_abc,
217 util=importlib_util)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000218
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500219
220class BadBytecodeTest:
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000221
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000222 def import_(self, file, module_name):
Brett Cannon61b14252010-07-03 21:48:25 +0000223 loader = self.loader(module_name, file)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000224 module = loader.load_module(module_name)
Eric V. Smithfaae3ad2012-06-27 15:26:26 -0400225 self.assertIn(module_name, sys.modules)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000226
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000227 def manipulate_bytecode(self, name, mapping, manipulator, *,
228 del_source=False):
229 """Manipulate the bytecode of a module by passing it into a callable
230 that returns what to use as the new bytecode."""
231 try:
232 del sys.modules['_temp']
233 except KeyError:
234 pass
235 py_compile.compile(mapping[name])
Brett Cannon61b14252010-07-03 21:48:25 +0000236 if not del_source:
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500237 bytecode_path = self.util.cache_from_source(mapping[name])
Brett Cannon61b14252010-07-03 21:48:25 +0000238 else:
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000239 os.unlink(mapping[name])
Brett Cannon61b14252010-07-03 21:48:25 +0000240 bytecode_path = make_legacy_pyc(mapping[name])
241 if manipulator:
242 with open(bytecode_path, 'rb') as file:
243 bc = file.read()
244 new_bc = manipulator(bc)
245 with open(bytecode_path, 'wb') as file:
246 if new_bc is not None:
247 file.write(new_bc)
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000248 return bytecode_path
249
Brett Cannon61b14252010-07-03 21:48:25 +0000250 def _test_empty_file(self, test, *, del_source=False):
251 with source_util.create_modules('_temp') as mapping:
252 bc_path = self.manipulate_bytecode('_temp', mapping,
253 lambda bc: b'',
254 del_source=del_source)
255 test('_temp', mapping, bc_path)
256
257 @source_util.writes_bytecode_files
258 def _test_partial_magic(self, test, *, del_source=False):
259 # When their are less than 4 bytes to a .pyc, regenerate it if
260 # possible, else raise ImportError.
261 with source_util.create_modules('_temp') as mapping:
262 bc_path = self.manipulate_bytecode('_temp', mapping,
263 lambda bc: bc[:3],
264 del_source=del_source)
265 test('_temp', mapping, bc_path)
266
267 def _test_magic_only(self, test, *, del_source=False):
268 with source_util.create_modules('_temp') as mapping:
269 bc_path = self.manipulate_bytecode('_temp', mapping,
270 lambda bc: bc[:4],
271 del_source=del_source)
272 test('_temp', mapping, bc_path)
273
274 def _test_partial_timestamp(self, test, *, del_source=False):
275 with source_util.create_modules('_temp') as mapping:
276 bc_path = self.manipulate_bytecode('_temp', mapping,
277 lambda bc: bc[:7],
278 del_source=del_source)
279 test('_temp', mapping, bc_path)
280
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100281 def _test_partial_size(self, test, *, del_source=False):
282 with source_util.create_modules('_temp') as mapping:
283 bc_path = self.manipulate_bytecode('_temp', mapping,
284 lambda bc: bc[:11],
285 del_source=del_source)
286 test('_temp', mapping, bc_path)
287
Brett Cannon61b14252010-07-03 21:48:25 +0000288 def _test_no_marshal(self, *, del_source=False):
289 with source_util.create_modules('_temp') as mapping:
290 bc_path = self.manipulate_bytecode('_temp', mapping,
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100291 lambda bc: bc[:12],
Brett Cannon61b14252010-07-03 21:48:25 +0000292 del_source=del_source)
293 file_path = mapping['_temp'] if not del_source else bc_path
294 with self.assertRaises(EOFError):
295 self.import_(file_path, '_temp')
296
297 def _test_non_code_marshal(self, *, del_source=False):
298 with source_util.create_modules('_temp') as mapping:
299 bytecode_path = self.manipulate_bytecode('_temp', mapping,
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100300 lambda bc: bc[:12] + marshal.dumps(b'abcd'),
Brett Cannon61b14252010-07-03 21:48:25 +0000301 del_source=del_source)
302 file_path = mapping['_temp'] if not del_source else bytecode_path
Brett Cannonbbb66802012-04-12 21:09:01 -0400303 with self.assertRaises(ImportError) as cm:
Brett Cannon61b14252010-07-03 21:48:25 +0000304 self.import_(file_path, '_temp')
Brett Cannonbbb66802012-04-12 21:09:01 -0400305 self.assertEqual(cm.exception.name, '_temp')
306 self.assertEqual(cm.exception.path, bytecode_path)
Brett Cannon61b14252010-07-03 21:48:25 +0000307
308 def _test_bad_marshal(self, *, del_source=False):
309 with source_util.create_modules('_temp') as mapping:
310 bytecode_path = self.manipulate_bytecode('_temp', mapping,
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100311 lambda bc: bc[:12] + b'<test>',
Brett Cannon61b14252010-07-03 21:48:25 +0000312 del_source=del_source)
313 file_path = mapping['_temp'] if not del_source else bytecode_path
Vinay Sajip5bdae3b2011-07-02 16:42:47 +0100314 with self.assertRaises(EOFError):
Brett Cannon61b14252010-07-03 21:48:25 +0000315 self.import_(file_path, '_temp')
316
317 def _test_bad_magic(self, test, *, del_source=False):
318 with source_util.create_modules('_temp') as mapping:
319 bc_path = self.manipulate_bytecode('_temp', mapping,
320 lambda bc: b'\x00\x00\x00\x00' + bc[4:])
321 test('_temp', mapping, bc_path)
322
Brett Cannon2f8c8352013-12-06 14:25:01 -0500323class BadBytecodeTestPEP451(BadBytecodeTest):
Brett Cannon61b14252010-07-03 21:48:25 +0000324
Brett Cannon2f8c8352013-12-06 14:25:01 -0500325 def import_(self, file, module_name):
326 loader = self.loader(module_name, file)
327 module = types.ModuleType(module_name)
328 module.__spec__ = self.util.spec_from_loader(module_name, loader)
329 loader.exec_module(module)
330
331class BadBytecodeTestPEP302(BadBytecodeTest):
332
333 def import_(self, file, module_name):
334 loader = self.loader(module_name, file)
335 module = loader.load_module(module_name)
336 self.assertIn(module_name, sys.modules)
337
338
339class SourceLoaderBadBytecodeTest:
Brett Cannon61b14252010-07-03 21:48:25 +0000340
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500341 @classmethod
342 def setUpClass(cls):
343 cls.loader = cls.machinery.SourceFileLoader
Brett Cannon61b14252010-07-03 21:48:25 +0000344
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000345 @source_util.writes_bytecode_files
346 def test_empty_file(self):
347 # When a .pyc is empty, regenerate it if possible, else raise
348 # ImportError.
Brett Cannon61b14252010-07-03 21:48:25 +0000349 def test(name, mapping, bytecode_path):
350 self.import_(mapping[name], name)
351 with open(bytecode_path, 'rb') as file:
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100352 self.assertGreater(len(file.read()), 12)
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000353
Brett Cannon61b14252010-07-03 21:48:25 +0000354 self._test_empty_file(test)
355
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000356 def test_partial_magic(self):
Brett Cannon61b14252010-07-03 21:48:25 +0000357 def test(name, mapping, bytecode_path):
358 self.import_(mapping[name], name)
359 with open(bytecode_path, 'rb') as file:
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100360 self.assertGreater(len(file.read()), 12)
Brett Cannon61b14252010-07-03 21:48:25 +0000361
362 self._test_partial_magic(test)
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000363
364 @source_util.writes_bytecode_files
365 def test_magic_only(self):
366 # When there is only the magic number, regenerate the .pyc if possible,
367 # else raise EOFError.
Brett Cannon61b14252010-07-03 21:48:25 +0000368 def test(name, mapping, bytecode_path):
369 self.import_(mapping[name], name)
370 with open(bytecode_path, 'rb') as file:
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100371 self.assertGreater(len(file.read()), 12)
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000372
Antoine Pitrou7c9907e2011-12-30 21:25:15 +0100373 self._test_magic_only(test)
374
Brett Cannon1262e7c2009-05-11 01:47:11 +0000375 @source_util.writes_bytecode_files
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000376 def test_bad_magic(self):
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000377 # When the magic number is different, the bytecode should be
378 # regenerated.
Brett Cannon61b14252010-07-03 21:48:25 +0000379 def test(name, mapping, bytecode_path):
380 self.import_(mapping[name], name)
381 with open(bytecode_path, 'rb') as bytecode_file:
Brett Cannonef888022013-06-15 18:39:21 -0400382 self.assertEqual(bytecode_file.read(4),
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500383 self.util.MAGIC_NUMBER)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000384
Brett Cannon61b14252010-07-03 21:48:25 +0000385 self._test_bad_magic(test)
386
387 @source_util.writes_bytecode_files
388 def test_partial_timestamp(self):
389 # When the timestamp is partial, regenerate the .pyc, else
390 # raise EOFError.
391 def test(name, mapping, bc_path):
392 self.import_(mapping[name], name)
393 with open(bc_path, 'rb') as file:
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100394 self.assertGreater(len(file.read()), 12)
Brett Cannon61b14252010-07-03 21:48:25 +0000395
Antoine Pitrou7c9907e2011-12-30 21:25:15 +0100396 self._test_partial_timestamp(test)
397
Brett Cannon61b14252010-07-03 21:48:25 +0000398 @source_util.writes_bytecode_files
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100399 def test_partial_size(self):
400 # When the size is partial, regenerate the .pyc, else
401 # raise EOFError.
402 def test(name, mapping, bc_path):
403 self.import_(mapping[name], name)
404 with open(bc_path, 'rb') as file:
405 self.assertGreater(len(file.read()), 12)
406
407 self._test_partial_size(test)
408
409 @source_util.writes_bytecode_files
Brett Cannon61b14252010-07-03 21:48:25 +0000410 def test_no_marshal(self):
411 # When there is only the magic number and timestamp, raise EOFError.
412 self._test_no_marshal()
413
414 @source_util.writes_bytecode_files
415 def test_non_code_marshal(self):
416 self._test_non_code_marshal()
417 # XXX ImportError when sourceless
418
419 # [bad marshal]
420 @source_util.writes_bytecode_files
421 def test_bad_marshal(self):
422 # Bad marshal data should raise a ValueError.
423 self._test_bad_marshal()
424
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000425 # [bad timestamp]
Brett Cannon1262e7c2009-05-11 01:47:11 +0000426 @source_util.writes_bytecode_files
Brett Cannon61b14252010-07-03 21:48:25 +0000427 def test_old_timestamp(self):
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000428 # When the timestamp is older than the source, bytecode should be
429 # regenerated.
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000430 zeros = b'\x00\x00\x00\x00'
Brett Cannon4ee2cda2009-02-01 03:08:31 +0000431 with source_util.create_modules('_temp') as mapping:
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000432 py_compile.compile(mapping['_temp'])
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500433 bytecode_path = self.util.cache_from_source(mapping['_temp'])
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000434 with open(bytecode_path, 'r+b') as bytecode_file:
435 bytecode_file.seek(4)
436 bytecode_file.write(zeros)
437 self.import_(mapping['_temp'], '_temp')
438 source_mtime = os.path.getmtime(mapping['_temp'])
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500439 source_timestamp = self.importlib._w_long(source_mtime)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000440 with open(bytecode_path, 'rb') as bytecode_file:
441 bytecode_file.seek(4)
442 self.assertEqual(bytecode_file.read(4), source_timestamp)
443
Brett Cannone52c9192009-11-07 23:55:05 +0000444 # [bytecode read-only]
445 @source_util.writes_bytecode_files
446 def test_read_only_bytecode(self):
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000447 # When bytecode is read-only but should be rewritten, fail silently.
Brett Cannone52c9192009-11-07 23:55:05 +0000448 with source_util.create_modules('_temp') as mapping:
449 # Create bytecode that will need to be re-created.
450 py_compile.compile(mapping['_temp'])
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500451 bytecode_path = self.util.cache_from_source(mapping['_temp'])
Brett Cannone52c9192009-11-07 23:55:05 +0000452 with open(bytecode_path, 'r+b') as bytecode_file:
453 bytecode_file.seek(0)
454 bytecode_file.write(b'\x00\x00\x00\x00')
455 # Make the bytecode read-only.
456 os.chmod(bytecode_path,
457 stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
458 try:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200459 # Should not raise OSError!
Brett Cannone52c9192009-11-07 23:55:05 +0000460 self.import_(mapping['_temp'], '_temp')
461 finally:
462 # Make writable for eventual clean-up.
463 os.chmod(bytecode_path, stat.S_IWUSR)
464
Brett Cannon2f8c8352013-12-06 14:25:01 -0500465class SourceLoaderBadBytecodeTestPEP451(
466 SourceLoaderBadBytecodeTest, BadBytecodeTestPEP451):
467 pass
468
469Frozen_SourceBadBytecodePEP451, Source_SourceBadBytecodePEP451 = util.test_both(
470 SourceLoaderBadBytecodeTestPEP451, importlib=importlib, machinery=machinery,
471 abc=importlib_abc, util=importlib_util)
472
473class SourceLoaderBadBytecodeTestPEP302(
474 SourceLoaderBadBytecodeTest, BadBytecodeTestPEP302):
475 pass
476
477Frozen_SourceBadBytecodePEP302, Source_SourceBadBytecodePEP302 = util.test_both(
478 SourceLoaderBadBytecodeTestPEP302, importlib=importlib, machinery=machinery,
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500479 abc=importlib_abc, util=importlib_util)
480
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000481
Brett Cannon2f8c8352013-12-06 14:25:01 -0500482class SourcelessLoaderBadBytecodeTest:
Brett Cannon61b14252010-07-03 21:48:25 +0000483
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500484 @classmethod
485 def setUpClass(cls):
486 cls.loader = cls.machinery.SourcelessFileLoader
Brett Cannon61b14252010-07-03 21:48:25 +0000487
488 def test_empty_file(self):
489 def test(name, mapping, bytecode_path):
Brett Cannonbbb66802012-04-12 21:09:01 -0400490 with self.assertRaises(ImportError) as cm:
Brett Cannon61b14252010-07-03 21:48:25 +0000491 self.import_(bytecode_path, name)
Brett Cannonbbb66802012-04-12 21:09:01 -0400492 self.assertEqual(cm.exception.name, name)
493 self.assertEqual(cm.exception.path, bytecode_path)
Brett Cannon61b14252010-07-03 21:48:25 +0000494
495 self._test_empty_file(test, del_source=True)
496
497 def test_partial_magic(self):
498 def test(name, mapping, bytecode_path):
Brett Cannonbbb66802012-04-12 21:09:01 -0400499 with self.assertRaises(ImportError) as cm:
Brett Cannon61b14252010-07-03 21:48:25 +0000500 self.import_(bytecode_path, name)
Brett Cannonbbb66802012-04-12 21:09:01 -0400501 self.assertEqual(cm.exception.name, name)
502 self.assertEqual(cm.exception.path, bytecode_path)
Brett Cannon61b14252010-07-03 21:48:25 +0000503 self._test_partial_magic(test, del_source=True)
504
505 def test_magic_only(self):
506 def test(name, mapping, bytecode_path):
507 with self.assertRaises(EOFError):
508 self.import_(bytecode_path, name)
509
510 self._test_magic_only(test, del_source=True)
511
512 def test_bad_magic(self):
513 def test(name, mapping, bytecode_path):
Brett Cannonbbb66802012-04-12 21:09:01 -0400514 with self.assertRaises(ImportError) as cm:
Brett Cannon61b14252010-07-03 21:48:25 +0000515 self.import_(bytecode_path, name)
Brett Cannonbbb66802012-04-12 21:09:01 -0400516 self.assertEqual(cm.exception.name, name)
517 self.assertEqual(cm.exception.path, bytecode_path)
Brett Cannon61b14252010-07-03 21:48:25 +0000518
519 self._test_bad_magic(test, del_source=True)
520
521 def test_partial_timestamp(self):
522 def test(name, mapping, bytecode_path):
523 with self.assertRaises(EOFError):
524 self.import_(bytecode_path, name)
525
526 self._test_partial_timestamp(test, del_source=True)
527
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100528 def test_partial_size(self):
529 def test(name, mapping, bytecode_path):
530 with self.assertRaises(EOFError):
531 self.import_(bytecode_path, name)
532
533 self._test_partial_size(test, del_source=True)
534
Brett Cannon61b14252010-07-03 21:48:25 +0000535 def test_no_marshal(self):
536 self._test_no_marshal(del_source=True)
537
538 def test_non_code_marshal(self):
539 self._test_non_code_marshal(del_source=True)
540
Brett Cannon2f8c8352013-12-06 14:25:01 -0500541class SourcelessLoaderBadBytecodeTestPEP451(SourcelessLoaderBadBytecodeTest,
542 BadBytecodeTestPEP451):
543 pass
544
545Frozen_SourcelessBadBytecodePEP451, Source_SourcelessBadBytecodePEP451 = util.test_both(
546 SourcelessLoaderBadBytecodeTestPEP451, importlib=importlib,
547 machinery=machinery, abc=importlib_abc, util=importlib_util)
548
549class SourcelessLoaderBadBytecodeTestPEP302(SourcelessLoaderBadBytecodeTest,
550 BadBytecodeTestPEP302):
551 pass
552
553Frozen_SourcelessBadBytecodePEP302, Source_SourcelessBadBytecodePEP302 = util.test_both(
554 SourcelessLoaderBadBytecodeTestPEP302, importlib=importlib,
Brett Cannon9ea8eda2013-11-08 14:25:37 -0500555 machinery=machinery, abc=importlib_abc, util=importlib_util)
Brett Cannon61b14252010-07-03 21:48:25 +0000556
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000557
558if __name__ == '__main__':
Brett Cannon0dbb4c72013-05-31 18:56:47 -0400559 unittest.main()