Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 1 | from .. import abc |
Brett Cannon | d71bed3 | 2010-07-03 22:18:47 +0000 | [diff] [blame] | 2 | from .. import util |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 3 | from . import util as source_util |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 4 | |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 5 | importlib = util.import_importlib('importlib') |
| 6 | importlib_abc = util.import_importlib('importlib.abc') |
| 7 | machinery = util.import_importlib('importlib.machinery') |
| 8 | importlib_util = util.import_importlib('importlib.util') |
| 9 | |
Antoine Pitrou | dd21f68 | 2012-01-25 03:00:57 +0100 | [diff] [blame] | 10 | import errno |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 11 | import marshal |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 12 | import os |
| 13 | import py_compile |
Brett Cannon | 186335b | 2010-08-22 22:11:06 +0000 | [diff] [blame] | 14 | import shutil |
Brett Cannon | e52c919 | 2009-11-07 23:55:05 +0000 | [diff] [blame] | 15 | import stat |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 16 | import sys |
Brett Cannon | ef88802 | 2013-06-15 18:39:21 -0400 | [diff] [blame] | 17 | import types |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 18 | import unittest |
| 19 | |
Brett Cannon | 0dbb4c7 | 2013-05-31 18:56:47 -0400 | [diff] [blame] | 20 | from test.support import make_legacy_pyc, unload |
Barry Warsaw | 04b5684 | 2010-05-18 14:15:20 +0000 | [diff] [blame] | 21 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 22 | |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 23 | class SimpleTest(abc.LoaderTests): |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 24 | |
| 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 Snow | d749c7a | 2014-01-04 15:06:49 -0700 | [diff] [blame^] | 30 | 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 Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 35 | def test_load_module_API(self): |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 36 | class Tester(self.abc.FileLoader): |
Brett Cannon | 0dbb4c7 | 2013-05-31 18:56:47 -0400 | [diff] [blame] | 37 | def get_source(self, _): return 'attr = 42' |
| 38 | def is_package(self, _): return False |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 39 | |
Brett Cannon | 0dbb4c7 | 2013-05-31 18:56:47 -0400 | [diff] [blame] | 40 | loader = Tester('blah', 'blah.py') |
| 41 | self.addCleanup(unload, 'blah') |
| 42 | module = loader.load_module() # Should not raise an exception. |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 43 | |
| 44 | def test_get_filename_API(self): |
| 45 | # If fullname is not set then assume self.path is desired. |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 46 | class Tester(self.abc.FileLoader): |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 47 | def get_code(self, _): pass |
| 48 | def get_source(self, _): pass |
| 49 | def is_package(self, _): pass |
Barry Warsaw | 0efcf99 | 2012-07-31 17:52:32 -0400 | [diff] [blame] | 50 | def module_repr(self, _): pass |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 51 | |
| 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 Snow | d749c7a | 2014-01-04 15:06:49 -0700 | [diff] [blame^] | 61 | 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 Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 69 | # [basic] |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 70 | def test_module(self): |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 71 | with source_util.create_modules('_temp') as mapping: |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 72 | loader = self.machinery.SourceFileLoader('_temp', mapping['_temp']) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 73 | module = loader.load_module('_temp') |
Eric V. Smith | faae3ad | 2012-06-27 15:26:26 -0400 | [diff] [blame] | 74 | self.assertIn('_temp', sys.modules) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 75 | check = {'__name__': '_temp', '__file__': mapping['_temp'], |
Brett Cannon | 06c9d96 | 2009-02-07 01:52:25 +0000 | [diff] [blame] | 76 | '__package__': ''} |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 77 | for attr, value in check.items(): |
| 78 | self.assertEqual(getattr(module, attr), value) |
| 79 | |
| 80 | def test_package(self): |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 81 | with source_util.create_modules('_pkg.__init__') as mapping: |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 82 | loader = self.machinery.SourceFileLoader('_pkg', |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 83 | mapping['_pkg.__init__']) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 84 | module = loader.load_module('_pkg') |
Eric V. Smith | faae3ad | 2012-06-27 15:26:26 -0400 | [diff] [blame] | 85 | self.assertIn('_pkg', sys.modules) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 86 | 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 Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 94 | with source_util.create_modules('_pkg.__init__', '_pkg.mod')as mapping: |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 95 | loader = self.machinery.SourceFileLoader('_pkg.mod', |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 96 | mapping['_pkg.mod']) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 97 | module = loader.load_module('_pkg.mod') |
Eric V. Smith | faae3ad | 2012-06-27 15:26:26 -0400 | [diff] [blame] | 98 | self.assertIn('_pkg.mod', sys.modules) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 99 | 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 Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 109 | with source_util.create_modules('_temp') as mapping: |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 110 | loader = self.machinery.SourceFileLoader('_temp', mapping['_temp']) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 111 | 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 Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 116 | module = loader.load_module('_temp') |
Eric V. Smith | faae3ad | 2012-06-27 15:26:26 -0400 | [diff] [blame] | 117 | self.assertIn('testing_var', module.__dict__, |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 118 | "'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 Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 129 | with source_util.create_modules(name) as mapping: |
Brett Cannon | ef88802 | 2013-06-15 18:39:21 -0400 | [diff] [blame] | 130 | orig_module = types.ModuleType(name) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 131 | for attr in attributes: |
| 132 | setattr(orig_module, attr, value) |
| 133 | with open(mapping[name], 'w') as file: |
| 134 | file.write('+++ bad syntax +++') |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 135 | loader = self.machinery.SourceFileLoader('_temp', mapping['_temp']) |
Brett Cannon | 2153dc0 | 2009-08-27 23:49:21 +0000 | [diff] [blame] | 136 | with self.assertRaises(SyntaxError): |
Brett Cannon | 2f8c835 | 2013-12-06 14:25:01 -0500 | [diff] [blame] | 137 | loader.exec_module(orig_module) |
| 138 | for attr in attributes: |
| 139 | self.assertEqual(getattr(orig_module, attr), value) |
| 140 | with self.assertRaises(SyntaxError): |
Brett Cannon | 2153dc0 | 2009-08-27 23:49:21 +0000 | [diff] [blame] | 141 | loader.load_module(name) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 142 | for attr in attributes: |
| 143 | self.assertEqual(getattr(orig_module, attr), value) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 144 | |
| 145 | # [syntax error] |
| 146 | def test_bad_syntax(self): |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 147 | with source_util.create_modules('_temp') as mapping: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 148 | with open(mapping['_temp'], 'w') as file: |
| 149 | file.write('=') |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 150 | loader = self.machinery.SourceFileLoader('_temp', mapping['_temp']) |
Brett Cannon | 2153dc0 | 2009-08-27 23:49:21 +0000 | [diff] [blame] | 151 | with self.assertRaises(SyntaxError): |
| 152 | loader.load_module('_temp') |
Eric V. Smith | faae3ad | 2012-06-27 15:26:26 -0400 | [diff] [blame] | 153 | self.assertNotIn('_temp', sys.modules) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 154 | |
Brett Cannon | d71bed3 | 2010-07-03 22:18:47 +0000 | [diff] [blame] | 155 | 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 Cannon | 186335b | 2010-08-22 22:11:06 +0000 | [diff] [blame] | 158 | file_path = '_temp.py' |
| 159 | with open(file_path, 'w') as file: |
Brett Cannon | d71bed3 | 2010-07-03 22:18:47 +0000 | [diff] [blame] | 160 | file.write("# test file for importlib") |
| 161 | try: |
| 162 | with util.uncache('_temp'): |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 163 | loader = self.machinery.SourceFileLoader('_temp', file_path) |
Brett Cannon | d71bed3 | 2010-07-03 22:18:47 +0000 | [diff] [blame] | 164 | mod = loader.load_module('_temp') |
Brett Cannon | 186335b | 2010-08-22 22:11:06 +0000 | [diff] [blame] | 165 | self.assertEqual(file_path, mod.__file__) |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 166 | self.assertEqual(self.util.cache_from_source(file_path), |
Brett Cannon | d71bed3 | 2010-07-03 22:18:47 +0000 | [diff] [blame] | 167 | mod.__cached__) |
Brett Cannon | d71bed3 | 2010-07-03 22:18:47 +0000 | [diff] [blame] | 168 | finally: |
Brett Cannon | 186335b | 2010-08-22 22:11:06 +0000 | [diff] [blame] | 169 | os.unlink(file_path) |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 170 | pycache = os.path.dirname(self.util.cache_from_source(file_path)) |
Ezio Melotti | c28f6fa | 2013-03-16 19:48:51 +0200 | [diff] [blame] | 171 | if os.path.exists(pycache): |
| 172 | shutil.rmtree(pycache) |
Brett Cannon | d71bed3 | 2010-07-03 22:18:47 +0000 | [diff] [blame] | 173 | |
Antoine Pitrou | 2be60af | 2012-01-24 17:44:06 +0100 | [diff] [blame] | 174 | 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 Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 179 | compiled = self.util.cache_from_source(source) |
Antoine Pitrou | 2be60af | 2012-01-24 17:44:06 +0100 | [diff] [blame] | 180 | with open(source, 'w') as f: |
| 181 | f.write("x = 5") |
Antoine Pitrou | dd21f68 | 2012-01-25 03:00:57 +0100 | [diff] [blame] | 182 | try: |
Antoine Pitrou | 33d15f7 | 2012-01-25 18:01:45 +0100 | [diff] [blame] | 183 | os.utime(source, (2 ** 33 - 5, 2 ** 33 - 5)) |
Antoine Pitrou | dd21f68 | 2012-01-25 03:00:57 +0100 | [diff] [blame] | 184 | 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 Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 190 | loader = self.machinery.SourceFileLoader('_temp', mapping['_temp']) |
Brett Cannon | 2f8c835 | 2013-12-06 14:25:01 -0500 | [diff] [blame] | 191 | # 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 Pitrou | 2be60af | 2012-01-24 17:44:06 +0100 | [diff] [blame] | 200 | # Sanity checks. |
| 201 | self.assertEqual(mod.__cached__, compiled) |
| 202 | self.assertEqual(mod.x, 5) |
| 203 | # The pyc file was created. |
Brett Cannon | 2f8c835 | 2013-12-06 14:25:01 -0500 | [diff] [blame] | 204 | self.assertTrue(os.path.exists(compiled)) |
Antoine Pitrou | 2be60af | 2012-01-24 17:44:06 +0100 | [diff] [blame] | 205 | |
Brett Cannon | b3d6aff | 2013-10-25 12:33:59 -0400 | [diff] [blame] | 206 | def test_unloadable(self): |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 207 | loader = self.machinery.SourceFileLoader('good name', {}) |
Brett Cannon | 2f8c835 | 2013-12-06 14:25:01 -0500 | [diff] [blame] | 208 | 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 Cannon | b3d6aff | 2013-10-25 12:33:59 -0400 | [diff] [blame] | 212 | with self.assertRaises(ImportError): |
| 213 | loader.load_module('bad name') |
| 214 | |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 215 | Frozen_SimpleTest, Source_SimpleTest = util.test_both( |
| 216 | SimpleTest, importlib=importlib, machinery=machinery, abc=importlib_abc, |
| 217 | util=importlib_util) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 218 | |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 219 | |
| 220 | class BadBytecodeTest: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 221 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 222 | def import_(self, file, module_name): |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 223 | loader = self.loader(module_name, file) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 224 | module = loader.load_module(module_name) |
Eric V. Smith | faae3ad | 2012-06-27 15:26:26 -0400 | [diff] [blame] | 225 | self.assertIn(module_name, sys.modules) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 226 | |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 227 | 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 Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 236 | if not del_source: |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 237 | bytecode_path = self.util.cache_from_source(mapping[name]) |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 238 | else: |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 239 | os.unlink(mapping[name]) |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 240 | 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 Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 248 | return bytecode_path |
| 249 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 250 | 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 Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 281 | 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 Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 288 | 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 Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 291 | lambda bc: bc[:12], |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 292 | 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 Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 300 | lambda bc: bc[:12] + marshal.dumps(b'abcd'), |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 301 | del_source=del_source) |
| 302 | file_path = mapping['_temp'] if not del_source else bytecode_path |
Brett Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 303 | with self.assertRaises(ImportError) as cm: |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 304 | self.import_(file_path, '_temp') |
Brett Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 305 | self.assertEqual(cm.exception.name, '_temp') |
| 306 | self.assertEqual(cm.exception.path, bytecode_path) |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 307 | |
| 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 Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 311 | lambda bc: bc[:12] + b'<test>', |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 312 | del_source=del_source) |
| 313 | file_path = mapping['_temp'] if not del_source else bytecode_path |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 314 | with self.assertRaises(EOFError): |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 315 | 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 Cannon | 2f8c835 | 2013-12-06 14:25:01 -0500 | [diff] [blame] | 323 | class BadBytecodeTestPEP451(BadBytecodeTest): |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 324 | |
Brett Cannon | 2f8c835 | 2013-12-06 14:25:01 -0500 | [diff] [blame] | 325 | 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 | |
| 331 | class 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 | |
| 339 | class SourceLoaderBadBytecodeTest: |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 340 | |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 341 | @classmethod |
| 342 | def setUpClass(cls): |
| 343 | cls.loader = cls.machinery.SourceFileLoader |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 344 | |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 345 | @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 Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 349 | def test(name, mapping, bytecode_path): |
| 350 | self.import_(mapping[name], name) |
| 351 | with open(bytecode_path, 'rb') as file: |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 352 | self.assertGreater(len(file.read()), 12) |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 353 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 354 | self._test_empty_file(test) |
| 355 | |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 356 | def test_partial_magic(self): |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 357 | def test(name, mapping, bytecode_path): |
| 358 | self.import_(mapping[name], name) |
| 359 | with open(bytecode_path, 'rb') as file: |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 360 | self.assertGreater(len(file.read()), 12) |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 361 | |
| 362 | self._test_partial_magic(test) |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 363 | |
| 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 Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 368 | def test(name, mapping, bytecode_path): |
| 369 | self.import_(mapping[name], name) |
| 370 | with open(bytecode_path, 'rb') as file: |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 371 | self.assertGreater(len(file.read()), 12) |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 372 | |
Antoine Pitrou | 7c9907e | 2011-12-30 21:25:15 +0100 | [diff] [blame] | 373 | self._test_magic_only(test) |
| 374 | |
Brett Cannon | 1262e7c | 2009-05-11 01:47:11 +0000 | [diff] [blame] | 375 | @source_util.writes_bytecode_files |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 376 | def test_bad_magic(self): |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 377 | # When the magic number is different, the bytecode should be |
| 378 | # regenerated. |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 379 | def test(name, mapping, bytecode_path): |
| 380 | self.import_(mapping[name], name) |
| 381 | with open(bytecode_path, 'rb') as bytecode_file: |
Brett Cannon | ef88802 | 2013-06-15 18:39:21 -0400 | [diff] [blame] | 382 | self.assertEqual(bytecode_file.read(4), |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 383 | self.util.MAGIC_NUMBER) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 384 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 385 | 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 Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 394 | self.assertGreater(len(file.read()), 12) |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 395 | |
Antoine Pitrou | 7c9907e | 2011-12-30 21:25:15 +0100 | [diff] [blame] | 396 | self._test_partial_timestamp(test) |
| 397 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 398 | @source_util.writes_bytecode_files |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 399 | 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 Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 410 | 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 Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 425 | # [bad timestamp] |
Brett Cannon | 1262e7c | 2009-05-11 01:47:11 +0000 | [diff] [blame] | 426 | @source_util.writes_bytecode_files |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 427 | def test_old_timestamp(self): |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 428 | # When the timestamp is older than the source, bytecode should be |
| 429 | # regenerated. |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 430 | zeros = b'\x00\x00\x00\x00' |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 431 | with source_util.create_modules('_temp') as mapping: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 432 | py_compile.compile(mapping['_temp']) |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 433 | bytecode_path = self.util.cache_from_source(mapping['_temp']) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 434 | 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 Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 439 | source_timestamp = self.importlib._w_long(source_mtime) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 440 | with open(bytecode_path, 'rb') as bytecode_file: |
| 441 | bytecode_file.seek(4) |
| 442 | self.assertEqual(bytecode_file.read(4), source_timestamp) |
| 443 | |
Brett Cannon | e52c919 | 2009-11-07 23:55:05 +0000 | [diff] [blame] | 444 | # [bytecode read-only] |
| 445 | @source_util.writes_bytecode_files |
| 446 | def test_read_only_bytecode(self): |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 447 | # When bytecode is read-only but should be rewritten, fail silently. |
Brett Cannon | e52c919 | 2009-11-07 23:55:05 +0000 | [diff] [blame] | 448 | 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 Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 451 | bytecode_path = self.util.cache_from_source(mapping['_temp']) |
Brett Cannon | e52c919 | 2009-11-07 23:55:05 +0000 | [diff] [blame] | 452 | 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 Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 459 | # Should not raise OSError! |
Brett Cannon | e52c919 | 2009-11-07 23:55:05 +0000 | [diff] [blame] | 460 | self.import_(mapping['_temp'], '_temp') |
| 461 | finally: |
| 462 | # Make writable for eventual clean-up. |
| 463 | os.chmod(bytecode_path, stat.S_IWUSR) |
| 464 | |
Brett Cannon | 2f8c835 | 2013-12-06 14:25:01 -0500 | [diff] [blame] | 465 | class SourceLoaderBadBytecodeTestPEP451( |
| 466 | SourceLoaderBadBytecodeTest, BadBytecodeTestPEP451): |
| 467 | pass |
| 468 | |
| 469 | Frozen_SourceBadBytecodePEP451, Source_SourceBadBytecodePEP451 = util.test_both( |
| 470 | SourceLoaderBadBytecodeTestPEP451, importlib=importlib, machinery=machinery, |
| 471 | abc=importlib_abc, util=importlib_util) |
| 472 | |
| 473 | class SourceLoaderBadBytecodeTestPEP302( |
| 474 | SourceLoaderBadBytecodeTest, BadBytecodeTestPEP302): |
| 475 | pass |
| 476 | |
| 477 | Frozen_SourceBadBytecodePEP302, Source_SourceBadBytecodePEP302 = util.test_both( |
| 478 | SourceLoaderBadBytecodeTestPEP302, importlib=importlib, machinery=machinery, |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 479 | abc=importlib_abc, util=importlib_util) |
| 480 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 481 | |
Brett Cannon | 2f8c835 | 2013-12-06 14:25:01 -0500 | [diff] [blame] | 482 | class SourcelessLoaderBadBytecodeTest: |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 483 | |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 484 | @classmethod |
| 485 | def setUpClass(cls): |
| 486 | cls.loader = cls.machinery.SourcelessFileLoader |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 487 | |
| 488 | def test_empty_file(self): |
| 489 | def test(name, mapping, bytecode_path): |
Brett Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 490 | with self.assertRaises(ImportError) as cm: |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 491 | self.import_(bytecode_path, name) |
Brett Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 492 | self.assertEqual(cm.exception.name, name) |
| 493 | self.assertEqual(cm.exception.path, bytecode_path) |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 494 | |
| 495 | self._test_empty_file(test, del_source=True) |
| 496 | |
| 497 | def test_partial_magic(self): |
| 498 | def test(name, mapping, bytecode_path): |
Brett Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 499 | with self.assertRaises(ImportError) as cm: |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 500 | self.import_(bytecode_path, name) |
Brett Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 501 | self.assertEqual(cm.exception.name, name) |
| 502 | self.assertEqual(cm.exception.path, bytecode_path) |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 503 | 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 Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 514 | with self.assertRaises(ImportError) as cm: |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 515 | self.import_(bytecode_path, name) |
Brett Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 516 | self.assertEqual(cm.exception.name, name) |
| 517 | self.assertEqual(cm.exception.path, bytecode_path) |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 518 | |
| 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 Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 528 | 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 Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 535 | 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 Cannon | 2f8c835 | 2013-12-06 14:25:01 -0500 | [diff] [blame] | 541 | class SourcelessLoaderBadBytecodeTestPEP451(SourcelessLoaderBadBytecodeTest, |
| 542 | BadBytecodeTestPEP451): |
| 543 | pass |
| 544 | |
| 545 | Frozen_SourcelessBadBytecodePEP451, Source_SourcelessBadBytecodePEP451 = util.test_both( |
| 546 | SourcelessLoaderBadBytecodeTestPEP451, importlib=importlib, |
| 547 | machinery=machinery, abc=importlib_abc, util=importlib_util) |
| 548 | |
| 549 | class SourcelessLoaderBadBytecodeTestPEP302(SourcelessLoaderBadBytecodeTest, |
| 550 | BadBytecodeTestPEP302): |
| 551 | pass |
| 552 | |
| 553 | Frozen_SourcelessBadBytecodePEP302, Source_SourcelessBadBytecodePEP302 = util.test_both( |
| 554 | SourcelessLoaderBadBytecodeTestPEP302, importlib=importlib, |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 555 | machinery=machinery, abc=importlib_abc, util=importlib_util) |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 556 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 557 | |
| 558 | if __name__ == '__main__': |
Brett Cannon | 0dbb4c7 | 2013-05-31 18:56:47 -0400 | [diff] [blame] | 559 | unittest.main() |