Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 1 | import importlib |
Brett Cannon | f87e04d | 2009-03-12 22:47:53 +0000 | [diff] [blame] | 2 | from importlib import _bootstrap |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 3 | from .. import abc |
Brett Cannon | d71bed3 | 2010-07-03 22:18:47 +0000 | [diff] [blame^] | 4 | from .. import util |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 5 | from . import util as source_util |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 6 | |
| 7 | import imp |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 8 | import marshal |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 9 | import os |
| 10 | import py_compile |
Brett Cannon | e52c919 | 2009-11-07 23:55:05 +0000 | [diff] [blame] | 11 | import stat |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 12 | import sys |
| 13 | import unittest |
| 14 | |
Barry Warsaw | 04b5684 | 2010-05-18 14:15:20 +0000 | [diff] [blame] | 15 | from test.support import make_legacy_pyc |
| 16 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 17 | |
| 18 | class SimpleTest(unittest.TestCase): |
| 19 | |
| 20 | """Should have no issue importing a source module [basic]. And if there is |
| 21 | a syntax error, it should raise a SyntaxError [syntax error]. |
| 22 | |
| 23 | """ |
| 24 | |
| 25 | # [basic] |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 26 | def test_module(self): |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 27 | with source_util.create_modules('_temp') as mapping: |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 28 | loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp']) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 29 | module = loader.load_module('_temp') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 30 | self.assertTrue('_temp' in sys.modules) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 31 | check = {'__name__': '_temp', '__file__': mapping['_temp'], |
Brett Cannon | 06c9d96 | 2009-02-07 01:52:25 +0000 | [diff] [blame] | 32 | '__package__': ''} |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 33 | for attr, value in check.items(): |
| 34 | self.assertEqual(getattr(module, attr), value) |
| 35 | |
| 36 | def test_package(self): |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 37 | with source_util.create_modules('_pkg.__init__') as mapping: |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 38 | loader = _bootstrap._SourceFileLoader('_pkg', |
| 39 | mapping['_pkg.__init__']) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 40 | module = loader.load_module('_pkg') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 41 | self.assertTrue('_pkg' in sys.modules) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 42 | check = {'__name__': '_pkg', '__file__': mapping['_pkg.__init__'], |
| 43 | '__path__': [os.path.dirname(mapping['_pkg.__init__'])], |
| 44 | '__package__': '_pkg'} |
| 45 | for attr, value in check.items(): |
| 46 | self.assertEqual(getattr(module, attr), value) |
| 47 | |
| 48 | |
| 49 | def test_lacking_parent(self): |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 50 | with source_util.create_modules('_pkg.__init__', '_pkg.mod')as mapping: |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 51 | loader = _bootstrap._SourceFileLoader('_pkg.mod', |
| 52 | mapping['_pkg.mod']) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 53 | module = loader.load_module('_pkg.mod') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 54 | self.assertTrue('_pkg.mod' in sys.modules) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 55 | check = {'__name__': '_pkg.mod', '__file__': mapping['_pkg.mod'], |
| 56 | '__package__': '_pkg'} |
| 57 | for attr, value in check.items(): |
| 58 | self.assertEqual(getattr(module, attr), value) |
| 59 | |
| 60 | def fake_mtime(self, fxn): |
| 61 | """Fake mtime to always be higher than expected.""" |
| 62 | return lambda name: fxn(name) + 1 |
| 63 | |
| 64 | def test_module_reuse(self): |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 65 | with source_util.create_modules('_temp') as mapping: |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 66 | loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp']) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 67 | module = loader.load_module('_temp') |
| 68 | module_id = id(module) |
| 69 | module_dict_id = id(module.__dict__) |
| 70 | with open(mapping['_temp'], 'w') as file: |
| 71 | file.write("testing_var = 42\n") |
| 72 | # For filesystems where the mtime is only to a second granularity, |
| 73 | # everything that has happened above can be too fast; |
| 74 | # force an mtime on the source that is guaranteed to be different |
| 75 | # than the original mtime. |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 76 | loader.path_mtime = self.fake_mtime(loader.path_mtime) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 77 | module = loader.load_module('_temp') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 78 | self.assertTrue('testing_var' in module.__dict__, |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 79 | "'testing_var' not in " |
| 80 | "{0}".format(list(module.__dict__.keys()))) |
| 81 | self.assertEqual(module, sys.modules['_temp']) |
| 82 | self.assertEqual(id(module), module_id) |
| 83 | self.assertEqual(id(module.__dict__), module_dict_id) |
| 84 | |
| 85 | def test_state_after_failure(self): |
| 86 | # A failed reload should leave the original module intact. |
| 87 | attributes = ('__file__', '__path__', '__package__') |
| 88 | value = '<test>' |
| 89 | name = '_temp' |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 90 | with source_util.create_modules(name) as mapping: |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 91 | orig_module = imp.new_module(name) |
| 92 | for attr in attributes: |
| 93 | setattr(orig_module, attr, value) |
| 94 | with open(mapping[name], 'w') as file: |
| 95 | file.write('+++ bad syntax +++') |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 96 | loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp']) |
Brett Cannon | 2153dc0 | 2009-08-27 23:49:21 +0000 | [diff] [blame] | 97 | with self.assertRaises(SyntaxError): |
| 98 | loader.load_module(name) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 99 | for attr in attributes: |
| 100 | self.assertEqual(getattr(orig_module, attr), value) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 101 | |
| 102 | # [syntax error] |
| 103 | def test_bad_syntax(self): |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 104 | with source_util.create_modules('_temp') as mapping: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 105 | with open(mapping['_temp'], 'w') as file: |
| 106 | file.write('=') |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 107 | loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp']) |
Brett Cannon | 2153dc0 | 2009-08-27 23:49:21 +0000 | [diff] [blame] | 108 | with self.assertRaises(SyntaxError): |
| 109 | loader.load_module('_temp') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 110 | self.assertTrue('_temp' not in sys.modules) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 111 | |
Brett Cannon | d71bed3 | 2010-07-03 22:18:47 +0000 | [diff] [blame^] | 112 | def test_file_from_empty_string_dir(self): |
| 113 | # Loading a module found from an empty string entry on sys.path should |
| 114 | # not only work, but keep all attributes relative. |
| 115 | with open('_temp.py', 'w') as file: |
| 116 | file.write("# test file for importlib") |
| 117 | try: |
| 118 | with util.uncache('_temp'): |
| 119 | loader = _bootstrap._SourceFileLoader('_temp', '_temp.py') |
| 120 | mod = loader.load_module('_temp') |
| 121 | self.assertEqual('_temp.py', mod.__file__) |
| 122 | self.assertEqual(imp.cache_from_source('_temp.py'), |
| 123 | mod.__cached__) |
| 124 | |
| 125 | finally: |
| 126 | os.unlink('_temp.py') |
| 127 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 128 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 129 | class BadBytecodeTest(unittest.TestCase): |
| 130 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 131 | def import_(self, file, module_name): |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 132 | loader = self.loader(module_name, file) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 133 | module = loader.load_module(module_name) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 134 | self.assertTrue(module_name in sys.modules) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 135 | |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 136 | def manipulate_bytecode(self, name, mapping, manipulator, *, |
| 137 | del_source=False): |
| 138 | """Manipulate the bytecode of a module by passing it into a callable |
| 139 | that returns what to use as the new bytecode.""" |
| 140 | try: |
| 141 | del sys.modules['_temp'] |
| 142 | except KeyError: |
| 143 | pass |
| 144 | py_compile.compile(mapping[name]) |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 145 | if not del_source: |
| 146 | bytecode_path = imp.cache_from_source(mapping[name]) |
| 147 | else: |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 148 | os.unlink(mapping[name]) |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 149 | bytecode_path = make_legacy_pyc(mapping[name]) |
| 150 | if manipulator: |
| 151 | with open(bytecode_path, 'rb') as file: |
| 152 | bc = file.read() |
| 153 | new_bc = manipulator(bc) |
| 154 | with open(bytecode_path, 'wb') as file: |
| 155 | if new_bc is not None: |
| 156 | file.write(new_bc) |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 157 | return bytecode_path |
| 158 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 159 | def _test_empty_file(self, test, *, del_source=False): |
| 160 | with source_util.create_modules('_temp') as mapping: |
| 161 | bc_path = self.manipulate_bytecode('_temp', mapping, |
| 162 | lambda bc: b'', |
| 163 | del_source=del_source) |
| 164 | test('_temp', mapping, bc_path) |
| 165 | |
| 166 | @source_util.writes_bytecode_files |
| 167 | def _test_partial_magic(self, test, *, del_source=False): |
| 168 | # When their are less than 4 bytes to a .pyc, regenerate it if |
| 169 | # possible, else raise ImportError. |
| 170 | with source_util.create_modules('_temp') as mapping: |
| 171 | bc_path = self.manipulate_bytecode('_temp', mapping, |
| 172 | lambda bc: bc[:3], |
| 173 | del_source=del_source) |
| 174 | test('_temp', mapping, bc_path) |
| 175 | |
| 176 | def _test_magic_only(self, test, *, del_source=False): |
| 177 | with source_util.create_modules('_temp') as mapping: |
| 178 | bc_path = self.manipulate_bytecode('_temp', mapping, |
| 179 | lambda bc: bc[:4], |
| 180 | del_source=del_source) |
| 181 | test('_temp', mapping, bc_path) |
| 182 | |
| 183 | def _test_partial_timestamp(self, test, *, del_source=False): |
| 184 | with source_util.create_modules('_temp') as mapping: |
| 185 | bc_path = self.manipulate_bytecode('_temp', mapping, |
| 186 | lambda bc: bc[:7], |
| 187 | del_source=del_source) |
| 188 | test('_temp', mapping, bc_path) |
| 189 | |
| 190 | def _test_no_marshal(self, *, del_source=False): |
| 191 | with source_util.create_modules('_temp') as mapping: |
| 192 | bc_path = self.manipulate_bytecode('_temp', mapping, |
| 193 | lambda bc: bc[:8], |
| 194 | del_source=del_source) |
| 195 | file_path = mapping['_temp'] if not del_source else bc_path |
| 196 | with self.assertRaises(EOFError): |
| 197 | self.import_(file_path, '_temp') |
| 198 | |
| 199 | def _test_non_code_marshal(self, *, del_source=False): |
| 200 | with source_util.create_modules('_temp') as mapping: |
| 201 | bytecode_path = self.manipulate_bytecode('_temp', mapping, |
| 202 | lambda bc: bc[:8] + marshal.dumps(b'abcd'), |
| 203 | del_source=del_source) |
| 204 | file_path = mapping['_temp'] if not del_source else bytecode_path |
| 205 | with self.assertRaises(ImportError): |
| 206 | self.import_(file_path, '_temp') |
| 207 | |
| 208 | def _test_bad_marshal(self, *, del_source=False): |
| 209 | with source_util.create_modules('_temp') as mapping: |
| 210 | bytecode_path = self.manipulate_bytecode('_temp', mapping, |
| 211 | lambda bc: bc[:8] + b'<test>', |
| 212 | del_source=del_source) |
| 213 | file_path = mapping['_temp'] if not del_source else bytecode_path |
| 214 | with self.assertRaises(ValueError): |
| 215 | self.import_(file_path, '_temp') |
| 216 | |
| 217 | def _test_bad_magic(self, test, *, del_source=False): |
| 218 | with source_util.create_modules('_temp') as mapping: |
| 219 | bc_path = self.manipulate_bytecode('_temp', mapping, |
| 220 | lambda bc: b'\x00\x00\x00\x00' + bc[4:]) |
| 221 | test('_temp', mapping, bc_path) |
| 222 | |
| 223 | |
| 224 | class SourceLoaderBadBytecodeTest(BadBytecodeTest): |
| 225 | |
| 226 | loader = _bootstrap._SourceFileLoader |
| 227 | |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 228 | @source_util.writes_bytecode_files |
| 229 | def test_empty_file(self): |
| 230 | # When a .pyc is empty, regenerate it if possible, else raise |
| 231 | # ImportError. |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 232 | def test(name, mapping, bytecode_path): |
| 233 | self.import_(mapping[name], name) |
| 234 | with open(bytecode_path, 'rb') as file: |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 235 | self.assertGreater(len(file.read()), 8) |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 236 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 237 | self._test_empty_file(test) |
| 238 | |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 239 | def test_partial_magic(self): |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 240 | def test(name, mapping, bytecode_path): |
| 241 | self.import_(mapping[name], name) |
| 242 | with open(bytecode_path, 'rb') as file: |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 243 | self.assertGreater(len(file.read()), 8) |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 244 | |
| 245 | self._test_partial_magic(test) |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 246 | |
| 247 | @source_util.writes_bytecode_files |
| 248 | def test_magic_only(self): |
| 249 | # When there is only the magic number, regenerate the .pyc if possible, |
| 250 | # else raise EOFError. |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 251 | def test(name, mapping, bytecode_path): |
| 252 | self.import_(mapping[name], name) |
| 253 | with open(bytecode_path, 'rb') as file: |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 254 | self.assertGreater(len(file.read()), 8) |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 255 | |
Brett Cannon | 1262e7c | 2009-05-11 01:47:11 +0000 | [diff] [blame] | 256 | @source_util.writes_bytecode_files |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 257 | def test_bad_magic(self): |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 258 | # When the magic number is different, the bytecode should be |
| 259 | # regenerated. |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 260 | def test(name, mapping, bytecode_path): |
| 261 | self.import_(mapping[name], name) |
| 262 | with open(bytecode_path, 'rb') as bytecode_file: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 263 | self.assertEqual(bytecode_file.read(4), imp.get_magic()) |
| 264 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 265 | self._test_bad_magic(test) |
| 266 | |
| 267 | @source_util.writes_bytecode_files |
| 268 | def test_partial_timestamp(self): |
| 269 | # When the timestamp is partial, regenerate the .pyc, else |
| 270 | # raise EOFError. |
| 271 | def test(name, mapping, bc_path): |
| 272 | self.import_(mapping[name], name) |
| 273 | with open(bc_path, 'rb') as file: |
| 274 | self.assertGreater(len(file.read()), 8) |
| 275 | |
| 276 | @source_util.writes_bytecode_files |
| 277 | def test_no_marshal(self): |
| 278 | # When there is only the magic number and timestamp, raise EOFError. |
| 279 | self._test_no_marshal() |
| 280 | |
| 281 | @source_util.writes_bytecode_files |
| 282 | def test_non_code_marshal(self): |
| 283 | self._test_non_code_marshal() |
| 284 | # XXX ImportError when sourceless |
| 285 | |
| 286 | # [bad marshal] |
| 287 | @source_util.writes_bytecode_files |
| 288 | def test_bad_marshal(self): |
| 289 | # Bad marshal data should raise a ValueError. |
| 290 | self._test_bad_marshal() |
| 291 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 292 | # [bad timestamp] |
Brett Cannon | 1262e7c | 2009-05-11 01:47:11 +0000 | [diff] [blame] | 293 | @source_util.writes_bytecode_files |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 294 | def test_old_timestamp(self): |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 295 | # When the timestamp is older than the source, bytecode should be |
| 296 | # regenerated. |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 297 | zeros = b'\x00\x00\x00\x00' |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 298 | with source_util.create_modules('_temp') as mapping: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 299 | py_compile.compile(mapping['_temp']) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 300 | bytecode_path = imp.cache_from_source(mapping['_temp']) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 301 | with open(bytecode_path, 'r+b') as bytecode_file: |
| 302 | bytecode_file.seek(4) |
| 303 | bytecode_file.write(zeros) |
| 304 | self.import_(mapping['_temp'], '_temp') |
| 305 | source_mtime = os.path.getmtime(mapping['_temp']) |
| 306 | source_timestamp = importlib._w_long(source_mtime) |
| 307 | with open(bytecode_path, 'rb') as bytecode_file: |
| 308 | bytecode_file.seek(4) |
| 309 | self.assertEqual(bytecode_file.read(4), source_timestamp) |
| 310 | |
Brett Cannon | e52c919 | 2009-11-07 23:55:05 +0000 | [diff] [blame] | 311 | # [bytecode read-only] |
| 312 | @source_util.writes_bytecode_files |
| 313 | def test_read_only_bytecode(self): |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 314 | # When bytecode is read-only but should be rewritten, fail silently. |
Brett Cannon | e52c919 | 2009-11-07 23:55:05 +0000 | [diff] [blame] | 315 | with source_util.create_modules('_temp') as mapping: |
| 316 | # Create bytecode that will need to be re-created. |
| 317 | py_compile.compile(mapping['_temp']) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 318 | bytecode_path = imp.cache_from_source(mapping['_temp']) |
Brett Cannon | e52c919 | 2009-11-07 23:55:05 +0000 | [diff] [blame] | 319 | with open(bytecode_path, 'r+b') as bytecode_file: |
| 320 | bytecode_file.seek(0) |
| 321 | bytecode_file.write(b'\x00\x00\x00\x00') |
| 322 | # Make the bytecode read-only. |
| 323 | os.chmod(bytecode_path, |
| 324 | stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH) |
| 325 | try: |
| 326 | # Should not raise IOError! |
| 327 | self.import_(mapping['_temp'], '_temp') |
| 328 | finally: |
| 329 | # Make writable for eventual clean-up. |
| 330 | os.chmod(bytecode_path, stat.S_IWUSR) |
| 331 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 332 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 333 | class SourcelessLoaderBadBytecodeTest(BadBytecodeTest): |
| 334 | |
| 335 | loader = _bootstrap._SourcelessFileLoader |
| 336 | |
| 337 | def test_empty_file(self): |
| 338 | def test(name, mapping, bytecode_path): |
| 339 | with self.assertRaises(ImportError): |
| 340 | self.import_(bytecode_path, name) |
| 341 | |
| 342 | self._test_empty_file(test, del_source=True) |
| 343 | |
| 344 | def test_partial_magic(self): |
| 345 | def test(name, mapping, bytecode_path): |
| 346 | with self.assertRaises(ImportError): |
| 347 | self.import_(bytecode_path, name) |
| 348 | self._test_partial_magic(test, del_source=True) |
| 349 | |
| 350 | def test_magic_only(self): |
| 351 | def test(name, mapping, bytecode_path): |
| 352 | with self.assertRaises(EOFError): |
| 353 | self.import_(bytecode_path, name) |
| 354 | |
| 355 | self._test_magic_only(test, del_source=True) |
| 356 | |
| 357 | def test_bad_magic(self): |
| 358 | def test(name, mapping, bytecode_path): |
| 359 | with self.assertRaises(ImportError): |
| 360 | self.import_(bytecode_path, name) |
| 361 | |
| 362 | self._test_bad_magic(test, del_source=True) |
| 363 | |
| 364 | def test_partial_timestamp(self): |
| 365 | def test(name, mapping, bytecode_path): |
| 366 | with self.assertRaises(EOFError): |
| 367 | self.import_(bytecode_path, name) |
| 368 | |
| 369 | self._test_partial_timestamp(test, del_source=True) |
| 370 | |
| 371 | def test_no_marshal(self): |
| 372 | self._test_no_marshal(del_source=True) |
| 373 | |
| 374 | def test_non_code_marshal(self): |
| 375 | self._test_non_code_marshal(del_source=True) |
| 376 | |
| 377 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 378 | def test_main(): |
| 379 | from test.support import run_unittest |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 380 | run_unittest(SimpleTest, |
| 381 | SourceLoaderBadBytecodeTest, |
| 382 | SourcelessLoaderBadBytecodeTest |
| 383 | ) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 384 | |
| 385 | |
| 386 | if __name__ == '__main__': |
| 387 | test_main() |