mirror of
https://github.com/python/cpython.git
synced 2025-11-09 10:01:42 +00:00
Issue #16803: test.test_importlib.source now tests frozen and source code
This commit is contained in:
parent
3a4586a9f9
commit
9ea8eda127
5 changed files with 91 additions and 74 deletions
|
|
@ -1,11 +1,12 @@
|
|||
from importlib import machinery
|
||||
import importlib
|
||||
import importlib.abc
|
||||
import importlib.util
|
||||
from .. import abc
|
||||
from .. import util
|
||||
from . import util as source_util
|
||||
|
||||
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')
|
||||
|
||||
import errno
|
||||
import marshal
|
||||
import os
|
||||
|
|
@ -19,7 +20,7 @@
|
|||
from test.support import make_legacy_pyc, unload
|
||||
|
||||
|
||||
class SimpleTest(unittest.TestCase, abc.LoaderTests):
|
||||
class SimpleTest(abc.LoaderTests):
|
||||
|
||||
"""Should have no issue importing a source module [basic]. And if there is
|
||||
a syntax error, it should raise a SyntaxError [syntax error].
|
||||
|
|
@ -27,7 +28,7 @@ class SimpleTest(unittest.TestCase, abc.LoaderTests):
|
|||
"""
|
||||
|
||||
def test_load_module_API(self):
|
||||
class Tester(importlib.abc.FileLoader):
|
||||
class Tester(self.abc.FileLoader):
|
||||
def get_source(self, _): return 'attr = 42'
|
||||
def is_package(self, _): return False
|
||||
|
||||
|
|
@ -37,7 +38,7 @@ def is_package(self, _): return False
|
|||
|
||||
def test_get_filename_API(self):
|
||||
# If fullname is not set then assume self.path is desired.
|
||||
class Tester(importlib.abc.FileLoader):
|
||||
class Tester(self.abc.FileLoader):
|
||||
def get_code(self, _): pass
|
||||
def get_source(self, _): pass
|
||||
def is_package(self, _): pass
|
||||
|
|
@ -55,7 +56,7 @@ def module_repr(self, _): pass
|
|||
# [basic]
|
||||
def test_module(self):
|
||||
with source_util.create_modules('_temp') as mapping:
|
||||
loader = machinery.SourceFileLoader('_temp', mapping['_temp'])
|
||||
loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
|
||||
module = loader.load_module('_temp')
|
||||
self.assertIn('_temp', sys.modules)
|
||||
check = {'__name__': '_temp', '__file__': mapping['_temp'],
|
||||
|
|
@ -65,7 +66,7 @@ def test_module(self):
|
|||
|
||||
def test_package(self):
|
||||
with source_util.create_modules('_pkg.__init__') as mapping:
|
||||
loader = machinery.SourceFileLoader('_pkg',
|
||||
loader = self.machinery.SourceFileLoader('_pkg',
|
||||
mapping['_pkg.__init__'])
|
||||
module = loader.load_module('_pkg')
|
||||
self.assertIn('_pkg', sys.modules)
|
||||
|
|
@ -78,7 +79,7 @@ def test_package(self):
|
|||
|
||||
def test_lacking_parent(self):
|
||||
with source_util.create_modules('_pkg.__init__', '_pkg.mod')as mapping:
|
||||
loader = machinery.SourceFileLoader('_pkg.mod',
|
||||
loader = self.machinery.SourceFileLoader('_pkg.mod',
|
||||
mapping['_pkg.mod'])
|
||||
module = loader.load_module('_pkg.mod')
|
||||
self.assertIn('_pkg.mod', sys.modules)
|
||||
|
|
@ -93,7 +94,7 @@ def fake_mtime(self, fxn):
|
|||
|
||||
def test_module_reuse(self):
|
||||
with source_util.create_modules('_temp') as mapping:
|
||||
loader = machinery.SourceFileLoader('_temp', mapping['_temp'])
|
||||
loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
|
||||
module = loader.load_module('_temp')
|
||||
module_id = id(module)
|
||||
module_dict_id = id(module.__dict__)
|
||||
|
|
@ -118,7 +119,7 @@ def test_state_after_failure(self):
|
|||
setattr(orig_module, attr, value)
|
||||
with open(mapping[name], 'w') as file:
|
||||
file.write('+++ bad syntax +++')
|
||||
loader = machinery.SourceFileLoader('_temp', mapping['_temp'])
|
||||
loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
|
||||
with self.assertRaises(SyntaxError):
|
||||
loader.load_module(name)
|
||||
for attr in attributes:
|
||||
|
|
@ -129,7 +130,7 @@ def test_bad_syntax(self):
|
|||
with source_util.create_modules('_temp') as mapping:
|
||||
with open(mapping['_temp'], 'w') as file:
|
||||
file.write('=')
|
||||
loader = machinery.SourceFileLoader('_temp', mapping['_temp'])
|
||||
loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
|
||||
with self.assertRaises(SyntaxError):
|
||||
loader.load_module('_temp')
|
||||
self.assertNotIn('_temp', sys.modules)
|
||||
|
|
@ -142,14 +143,14 @@ def test_file_from_empty_string_dir(self):
|
|||
file.write("# test file for importlib")
|
||||
try:
|
||||
with util.uncache('_temp'):
|
||||
loader = machinery.SourceFileLoader('_temp', file_path)
|
||||
loader = self.machinery.SourceFileLoader('_temp', file_path)
|
||||
mod = loader.load_module('_temp')
|
||||
self.assertEqual(file_path, mod.__file__)
|
||||
self.assertEqual(importlib.util.cache_from_source(file_path),
|
||||
self.assertEqual(self.util.cache_from_source(file_path),
|
||||
mod.__cached__)
|
||||
finally:
|
||||
os.unlink(file_path)
|
||||
pycache = os.path.dirname(importlib.util.cache_from_source(file_path))
|
||||
pycache = os.path.dirname(self.util.cache_from_source(file_path))
|
||||
if os.path.exists(pycache):
|
||||
shutil.rmtree(pycache)
|
||||
|
||||
|
|
@ -158,7 +159,7 @@ def test_timestamp_overflow(self):
|
|||
# truncated rather than raise an OverflowError.
|
||||
with source_util.create_modules('_temp') as mapping:
|
||||
source = mapping['_temp']
|
||||
compiled = importlib.util.cache_from_source(source)
|
||||
compiled = self.util.cache_from_source(source)
|
||||
with open(source, 'w') as f:
|
||||
f.write("x = 5")
|
||||
try:
|
||||
|
|
@ -169,7 +170,7 @@ def test_timestamp_overflow(self):
|
|||
if e.errno != getattr(errno, 'EOVERFLOW', None):
|
||||
raise
|
||||
self.skipTest("cannot set modification time to large integer ({})".format(e))
|
||||
loader = machinery.SourceFileLoader('_temp', mapping['_temp'])
|
||||
loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
|
||||
mod = loader.load_module('_temp')
|
||||
# Sanity checks.
|
||||
self.assertEqual(mod.__cached__, compiled)
|
||||
|
|
@ -178,12 +179,16 @@ def test_timestamp_overflow(self):
|
|||
os.stat(compiled)
|
||||
|
||||
def test_unloadable(self):
|
||||
loader = machinery.SourceFileLoader('good name', {})
|
||||
loader = self.machinery.SourceFileLoader('good name', {})
|
||||
with self.assertRaises(ImportError):
|
||||
loader.load_module('bad name')
|
||||
|
||||
Frozen_SimpleTest, Source_SimpleTest = util.test_both(
|
||||
SimpleTest, importlib=importlib, machinery=machinery, abc=importlib_abc,
|
||||
util=importlib_util)
|
||||
|
||||
class BadBytecodeTest(unittest.TestCase):
|
||||
|
||||
class BadBytecodeTest:
|
||||
|
||||
def import_(self, file, module_name):
|
||||
loader = self.loader(module_name, file)
|
||||
|
|
@ -200,7 +205,7 @@ def manipulate_bytecode(self, name, mapping, manipulator, *,
|
|||
pass
|
||||
py_compile.compile(mapping[name])
|
||||
if not del_source:
|
||||
bytecode_path = importlib.util.cache_from_source(mapping[name])
|
||||
bytecode_path = self.util.cache_from_source(mapping[name])
|
||||
else:
|
||||
os.unlink(mapping[name])
|
||||
bytecode_path = make_legacy_pyc(mapping[name])
|
||||
|
|
@ -289,7 +294,9 @@ def _test_bad_magic(self, test, *, del_source=False):
|
|||
|
||||
class SourceLoaderBadBytecodeTest(BadBytecodeTest):
|
||||
|
||||
loader = machinery.SourceFileLoader
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.loader = cls.machinery.SourceFileLoader
|
||||
|
||||
@source_util.writes_bytecode_files
|
||||
def test_empty_file(self):
|
||||
|
|
@ -329,7 +336,7 @@ def test(name, mapping, bytecode_path):
|
|||
self.import_(mapping[name], name)
|
||||
with open(bytecode_path, 'rb') as bytecode_file:
|
||||
self.assertEqual(bytecode_file.read(4),
|
||||
importlib.util.MAGIC_NUMBER)
|
||||
self.util.MAGIC_NUMBER)
|
||||
|
||||
self._test_bad_magic(test)
|
||||
|
||||
|
|
@ -379,13 +386,13 @@ def test_old_timestamp(self):
|
|||
zeros = b'\x00\x00\x00\x00'
|
||||
with source_util.create_modules('_temp') as mapping:
|
||||
py_compile.compile(mapping['_temp'])
|
||||
bytecode_path = importlib.util.cache_from_source(mapping['_temp'])
|
||||
bytecode_path = self.util.cache_from_source(mapping['_temp'])
|
||||
with open(bytecode_path, 'r+b') as bytecode_file:
|
||||
bytecode_file.seek(4)
|
||||
bytecode_file.write(zeros)
|
||||
self.import_(mapping['_temp'], '_temp')
|
||||
source_mtime = os.path.getmtime(mapping['_temp'])
|
||||
source_timestamp = importlib._w_long(source_mtime)
|
||||
source_timestamp = self.importlib._w_long(source_mtime)
|
||||
with open(bytecode_path, 'rb') as bytecode_file:
|
||||
bytecode_file.seek(4)
|
||||
self.assertEqual(bytecode_file.read(4), source_timestamp)
|
||||
|
|
@ -397,7 +404,7 @@ def test_read_only_bytecode(self):
|
|||
with source_util.create_modules('_temp') as mapping:
|
||||
# Create bytecode that will need to be re-created.
|
||||
py_compile.compile(mapping['_temp'])
|
||||
bytecode_path = importlib.util.cache_from_source(mapping['_temp'])
|
||||
bytecode_path = self.util.cache_from_source(mapping['_temp'])
|
||||
with open(bytecode_path, 'r+b') as bytecode_file:
|
||||
bytecode_file.seek(0)
|
||||
bytecode_file.write(b'\x00\x00\x00\x00')
|
||||
|
|
@ -411,10 +418,16 @@ def test_read_only_bytecode(self):
|
|||
# Make writable for eventual clean-up.
|
||||
os.chmod(bytecode_path, stat.S_IWUSR)
|
||||
|
||||
Frozen_SourceBadBytecode, Source_SourceBadBytecode = util.test_both(
|
||||
SourceLoaderBadBytecodeTest, importlib=importlib, machinery=machinery,
|
||||
abc=importlib_abc, util=importlib_util)
|
||||
|
||||
|
||||
class SourcelessLoaderBadBytecodeTest(BadBytecodeTest):
|
||||
|
||||
loader = machinery.SourcelessFileLoader
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.loader = cls.machinery.SourcelessFileLoader
|
||||
|
||||
def test_empty_file(self):
|
||||
def test(name, mapping, bytecode_path):
|
||||
|
|
@ -469,6 +482,9 @@ def test_no_marshal(self):
|
|||
def test_non_code_marshal(self):
|
||||
self._test_non_code_marshal(del_source=True)
|
||||
|
||||
Frozen_SourcelessBadBytecode, Source_SourcelessBadBytecode = util.test_both(
|
||||
SourcelessLoaderBadBytecodeTest, importlib=importlib,
|
||||
machinery=machinery, abc=importlib_abc, util=importlib_util)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue