2009-01-31 22:05:11 -04:00
|
|
|
from .. import abc
|
2010-07-03 19:18:47 -03:00
|
|
|
from .. import util
|
2009-01-17 20:24:28 -04:00
|
|
|
|
2013-11-08 15:25:37 -04:00
|
|
|
importlib = util.import_importlib('importlib')
|
|
|
|
importlib_abc = util.import_importlib('importlib.abc')
|
|
|
|
machinery = util.import_importlib('importlib.machinery')
|
|
|
|
importlib_util = util.import_importlib('importlib.util')
|
|
|
|
|
2012-01-24 22:00:57 -04:00
|
|
|
import errno
|
2010-07-03 18:48:25 -03:00
|
|
|
import marshal
|
2009-01-17 20:24:28 -04:00
|
|
|
import os
|
|
|
|
import py_compile
|
2010-08-22 19:11:06 -03:00
|
|
|
import shutil
|
2009-11-07 19:55:05 -04:00
|
|
|
import stat
|
2009-01-17 20:24:28 -04:00
|
|
|
import sys
|
2013-06-15 19:39:21 -03:00
|
|
|
import types
|
2009-01-17 20:24:28 -04:00
|
|
|
import unittest
|
2014-01-06 23:49:04 -04:00
|
|
|
import warnings
|
2009-01-17 20:24:28 -04:00
|
|
|
|
2013-05-31 19:56:47 -03:00
|
|
|
from test.support import make_legacy_pyc, unload
|
2010-05-18 11:15:20 -03:00
|
|
|
|
2018-10-10 13:43:14 -03:00
|
|
|
from test.test_py_compile import without_source_date_epoch
|
|
|
|
from test.test_py_compile import SourceDateEpochTestMeta
|
|
|
|
|
2009-01-17 20:24:28 -04:00
|
|
|
|
2013-11-08 15:25:37 -04:00
|
|
|
class SimpleTest(abc.LoaderTests):
|
2009-01-17 20:24:28 -04:00
|
|
|
|
|
|
|
"""Should have no issue importing a source module [basic]. And if there is
|
|
|
|
a syntax error, it should raise a SyntaxError [syntax error].
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2014-01-04 18:06:49 -04:00
|
|
|
def setUp(self):
|
|
|
|
self.name = 'spam'
|
|
|
|
self.filepath = os.path.join('ham', self.name + '.py')
|
|
|
|
self.loader = self.machinery.SourceFileLoader(self.name, self.filepath)
|
|
|
|
|
2012-05-11 15:48:41 -03:00
|
|
|
def test_load_module_API(self):
|
2013-11-08 15:25:37 -04:00
|
|
|
class Tester(self.abc.FileLoader):
|
2013-05-31 19:56:47 -03:00
|
|
|
def get_source(self, _): return 'attr = 42'
|
|
|
|
def is_package(self, _): return False
|
2012-05-11 15:48:41 -03:00
|
|
|
|
2013-05-31 19:56:47 -03:00
|
|
|
loader = Tester('blah', 'blah.py')
|
|
|
|
self.addCleanup(unload, 'blah')
|
2014-01-06 23:49:04 -04:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter('ignore', DeprecationWarning)
|
|
|
|
module = loader.load_module() # Should not raise an exception.
|
2012-05-11 15:48:41 -03:00
|
|
|
|
|
|
|
def test_get_filename_API(self):
|
|
|
|
# If fullname is not set then assume self.path is desired.
|
2013-11-08 15:25:37 -04:00
|
|
|
class Tester(self.abc.FileLoader):
|
2012-05-11 15:48:41 -03:00
|
|
|
def get_code(self, _): pass
|
|
|
|
def get_source(self, _): pass
|
|
|
|
def is_package(self, _): pass
|
2012-07-31 18:52:32 -03:00
|
|
|
def module_repr(self, _): pass
|
2012-05-11 15:48:41 -03:00
|
|
|
|
|
|
|
path = 'some_path'
|
|
|
|
name = 'some_name'
|
|
|
|
loader = Tester(name, path)
|
|
|
|
self.assertEqual(path, loader.get_filename(name))
|
|
|
|
self.assertEqual(path, loader.get_filename())
|
|
|
|
self.assertEqual(path, loader.get_filename(None))
|
|
|
|
with self.assertRaises(ImportError):
|
|
|
|
loader.get_filename(name + 'XXX')
|
|
|
|
|
2014-01-04 18:06:49 -04:00
|
|
|
def test_equality(self):
|
|
|
|
other = self.machinery.SourceFileLoader(self.name, self.filepath)
|
|
|
|
self.assertEqual(self.loader, other)
|
|
|
|
|
|
|
|
def test_inequality(self):
|
|
|
|
other = self.machinery.SourceFileLoader('_' + self.name, self.filepath)
|
|
|
|
self.assertNotEqual(self.loader, other)
|
|
|
|
|
2009-01-17 20:24:28 -04:00
|
|
|
# [basic]
|
2009-01-31 22:05:11 -04:00
|
|
|
def test_module(self):
|
2014-05-09 15:32:57 -03:00
|
|
|
with util.create_modules('_temp') as mapping:
|
2013-11-08 15:25:37 -04:00
|
|
|
loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
|
2014-01-06 23:49:04 -04:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter('ignore', DeprecationWarning)
|
|
|
|
module = loader.load_module('_temp')
|
2012-06-27 16:26:26 -03:00
|
|
|
self.assertIn('_temp', sys.modules)
|
2009-01-31 22:05:11 -04:00
|
|
|
check = {'__name__': '_temp', '__file__': mapping['_temp'],
|
2009-02-06 21:52:25 -04:00
|
|
|
'__package__': ''}
|
2009-01-31 22:05:11 -04:00
|
|
|
for attr, value in check.items():
|
|
|
|
self.assertEqual(getattr(module, attr), value)
|
|
|
|
|
|
|
|
def test_package(self):
|
2014-05-09 15:32:57 -03:00
|
|
|
with util.create_modules('_pkg.__init__') as mapping:
|
2013-11-08 15:25:37 -04:00
|
|
|
loader = self.machinery.SourceFileLoader('_pkg',
|
2010-07-03 18:48:25 -03:00
|
|
|
mapping['_pkg.__init__'])
|
2014-01-06 23:49:04 -04:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter('ignore', DeprecationWarning)
|
|
|
|
module = loader.load_module('_pkg')
|
2012-06-27 16:26:26 -03:00
|
|
|
self.assertIn('_pkg', sys.modules)
|
2009-01-31 22:05:11 -04:00
|
|
|
check = {'__name__': '_pkg', '__file__': mapping['_pkg.__init__'],
|
|
|
|
'__path__': [os.path.dirname(mapping['_pkg.__init__'])],
|
|
|
|
'__package__': '_pkg'}
|
|
|
|
for attr, value in check.items():
|
|
|
|
self.assertEqual(getattr(module, attr), value)
|
|
|
|
|
|
|
|
|
|
|
|
def test_lacking_parent(self):
|
2014-05-09 15:32:57 -03:00
|
|
|
with util.create_modules('_pkg.__init__', '_pkg.mod')as mapping:
|
2013-11-08 15:25:37 -04:00
|
|
|
loader = self.machinery.SourceFileLoader('_pkg.mod',
|
2010-07-03 18:48:25 -03:00
|
|
|
mapping['_pkg.mod'])
|
2014-01-06 23:49:04 -04:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter('ignore', DeprecationWarning)
|
|
|
|
module = loader.load_module('_pkg.mod')
|
2012-06-27 16:26:26 -03:00
|
|
|
self.assertIn('_pkg.mod', sys.modules)
|
2009-01-31 22:05:11 -04:00
|
|
|
check = {'__name__': '_pkg.mod', '__file__': mapping['_pkg.mod'],
|
|
|
|
'__package__': '_pkg'}
|
|
|
|
for attr, value in check.items():
|
|
|
|
self.assertEqual(getattr(module, attr), value)
|
|
|
|
|
|
|
|
def fake_mtime(self, fxn):
|
|
|
|
"""Fake mtime to always be higher than expected."""
|
|
|
|
return lambda name: fxn(name) + 1
|
|
|
|
|
|
|
|
def test_module_reuse(self):
|
2014-05-09 15:32:57 -03:00
|
|
|
with util.create_modules('_temp') as mapping:
|
2013-11-08 15:25:37 -04:00
|
|
|
loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
|
2014-01-06 23:49:04 -04:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter('ignore', DeprecationWarning)
|
|
|
|
module = loader.load_module('_temp')
|
2009-01-31 22:05:11 -04:00
|
|
|
module_id = id(module)
|
|
|
|
module_dict_id = id(module.__dict__)
|
|
|
|
with open(mapping['_temp'], 'w') as file:
|
|
|
|
file.write("testing_var = 42\n")
|
2014-01-06 23:49:04 -04:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter('ignore', DeprecationWarning)
|
|
|
|
module = loader.load_module('_temp')
|
2012-06-27 16:26:26 -03:00
|
|
|
self.assertIn('testing_var', module.__dict__,
|
2009-01-31 22:05:11 -04:00
|
|
|
"'testing_var' not in "
|
|
|
|
"{0}".format(list(module.__dict__.keys())))
|
|
|
|
self.assertEqual(module, sys.modules['_temp'])
|
|
|
|
self.assertEqual(id(module), module_id)
|
|
|
|
self.assertEqual(id(module.__dict__), module_dict_id)
|
|
|
|
|
|
|
|
def test_state_after_failure(self):
|
|
|
|
# A failed reload should leave the original module intact.
|
|
|
|
attributes = ('__file__', '__path__', '__package__')
|
|
|
|
value = '<test>'
|
|
|
|
name = '_temp'
|
2014-05-09 15:32:57 -03:00
|
|
|
with util.create_modules(name) as mapping:
|
2013-06-15 19:39:21 -03:00
|
|
|
orig_module = types.ModuleType(name)
|
2009-01-31 22:05:11 -04:00
|
|
|
for attr in attributes:
|
|
|
|
setattr(orig_module, attr, value)
|
|
|
|
with open(mapping[name], 'w') as file:
|
|
|
|
file.write('+++ bad syntax +++')
|
2013-11-08 15:25:37 -04:00
|
|
|
loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
|
2013-12-06 15:25:01 -04:00
|
|
|
with self.assertRaises(SyntaxError):
|
|
|
|
loader.exec_module(orig_module)
|
|
|
|
for attr in attributes:
|
|
|
|
self.assertEqual(getattr(orig_module, attr), value)
|
2009-08-27 20:49:21 -03:00
|
|
|
with self.assertRaises(SyntaxError):
|
2014-01-06 23:49:04 -04:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter('ignore', DeprecationWarning)
|
|
|
|
loader.load_module(name)
|
2009-01-31 22:05:11 -04:00
|
|
|
for attr in attributes:
|
|
|
|
self.assertEqual(getattr(orig_module, attr), value)
|
2009-01-17 20:24:28 -04:00
|
|
|
|
|
|
|
# [syntax error]
|
|
|
|
def test_bad_syntax(self):
|
2014-05-09 15:32:57 -03:00
|
|
|
with util.create_modules('_temp') as mapping:
|
2009-01-17 20:24:28 -04:00
|
|
|
with open(mapping['_temp'], 'w') as file:
|
|
|
|
file.write('=')
|
2013-11-08 15:25:37 -04:00
|
|
|
loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
|
2009-08-27 20:49:21 -03:00
|
|
|
with self.assertRaises(SyntaxError):
|
2014-01-06 23:49:04 -04:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter('ignore', DeprecationWarning)
|
|
|
|
loader.load_module('_temp')
|
2012-06-27 16:26:26 -03:00
|
|
|
self.assertNotIn('_temp', sys.modules)
|
2009-01-17 20:24:28 -04:00
|
|
|
|
2010-07-03 19:18:47 -03:00
|
|
|
def test_file_from_empty_string_dir(self):
|
|
|
|
# Loading a module found from an empty string entry on sys.path should
|
|
|
|
# not only work, but keep all attributes relative.
|
2010-08-22 19:11:06 -03:00
|
|
|
file_path = '_temp.py'
|
|
|
|
with open(file_path, 'w') as file:
|
2010-07-03 19:18:47 -03:00
|
|
|
file.write("# test file for importlib")
|
|
|
|
try:
|
|
|
|
with util.uncache('_temp'):
|
2013-11-08 15:25:37 -04:00
|
|
|
loader = self.machinery.SourceFileLoader('_temp', file_path)
|
2014-01-06 23:49:04 -04:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter('ignore', DeprecationWarning)
|
|
|
|
mod = loader.load_module('_temp')
|
2010-08-22 19:11:06 -03:00
|
|
|
self.assertEqual(file_path, mod.__file__)
|
2013-11-08 15:25:37 -04:00
|
|
|
self.assertEqual(self.util.cache_from_source(file_path),
|
2010-07-03 19:18:47 -03:00
|
|
|
mod.__cached__)
|
|
|
|
finally:
|
2010-08-22 19:11:06 -03:00
|
|
|
os.unlink(file_path)
|
2013-11-08 15:25:37 -04:00
|
|
|
pycache = os.path.dirname(self.util.cache_from_source(file_path))
|
2013-03-16 14:48:51 -03:00
|
|
|
if os.path.exists(pycache):
|
|
|
|
shutil.rmtree(pycache)
|
2010-07-03 19:18:47 -03:00
|
|
|
|
2014-05-09 15:32:57 -03:00
|
|
|
@util.writes_bytecode_files
|
2012-01-24 12:44:06 -04:00
|
|
|
def test_timestamp_overflow(self):
|
|
|
|
# When a modification timestamp is larger than 2**32, it should be
|
|
|
|
# truncated rather than raise an OverflowError.
|
2014-05-09 15:32:57 -03:00
|
|
|
with util.create_modules('_temp') as mapping:
|
2012-01-24 12:44:06 -04:00
|
|
|
source = mapping['_temp']
|
2013-11-08 15:25:37 -04:00
|
|
|
compiled = self.util.cache_from_source(source)
|
2012-01-24 12:44:06 -04:00
|
|
|
with open(source, 'w') as f:
|
|
|
|
f.write("x = 5")
|
2012-01-24 22:00:57 -04:00
|
|
|
try:
|
2012-01-25 13:01:45 -04:00
|
|
|
os.utime(source, (2 ** 33 - 5, 2 ** 33 - 5))
|
2012-01-24 22:00:57 -04:00
|
|
|
except OverflowError:
|
|
|
|
self.skipTest("cannot set modification time to large integer")
|
|
|
|
except OSError as e:
|
|
|
|
if e.errno != getattr(errno, 'EOVERFLOW', None):
|
|
|
|
raise
|
|
|
|
self.skipTest("cannot set modification time to large integer ({})".format(e))
|
2013-11-08 15:25:37 -04:00
|
|
|
loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
|
2013-12-06 15:25:01 -04:00
|
|
|
# PEP 451
|
|
|
|
module = types.ModuleType('_temp')
|
|
|
|
module.__spec__ = self.util.spec_from_loader('_temp', loader)
|
|
|
|
loader.exec_module(module)
|
|
|
|
self.assertEqual(module.x, 5)
|
|
|
|
self.assertTrue(os.path.exists(compiled))
|
|
|
|
os.unlink(compiled)
|
|
|
|
# PEP 302
|
2014-01-06 23:49:04 -04:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter('ignore', DeprecationWarning)
|
2016-05-10 19:21:03 -03:00
|
|
|
mod = loader.load_module('_temp')
|
2012-01-24 12:44:06 -04:00
|
|
|
# Sanity checks.
|
|
|
|
self.assertEqual(mod.__cached__, compiled)
|
|
|
|
self.assertEqual(mod.x, 5)
|
|
|
|
# The pyc file was created.
|
2013-12-06 15:25:01 -04:00
|
|
|
self.assertTrue(os.path.exists(compiled))
|
2012-01-24 12:44:06 -04:00
|
|
|
|
2013-10-25 13:33:59 -03:00
|
|
|
def test_unloadable(self):
|
2013-11-08 15:25:37 -04:00
|
|
|
loader = self.machinery.SourceFileLoader('good name', {})
|
2013-12-06 15:25:01 -04:00
|
|
|
module = types.ModuleType('bad name')
|
|
|
|
module.__spec__ = self.machinery.ModuleSpec('bad name', loader)
|
|
|
|
with self.assertRaises(ImportError):
|
|
|
|
loader.exec_module(module)
|
2013-10-25 13:33:59 -03:00
|
|
|
with self.assertRaises(ImportError):
|
2014-01-06 23:49:04 -04:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter('ignore', DeprecationWarning)
|
|
|
|
loader.load_module('bad name')
|
2013-10-25 13:33:59 -03:00
|
|
|
|
2017-12-09 14:26:52 -04:00
|
|
|
@util.writes_bytecode_files
|
|
|
|
def test_checked_hash_based_pyc(self):
|
|
|
|
with util.create_modules('_temp') as mapping:
|
|
|
|
source = mapping['_temp']
|
|
|
|
pyc = self.util.cache_from_source(source)
|
|
|
|
with open(source, 'wb') as fp:
|
|
|
|
fp.write(b'state = "old"')
|
|
|
|
os.utime(source, (50, 50))
|
|
|
|
py_compile.compile(
|
|
|
|
source,
|
|
|
|
invalidation_mode=py_compile.PycInvalidationMode.CHECKED_HASH,
|
|
|
|
)
|
|
|
|
loader = self.machinery.SourceFileLoader('_temp', source)
|
|
|
|
mod = types.ModuleType('_temp')
|
|
|
|
mod.__spec__ = self.util.spec_from_loader('_temp', loader)
|
|
|
|
loader.exec_module(mod)
|
|
|
|
self.assertEqual(mod.state, 'old')
|
|
|
|
# Write a new source with the same mtime and size as before.
|
|
|
|
with open(source, 'wb') as fp:
|
|
|
|
fp.write(b'state = "new"')
|
|
|
|
os.utime(source, (50, 50))
|
|
|
|
loader.exec_module(mod)
|
|
|
|
self.assertEqual(mod.state, 'new')
|
|
|
|
with open(pyc, 'rb') as fp:
|
|
|
|
data = fp.read()
|
|
|
|
self.assertEqual(int.from_bytes(data[4:8], 'little'), 0b11)
|
|
|
|
self.assertEqual(
|
|
|
|
self.util.source_hash(b'state = "new"'),
|
|
|
|
data[8:16],
|
|
|
|
)
|
|
|
|
|
|
|
|
@util.writes_bytecode_files
|
2017-12-14 07:04:53 -04:00
|
|
|
def test_overridden_checked_hash_based_pyc(self):
|
2017-12-09 14:26:52 -04:00
|
|
|
with util.create_modules('_temp') as mapping, \
|
|
|
|
unittest.mock.patch('_imp.check_hash_based_pycs', 'never'):
|
|
|
|
source = mapping['_temp']
|
|
|
|
pyc = self.util.cache_from_source(source)
|
|
|
|
with open(source, 'wb') as fp:
|
|
|
|
fp.write(b'state = "old"')
|
|
|
|
os.utime(source, (50, 50))
|
|
|
|
py_compile.compile(
|
|
|
|
source,
|
|
|
|
invalidation_mode=py_compile.PycInvalidationMode.CHECKED_HASH,
|
|
|
|
)
|
|
|
|
loader = self.machinery.SourceFileLoader('_temp', source)
|
|
|
|
mod = types.ModuleType('_temp')
|
|
|
|
mod.__spec__ = self.util.spec_from_loader('_temp', loader)
|
|
|
|
loader.exec_module(mod)
|
|
|
|
self.assertEqual(mod.state, 'old')
|
|
|
|
# Write a new source with the same mtime and size as before.
|
|
|
|
with open(source, 'wb') as fp:
|
|
|
|
fp.write(b'state = "new"')
|
|
|
|
os.utime(source, (50, 50))
|
|
|
|
loader.exec_module(mod)
|
|
|
|
self.assertEqual(mod.state, 'old')
|
|
|
|
|
|
|
|
@util.writes_bytecode_files
|
|
|
|
def test_unchecked_hash_based_pyc(self):
|
|
|
|
with util.create_modules('_temp') as mapping:
|
|
|
|
source = mapping['_temp']
|
|
|
|
pyc = self.util.cache_from_source(source)
|
|
|
|
with open(source, 'wb') as fp:
|
|
|
|
fp.write(b'state = "old"')
|
|
|
|
os.utime(source, (50, 50))
|
|
|
|
py_compile.compile(
|
|
|
|
source,
|
|
|
|
invalidation_mode=py_compile.PycInvalidationMode.UNCHECKED_HASH,
|
|
|
|
)
|
|
|
|
loader = self.machinery.SourceFileLoader('_temp', source)
|
|
|
|
mod = types.ModuleType('_temp')
|
|
|
|
mod.__spec__ = self.util.spec_from_loader('_temp', loader)
|
|
|
|
loader.exec_module(mod)
|
|
|
|
self.assertEqual(mod.state, 'old')
|
|
|
|
# Update the source file, which should be ignored.
|
|
|
|
with open(source, 'wb') as fp:
|
|
|
|
fp.write(b'state = "new"')
|
|
|
|
loader.exec_module(mod)
|
|
|
|
self.assertEqual(mod.state, 'old')
|
|
|
|
with open(pyc, 'rb') as fp:
|
|
|
|
data = fp.read()
|
|
|
|
self.assertEqual(int.from_bytes(data[4:8], 'little'), 0b1)
|
|
|
|
self.assertEqual(
|
|
|
|
self.util.source_hash(b'state = "old"'),
|
|
|
|
data[8:16],
|
|
|
|
)
|
|
|
|
|
|
|
|
@util.writes_bytecode_files
|
2019-08-30 17:21:19 -03:00
|
|
|
def test_overridden_unchecked_hash_based_pyc(self):
|
2017-12-09 14:26:52 -04:00
|
|
|
with util.create_modules('_temp') as mapping, \
|
|
|
|
unittest.mock.patch('_imp.check_hash_based_pycs', 'always'):
|
|
|
|
source = mapping['_temp']
|
|
|
|
pyc = self.util.cache_from_source(source)
|
|
|
|
with open(source, 'wb') as fp:
|
|
|
|
fp.write(b'state = "old"')
|
|
|
|
os.utime(source, (50, 50))
|
|
|
|
py_compile.compile(
|
|
|
|
source,
|
|
|
|
invalidation_mode=py_compile.PycInvalidationMode.UNCHECKED_HASH,
|
|
|
|
)
|
|
|
|
loader = self.machinery.SourceFileLoader('_temp', source)
|
|
|
|
mod = types.ModuleType('_temp')
|
|
|
|
mod.__spec__ = self.util.spec_from_loader('_temp', loader)
|
|
|
|
loader.exec_module(mod)
|
|
|
|
self.assertEqual(mod.state, 'old')
|
|
|
|
# Update the source file, which should be ignored.
|
|
|
|
with open(source, 'wb') as fp:
|
|
|
|
fp.write(b'state = "new"')
|
|
|
|
loader.exec_module(mod)
|
|
|
|
self.assertEqual(mod.state, 'new')
|
|
|
|
with open(pyc, 'rb') as fp:
|
|
|
|
data = fp.read()
|
|
|
|
self.assertEqual(int.from_bytes(data[4:8], 'little'), 0b1)
|
|
|
|
self.assertEqual(
|
|
|
|
self.util.source_hash(b'state = "new"'),
|
|
|
|
data[8:16],
|
|
|
|
)
|
|
|
|
|
2014-05-16 14:40:40 -03:00
|
|
|
|
|
|
|
(Frozen_SimpleTest,
|
|
|
|
Source_SimpleTest
|
|
|
|
) = util.test_both(SimpleTest, importlib=importlib, machinery=machinery,
|
|
|
|
abc=importlib_abc, util=importlib_util)
|
2013-11-08 15:25:37 -04:00
|
|
|
|
2009-01-17 20:24:28 -04:00
|
|
|
|
2018-10-10 13:43:14 -03:00
|
|
|
class SourceDateEpochTestMeta(SourceDateEpochTestMeta,
|
|
|
|
type(Source_SimpleTest)):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class SourceDateEpoch_SimpleTest(Source_SimpleTest,
|
|
|
|
metaclass=SourceDateEpochTestMeta,
|
|
|
|
source_date_epoch=True):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2013-11-08 15:25:37 -04:00
|
|
|
class BadBytecodeTest:
|
2009-01-17 20:24:28 -04:00
|
|
|
|
|
|
|
def import_(self, file, module_name):
|
2016-05-10 19:21:03 -03:00
|
|
|
raise NotImplementedError
|
2009-01-17 20:24:28 -04:00
|
|
|
|
2017-12-09 14:26:52 -04:00
|
|
|
def manipulate_bytecode(self,
|
|
|
|
name, mapping, manipulator, *,
|
|
|
|
del_source=False,
|
|
|
|
invalidation_mode=py_compile.PycInvalidationMode.TIMESTAMP):
|
2010-02-19 12:01:06 -04:00
|
|
|
"""Manipulate the bytecode of a module by passing it into a callable
|
|
|
|
that returns what to use as the new bytecode."""
|
|
|
|
try:
|
|
|
|
del sys.modules['_temp']
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2017-12-09 14:26:52 -04:00
|
|
|
py_compile.compile(mapping[name], invalidation_mode=invalidation_mode)
|
2010-07-03 18:48:25 -03:00
|
|
|
if not del_source:
|
2013-11-08 15:25:37 -04:00
|
|
|
bytecode_path = self.util.cache_from_source(mapping[name])
|
2010-07-03 18:48:25 -03:00
|
|
|
else:
|
2010-02-19 12:01:06 -04:00
|
|
|
os.unlink(mapping[name])
|
2010-07-03 18:48:25 -03:00
|
|
|
bytecode_path = make_legacy_pyc(mapping[name])
|
|
|
|
if manipulator:
|
|
|
|
with open(bytecode_path, 'rb') as file:
|
|
|
|
bc = file.read()
|
|
|
|
new_bc = manipulator(bc)
|
|
|
|
with open(bytecode_path, 'wb') as file:
|
|
|
|
if new_bc is not None:
|
|
|
|
file.write(new_bc)
|
2010-02-19 12:01:06 -04:00
|
|
|
return bytecode_path
|
|
|
|
|
2010-07-03 18:48:25 -03:00
|
|
|
def _test_empty_file(self, test, *, del_source=False):
|
2014-05-09 15:32:57 -03:00
|
|
|
with util.create_modules('_temp') as mapping:
|
2010-02-19 12:01:06 -04:00
|
|
|
bc_path = self.manipulate_bytecode('_temp', mapping,
|
2010-07-03 18:48:25 -03:00
|
|
|
lambda bc: b'',
|
|
|
|
del_source=del_source)
|
|
|
|
test('_temp', mapping, bc_path)
|
2010-02-19 12:01:06 -04:00
|
|
|
|
2014-05-09 15:32:57 -03:00
|
|
|
@util.writes_bytecode_files
|
2010-07-03 18:48:25 -03:00
|
|
|
def _test_partial_magic(self, test, *, del_source=False):
|
2010-02-19 12:01:06 -04:00
|
|
|
# When their are less than 4 bytes to a .pyc, regenerate it if
|
|
|
|
# possible, else raise ImportError.
|
2014-05-09 15:32:57 -03:00
|
|
|
with util.create_modules('_temp') as mapping:
|
2010-02-19 12:01:06 -04:00
|
|
|
bc_path = self.manipulate_bytecode('_temp', mapping,
|
2010-07-03 18:48:25 -03:00
|
|
|
lambda bc: bc[:3],
|
|
|
|
del_source=del_source)
|
|
|
|
test('_temp', mapping, bc_path)
|
|
|
|
|
|
|
|
def _test_magic_only(self, test, *, del_source=False):
|
2014-05-09 15:32:57 -03:00
|
|
|
with util.create_modules('_temp') as mapping:
|
2010-07-03 18:48:25 -03:00
|
|
|
bc_path = self.manipulate_bytecode('_temp', mapping,
|
|
|
|
lambda bc: bc[:4],
|
|
|
|
del_source=del_source)
|
|
|
|
test('_temp', mapping, bc_path)
|
|
|
|
|
2017-12-09 14:26:52 -04:00
|
|
|
def _test_partial_flags(self, test, *, del_source=False):
|
|
|
|
with util.create_modules('_temp') as mapping:
|
|
|
|
bc_path = self.manipulate_bytecode('_temp', mapping,
|
|
|
|
lambda bc: bc[:7],
|
|
|
|
del_source=del_source)
|
|
|
|
test('_temp', mapping, bc_path)
|
|
|
|
|
|
|
|
def _test_partial_hash(self, test, *, del_source=False):
|
|
|
|
with util.create_modules('_temp') as mapping:
|
|
|
|
bc_path = self.manipulate_bytecode(
|
|
|
|
'_temp',
|
|
|
|
mapping,
|
|
|
|
lambda bc: bc[:13],
|
|
|
|
del_source=del_source,
|
|
|
|
invalidation_mode=py_compile.PycInvalidationMode.CHECKED_HASH,
|
|
|
|
)
|
|
|
|
test('_temp', mapping, bc_path)
|
|
|
|
with util.create_modules('_temp') as mapping:
|
|
|
|
bc_path = self.manipulate_bytecode(
|
|
|
|
'_temp',
|
|
|
|
mapping,
|
|
|
|
lambda bc: bc[:13],
|
|
|
|
del_source=del_source,
|
|
|
|
invalidation_mode=py_compile.PycInvalidationMode.UNCHECKED_HASH,
|
|
|
|
)
|
|
|
|
test('_temp', mapping, bc_path)
|
|
|
|
|
2010-07-03 18:48:25 -03:00
|
|
|
def _test_partial_timestamp(self, test, *, del_source=False):
|
2014-05-09 15:32:57 -03:00
|
|
|
with util.create_modules('_temp') as mapping:
|
2010-07-03 18:48:25 -03:00
|
|
|
bc_path = self.manipulate_bytecode('_temp', mapping,
|
2017-12-09 14:26:52 -04:00
|
|
|
lambda bc: bc[:11],
|
2010-07-03 18:48:25 -03:00
|
|
|
del_source=del_source)
|
|
|
|
test('_temp', mapping, bc_path)
|
|
|
|
|
2012-01-13 13:52:16 -04:00
|
|
|
def _test_partial_size(self, test, *, del_source=False):
|
2014-05-09 15:32:57 -03:00
|
|
|
with util.create_modules('_temp') as mapping:
|
2012-01-13 13:52:16 -04:00
|
|
|
bc_path = self.manipulate_bytecode('_temp', mapping,
|
2017-12-09 14:26:52 -04:00
|
|
|
lambda bc: bc[:15],
|
2012-01-13 13:52:16 -04:00
|
|
|
del_source=del_source)
|
|
|
|
test('_temp', mapping, bc_path)
|
|
|
|
|
2010-07-03 18:48:25 -03:00
|
|
|
def _test_no_marshal(self, *, del_source=False):
|
2014-05-09 15:32:57 -03:00
|
|
|
with util.create_modules('_temp') as mapping:
|
2010-07-03 18:48:25 -03:00
|
|
|
bc_path = self.manipulate_bytecode('_temp', mapping,
|
2017-12-09 14:26:52 -04:00
|
|
|
lambda bc: bc[:16],
|
2010-07-03 18:48:25 -03:00
|
|
|
del_source=del_source)
|
|
|
|
file_path = mapping['_temp'] if not del_source else bc_path
|
|
|
|
with self.assertRaises(EOFError):
|
|
|
|
self.import_(file_path, '_temp')
|
|
|
|
|
|
|
|
def _test_non_code_marshal(self, *, del_source=False):
|
2014-05-09 15:32:57 -03:00
|
|
|
with util.create_modules('_temp') as mapping:
|
2010-07-03 18:48:25 -03:00
|
|
|
bytecode_path = self.manipulate_bytecode('_temp', mapping,
|
2017-12-09 14:26:52 -04:00
|
|
|
lambda bc: bc[:16] + marshal.dumps(b'abcd'),
|
2010-07-03 18:48:25 -03:00
|
|
|
del_source=del_source)
|
|
|
|
file_path = mapping['_temp'] if not del_source else bytecode_path
|
2012-04-12 22:09:01 -03:00
|
|
|
with self.assertRaises(ImportError) as cm:
|
2010-07-03 18:48:25 -03:00
|
|
|
self.import_(file_path, '_temp')
|
2012-04-12 22:09:01 -03:00
|
|
|
self.assertEqual(cm.exception.name, '_temp')
|
|
|
|
self.assertEqual(cm.exception.path, bytecode_path)
|
2010-07-03 18:48:25 -03:00
|
|
|
|
|
|
|
def _test_bad_marshal(self, *, del_source=False):
|
2014-05-09 15:32:57 -03:00
|
|
|
with util.create_modules('_temp') as mapping:
|
2010-07-03 18:48:25 -03:00
|
|
|
bytecode_path = self.manipulate_bytecode('_temp', mapping,
|
2017-12-09 14:26:52 -04:00
|
|
|
lambda bc: bc[:16] + b'<test>',
|
2010-07-03 18:48:25 -03:00
|
|
|
del_source=del_source)
|
|
|
|
file_path = mapping['_temp'] if not del_source else bytecode_path
|
2011-07-02 12:42:47 -03:00
|
|
|
with self.assertRaises(EOFError):
|
2010-07-03 18:48:25 -03:00
|
|
|
self.import_(file_path, '_temp')
|
|
|
|
|
|
|
|
def _test_bad_magic(self, test, *, del_source=False):
|
2014-05-09 15:32:57 -03:00
|
|
|
with util.create_modules('_temp') as mapping:
|
2010-07-03 18:48:25 -03:00
|
|
|
bc_path = self.manipulate_bytecode('_temp', mapping,
|
|
|
|
lambda bc: b'\x00\x00\x00\x00' + bc[4:])
|
|
|
|
test('_temp', mapping, bc_path)
|
|
|
|
|
2014-05-16 14:40:40 -03:00
|
|
|
|
2013-12-06 15:25:01 -04:00
|
|
|
class BadBytecodeTestPEP451(BadBytecodeTest):
|
|
|
|
|
|
|
|
def import_(self, file, module_name):
|
|
|
|
loader = self.loader(module_name, file)
|
|
|
|
module = types.ModuleType(module_name)
|
|
|
|
module.__spec__ = self.util.spec_from_loader(module_name, loader)
|
|
|
|
loader.exec_module(module)
|
|
|
|
|
2014-05-16 14:40:40 -03:00
|
|
|
|
2013-12-06 15:25:01 -04:00
|
|
|
class BadBytecodeTestPEP302(BadBytecodeTest):
|
|
|
|
|
|
|
|
def import_(self, file, module_name):
|
|
|
|
loader = self.loader(module_name, file)
|
2014-01-06 23:49:04 -04:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter('ignore', DeprecationWarning)
|
|
|
|
module = loader.load_module(module_name)
|
2013-12-06 15:25:01 -04:00
|
|
|
self.assertIn(module_name, sys.modules)
|
|
|
|
|
2010-07-03 18:48:25 -03:00
|
|
|
|
2013-12-06 15:25:01 -04:00
|
|
|
class SourceLoaderBadBytecodeTest:
|
2010-07-03 18:48:25 -03:00
|
|
|
|
2013-11-08 15:25:37 -04:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
cls.loader = cls.machinery.SourceFileLoader
|
2010-07-03 18:48:25 -03:00
|
|
|
|
2014-05-09 15:32:57 -03:00
|
|
|
@util.writes_bytecode_files
|
2010-07-03 18:48:25 -03:00
|
|
|
def test_empty_file(self):
|
|
|
|
# When a .pyc is empty, regenerate it if possible, else raise
|
|
|
|
# ImportError.
|
|
|
|
def test(name, mapping, bytecode_path):
|
|
|
|
self.import_(mapping[name], name)
|
|
|
|
with open(bytecode_path, 'rb') as file:
|
2017-12-09 14:26:52 -04:00
|
|
|
self.assertGreater(len(file.read()), 16)
|
2010-07-03 18:48:25 -03:00
|
|
|
|
|
|
|
self._test_empty_file(test)
|
|
|
|
|
|
|
|
def test_partial_magic(self):
|
|
|
|
def test(name, mapping, bytecode_path):
|
|
|
|
self.import_(mapping[name], name)
|
|
|
|
with open(bytecode_path, 'rb') as file:
|
2017-12-09 14:26:52 -04:00
|
|
|
self.assertGreater(len(file.read()), 16)
|
2010-07-03 18:48:25 -03:00
|
|
|
|
|
|
|
self._test_partial_magic(test)
|
2010-02-19 12:01:06 -04:00
|
|
|
|
2014-05-09 15:32:57 -03:00
|
|
|
@util.writes_bytecode_files
|
2010-02-19 12:01:06 -04:00
|
|
|
def test_magic_only(self):
|
|
|
|
# When there is only the magic number, regenerate the .pyc if possible,
|
|
|
|
# else raise EOFError.
|
2010-07-03 18:48:25 -03:00
|
|
|
def test(name, mapping, bytecode_path):
|
|
|
|
self.import_(mapping[name], name)
|
|
|
|
with open(bytecode_path, 'rb') as file:
|
2017-12-09 14:26:52 -04:00
|
|
|
self.assertGreater(len(file.read()), 16)
|
2010-07-03 18:48:25 -03:00
|
|
|
|
2011-12-30 16:25:15 -04:00
|
|
|
self._test_magic_only(test)
|
|
|
|
|
2014-05-09 15:32:57 -03:00
|
|
|
@util.writes_bytecode_files
|
2010-07-03 18:48:25 -03:00
|
|
|
def test_bad_magic(self):
|
|
|
|
# When the magic number is different, the bytecode should be
|
|
|
|
# regenerated.
|
|
|
|
def test(name, mapping, bytecode_path):
|
|
|
|
self.import_(mapping[name], name)
|
|
|
|
with open(bytecode_path, 'rb') as bytecode_file:
|
2013-06-15 19:39:21 -03:00
|
|
|
self.assertEqual(bytecode_file.read(4),
|
2013-11-08 15:25:37 -04:00
|
|
|
self.util.MAGIC_NUMBER)
|
2010-07-03 18:48:25 -03:00
|
|
|
|
|
|
|
self._test_bad_magic(test)
|
2010-02-19 12:01:06 -04:00
|
|
|
|
2014-05-09 15:32:57 -03:00
|
|
|
@util.writes_bytecode_files
|
2010-02-19 12:01:06 -04:00
|
|
|
def test_partial_timestamp(self):
|
|
|
|
# When the timestamp is partial, regenerate the .pyc, else
|
|
|
|
# raise EOFError.
|
2010-07-03 18:48:25 -03:00
|
|
|
def test(name, mapping, bc_path):
|
|
|
|
self.import_(mapping[name], name)
|
2010-02-19 12:01:06 -04:00
|
|
|
with open(bc_path, 'rb') as file:
|
2017-12-09 14:26:52 -04:00
|
|
|
self.assertGreater(len(file.read()), 16)
|
2010-02-19 12:01:06 -04:00
|
|
|
|
2011-12-30 16:25:15 -04:00
|
|
|
self._test_partial_timestamp(test)
|
|
|
|
|
2017-12-09 14:26:52 -04:00
|
|
|
@util.writes_bytecode_files
|
|
|
|
def test_partial_flags(self):
|
|
|
|
# When the flags is partial, regenerate the .pyc, else raise EOFError.
|
|
|
|
def test(name, mapping, bc_path):
|
|
|
|
self.import_(mapping[name], name)
|
|
|
|
with open(bc_path, 'rb') as file:
|
|
|
|
self.assertGreater(len(file.read()), 16)
|
|
|
|
|
|
|
|
self._test_partial_flags(test)
|
|
|
|
|
|
|
|
@util.writes_bytecode_files
|
|
|
|
def test_partial_hash(self):
|
|
|
|
# When the hash is partial, regenerate the .pyc, else raise EOFError.
|
|
|
|
def test(name, mapping, bc_path):
|
|
|
|
self.import_(mapping[name], name)
|
|
|
|
with open(bc_path, 'rb') as file:
|
|
|
|
self.assertGreater(len(file.read()), 16)
|
|
|
|
|
|
|
|
self._test_partial_hash(test)
|
|
|
|
|
2014-05-09 15:32:57 -03:00
|
|
|
@util.writes_bytecode_files
|
2012-01-13 13:52:16 -04:00
|
|
|
def test_partial_size(self):
|
|
|
|
# When the size is partial, regenerate the .pyc, else
|
|
|
|
# raise EOFError.
|
|
|
|
def test(name, mapping, bc_path):
|
|
|
|
self.import_(mapping[name], name)
|
|
|
|
with open(bc_path, 'rb') as file:
|
2017-12-09 14:26:52 -04:00
|
|
|
self.assertGreater(len(file.read()), 16)
|
2012-01-13 13:52:16 -04:00
|
|
|
|
|
|
|
self._test_partial_size(test)
|
|
|
|
|
2014-05-09 15:32:57 -03:00
|
|
|
@util.writes_bytecode_files
|
2010-02-19 12:01:06 -04:00
|
|
|
def test_no_marshal(self):
|
|
|
|
# When there is only the magic number and timestamp, raise EOFError.
|
2010-07-03 18:48:25 -03:00
|
|
|
self._test_no_marshal()
|
2010-02-19 12:01:06 -04:00
|
|
|
|
2014-05-09 15:32:57 -03:00
|
|
|
@util.writes_bytecode_files
|
2010-07-03 18:48:25 -03:00
|
|
|
def test_non_code_marshal(self):
|
|
|
|
self._test_non_code_marshal()
|
|
|
|
# XXX ImportError when sourceless
|
|
|
|
|
|
|
|
# [bad marshal]
|
2014-05-09 15:32:57 -03:00
|
|
|
@util.writes_bytecode_files
|
2010-07-03 18:48:25 -03:00
|
|
|
def test_bad_marshal(self):
|
|
|
|
# Bad marshal data should raise a ValueError.
|
|
|
|
self._test_bad_marshal()
|
2009-01-17 20:24:28 -04:00
|
|
|
|
|
|
|
# [bad timestamp]
|
2014-05-09 15:32:57 -03:00
|
|
|
@util.writes_bytecode_files
|
2018-10-10 13:43:14 -03:00
|
|
|
@without_source_date_epoch
|
2010-07-03 18:48:25 -03:00
|
|
|
def test_old_timestamp(self):
|
2010-02-19 12:01:06 -04:00
|
|
|
# When the timestamp is older than the source, bytecode should be
|
|
|
|
# regenerated.
|
2009-01-17 20:24:28 -04:00
|
|
|
zeros = b'\x00\x00\x00\x00'
|
2014-05-09 15:32:57 -03:00
|
|
|
with util.create_modules('_temp') as mapping:
|
2009-01-17 20:24:28 -04:00
|
|
|
py_compile.compile(mapping['_temp'])
|
2013-11-08 15:25:37 -04:00
|
|
|
bytecode_path = self.util.cache_from_source(mapping['_temp'])
|
2009-01-17 20:24:28 -04:00
|
|
|
with open(bytecode_path, 'r+b') as bytecode_file:
|
2017-12-09 14:26:52 -04:00
|
|
|
bytecode_file.seek(8)
|
2009-01-17 20:24:28 -04:00
|
|
|
bytecode_file.write(zeros)
|
|
|
|
self.import_(mapping['_temp'], '_temp')
|
|
|
|
source_mtime = os.path.getmtime(mapping['_temp'])
|
2018-09-18 16:22:29 -03:00
|
|
|
source_timestamp = self.importlib._pack_uint32(source_mtime)
|
2009-01-17 20:24:28 -04:00
|
|
|
with open(bytecode_path, 'rb') as bytecode_file:
|
2017-12-09 14:26:52 -04:00
|
|
|
bytecode_file.seek(8)
|
2009-01-17 20:24:28 -04:00
|
|
|
self.assertEqual(bytecode_file.read(4), source_timestamp)
|
|
|
|
|
2009-11-07 19:55:05 -04:00
|
|
|
# [bytecode read-only]
|
2014-05-09 15:32:57 -03:00
|
|
|
@util.writes_bytecode_files
|
2009-11-07 19:55:05 -04:00
|
|
|
def test_read_only_bytecode(self):
|
2010-02-19 12:01:06 -04:00
|
|
|
# When bytecode is read-only but should be rewritten, fail silently.
|
2014-05-09 15:32:57 -03:00
|
|
|
with util.create_modules('_temp') as mapping:
|
2009-11-07 19:55:05 -04:00
|
|
|
# Create bytecode that will need to be re-created.
|
|
|
|
py_compile.compile(mapping['_temp'])
|
2013-11-08 15:25:37 -04:00
|
|
|
bytecode_path = self.util.cache_from_source(mapping['_temp'])
|
2009-11-07 19:55:05 -04:00
|
|
|
with open(bytecode_path, 'r+b') as bytecode_file:
|
|
|
|
bytecode_file.seek(0)
|
|
|
|
bytecode_file.write(b'\x00\x00\x00\x00')
|
|
|
|
# Make the bytecode read-only.
|
|
|
|
os.chmod(bytecode_path,
|
|
|
|
stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
|
|
|
|
try:
|
2012-12-25 10:47:37 -04:00
|
|
|
# Should not raise OSError!
|
2009-11-07 19:55:05 -04:00
|
|
|
self.import_(mapping['_temp'], '_temp')
|
|
|
|
finally:
|
|
|
|
# Make writable for eventual clean-up.
|
|
|
|
os.chmod(bytecode_path, stat.S_IWUSR)
|
|
|
|
|
2014-05-16 14:40:40 -03:00
|
|
|
|
2013-12-06 15:25:01 -04:00
|
|
|
class SourceLoaderBadBytecodeTestPEP451(
|
|
|
|
SourceLoaderBadBytecodeTest, BadBytecodeTestPEP451):
|
|
|
|
pass
|
|
|
|
|
2014-05-16 14:40:40 -03:00
|
|
|
|
|
|
|
(Frozen_SourceBadBytecodePEP451,
|
|
|
|
Source_SourceBadBytecodePEP451
|
|
|
|
) = util.test_both(SourceLoaderBadBytecodeTestPEP451, importlib=importlib,
|
|
|
|
machinery=machinery, abc=importlib_abc,
|
|
|
|
util=importlib_util)
|
|
|
|
|
2013-11-08 15:25:37 -04:00
|
|
|
|
2013-12-06 15:25:01 -04:00
|
|
|
class SourceLoaderBadBytecodeTestPEP302(
|
|
|
|
SourceLoaderBadBytecodeTest, BadBytecodeTestPEP302):
|
|
|
|
pass
|
|
|
|
|
2014-05-16 14:40:40 -03:00
|
|
|
|
|
|
|
(Frozen_SourceBadBytecodePEP302,
|
|
|
|
Source_SourceBadBytecodePEP302
|
|
|
|
) = util.test_both(SourceLoaderBadBytecodeTestPEP302, importlib=importlib,
|
|
|
|
machinery=machinery, abc=importlib_abc,
|
|
|
|
util=importlib_util)
|
2009-01-17 20:24:28 -04:00
|
|
|
|
2013-12-06 15:25:01 -04:00
|
|
|
|
|
|
|
class SourcelessLoaderBadBytecodeTest:
|
2010-07-03 18:48:25 -03:00
|
|
|
|
2013-11-08 15:25:37 -04:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
cls.loader = cls.machinery.SourcelessFileLoader
|
2010-07-03 18:48:25 -03:00
|
|
|
|
|
|
|
def test_empty_file(self):
|
|
|
|
def test(name, mapping, bytecode_path):
|
2012-04-12 22:09:01 -03:00
|
|
|
with self.assertRaises(ImportError) as cm:
|
2010-07-03 18:48:25 -03:00
|
|
|
self.import_(bytecode_path, name)
|
2012-04-12 22:09:01 -03:00
|
|
|
self.assertEqual(cm.exception.name, name)
|
|
|
|
self.assertEqual(cm.exception.path, bytecode_path)
|
2010-07-03 18:48:25 -03:00
|
|
|
|
|
|
|
self._test_empty_file(test, del_source=True)
|
|
|
|
|
|
|
|
def test_partial_magic(self):
|
|
|
|
def test(name, mapping, bytecode_path):
|
2012-04-12 22:09:01 -03:00
|
|
|
with self.assertRaises(ImportError) as cm:
|
2010-07-03 18:48:25 -03:00
|
|
|
self.import_(bytecode_path, name)
|
2012-04-12 22:09:01 -03:00
|
|
|
self.assertEqual(cm.exception.name, name)
|
|
|
|
self.assertEqual(cm.exception.path, bytecode_path)
|
2010-07-03 18:48:25 -03:00
|
|
|
self._test_partial_magic(test, del_source=True)
|
|
|
|
|
|
|
|
def test_magic_only(self):
|
|
|
|
def test(name, mapping, bytecode_path):
|
|
|
|
with self.assertRaises(EOFError):
|
|
|
|
self.import_(bytecode_path, name)
|
|
|
|
|
|
|
|
self._test_magic_only(test, del_source=True)
|
|
|
|
|
|
|
|
def test_bad_magic(self):
|
|
|
|
def test(name, mapping, bytecode_path):
|
2012-04-12 22:09:01 -03:00
|
|
|
with self.assertRaises(ImportError) as cm:
|
2010-07-03 18:48:25 -03:00
|
|
|
self.import_(bytecode_path, name)
|
2012-04-12 22:09:01 -03:00
|
|
|
self.assertEqual(cm.exception.name, name)
|
|
|
|
self.assertEqual(cm.exception.path, bytecode_path)
|
2010-07-03 18:48:25 -03:00
|
|
|
|
|
|
|
self._test_bad_magic(test, del_source=True)
|
|
|
|
|
|
|
|
def test_partial_timestamp(self):
|
|
|
|
def test(name, mapping, bytecode_path):
|
|
|
|
with self.assertRaises(EOFError):
|
|
|
|
self.import_(bytecode_path, name)
|
|
|
|
|
|
|
|
self._test_partial_timestamp(test, del_source=True)
|
|
|
|
|
2017-12-09 14:26:52 -04:00
|
|
|
def test_partial_flags(self):
|
|
|
|
def test(name, mapping, bytecode_path):
|
|
|
|
with self.assertRaises(EOFError):
|
|
|
|
self.import_(bytecode_path, name)
|
|
|
|
|
|
|
|
self._test_partial_flags(test, del_source=True)
|
|
|
|
|
|
|
|
def test_partial_hash(self):
|
|
|
|
def test(name, mapping, bytecode_path):
|
|
|
|
with self.assertRaises(EOFError):
|
|
|
|
self.import_(bytecode_path, name)
|
|
|
|
|
|
|
|
self._test_partial_hash(test, del_source=True)
|
|
|
|
|
2012-01-13 13:52:16 -04:00
|
|
|
def test_partial_size(self):
|
|
|
|
def test(name, mapping, bytecode_path):
|
|
|
|
with self.assertRaises(EOFError):
|
|
|
|
self.import_(bytecode_path, name)
|
|
|
|
|
|
|
|
self._test_partial_size(test, del_source=True)
|
|
|
|
|
2010-07-03 18:48:25 -03:00
|
|
|
def test_no_marshal(self):
|
|
|
|
self._test_no_marshal(del_source=True)
|
|
|
|
|
|
|
|
def test_non_code_marshal(self):
|
|
|
|
self._test_non_code_marshal(del_source=True)
|
|
|
|
|
2014-05-16 14:40:40 -03:00
|
|
|
|
2013-12-06 15:25:01 -04:00
|
|
|
class SourcelessLoaderBadBytecodeTestPEP451(SourcelessLoaderBadBytecodeTest,
|
|
|
|
BadBytecodeTestPEP451):
|
|
|
|
pass
|
|
|
|
|
2014-05-16 14:40:40 -03:00
|
|
|
|
|
|
|
(Frozen_SourcelessBadBytecodePEP451,
|
|
|
|
Source_SourcelessBadBytecodePEP451
|
|
|
|
) = util.test_both(SourcelessLoaderBadBytecodeTestPEP451, importlib=importlib,
|
|
|
|
machinery=machinery, abc=importlib_abc,
|
|
|
|
util=importlib_util)
|
|
|
|
|
2013-12-06 15:25:01 -04:00
|
|
|
|
|
|
|
class SourcelessLoaderBadBytecodeTestPEP302(SourcelessLoaderBadBytecodeTest,
|
|
|
|
BadBytecodeTestPEP302):
|
|
|
|
pass
|
|
|
|
|
2014-05-16 14:40:40 -03:00
|
|
|
|
|
|
|
(Frozen_SourcelessBadBytecodePEP302,
|
|
|
|
Source_SourcelessBadBytecodePEP302
|
|
|
|
) = util.test_both(SourcelessLoaderBadBytecodeTestPEP302, importlib=importlib,
|
|
|
|
machinery=machinery, abc=importlib_abc,
|
|
|
|
util=importlib_util)
|
2010-07-03 18:48:25 -03:00
|
|
|
|
2009-01-17 20:24:28 -04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2013-05-31 19:56:47 -03:00
|
|
|
unittest.main()
|