blob: cb1c3171675fc7e4bfd4a4ce235724c68c226c3b [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")
Brett Cannon30b047d2009-02-01 02:05:11 +000074 module = loader.load_module('_temp')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000075 self.assertTrue('testing_var' in module.__dict__,
Brett Cannon30b047d2009-02-01 02:05:11 +000076 "'testing_var' not in "
77 "{0}".format(list(module.__dict__.keys())))
78 self.assertEqual(module, sys.modules['_temp'])
79 self.assertEqual(id(module), module_id)
80 self.assertEqual(id(module.__dict__), module_dict_id)
81
82 def test_state_after_failure(self):
83 # A failed reload should leave the original module intact.
84 attributes = ('__file__', '__path__', '__package__')
85 value = '<test>'
86 name = '_temp'
Brett Cannon4ee2cda2009-02-01 03:08:31 +000087 with source_util.create_modules(name) as mapping:
Brett Cannon30b047d2009-02-01 02:05:11 +000088 orig_module = imp.new_module(name)
89 for attr in attributes:
90 setattr(orig_module, attr, value)
91 with open(mapping[name], 'w') as file:
92 file.write('+++ bad syntax +++')
Brett Cannon61b14252010-07-03 21:48:25 +000093 loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp'])
Brett Cannon2153dc02009-08-27 23:49:21 +000094 with self.assertRaises(SyntaxError):
95 loader.load_module(name)
Brett Cannon30b047d2009-02-01 02:05:11 +000096 for attr in attributes:
97 self.assertEqual(getattr(orig_module, attr), value)
Brett Cannon23cbd8a2009-01-18 00:24:28 +000098
99 # [syntax error]
100 def test_bad_syntax(self):
Brett Cannon4ee2cda2009-02-01 03:08:31 +0000101 with source_util.create_modules('_temp') as mapping:
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000102 with open(mapping['_temp'], 'w') as file:
103 file.write('=')
Brett Cannon61b14252010-07-03 21:48:25 +0000104 loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp'])
Brett Cannon2153dc02009-08-27 23:49:21 +0000105 with self.assertRaises(SyntaxError):
106 loader.load_module('_temp')
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000107 self.assertTrue('_temp' not in sys.modules)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000108
Brett Cannond71bed32010-07-03 22:18:47 +0000109 def test_file_from_empty_string_dir(self):
110 # Loading a module found from an empty string entry on sys.path should
111 # not only work, but keep all attributes relative.
Brett Cannon186335b2010-08-22 22:11:06 +0000112 file_path = '_temp.py'
113 with open(file_path, 'w') as file:
Brett Cannond71bed32010-07-03 22:18:47 +0000114 file.write("# test file for importlib")
115 try:
116 with util.uncache('_temp'):
Brett Cannon186335b2010-08-22 22:11:06 +0000117 loader = _bootstrap._SourceFileLoader('_temp', file_path)
Brett Cannond71bed32010-07-03 22:18:47 +0000118 mod = loader.load_module('_temp')
Brett Cannon186335b2010-08-22 22:11:06 +0000119 self.assertEqual(file_path, mod.__file__)
120 self.assertEqual(imp.cache_from_source(file_path),
Brett Cannond71bed32010-07-03 22:18:47 +0000121 mod.__cached__)
Brett Cannond71bed32010-07-03 22:18:47 +0000122 finally:
Brett Cannon186335b2010-08-22 22:11:06 +0000123 os.unlink(file_path)
124 pycache = os.path.dirname(imp.cache_from_source(file_path))
125 shutil.rmtree(pycache)
Brett Cannond71bed32010-07-03 22:18:47 +0000126
Antoine Pitrou2be60af2012-01-24 17:44:06 +0100127 def test_timestamp_overflow(self):
128 # When a modification timestamp is larger than 2**32, it should be
129 # truncated rather than raise an OverflowError.
130 with source_util.create_modules('_temp') as mapping:
131 source = mapping['_temp']
132 compiled = imp.cache_from_source(source)
133 with open(source, 'w') as f:
134 f.write("x = 5")
Antoine Pitroudd21f682012-01-25 03:00:57 +0100135 try:
Antoine Pitrou33d15f72012-01-25 18:01:45 +0100136 os.utime(source, (2 ** 33 - 5, 2 ** 33 - 5))
Antoine Pitroudd21f682012-01-25 03:00:57 +0100137 except OverflowError:
138 self.skipTest("cannot set modification time to large integer")
139 except OSError as e:
140 if e.errno != getattr(errno, 'EOVERFLOW', None):
141 raise
142 self.skipTest("cannot set modification time to large integer ({})".format(e))
Antoine Pitrou2be60af2012-01-24 17:44:06 +0100143 loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp'])
144 mod = loader.load_module('_temp')
145 # Sanity checks.
146 self.assertEqual(mod.__cached__, compiled)
147 self.assertEqual(mod.x, 5)
148 # The pyc file was created.
149 os.stat(compiled)
150
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000151
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000152class BadBytecodeTest(unittest.TestCase):
153
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000154 def import_(self, file, module_name):
Brett Cannon61b14252010-07-03 21:48:25 +0000155 loader = self.loader(module_name, file)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000156 module = loader.load_module(module_name)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000157 self.assertTrue(module_name in sys.modules)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000158
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000159 def manipulate_bytecode(self, name, mapping, manipulator, *,
160 del_source=False):
161 """Manipulate the bytecode of a module by passing it into a callable
162 that returns what to use as the new bytecode."""
163 try:
164 del sys.modules['_temp']
165 except KeyError:
166 pass
167 py_compile.compile(mapping[name])
Brett Cannon61b14252010-07-03 21:48:25 +0000168 if not del_source:
169 bytecode_path = imp.cache_from_source(mapping[name])
170 else:
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000171 os.unlink(mapping[name])
Brett Cannon61b14252010-07-03 21:48:25 +0000172 bytecode_path = make_legacy_pyc(mapping[name])
173 if manipulator:
174 with open(bytecode_path, 'rb') as file:
175 bc = file.read()
176 new_bc = manipulator(bc)
177 with open(bytecode_path, 'wb') as file:
178 if new_bc is not None:
179 file.write(new_bc)
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000180 return bytecode_path
181
Brett Cannon61b14252010-07-03 21:48:25 +0000182 def _test_empty_file(self, test, *, del_source=False):
183 with source_util.create_modules('_temp') as mapping:
184 bc_path = self.manipulate_bytecode('_temp', mapping,
185 lambda bc: b'',
186 del_source=del_source)
187 test('_temp', mapping, bc_path)
188
189 @source_util.writes_bytecode_files
190 def _test_partial_magic(self, test, *, del_source=False):
191 # When their are less than 4 bytes to a .pyc, regenerate it if
192 # possible, else raise ImportError.
193 with source_util.create_modules('_temp') as mapping:
194 bc_path = self.manipulate_bytecode('_temp', mapping,
195 lambda bc: bc[:3],
196 del_source=del_source)
197 test('_temp', mapping, bc_path)
198
199 def _test_magic_only(self, test, *, del_source=False):
200 with source_util.create_modules('_temp') as mapping:
201 bc_path = self.manipulate_bytecode('_temp', mapping,
202 lambda bc: bc[:4],
203 del_source=del_source)
204 test('_temp', mapping, bc_path)
205
206 def _test_partial_timestamp(self, test, *, del_source=False):
207 with source_util.create_modules('_temp') as mapping:
208 bc_path = self.manipulate_bytecode('_temp', mapping,
209 lambda bc: bc[:7],
210 del_source=del_source)
211 test('_temp', mapping, bc_path)
212
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100213 def _test_partial_size(self, test, *, del_source=False):
214 with source_util.create_modules('_temp') as mapping:
215 bc_path = self.manipulate_bytecode('_temp', mapping,
216 lambda bc: bc[:11],
217 del_source=del_source)
218 test('_temp', mapping, bc_path)
219
Brett Cannon61b14252010-07-03 21:48:25 +0000220 def _test_no_marshal(self, *, del_source=False):
221 with source_util.create_modules('_temp') as mapping:
222 bc_path = self.manipulate_bytecode('_temp', mapping,
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100223 lambda bc: bc[:12],
Brett Cannon61b14252010-07-03 21:48:25 +0000224 del_source=del_source)
225 file_path = mapping['_temp'] if not del_source else bc_path
226 with self.assertRaises(EOFError):
227 self.import_(file_path, '_temp')
228
229 def _test_non_code_marshal(self, *, del_source=False):
230 with source_util.create_modules('_temp') as mapping:
231 bytecode_path = self.manipulate_bytecode('_temp', mapping,
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100232 lambda bc: bc[:12] + marshal.dumps(b'abcd'),
Brett Cannon61b14252010-07-03 21:48:25 +0000233 del_source=del_source)
234 file_path = mapping['_temp'] if not del_source else bytecode_path
Brett Cannonbbb66802012-04-12 21:09:01 -0400235 with self.assertRaises(ImportError) as cm:
Brett Cannon61b14252010-07-03 21:48:25 +0000236 self.import_(file_path, '_temp')
Brett Cannonbbb66802012-04-12 21:09:01 -0400237 self.assertEqual(cm.exception.name, '_temp')
238 self.assertEqual(cm.exception.path, bytecode_path)
Brett Cannon61b14252010-07-03 21:48:25 +0000239
240 def _test_bad_marshal(self, *, del_source=False):
241 with source_util.create_modules('_temp') as mapping:
242 bytecode_path = self.manipulate_bytecode('_temp', mapping,
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100243 lambda bc: bc[:12] + b'<test>',
Brett Cannon61b14252010-07-03 21:48:25 +0000244 del_source=del_source)
245 file_path = mapping['_temp'] if not del_source else bytecode_path
Vinay Sajip5bdae3b2011-07-02 16:42:47 +0100246 with self.assertRaises(EOFError):
Brett Cannon61b14252010-07-03 21:48:25 +0000247 self.import_(file_path, '_temp')
248
249 def _test_bad_magic(self, test, *, del_source=False):
250 with source_util.create_modules('_temp') as mapping:
251 bc_path = self.manipulate_bytecode('_temp', mapping,
252 lambda bc: b'\x00\x00\x00\x00' + bc[4:])
253 test('_temp', mapping, bc_path)
254
255
256class SourceLoaderBadBytecodeTest(BadBytecodeTest):
257
258 loader = _bootstrap._SourceFileLoader
259
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000260 @source_util.writes_bytecode_files
261 def test_empty_file(self):
262 # When a .pyc is empty, regenerate it if possible, else raise
263 # ImportError.
Brett Cannon61b14252010-07-03 21:48:25 +0000264 def test(name, mapping, bytecode_path):
265 self.import_(mapping[name], name)
266 with open(bytecode_path, 'rb') as file:
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100267 self.assertGreater(len(file.read()), 12)
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000268
Brett Cannon61b14252010-07-03 21:48:25 +0000269 self._test_empty_file(test)
270
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000271 def test_partial_magic(self):
Brett Cannon61b14252010-07-03 21:48:25 +0000272 def test(name, mapping, bytecode_path):
273 self.import_(mapping[name], name)
274 with open(bytecode_path, 'rb') as file:
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100275 self.assertGreater(len(file.read()), 12)
Brett Cannon61b14252010-07-03 21:48:25 +0000276
277 self._test_partial_magic(test)
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000278
279 @source_util.writes_bytecode_files
280 def test_magic_only(self):
281 # When there is only the magic number, regenerate the .pyc if possible,
282 # else raise EOFError.
Brett Cannon61b14252010-07-03 21:48:25 +0000283 def test(name, mapping, bytecode_path):
284 self.import_(mapping[name], name)
285 with open(bytecode_path, 'rb') as file:
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100286 self.assertGreater(len(file.read()), 12)
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000287
Antoine Pitrou7c9907e2011-12-30 21:25:15 +0100288 self._test_magic_only(test)
289
Brett Cannon1262e7c2009-05-11 01:47:11 +0000290 @source_util.writes_bytecode_files
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000291 def test_bad_magic(self):
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000292 # When the magic number is different, the bytecode should be
293 # regenerated.
Brett Cannon61b14252010-07-03 21:48:25 +0000294 def test(name, mapping, bytecode_path):
295 self.import_(mapping[name], name)
296 with open(bytecode_path, 'rb') as bytecode_file:
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000297 self.assertEqual(bytecode_file.read(4), imp.get_magic())
298
Brett Cannon61b14252010-07-03 21:48:25 +0000299 self._test_bad_magic(test)
300
301 @source_util.writes_bytecode_files
302 def test_partial_timestamp(self):
303 # When the timestamp is partial, regenerate the .pyc, else
304 # raise EOFError.
305 def test(name, mapping, bc_path):
306 self.import_(mapping[name], name)
307 with open(bc_path, 'rb') as file:
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100308 self.assertGreater(len(file.read()), 12)
Brett Cannon61b14252010-07-03 21:48:25 +0000309
Antoine Pitrou7c9907e2011-12-30 21:25:15 +0100310 self._test_partial_timestamp(test)
311
Brett Cannon61b14252010-07-03 21:48:25 +0000312 @source_util.writes_bytecode_files
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100313 def test_partial_size(self):
314 # When the size is partial, regenerate the .pyc, else
315 # raise EOFError.
316 def test(name, mapping, bc_path):
317 self.import_(mapping[name], name)
318 with open(bc_path, 'rb') as file:
319 self.assertGreater(len(file.read()), 12)
320
321 self._test_partial_size(test)
322
323 @source_util.writes_bytecode_files
Brett Cannon61b14252010-07-03 21:48:25 +0000324 def test_no_marshal(self):
325 # When there is only the magic number and timestamp, raise EOFError.
326 self._test_no_marshal()
327
328 @source_util.writes_bytecode_files
329 def test_non_code_marshal(self):
330 self._test_non_code_marshal()
331 # XXX ImportError when sourceless
332
333 # [bad marshal]
334 @source_util.writes_bytecode_files
335 def test_bad_marshal(self):
336 # Bad marshal data should raise a ValueError.
337 self._test_bad_marshal()
338
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000339 # [bad timestamp]
Brett Cannon1262e7c2009-05-11 01:47:11 +0000340 @source_util.writes_bytecode_files
Brett Cannon61b14252010-07-03 21:48:25 +0000341 def test_old_timestamp(self):
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000342 # When the timestamp is older than the source, bytecode should be
343 # regenerated.
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000344 zeros = b'\x00\x00\x00\x00'
Brett Cannon4ee2cda2009-02-01 03:08:31 +0000345 with source_util.create_modules('_temp') as mapping:
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000346 py_compile.compile(mapping['_temp'])
Barry Warsaw28a691b2010-04-17 00:19:56 +0000347 bytecode_path = imp.cache_from_source(mapping['_temp'])
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000348 with open(bytecode_path, 'r+b') as bytecode_file:
349 bytecode_file.seek(4)
350 bytecode_file.write(zeros)
351 self.import_(mapping['_temp'], '_temp')
352 source_mtime = os.path.getmtime(mapping['_temp'])
353 source_timestamp = importlib._w_long(source_mtime)
354 with open(bytecode_path, 'rb') as bytecode_file:
355 bytecode_file.seek(4)
356 self.assertEqual(bytecode_file.read(4), source_timestamp)
357
Brett Cannone52c9192009-11-07 23:55:05 +0000358 # [bytecode read-only]
359 @source_util.writes_bytecode_files
360 def test_read_only_bytecode(self):
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000361 # When bytecode is read-only but should be rewritten, fail silently.
Brett Cannone52c9192009-11-07 23:55:05 +0000362 with source_util.create_modules('_temp') as mapping:
363 # Create bytecode that will need to be re-created.
364 py_compile.compile(mapping['_temp'])
Barry Warsaw28a691b2010-04-17 00:19:56 +0000365 bytecode_path = imp.cache_from_source(mapping['_temp'])
Brett Cannone52c9192009-11-07 23:55:05 +0000366 with open(bytecode_path, 'r+b') as bytecode_file:
367 bytecode_file.seek(0)
368 bytecode_file.write(b'\x00\x00\x00\x00')
369 # Make the bytecode read-only.
370 os.chmod(bytecode_path,
371 stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
372 try:
373 # Should not raise IOError!
374 self.import_(mapping['_temp'], '_temp')
375 finally:
376 # Make writable for eventual clean-up.
377 os.chmod(bytecode_path, stat.S_IWUSR)
378
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000379
Brett Cannon61b14252010-07-03 21:48:25 +0000380class SourcelessLoaderBadBytecodeTest(BadBytecodeTest):
381
382 loader = _bootstrap._SourcelessFileLoader
383
384 def test_empty_file(self):
385 def test(name, mapping, bytecode_path):
Brett Cannonbbb66802012-04-12 21:09:01 -0400386 with self.assertRaises(ImportError) as cm:
Brett Cannon61b14252010-07-03 21:48:25 +0000387 self.import_(bytecode_path, name)
Brett Cannonbbb66802012-04-12 21:09:01 -0400388 self.assertEqual(cm.exception.name, name)
389 self.assertEqual(cm.exception.path, bytecode_path)
Brett Cannon61b14252010-07-03 21:48:25 +0000390
391 self._test_empty_file(test, del_source=True)
392
393 def test_partial_magic(self):
394 def test(name, mapping, bytecode_path):
Brett Cannonbbb66802012-04-12 21:09:01 -0400395 with self.assertRaises(ImportError) as cm:
Brett Cannon61b14252010-07-03 21:48:25 +0000396 self.import_(bytecode_path, name)
Brett Cannonbbb66802012-04-12 21:09:01 -0400397 self.assertEqual(cm.exception.name, name)
398 self.assertEqual(cm.exception.path, bytecode_path)
Brett Cannon61b14252010-07-03 21:48:25 +0000399 self._test_partial_magic(test, del_source=True)
400
401 def test_magic_only(self):
402 def test(name, mapping, bytecode_path):
403 with self.assertRaises(EOFError):
404 self.import_(bytecode_path, name)
405
406 self._test_magic_only(test, del_source=True)
407
408 def test_bad_magic(self):
409 def test(name, mapping, bytecode_path):
Brett Cannonbbb66802012-04-12 21:09:01 -0400410 with self.assertRaises(ImportError) as cm:
Brett Cannon61b14252010-07-03 21:48:25 +0000411 self.import_(bytecode_path, name)
Brett Cannonbbb66802012-04-12 21:09:01 -0400412 self.assertEqual(cm.exception.name, name)
413 self.assertEqual(cm.exception.path, bytecode_path)
Brett Cannon61b14252010-07-03 21:48:25 +0000414
415 self._test_bad_magic(test, del_source=True)
416
417 def test_partial_timestamp(self):
418 def test(name, mapping, bytecode_path):
419 with self.assertRaises(EOFError):
420 self.import_(bytecode_path, name)
421
422 self._test_partial_timestamp(test, del_source=True)
423
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100424 def test_partial_size(self):
425 def test(name, mapping, bytecode_path):
426 with self.assertRaises(EOFError):
427 self.import_(bytecode_path, name)
428
429 self._test_partial_size(test, del_source=True)
430
Brett Cannon61b14252010-07-03 21:48:25 +0000431 def test_no_marshal(self):
432 self._test_no_marshal(del_source=True)
433
434 def test_non_code_marshal(self):
435 self._test_non_code_marshal(del_source=True)
436
437
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000438def test_main():
439 from test.support import run_unittest
Brett Cannon61b14252010-07-03 21:48:25 +0000440 run_unittest(SimpleTest,
441 SourceLoaderBadBytecodeTest,
442 SourcelessLoaderBadBytecodeTest
Brett Cannon186335b2010-08-22 22:11:06 +0000443 )
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000444
445
446if __name__ == '__main__':
447 test_main()