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 |
Eric Snow | 1500d49 | 2014-01-06 20:49:04 -0700 | [diff] [blame] | 19 | import warnings |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 20 | |
Brett Cannon | 0dbb4c7 | 2013-05-31 18:56:47 -0400 | [diff] [blame] | 21 | from test.support import make_legacy_pyc, unload |
Barry Warsaw | 04b5684 | 2010-05-18 14:15:20 +0000 | [diff] [blame] | 22 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 23 | |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 24 | class SimpleTest(abc.LoaderTests): |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 25 | |
| 26 | """Should have no issue importing a source module [basic]. And if there is |
| 27 | a syntax error, it should raise a SyntaxError [syntax error]. |
| 28 | |
| 29 | """ |
| 30 | |
Eric Snow | d749c7a | 2014-01-04 15:06:49 -0700 | [diff] [blame] | 31 | def setUp(self): |
| 32 | self.name = 'spam' |
| 33 | self.filepath = os.path.join('ham', self.name + '.py') |
| 34 | self.loader = self.machinery.SourceFileLoader(self.name, self.filepath) |
| 35 | |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 36 | def test_load_module_API(self): |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 37 | class Tester(self.abc.FileLoader): |
Brett Cannon | 0dbb4c7 | 2013-05-31 18:56:47 -0400 | [diff] [blame] | 38 | def get_source(self, _): return 'attr = 42' |
| 39 | def is_package(self, _): return False |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 40 | |
Brett Cannon | 0dbb4c7 | 2013-05-31 18:56:47 -0400 | [diff] [blame] | 41 | loader = Tester('blah', 'blah.py') |
| 42 | self.addCleanup(unload, 'blah') |
Eric Snow | 1500d49 | 2014-01-06 20:49:04 -0700 | [diff] [blame] | 43 | with warnings.catch_warnings(): |
| 44 | warnings.simplefilter('ignore', DeprecationWarning) |
| 45 | module = loader.load_module() # Should not raise an exception. |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 46 | |
| 47 | def test_get_filename_API(self): |
| 48 | # If fullname is not set then assume self.path is desired. |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 49 | class Tester(self.abc.FileLoader): |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 50 | def get_code(self, _): pass |
| 51 | def get_source(self, _): pass |
| 52 | def is_package(self, _): pass |
Barry Warsaw | 0efcf99 | 2012-07-31 17:52:32 -0400 | [diff] [blame] | 53 | def module_repr(self, _): pass |
Brett Cannon | c049952 | 2012-05-11 14:48:41 -0400 | [diff] [blame] | 54 | |
| 55 | path = 'some_path' |
| 56 | name = 'some_name' |
| 57 | loader = Tester(name, path) |
| 58 | self.assertEqual(path, loader.get_filename(name)) |
| 59 | self.assertEqual(path, loader.get_filename()) |
| 60 | self.assertEqual(path, loader.get_filename(None)) |
| 61 | with self.assertRaises(ImportError): |
| 62 | loader.get_filename(name + 'XXX') |
| 63 | |
Eric Snow | d749c7a | 2014-01-04 15:06:49 -0700 | [diff] [blame] | 64 | def test_equality(self): |
| 65 | other = self.machinery.SourceFileLoader(self.name, self.filepath) |
| 66 | self.assertEqual(self.loader, other) |
| 67 | |
| 68 | def test_inequality(self): |
| 69 | other = self.machinery.SourceFileLoader('_' + self.name, self.filepath) |
| 70 | self.assertNotEqual(self.loader, other) |
| 71 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 72 | # [basic] |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 73 | def test_module(self): |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 74 | with source_util.create_modules('_temp') as mapping: |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 75 | loader = self.machinery.SourceFileLoader('_temp', mapping['_temp']) |
Eric Snow | 1500d49 | 2014-01-06 20:49:04 -0700 | [diff] [blame] | 76 | with warnings.catch_warnings(): |
| 77 | warnings.simplefilter('ignore', DeprecationWarning) |
| 78 | module = loader.load_module('_temp') |
Eric V. Smith | faae3ad | 2012-06-27 15:26:26 -0400 | [diff] [blame] | 79 | self.assertIn('_temp', sys.modules) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 80 | check = {'__name__': '_temp', '__file__': mapping['_temp'], |
Brett Cannon | 06c9d96 | 2009-02-07 01:52:25 +0000 | [diff] [blame] | 81 | '__package__': ''} |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 82 | for attr, value in check.items(): |
| 83 | self.assertEqual(getattr(module, attr), value) |
| 84 | |
| 85 | def test_package(self): |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 86 | with source_util.create_modules('_pkg.__init__') as mapping: |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 87 | loader = self.machinery.SourceFileLoader('_pkg', |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 88 | mapping['_pkg.__init__']) |
Eric Snow | 1500d49 | 2014-01-06 20:49:04 -0700 | [diff] [blame] | 89 | with warnings.catch_warnings(): |
| 90 | warnings.simplefilter('ignore', DeprecationWarning) |
| 91 | module = loader.load_module('_pkg') |
Eric V. Smith | faae3ad | 2012-06-27 15:26:26 -0400 | [diff] [blame] | 92 | self.assertIn('_pkg', sys.modules) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 93 | check = {'__name__': '_pkg', '__file__': mapping['_pkg.__init__'], |
| 94 | '__path__': [os.path.dirname(mapping['_pkg.__init__'])], |
| 95 | '__package__': '_pkg'} |
| 96 | for attr, value in check.items(): |
| 97 | self.assertEqual(getattr(module, attr), value) |
| 98 | |
| 99 | |
| 100 | def test_lacking_parent(self): |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 101 | with source_util.create_modules('_pkg.__init__', '_pkg.mod')as mapping: |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 102 | loader = self.machinery.SourceFileLoader('_pkg.mod', |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 103 | mapping['_pkg.mod']) |
Eric Snow | 1500d49 | 2014-01-06 20:49:04 -0700 | [diff] [blame] | 104 | with warnings.catch_warnings(): |
| 105 | warnings.simplefilter('ignore', DeprecationWarning) |
| 106 | module = loader.load_module('_pkg.mod') |
Eric V. Smith | faae3ad | 2012-06-27 15:26:26 -0400 | [diff] [blame] | 107 | self.assertIn('_pkg.mod', sys.modules) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 108 | check = {'__name__': '_pkg.mod', '__file__': mapping['_pkg.mod'], |
| 109 | '__package__': '_pkg'} |
| 110 | for attr, value in check.items(): |
| 111 | self.assertEqual(getattr(module, attr), value) |
| 112 | |
| 113 | def fake_mtime(self, fxn): |
| 114 | """Fake mtime to always be higher than expected.""" |
| 115 | return lambda name: fxn(name) + 1 |
| 116 | |
| 117 | def test_module_reuse(self): |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 118 | with source_util.create_modules('_temp') as mapping: |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 119 | loader = self.machinery.SourceFileLoader('_temp', mapping['_temp']) |
Eric Snow | 1500d49 | 2014-01-06 20:49:04 -0700 | [diff] [blame] | 120 | with warnings.catch_warnings(): |
| 121 | warnings.simplefilter('ignore', DeprecationWarning) |
| 122 | module = loader.load_module('_temp') |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 123 | module_id = id(module) |
| 124 | module_dict_id = id(module.__dict__) |
| 125 | with open(mapping['_temp'], 'w') as file: |
| 126 | file.write("testing_var = 42\n") |
Eric Snow | 1500d49 | 2014-01-06 20:49:04 -0700 | [diff] [blame] | 127 | with warnings.catch_warnings(): |
| 128 | warnings.simplefilter('ignore', DeprecationWarning) |
| 129 | module = loader.load_module('_temp') |
Eric V. Smith | faae3ad | 2012-06-27 15:26:26 -0400 | [diff] [blame] | 130 | self.assertIn('testing_var', module.__dict__, |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 131 | "'testing_var' not in " |
| 132 | "{0}".format(list(module.__dict__.keys()))) |
| 133 | self.assertEqual(module, sys.modules['_temp']) |
| 134 | self.assertEqual(id(module), module_id) |
| 135 | self.assertEqual(id(module.__dict__), module_dict_id) |
| 136 | |
| 137 | def test_state_after_failure(self): |
| 138 | # A failed reload should leave the original module intact. |
| 139 | attributes = ('__file__', '__path__', '__package__') |
| 140 | value = '<test>' |
| 141 | name = '_temp' |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 142 | with source_util.create_modules(name) as mapping: |
Brett Cannon | ef88802 | 2013-06-15 18:39:21 -0400 | [diff] [blame] | 143 | orig_module = types.ModuleType(name) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 144 | for attr in attributes: |
| 145 | setattr(orig_module, attr, value) |
| 146 | with open(mapping[name], 'w') as file: |
| 147 | file.write('+++ bad syntax +++') |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 148 | loader = self.machinery.SourceFileLoader('_temp', mapping['_temp']) |
Brett Cannon | 2153dc0 | 2009-08-27 23:49:21 +0000 | [diff] [blame] | 149 | with self.assertRaises(SyntaxError): |
Brett Cannon | 2f8c835 | 2013-12-06 14:25:01 -0500 | [diff] [blame] | 150 | loader.exec_module(orig_module) |
| 151 | for attr in attributes: |
| 152 | self.assertEqual(getattr(orig_module, attr), value) |
| 153 | with self.assertRaises(SyntaxError): |
Eric Snow | 1500d49 | 2014-01-06 20:49:04 -0700 | [diff] [blame] | 154 | with warnings.catch_warnings(): |
| 155 | warnings.simplefilter('ignore', DeprecationWarning) |
| 156 | loader.load_module(name) |
Brett Cannon | 30b047d | 2009-02-01 02:05:11 +0000 | [diff] [blame] | 157 | for attr in attributes: |
| 158 | self.assertEqual(getattr(orig_module, attr), value) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 159 | |
| 160 | # [syntax error] |
| 161 | def test_bad_syntax(self): |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 162 | with source_util.create_modules('_temp') as mapping: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 163 | with open(mapping['_temp'], 'w') as file: |
| 164 | file.write('=') |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 165 | loader = self.machinery.SourceFileLoader('_temp', mapping['_temp']) |
Brett Cannon | 2153dc0 | 2009-08-27 23:49:21 +0000 | [diff] [blame] | 166 | with self.assertRaises(SyntaxError): |
Eric Snow | 1500d49 | 2014-01-06 20:49:04 -0700 | [diff] [blame] | 167 | with warnings.catch_warnings(): |
| 168 | warnings.simplefilter('ignore', DeprecationWarning) |
| 169 | loader.load_module('_temp') |
Eric V. Smith | faae3ad | 2012-06-27 15:26:26 -0400 | [diff] [blame] | 170 | self.assertNotIn('_temp', sys.modules) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 171 | |
Brett Cannon | d71bed3 | 2010-07-03 22:18:47 +0000 | [diff] [blame] | 172 | def test_file_from_empty_string_dir(self): |
| 173 | # Loading a module found from an empty string entry on sys.path should |
| 174 | # not only work, but keep all attributes relative. |
Brett Cannon | 186335b | 2010-08-22 22:11:06 +0000 | [diff] [blame] | 175 | file_path = '_temp.py' |
| 176 | with open(file_path, 'w') as file: |
Brett Cannon | d71bed3 | 2010-07-03 22:18:47 +0000 | [diff] [blame] | 177 | file.write("# test file for importlib") |
| 178 | try: |
| 179 | with util.uncache('_temp'): |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 180 | loader = self.machinery.SourceFileLoader('_temp', file_path) |
Eric Snow | 1500d49 | 2014-01-06 20:49:04 -0700 | [diff] [blame] | 181 | with warnings.catch_warnings(): |
| 182 | warnings.simplefilter('ignore', DeprecationWarning) |
| 183 | mod = loader.load_module('_temp') |
Brett Cannon | 186335b | 2010-08-22 22:11:06 +0000 | [diff] [blame] | 184 | self.assertEqual(file_path, mod.__file__) |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 185 | self.assertEqual(self.util.cache_from_source(file_path), |
Brett Cannon | d71bed3 | 2010-07-03 22:18:47 +0000 | [diff] [blame] | 186 | mod.__cached__) |
Brett Cannon | d71bed3 | 2010-07-03 22:18:47 +0000 | [diff] [blame] | 187 | finally: |
Brett Cannon | 186335b | 2010-08-22 22:11:06 +0000 | [diff] [blame] | 188 | os.unlink(file_path) |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 189 | pycache = os.path.dirname(self.util.cache_from_source(file_path)) |
Ezio Melotti | c28f6fa | 2013-03-16 19:48:51 +0200 | [diff] [blame] | 190 | if os.path.exists(pycache): |
| 191 | shutil.rmtree(pycache) |
Brett Cannon | d71bed3 | 2010-07-03 22:18:47 +0000 | [diff] [blame] | 192 | |
Benjamin Peterson | 3673670 | 2014-02-27 13:49:34 -0500 | [diff] [blame] | 193 | @source_util.writes_bytecode_files |
Antoine Pitrou | 2be60af | 2012-01-24 17:44:06 +0100 | [diff] [blame] | 194 | def test_timestamp_overflow(self): |
| 195 | # When a modification timestamp is larger than 2**32, it should be |
| 196 | # truncated rather than raise an OverflowError. |
| 197 | with source_util.create_modules('_temp') as mapping: |
| 198 | source = mapping['_temp'] |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 199 | compiled = self.util.cache_from_source(source) |
Antoine Pitrou | 2be60af | 2012-01-24 17:44:06 +0100 | [diff] [blame] | 200 | with open(source, 'w') as f: |
| 201 | f.write("x = 5") |
Antoine Pitrou | dd21f68 | 2012-01-25 03:00:57 +0100 | [diff] [blame] | 202 | try: |
Antoine Pitrou | 33d15f7 | 2012-01-25 18:01:45 +0100 | [diff] [blame] | 203 | os.utime(source, (2 ** 33 - 5, 2 ** 33 - 5)) |
Antoine Pitrou | dd21f68 | 2012-01-25 03:00:57 +0100 | [diff] [blame] | 204 | except OverflowError: |
| 205 | self.skipTest("cannot set modification time to large integer") |
| 206 | except OSError as e: |
| 207 | if e.errno != getattr(errno, 'EOVERFLOW', None): |
| 208 | raise |
| 209 | self.skipTest("cannot set modification time to large integer ({})".format(e)) |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 210 | loader = self.machinery.SourceFileLoader('_temp', mapping['_temp']) |
Brett Cannon | 2f8c835 | 2013-12-06 14:25:01 -0500 | [diff] [blame] | 211 | # PEP 451 |
| 212 | module = types.ModuleType('_temp') |
| 213 | module.__spec__ = self.util.spec_from_loader('_temp', loader) |
| 214 | loader.exec_module(module) |
| 215 | self.assertEqual(module.x, 5) |
| 216 | self.assertTrue(os.path.exists(compiled)) |
| 217 | os.unlink(compiled) |
| 218 | # PEP 302 |
Eric Snow | 1500d49 | 2014-01-06 20:49:04 -0700 | [diff] [blame] | 219 | with warnings.catch_warnings(): |
| 220 | warnings.simplefilter('ignore', DeprecationWarning) |
| 221 | mod = loader.load_module('_temp') # XXX |
Antoine Pitrou | 2be60af | 2012-01-24 17:44:06 +0100 | [diff] [blame] | 222 | # Sanity checks. |
| 223 | self.assertEqual(mod.__cached__, compiled) |
| 224 | self.assertEqual(mod.x, 5) |
| 225 | # The pyc file was created. |
Brett Cannon | 2f8c835 | 2013-12-06 14:25:01 -0500 | [diff] [blame] | 226 | self.assertTrue(os.path.exists(compiled)) |
Antoine Pitrou | 2be60af | 2012-01-24 17:44:06 +0100 | [diff] [blame] | 227 | |
Brett Cannon | b3d6aff | 2013-10-25 12:33:59 -0400 | [diff] [blame] | 228 | def test_unloadable(self): |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 229 | loader = self.machinery.SourceFileLoader('good name', {}) |
Brett Cannon | 2f8c835 | 2013-12-06 14:25:01 -0500 | [diff] [blame] | 230 | module = types.ModuleType('bad name') |
| 231 | module.__spec__ = self.machinery.ModuleSpec('bad name', loader) |
| 232 | with self.assertRaises(ImportError): |
| 233 | loader.exec_module(module) |
Brett Cannon | b3d6aff | 2013-10-25 12:33:59 -0400 | [diff] [blame] | 234 | with self.assertRaises(ImportError): |
Eric Snow | 1500d49 | 2014-01-06 20:49:04 -0700 | [diff] [blame] | 235 | with warnings.catch_warnings(): |
| 236 | warnings.simplefilter('ignore', DeprecationWarning) |
| 237 | loader.load_module('bad name') |
Brett Cannon | b3d6aff | 2013-10-25 12:33:59 -0400 | [diff] [blame] | 238 | |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 239 | Frozen_SimpleTest, Source_SimpleTest = util.test_both( |
| 240 | SimpleTest, importlib=importlib, machinery=machinery, abc=importlib_abc, |
| 241 | util=importlib_util) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 242 | |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 243 | |
| 244 | class BadBytecodeTest: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 245 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 246 | def import_(self, file, module_name): |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 247 | loader = self.loader(module_name, file) |
Eric Snow | 1500d49 | 2014-01-06 20:49:04 -0700 | [diff] [blame] | 248 | with warnings.catch_warnings(): |
| 249 | warnings.simplefilter('ignore', DeprecationWarning) |
| 250 | # XXX Change to use exec_module(). |
| 251 | module = loader.load_module(module_name) |
Eric V. Smith | faae3ad | 2012-06-27 15:26:26 -0400 | [diff] [blame] | 252 | self.assertIn(module_name, sys.modules) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 253 | |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 254 | def manipulate_bytecode(self, name, mapping, manipulator, *, |
| 255 | del_source=False): |
| 256 | """Manipulate the bytecode of a module by passing it into a callable |
| 257 | that returns what to use as the new bytecode.""" |
| 258 | try: |
| 259 | del sys.modules['_temp'] |
| 260 | except KeyError: |
| 261 | pass |
| 262 | py_compile.compile(mapping[name]) |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 263 | if not del_source: |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 264 | bytecode_path = self.util.cache_from_source(mapping[name]) |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 265 | else: |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 266 | os.unlink(mapping[name]) |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 267 | bytecode_path = make_legacy_pyc(mapping[name]) |
| 268 | if manipulator: |
| 269 | with open(bytecode_path, 'rb') as file: |
| 270 | bc = file.read() |
| 271 | new_bc = manipulator(bc) |
| 272 | with open(bytecode_path, 'wb') as file: |
| 273 | if new_bc is not None: |
| 274 | file.write(new_bc) |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 275 | return bytecode_path |
| 276 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 277 | def _test_empty_file(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'', |
| 281 | del_source=del_source) |
| 282 | test('_temp', mapping, bc_path) |
| 283 | |
| 284 | @source_util.writes_bytecode_files |
| 285 | def _test_partial_magic(self, test, *, del_source=False): |
| 286 | # When their are less than 4 bytes to a .pyc, regenerate it if |
| 287 | # possible, else raise ImportError. |
| 288 | with source_util.create_modules('_temp') as mapping: |
| 289 | bc_path = self.manipulate_bytecode('_temp', mapping, |
| 290 | lambda bc: bc[:3], |
| 291 | del_source=del_source) |
| 292 | test('_temp', mapping, bc_path) |
| 293 | |
| 294 | def _test_magic_only(self, test, *, del_source=False): |
| 295 | with source_util.create_modules('_temp') as mapping: |
| 296 | bc_path = self.manipulate_bytecode('_temp', mapping, |
| 297 | lambda bc: bc[:4], |
| 298 | del_source=del_source) |
| 299 | test('_temp', mapping, bc_path) |
| 300 | |
| 301 | def _test_partial_timestamp(self, test, *, del_source=False): |
| 302 | with source_util.create_modules('_temp') as mapping: |
| 303 | bc_path = self.manipulate_bytecode('_temp', mapping, |
| 304 | lambda bc: bc[:7], |
| 305 | del_source=del_source) |
| 306 | test('_temp', mapping, bc_path) |
| 307 | |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 308 | def _test_partial_size(self, test, *, del_source=False): |
| 309 | with source_util.create_modules('_temp') as mapping: |
| 310 | bc_path = self.manipulate_bytecode('_temp', mapping, |
| 311 | lambda bc: bc[:11], |
| 312 | del_source=del_source) |
| 313 | test('_temp', mapping, bc_path) |
| 314 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 315 | def _test_no_marshal(self, *, del_source=False): |
| 316 | with source_util.create_modules('_temp') as mapping: |
| 317 | bc_path = self.manipulate_bytecode('_temp', mapping, |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 318 | lambda bc: bc[:12], |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 319 | del_source=del_source) |
| 320 | file_path = mapping['_temp'] if not del_source else bc_path |
| 321 | with self.assertRaises(EOFError): |
| 322 | self.import_(file_path, '_temp') |
| 323 | |
| 324 | def _test_non_code_marshal(self, *, del_source=False): |
| 325 | with source_util.create_modules('_temp') as mapping: |
| 326 | bytecode_path = self.manipulate_bytecode('_temp', mapping, |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 327 | lambda bc: bc[:12] + marshal.dumps(b'abcd'), |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 328 | del_source=del_source) |
| 329 | file_path = mapping['_temp'] if not del_source else bytecode_path |
Brett Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 330 | with self.assertRaises(ImportError) as cm: |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 331 | self.import_(file_path, '_temp') |
Brett Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 332 | self.assertEqual(cm.exception.name, '_temp') |
| 333 | self.assertEqual(cm.exception.path, bytecode_path) |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 334 | |
| 335 | def _test_bad_marshal(self, *, del_source=False): |
| 336 | with source_util.create_modules('_temp') as mapping: |
| 337 | bytecode_path = self.manipulate_bytecode('_temp', mapping, |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 338 | lambda bc: bc[:12] + b'<test>', |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 339 | del_source=del_source) |
| 340 | file_path = mapping['_temp'] if not del_source else bytecode_path |
Vinay Sajip | 5bdae3b | 2011-07-02 16:42:47 +0100 | [diff] [blame] | 341 | with self.assertRaises(EOFError): |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 342 | self.import_(file_path, '_temp') |
| 343 | |
| 344 | def _test_bad_magic(self, test, *, del_source=False): |
| 345 | with source_util.create_modules('_temp') as mapping: |
| 346 | bc_path = self.manipulate_bytecode('_temp', mapping, |
| 347 | lambda bc: b'\x00\x00\x00\x00' + bc[4:]) |
| 348 | test('_temp', mapping, bc_path) |
| 349 | |
Brett Cannon | 2f8c835 | 2013-12-06 14:25:01 -0500 | [diff] [blame] | 350 | class BadBytecodeTestPEP451(BadBytecodeTest): |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 351 | |
Brett Cannon | 2f8c835 | 2013-12-06 14:25:01 -0500 | [diff] [blame] | 352 | def import_(self, file, module_name): |
| 353 | loader = self.loader(module_name, file) |
| 354 | module = types.ModuleType(module_name) |
| 355 | module.__spec__ = self.util.spec_from_loader(module_name, loader) |
| 356 | loader.exec_module(module) |
| 357 | |
| 358 | class BadBytecodeTestPEP302(BadBytecodeTest): |
| 359 | |
| 360 | def import_(self, file, module_name): |
| 361 | loader = self.loader(module_name, file) |
Eric Snow | 1500d49 | 2014-01-06 20:49:04 -0700 | [diff] [blame] | 362 | with warnings.catch_warnings(): |
| 363 | warnings.simplefilter('ignore', DeprecationWarning) |
| 364 | module = loader.load_module(module_name) |
Brett Cannon | 2f8c835 | 2013-12-06 14:25:01 -0500 | [diff] [blame] | 365 | self.assertIn(module_name, sys.modules) |
| 366 | |
| 367 | |
| 368 | class SourceLoaderBadBytecodeTest: |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 369 | |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 370 | @classmethod |
| 371 | def setUpClass(cls): |
| 372 | cls.loader = cls.machinery.SourceFileLoader |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 373 | |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 374 | @source_util.writes_bytecode_files |
| 375 | def test_empty_file(self): |
| 376 | # When a .pyc is empty, regenerate it if possible, else raise |
| 377 | # ImportError. |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 378 | def test(name, mapping, bytecode_path): |
| 379 | self.import_(mapping[name], name) |
| 380 | with open(bytecode_path, 'rb') as file: |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 381 | self.assertGreater(len(file.read()), 12) |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 382 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 383 | self._test_empty_file(test) |
| 384 | |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 385 | def test_partial_magic(self): |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 386 | def test(name, mapping, bytecode_path): |
| 387 | self.import_(mapping[name], name) |
| 388 | with open(bytecode_path, 'rb') as file: |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 389 | self.assertGreater(len(file.read()), 12) |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 390 | |
| 391 | self._test_partial_magic(test) |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 392 | |
| 393 | @source_util.writes_bytecode_files |
| 394 | def test_magic_only(self): |
| 395 | # When there is only the magic number, regenerate the .pyc if possible, |
| 396 | # else raise EOFError. |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 397 | def test(name, mapping, bytecode_path): |
| 398 | self.import_(mapping[name], name) |
| 399 | with open(bytecode_path, 'rb') as file: |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 400 | self.assertGreater(len(file.read()), 12) |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 401 | |
Antoine Pitrou | 7c9907e | 2011-12-30 21:25:15 +0100 | [diff] [blame] | 402 | self._test_magic_only(test) |
| 403 | |
Brett Cannon | 1262e7c | 2009-05-11 01:47:11 +0000 | [diff] [blame] | 404 | @source_util.writes_bytecode_files |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 405 | def test_bad_magic(self): |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 406 | # When the magic number is different, the bytecode should be |
| 407 | # regenerated. |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 408 | def test(name, mapping, bytecode_path): |
| 409 | self.import_(mapping[name], name) |
| 410 | with open(bytecode_path, 'rb') as bytecode_file: |
Brett Cannon | ef88802 | 2013-06-15 18:39:21 -0400 | [diff] [blame] | 411 | self.assertEqual(bytecode_file.read(4), |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 412 | self.util.MAGIC_NUMBER) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 413 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 414 | self._test_bad_magic(test) |
| 415 | |
| 416 | @source_util.writes_bytecode_files |
| 417 | def test_partial_timestamp(self): |
| 418 | # When the timestamp is partial, regenerate the .pyc, else |
| 419 | # raise EOFError. |
| 420 | def test(name, mapping, bc_path): |
| 421 | self.import_(mapping[name], name) |
| 422 | with open(bc_path, 'rb') as file: |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 423 | self.assertGreater(len(file.read()), 12) |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 424 | |
Antoine Pitrou | 7c9907e | 2011-12-30 21:25:15 +0100 | [diff] [blame] | 425 | self._test_partial_timestamp(test) |
| 426 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 427 | @source_util.writes_bytecode_files |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 428 | def test_partial_size(self): |
| 429 | # When the size is partial, regenerate the .pyc, else |
| 430 | # raise EOFError. |
| 431 | def test(name, mapping, bc_path): |
| 432 | self.import_(mapping[name], name) |
| 433 | with open(bc_path, 'rb') as file: |
| 434 | self.assertGreater(len(file.read()), 12) |
| 435 | |
| 436 | self._test_partial_size(test) |
| 437 | |
| 438 | @source_util.writes_bytecode_files |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 439 | def test_no_marshal(self): |
| 440 | # When there is only the magic number and timestamp, raise EOFError. |
| 441 | self._test_no_marshal() |
| 442 | |
| 443 | @source_util.writes_bytecode_files |
| 444 | def test_non_code_marshal(self): |
| 445 | self._test_non_code_marshal() |
| 446 | # XXX ImportError when sourceless |
| 447 | |
| 448 | # [bad marshal] |
| 449 | @source_util.writes_bytecode_files |
| 450 | def test_bad_marshal(self): |
| 451 | # Bad marshal data should raise a ValueError. |
| 452 | self._test_bad_marshal() |
| 453 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 454 | # [bad timestamp] |
Brett Cannon | 1262e7c | 2009-05-11 01:47:11 +0000 | [diff] [blame] | 455 | @source_util.writes_bytecode_files |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 456 | def test_old_timestamp(self): |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 457 | # When the timestamp is older than the source, bytecode should be |
| 458 | # regenerated. |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 459 | zeros = b'\x00\x00\x00\x00' |
Brett Cannon | 4ee2cda | 2009-02-01 03:08:31 +0000 | [diff] [blame] | 460 | with source_util.create_modules('_temp') as mapping: |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 461 | py_compile.compile(mapping['_temp']) |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 462 | bytecode_path = self.util.cache_from_source(mapping['_temp']) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 463 | with open(bytecode_path, 'r+b') as bytecode_file: |
| 464 | bytecode_file.seek(4) |
| 465 | bytecode_file.write(zeros) |
| 466 | self.import_(mapping['_temp'], '_temp') |
| 467 | source_mtime = os.path.getmtime(mapping['_temp']) |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 468 | source_timestamp = self.importlib._w_long(source_mtime) |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 469 | with open(bytecode_path, 'rb') as bytecode_file: |
| 470 | bytecode_file.seek(4) |
| 471 | self.assertEqual(bytecode_file.read(4), source_timestamp) |
| 472 | |
Brett Cannon | e52c919 | 2009-11-07 23:55:05 +0000 | [diff] [blame] | 473 | # [bytecode read-only] |
| 474 | @source_util.writes_bytecode_files |
| 475 | def test_read_only_bytecode(self): |
Brett Cannon | 9b3e15f | 2010-02-19 16:01:06 +0000 | [diff] [blame] | 476 | # When bytecode is read-only but should be rewritten, fail silently. |
Brett Cannon | e52c919 | 2009-11-07 23:55:05 +0000 | [diff] [blame] | 477 | with source_util.create_modules('_temp') as mapping: |
| 478 | # Create bytecode that will need to be re-created. |
| 479 | py_compile.compile(mapping['_temp']) |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 480 | bytecode_path = self.util.cache_from_source(mapping['_temp']) |
Brett Cannon | e52c919 | 2009-11-07 23:55:05 +0000 | [diff] [blame] | 481 | with open(bytecode_path, 'r+b') as bytecode_file: |
| 482 | bytecode_file.seek(0) |
| 483 | bytecode_file.write(b'\x00\x00\x00\x00') |
| 484 | # Make the bytecode read-only. |
| 485 | os.chmod(bytecode_path, |
| 486 | stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH) |
| 487 | try: |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 488 | # Should not raise OSError! |
Brett Cannon | e52c919 | 2009-11-07 23:55:05 +0000 | [diff] [blame] | 489 | self.import_(mapping['_temp'], '_temp') |
| 490 | finally: |
| 491 | # Make writable for eventual clean-up. |
| 492 | os.chmod(bytecode_path, stat.S_IWUSR) |
| 493 | |
Brett Cannon | 2f8c835 | 2013-12-06 14:25:01 -0500 | [diff] [blame] | 494 | class SourceLoaderBadBytecodeTestPEP451( |
| 495 | SourceLoaderBadBytecodeTest, BadBytecodeTestPEP451): |
| 496 | pass |
| 497 | |
| 498 | Frozen_SourceBadBytecodePEP451, Source_SourceBadBytecodePEP451 = util.test_both( |
| 499 | SourceLoaderBadBytecodeTestPEP451, importlib=importlib, machinery=machinery, |
| 500 | abc=importlib_abc, util=importlib_util) |
| 501 | |
| 502 | class SourceLoaderBadBytecodeTestPEP302( |
| 503 | SourceLoaderBadBytecodeTest, BadBytecodeTestPEP302): |
| 504 | pass |
| 505 | |
| 506 | Frozen_SourceBadBytecodePEP302, Source_SourceBadBytecodePEP302 = util.test_both( |
| 507 | SourceLoaderBadBytecodeTestPEP302, importlib=importlib, machinery=machinery, |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 508 | abc=importlib_abc, util=importlib_util) |
| 509 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 510 | |
Brett Cannon | 2f8c835 | 2013-12-06 14:25:01 -0500 | [diff] [blame] | 511 | class SourcelessLoaderBadBytecodeTest: |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 512 | |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 513 | @classmethod |
| 514 | def setUpClass(cls): |
| 515 | cls.loader = cls.machinery.SourcelessFileLoader |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 516 | |
| 517 | def test_empty_file(self): |
| 518 | def test(name, mapping, bytecode_path): |
Brett Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 519 | with self.assertRaises(ImportError) as cm: |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 520 | self.import_(bytecode_path, name) |
Brett Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 521 | self.assertEqual(cm.exception.name, name) |
| 522 | self.assertEqual(cm.exception.path, bytecode_path) |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 523 | |
| 524 | self._test_empty_file(test, del_source=True) |
| 525 | |
| 526 | def test_partial_magic(self): |
| 527 | def test(name, mapping, bytecode_path): |
Brett Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 528 | with self.assertRaises(ImportError) as cm: |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 529 | self.import_(bytecode_path, name) |
Brett Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 530 | self.assertEqual(cm.exception.name, name) |
| 531 | self.assertEqual(cm.exception.path, bytecode_path) |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 532 | self._test_partial_magic(test, del_source=True) |
| 533 | |
| 534 | def test_magic_only(self): |
| 535 | def test(name, mapping, bytecode_path): |
| 536 | with self.assertRaises(EOFError): |
| 537 | self.import_(bytecode_path, name) |
| 538 | |
| 539 | self._test_magic_only(test, del_source=True) |
| 540 | |
| 541 | def test_bad_magic(self): |
| 542 | def test(name, mapping, bytecode_path): |
Brett Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 543 | with self.assertRaises(ImportError) as cm: |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 544 | self.import_(bytecode_path, name) |
Brett Cannon | bbb6680 | 2012-04-12 21:09:01 -0400 | [diff] [blame] | 545 | self.assertEqual(cm.exception.name, name) |
| 546 | self.assertEqual(cm.exception.path, bytecode_path) |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 547 | |
| 548 | self._test_bad_magic(test, del_source=True) |
| 549 | |
| 550 | def test_partial_timestamp(self): |
| 551 | def test(name, mapping, bytecode_path): |
| 552 | with self.assertRaises(EOFError): |
| 553 | self.import_(bytecode_path, name) |
| 554 | |
| 555 | self._test_partial_timestamp(test, del_source=True) |
| 556 | |
Antoine Pitrou | 5136ac0 | 2012-01-13 18:52:16 +0100 | [diff] [blame] | 557 | def test_partial_size(self): |
| 558 | def test(name, mapping, bytecode_path): |
| 559 | with self.assertRaises(EOFError): |
| 560 | self.import_(bytecode_path, name) |
| 561 | |
| 562 | self._test_partial_size(test, del_source=True) |
| 563 | |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 564 | def test_no_marshal(self): |
| 565 | self._test_no_marshal(del_source=True) |
| 566 | |
| 567 | def test_non_code_marshal(self): |
| 568 | self._test_non_code_marshal(del_source=True) |
| 569 | |
Brett Cannon | 2f8c835 | 2013-12-06 14:25:01 -0500 | [diff] [blame] | 570 | class SourcelessLoaderBadBytecodeTestPEP451(SourcelessLoaderBadBytecodeTest, |
| 571 | BadBytecodeTestPEP451): |
| 572 | pass |
| 573 | |
| 574 | Frozen_SourcelessBadBytecodePEP451, Source_SourcelessBadBytecodePEP451 = util.test_both( |
| 575 | SourcelessLoaderBadBytecodeTestPEP451, importlib=importlib, |
| 576 | machinery=machinery, abc=importlib_abc, util=importlib_util) |
| 577 | |
| 578 | class SourcelessLoaderBadBytecodeTestPEP302(SourcelessLoaderBadBytecodeTest, |
| 579 | BadBytecodeTestPEP302): |
| 580 | pass |
| 581 | |
| 582 | Frozen_SourcelessBadBytecodePEP302, Source_SourcelessBadBytecodePEP302 = util.test_both( |
| 583 | SourcelessLoaderBadBytecodeTestPEP302, importlib=importlib, |
Brett Cannon | 9ea8eda | 2013-11-08 14:25:37 -0500 | [diff] [blame] | 584 | machinery=machinery, abc=importlib_abc, util=importlib_util) |
Brett Cannon | 61b1425 | 2010-07-03 21:48:25 +0000 | [diff] [blame] | 585 | |
Brett Cannon | 23cbd8a | 2009-01-18 00:24:28 +0000 | [diff] [blame] | 586 | |
| 587 | if __name__ == '__main__': |
Brett Cannon | 0dbb4c7 | 2013-05-31 18:56:47 -0400 | [diff] [blame] | 588 | unittest.main() |