blob: 6e248f3bec5ebd202f473f6721dbd5c94433db9f [file] [log] [blame]
Brett Cannon45a5e3a2012-07-20 14:48:53 -04001from importlib import machinery
Brett Cannon23cbd8a2009-01-18 00:24:28 +00002import importlib
Brett Cannonc0499522012-05-11 14:48:41 -04003import importlib.abc
Brett Cannon30b047d2009-02-01 02:05:11 +00004from .. import abc
Brett Cannond71bed32010-07-03 22:18:47 +00005from .. import util
Brett Cannon4ee2cda2009-02-01 03:08:31 +00006from . import util as source_util
Brett Cannon23cbd8a2009-01-18 00:24:28 +00007
Antoine Pitroudd21f682012-01-25 03:00:57 +01008import errno
Brett Cannon23cbd8a2009-01-18 00:24:28 +00009import imp
Brett Cannon61b14252010-07-03 21:48:25 +000010import marshal
Brett Cannon23cbd8a2009-01-18 00:24:28 +000011import os
12import py_compile
Brett Cannon186335b2010-08-22 22:11:06 +000013import shutil
Brett Cannone52c9192009-11-07 23:55:05 +000014import stat
Brett Cannon23cbd8a2009-01-18 00:24:28 +000015import sys
16import unittest
17
Brett Cannon0dbb4c72013-05-31 18:56:47 -040018from test.support import make_legacy_pyc, unload
Barry Warsaw04b56842010-05-18 14:15:20 +000019
Brett Cannon23cbd8a2009-01-18 00:24:28 +000020
21class SimpleTest(unittest.TestCase):
22
23 """Should have no issue importing a source module [basic]. And if there is
24 a syntax error, it should raise a SyntaxError [syntax error].
25
26 """
27
Brett Cannonc0499522012-05-11 14:48:41 -040028 def test_load_module_API(self):
Brett Cannon0dbb4c72013-05-31 18:56:47 -040029 class Tester(importlib.abc.FileLoader):
30 def get_source(self, _): return 'attr = 42'
31 def is_package(self, _): return False
Brett Cannonc0499522012-05-11 14:48:41 -040032
Brett Cannon0dbb4c72013-05-31 18:56:47 -040033 loader = Tester('blah', 'blah.py')
34 self.addCleanup(unload, 'blah')
35 module = loader.load_module() # Should not raise an exception.
Brett Cannonc0499522012-05-11 14:48:41 -040036
37 def test_get_filename_API(self):
38 # If fullname is not set then assume self.path is desired.
39 class Tester(importlib.abc.FileLoader):
40 def get_code(self, _): pass
41 def get_source(self, _): pass
42 def is_package(self, _): pass
Barry Warsaw0efcf992012-07-31 17:52:32 -040043 def module_repr(self, _): pass
Brett Cannonc0499522012-05-11 14:48:41 -040044
45 path = 'some_path'
46 name = 'some_name'
47 loader = Tester(name, path)
48 self.assertEqual(path, loader.get_filename(name))
49 self.assertEqual(path, loader.get_filename())
50 self.assertEqual(path, loader.get_filename(None))
51 with self.assertRaises(ImportError):
52 loader.get_filename(name + 'XXX')
53
Brett Cannon23cbd8a2009-01-18 00:24:28 +000054 # [basic]
Brett Cannon30b047d2009-02-01 02:05:11 +000055 def test_module(self):
Brett Cannon4ee2cda2009-02-01 03:08:31 +000056 with source_util.create_modules('_temp') as mapping:
Brett Cannon45a5e3a2012-07-20 14:48:53 -040057 loader = machinery.SourceFileLoader('_temp', mapping['_temp'])
Brett Cannon30b047d2009-02-01 02:05:11 +000058 module = loader.load_module('_temp')
Eric V. Smithfaae3ad2012-06-27 15:26:26 -040059 self.assertIn('_temp', sys.modules)
Brett Cannon30b047d2009-02-01 02:05:11 +000060 check = {'__name__': '_temp', '__file__': mapping['_temp'],
Brett Cannon06c9d962009-02-07 01:52:25 +000061 '__package__': ''}
Brett Cannon30b047d2009-02-01 02:05:11 +000062 for attr, value in check.items():
63 self.assertEqual(getattr(module, attr), value)
64
65 def test_package(self):
Brett Cannon4ee2cda2009-02-01 03:08:31 +000066 with source_util.create_modules('_pkg.__init__') as mapping:
Brett Cannon45a5e3a2012-07-20 14:48:53 -040067 loader = machinery.SourceFileLoader('_pkg',
Brett Cannon61b14252010-07-03 21:48:25 +000068 mapping['_pkg.__init__'])
Brett Cannon30b047d2009-02-01 02:05:11 +000069 module = loader.load_module('_pkg')
Eric V. Smithfaae3ad2012-06-27 15:26:26 -040070 self.assertIn('_pkg', sys.modules)
Brett Cannon30b047d2009-02-01 02:05:11 +000071 check = {'__name__': '_pkg', '__file__': mapping['_pkg.__init__'],
72 '__path__': [os.path.dirname(mapping['_pkg.__init__'])],
73 '__package__': '_pkg'}
74 for attr, value in check.items():
75 self.assertEqual(getattr(module, attr), value)
76
77
78 def test_lacking_parent(self):
Brett Cannon4ee2cda2009-02-01 03:08:31 +000079 with source_util.create_modules('_pkg.__init__', '_pkg.mod')as mapping:
Brett Cannon45a5e3a2012-07-20 14:48:53 -040080 loader = machinery.SourceFileLoader('_pkg.mod',
Brett Cannon61b14252010-07-03 21:48:25 +000081 mapping['_pkg.mod'])
Brett Cannon30b047d2009-02-01 02:05:11 +000082 module = loader.load_module('_pkg.mod')
Eric V. Smithfaae3ad2012-06-27 15:26:26 -040083 self.assertIn('_pkg.mod', sys.modules)
Brett Cannon30b047d2009-02-01 02:05:11 +000084 check = {'__name__': '_pkg.mod', '__file__': mapping['_pkg.mod'],
85 '__package__': '_pkg'}
86 for attr, value in check.items():
87 self.assertEqual(getattr(module, attr), value)
88
89 def fake_mtime(self, fxn):
90 """Fake mtime to always be higher than expected."""
91 return lambda name: fxn(name) + 1
92
93 def test_module_reuse(self):
Brett Cannon4ee2cda2009-02-01 03:08:31 +000094 with source_util.create_modules('_temp') as mapping:
Brett Cannon45a5e3a2012-07-20 14:48:53 -040095 loader = machinery.SourceFileLoader('_temp', mapping['_temp'])
Brett Cannon30b047d2009-02-01 02:05:11 +000096 module = loader.load_module('_temp')
97 module_id = id(module)
98 module_dict_id = id(module.__dict__)
99 with open(mapping['_temp'], 'w') as file:
100 file.write("testing_var = 42\n")
Brett Cannon30b047d2009-02-01 02:05:11 +0000101 module = loader.load_module('_temp')
Eric V. Smithfaae3ad2012-06-27 15:26:26 -0400102 self.assertIn('testing_var', module.__dict__,
Brett Cannon30b047d2009-02-01 02:05:11 +0000103 "'testing_var' not in "
104 "{0}".format(list(module.__dict__.keys())))
105 self.assertEqual(module, sys.modules['_temp'])
106 self.assertEqual(id(module), module_id)
107 self.assertEqual(id(module.__dict__), module_dict_id)
108
109 def test_state_after_failure(self):
110 # A failed reload should leave the original module intact.
111 attributes = ('__file__', '__path__', '__package__')
112 value = '<test>'
113 name = '_temp'
Brett Cannon4ee2cda2009-02-01 03:08:31 +0000114 with source_util.create_modules(name) as mapping:
Brett Cannon30b047d2009-02-01 02:05:11 +0000115 orig_module = imp.new_module(name)
116 for attr in attributes:
117 setattr(orig_module, attr, value)
118 with open(mapping[name], 'w') as file:
119 file.write('+++ bad syntax +++')
Brett Cannon45a5e3a2012-07-20 14:48:53 -0400120 loader = machinery.SourceFileLoader('_temp', mapping['_temp'])
Brett Cannon2153dc02009-08-27 23:49:21 +0000121 with self.assertRaises(SyntaxError):
122 loader.load_module(name)
Brett Cannon30b047d2009-02-01 02:05:11 +0000123 for attr in attributes:
124 self.assertEqual(getattr(orig_module, attr), value)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000125
126 # [syntax error]
127 def test_bad_syntax(self):
Brett Cannon4ee2cda2009-02-01 03:08:31 +0000128 with source_util.create_modules('_temp') as mapping:
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000129 with open(mapping['_temp'], 'w') as file:
130 file.write('=')
Brett Cannon45a5e3a2012-07-20 14:48:53 -0400131 loader = machinery.SourceFileLoader('_temp', mapping['_temp'])
Brett Cannon2153dc02009-08-27 23:49:21 +0000132 with self.assertRaises(SyntaxError):
133 loader.load_module('_temp')
Eric V. Smithfaae3ad2012-06-27 15:26:26 -0400134 self.assertNotIn('_temp', sys.modules)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000135
Brett Cannond71bed32010-07-03 22:18:47 +0000136 def test_file_from_empty_string_dir(self):
137 # Loading a module found from an empty string entry on sys.path should
138 # not only work, but keep all attributes relative.
Brett Cannon186335b2010-08-22 22:11:06 +0000139 file_path = '_temp.py'
140 with open(file_path, 'w') as file:
Brett Cannond71bed32010-07-03 22:18:47 +0000141 file.write("# test file for importlib")
142 try:
143 with util.uncache('_temp'):
Brett Cannon45a5e3a2012-07-20 14:48:53 -0400144 loader = machinery.SourceFileLoader('_temp', file_path)
Brett Cannond71bed32010-07-03 22:18:47 +0000145 mod = loader.load_module('_temp')
Brett Cannon186335b2010-08-22 22:11:06 +0000146 self.assertEqual(file_path, mod.__file__)
147 self.assertEqual(imp.cache_from_source(file_path),
Brett Cannond71bed32010-07-03 22:18:47 +0000148 mod.__cached__)
Brett Cannond71bed32010-07-03 22:18:47 +0000149 finally:
Brett Cannon186335b2010-08-22 22:11:06 +0000150 os.unlink(file_path)
151 pycache = os.path.dirname(imp.cache_from_source(file_path))
Ezio Melottic28f6fa2013-03-16 19:48:51 +0200152 if os.path.exists(pycache):
153 shutil.rmtree(pycache)
Brett Cannond71bed32010-07-03 22:18:47 +0000154
Antoine Pitrou2be60af2012-01-24 17:44:06 +0100155 def test_timestamp_overflow(self):
156 # When a modification timestamp is larger than 2**32, it should be
157 # truncated rather than raise an OverflowError.
158 with source_util.create_modules('_temp') as mapping:
159 source = mapping['_temp']
160 compiled = imp.cache_from_source(source)
161 with open(source, 'w') as f:
162 f.write("x = 5")
Antoine Pitroudd21f682012-01-25 03:00:57 +0100163 try:
Antoine Pitrou33d15f72012-01-25 18:01:45 +0100164 os.utime(source, (2 ** 33 - 5, 2 ** 33 - 5))
Antoine Pitroudd21f682012-01-25 03:00:57 +0100165 except OverflowError:
166 self.skipTest("cannot set modification time to large integer")
167 except OSError as e:
168 if e.errno != getattr(errno, 'EOVERFLOW', None):
169 raise
170 self.skipTest("cannot set modification time to large integer ({})".format(e))
Brett Cannon45a5e3a2012-07-20 14:48:53 -0400171 loader = machinery.SourceFileLoader('_temp', mapping['_temp'])
Antoine Pitrou2be60af2012-01-24 17:44:06 +0100172 mod = loader.load_module('_temp')
173 # Sanity checks.
174 self.assertEqual(mod.__cached__, compiled)
175 self.assertEqual(mod.x, 5)
176 # The pyc file was created.
177 os.stat(compiled)
178
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000179
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000180class BadBytecodeTest(unittest.TestCase):
181
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000182 def import_(self, file, module_name):
Brett Cannon61b14252010-07-03 21:48:25 +0000183 loader = self.loader(module_name, file)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000184 module = loader.load_module(module_name)
Eric V. Smithfaae3ad2012-06-27 15:26:26 -0400185 self.assertIn(module_name, sys.modules)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000186
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000187 def manipulate_bytecode(self, name, mapping, manipulator, *,
188 del_source=False):
189 """Manipulate the bytecode of a module by passing it into a callable
190 that returns what to use as the new bytecode."""
191 try:
192 del sys.modules['_temp']
193 except KeyError:
194 pass
195 py_compile.compile(mapping[name])
Brett Cannon61b14252010-07-03 21:48:25 +0000196 if not del_source:
197 bytecode_path = imp.cache_from_source(mapping[name])
198 else:
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000199 os.unlink(mapping[name])
Brett Cannon61b14252010-07-03 21:48:25 +0000200 bytecode_path = make_legacy_pyc(mapping[name])
201 if manipulator:
202 with open(bytecode_path, 'rb') as file:
203 bc = file.read()
204 new_bc = manipulator(bc)
205 with open(bytecode_path, 'wb') as file:
206 if new_bc is not None:
207 file.write(new_bc)
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000208 return bytecode_path
209
Brett Cannon61b14252010-07-03 21:48:25 +0000210 def _test_empty_file(self, test, *, del_source=False):
211 with source_util.create_modules('_temp') as mapping:
212 bc_path = self.manipulate_bytecode('_temp', mapping,
213 lambda bc: b'',
214 del_source=del_source)
215 test('_temp', mapping, bc_path)
216
217 @source_util.writes_bytecode_files
218 def _test_partial_magic(self, test, *, del_source=False):
219 # When their are less than 4 bytes to a .pyc, regenerate it if
220 # possible, else raise ImportError.
221 with source_util.create_modules('_temp') as mapping:
222 bc_path = self.manipulate_bytecode('_temp', mapping,
223 lambda bc: bc[:3],
224 del_source=del_source)
225 test('_temp', mapping, bc_path)
226
227 def _test_magic_only(self, test, *, del_source=False):
228 with source_util.create_modules('_temp') as mapping:
229 bc_path = self.manipulate_bytecode('_temp', mapping,
230 lambda bc: bc[:4],
231 del_source=del_source)
232 test('_temp', mapping, bc_path)
233
234 def _test_partial_timestamp(self, test, *, del_source=False):
235 with source_util.create_modules('_temp') as mapping:
236 bc_path = self.manipulate_bytecode('_temp', mapping,
237 lambda bc: bc[:7],
238 del_source=del_source)
239 test('_temp', mapping, bc_path)
240
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100241 def _test_partial_size(self, test, *, del_source=False):
242 with source_util.create_modules('_temp') as mapping:
243 bc_path = self.manipulate_bytecode('_temp', mapping,
244 lambda bc: bc[:11],
245 del_source=del_source)
246 test('_temp', mapping, bc_path)
247
Brett Cannon61b14252010-07-03 21:48:25 +0000248 def _test_no_marshal(self, *, del_source=False):
249 with source_util.create_modules('_temp') as mapping:
250 bc_path = self.manipulate_bytecode('_temp', mapping,
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100251 lambda bc: bc[:12],
Brett Cannon61b14252010-07-03 21:48:25 +0000252 del_source=del_source)
253 file_path = mapping['_temp'] if not del_source else bc_path
254 with self.assertRaises(EOFError):
255 self.import_(file_path, '_temp')
256
257 def _test_non_code_marshal(self, *, del_source=False):
258 with source_util.create_modules('_temp') as mapping:
259 bytecode_path = self.manipulate_bytecode('_temp', mapping,
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100260 lambda bc: bc[:12] + marshal.dumps(b'abcd'),
Brett Cannon61b14252010-07-03 21:48:25 +0000261 del_source=del_source)
262 file_path = mapping['_temp'] if not del_source else bytecode_path
Brett Cannonbbb66802012-04-12 21:09:01 -0400263 with self.assertRaises(ImportError) as cm:
Brett Cannon61b14252010-07-03 21:48:25 +0000264 self.import_(file_path, '_temp')
Brett Cannonbbb66802012-04-12 21:09:01 -0400265 self.assertEqual(cm.exception.name, '_temp')
266 self.assertEqual(cm.exception.path, bytecode_path)
Brett Cannon61b14252010-07-03 21:48:25 +0000267
268 def _test_bad_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] + b'<test>',
Brett Cannon61b14252010-07-03 21:48:25 +0000272 del_source=del_source)
273 file_path = mapping['_temp'] if not del_source else bytecode_path
Vinay Sajip5bdae3b2011-07-02 16:42:47 +0100274 with self.assertRaises(EOFError):
Brett Cannon61b14252010-07-03 21:48:25 +0000275 self.import_(file_path, '_temp')
276
277 def _test_bad_magic(self, test, *, del_source=False):
278 with source_util.create_modules('_temp') as mapping:
279 bc_path = self.manipulate_bytecode('_temp', mapping,
280 lambda bc: b'\x00\x00\x00\x00' + bc[4:])
281 test('_temp', mapping, bc_path)
282
283
284class SourceLoaderBadBytecodeTest(BadBytecodeTest):
285
Brett Cannon45a5e3a2012-07-20 14:48:53 -0400286 loader = machinery.SourceFileLoader
Brett Cannon61b14252010-07-03 21:48:25 +0000287
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000288 @source_util.writes_bytecode_files
289 def test_empty_file(self):
290 # When a .pyc is empty, regenerate it if possible, else raise
291 # ImportError.
Brett Cannon61b14252010-07-03 21:48:25 +0000292 def test(name, mapping, bytecode_path):
293 self.import_(mapping[name], name)
294 with open(bytecode_path, 'rb') as file:
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100295 self.assertGreater(len(file.read()), 12)
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000296
Brett Cannon61b14252010-07-03 21:48:25 +0000297 self._test_empty_file(test)
298
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000299 def test_partial_magic(self):
Brett Cannon61b14252010-07-03 21:48:25 +0000300 def test(name, mapping, bytecode_path):
301 self.import_(mapping[name], name)
302 with open(bytecode_path, 'rb') as file:
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100303 self.assertGreater(len(file.read()), 12)
Brett Cannon61b14252010-07-03 21:48:25 +0000304
305 self._test_partial_magic(test)
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000306
307 @source_util.writes_bytecode_files
308 def test_magic_only(self):
309 # When there is only the magic number, regenerate the .pyc if possible,
310 # else raise EOFError.
Brett Cannon61b14252010-07-03 21:48:25 +0000311 def test(name, mapping, bytecode_path):
312 self.import_(mapping[name], name)
313 with open(bytecode_path, 'rb') as file:
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100314 self.assertGreater(len(file.read()), 12)
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000315
Antoine Pitrou7c9907e2011-12-30 21:25:15 +0100316 self._test_magic_only(test)
317
Brett Cannon1262e7c2009-05-11 01:47:11 +0000318 @source_util.writes_bytecode_files
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000319 def test_bad_magic(self):
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000320 # When the magic number is different, the bytecode should be
321 # regenerated.
Brett Cannon61b14252010-07-03 21:48:25 +0000322 def test(name, mapping, bytecode_path):
323 self.import_(mapping[name], name)
324 with open(bytecode_path, 'rb') as bytecode_file:
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000325 self.assertEqual(bytecode_file.read(4), imp.get_magic())
326
Brett Cannon61b14252010-07-03 21:48:25 +0000327 self._test_bad_magic(test)
328
329 @source_util.writes_bytecode_files
330 def test_partial_timestamp(self):
331 # When the timestamp is partial, regenerate the .pyc, else
332 # raise EOFError.
333 def test(name, mapping, bc_path):
334 self.import_(mapping[name], name)
335 with open(bc_path, 'rb') as file:
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100336 self.assertGreater(len(file.read()), 12)
Brett Cannon61b14252010-07-03 21:48:25 +0000337
Antoine Pitrou7c9907e2011-12-30 21:25:15 +0100338 self._test_partial_timestamp(test)
339
Brett Cannon61b14252010-07-03 21:48:25 +0000340 @source_util.writes_bytecode_files
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100341 def test_partial_size(self):
342 # When the size is partial, regenerate the .pyc, else
343 # raise EOFError.
344 def test(name, mapping, bc_path):
345 self.import_(mapping[name], name)
346 with open(bc_path, 'rb') as file:
347 self.assertGreater(len(file.read()), 12)
348
349 self._test_partial_size(test)
350
351 @source_util.writes_bytecode_files
Brett Cannon61b14252010-07-03 21:48:25 +0000352 def test_no_marshal(self):
353 # When there is only the magic number and timestamp, raise EOFError.
354 self._test_no_marshal()
355
356 @source_util.writes_bytecode_files
357 def test_non_code_marshal(self):
358 self._test_non_code_marshal()
359 # XXX ImportError when sourceless
360
361 # [bad marshal]
362 @source_util.writes_bytecode_files
363 def test_bad_marshal(self):
364 # Bad marshal data should raise a ValueError.
365 self._test_bad_marshal()
366
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000367 # [bad timestamp]
Brett Cannon1262e7c2009-05-11 01:47:11 +0000368 @source_util.writes_bytecode_files
Brett Cannon61b14252010-07-03 21:48:25 +0000369 def test_old_timestamp(self):
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000370 # When the timestamp is older than the source, bytecode should be
371 # regenerated.
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000372 zeros = b'\x00\x00\x00\x00'
Brett Cannon4ee2cda2009-02-01 03:08:31 +0000373 with source_util.create_modules('_temp') as mapping:
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000374 py_compile.compile(mapping['_temp'])
Barry Warsaw28a691b2010-04-17 00:19:56 +0000375 bytecode_path = imp.cache_from_source(mapping['_temp'])
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000376 with open(bytecode_path, 'r+b') as bytecode_file:
377 bytecode_file.seek(4)
378 bytecode_file.write(zeros)
379 self.import_(mapping['_temp'], '_temp')
380 source_mtime = os.path.getmtime(mapping['_temp'])
381 source_timestamp = importlib._w_long(source_mtime)
382 with open(bytecode_path, 'rb') as bytecode_file:
383 bytecode_file.seek(4)
384 self.assertEqual(bytecode_file.read(4), source_timestamp)
385
Brett Cannone52c9192009-11-07 23:55:05 +0000386 # [bytecode read-only]
387 @source_util.writes_bytecode_files
388 def test_read_only_bytecode(self):
Brett Cannon9b3e15f2010-02-19 16:01:06 +0000389 # When bytecode is read-only but should be rewritten, fail silently.
Brett Cannone52c9192009-11-07 23:55:05 +0000390 with source_util.create_modules('_temp') as mapping:
391 # Create bytecode that will need to be re-created.
392 py_compile.compile(mapping['_temp'])
Barry Warsaw28a691b2010-04-17 00:19:56 +0000393 bytecode_path = imp.cache_from_source(mapping['_temp'])
Brett Cannone52c9192009-11-07 23:55:05 +0000394 with open(bytecode_path, 'r+b') as bytecode_file:
395 bytecode_file.seek(0)
396 bytecode_file.write(b'\x00\x00\x00\x00')
397 # Make the bytecode read-only.
398 os.chmod(bytecode_path,
399 stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
400 try:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200401 # Should not raise OSError!
Brett Cannone52c9192009-11-07 23:55:05 +0000402 self.import_(mapping['_temp'], '_temp')
403 finally:
404 # Make writable for eventual clean-up.
405 os.chmod(bytecode_path, stat.S_IWUSR)
406
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000407
Brett Cannon61b14252010-07-03 21:48:25 +0000408class SourcelessLoaderBadBytecodeTest(BadBytecodeTest):
409
Brett Cannon45a5e3a2012-07-20 14:48:53 -0400410 loader = machinery.SourcelessFileLoader
Brett Cannon61b14252010-07-03 21:48:25 +0000411
412 def test_empty_file(self):
413 def test(name, mapping, bytecode_path):
Brett Cannonbbb66802012-04-12 21:09:01 -0400414 with self.assertRaises(ImportError) as cm:
Brett Cannon61b14252010-07-03 21:48:25 +0000415 self.import_(bytecode_path, name)
Brett Cannonbbb66802012-04-12 21:09:01 -0400416 self.assertEqual(cm.exception.name, name)
417 self.assertEqual(cm.exception.path, bytecode_path)
Brett Cannon61b14252010-07-03 21:48:25 +0000418
419 self._test_empty_file(test, del_source=True)
420
421 def test_partial_magic(self):
422 def test(name, mapping, bytecode_path):
Brett Cannonbbb66802012-04-12 21:09:01 -0400423 with self.assertRaises(ImportError) as cm:
Brett Cannon61b14252010-07-03 21:48:25 +0000424 self.import_(bytecode_path, name)
Brett Cannonbbb66802012-04-12 21:09:01 -0400425 self.assertEqual(cm.exception.name, name)
426 self.assertEqual(cm.exception.path, bytecode_path)
Brett Cannon61b14252010-07-03 21:48:25 +0000427 self._test_partial_magic(test, del_source=True)
428
429 def test_magic_only(self):
430 def test(name, mapping, bytecode_path):
431 with self.assertRaises(EOFError):
432 self.import_(bytecode_path, name)
433
434 self._test_magic_only(test, del_source=True)
435
436 def test_bad_magic(self):
437 def test(name, mapping, bytecode_path):
Brett Cannonbbb66802012-04-12 21:09:01 -0400438 with self.assertRaises(ImportError) as cm:
Brett Cannon61b14252010-07-03 21:48:25 +0000439 self.import_(bytecode_path, name)
Brett Cannonbbb66802012-04-12 21:09:01 -0400440 self.assertEqual(cm.exception.name, name)
441 self.assertEqual(cm.exception.path, bytecode_path)
Brett Cannon61b14252010-07-03 21:48:25 +0000442
443 self._test_bad_magic(test, del_source=True)
444
445 def test_partial_timestamp(self):
446 def test(name, mapping, bytecode_path):
447 with self.assertRaises(EOFError):
448 self.import_(bytecode_path, name)
449
450 self._test_partial_timestamp(test, del_source=True)
451
Antoine Pitrou5136ac02012-01-13 18:52:16 +0100452 def test_partial_size(self):
453 def test(name, mapping, bytecode_path):
454 with self.assertRaises(EOFError):
455 self.import_(bytecode_path, name)
456
457 self._test_partial_size(test, del_source=True)
458
Brett Cannon61b14252010-07-03 21:48:25 +0000459 def test_no_marshal(self):
460 self._test_no_marshal(del_source=True)
461
462 def test_non_code_marshal(self):
463 self._test_non_code_marshal(del_source=True)
464
465
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000466
467if __name__ == '__main__':
Brett Cannon0dbb4c72013-05-31 18:56:47 -0400468 unittest.main()