mirror of
https://github.com/bunny-lab-io/Borealis.git
synced 2025-07-28 07:38:27 -06:00
Removed the Requirement to Install Python and NodeJS (Now Bundled with Borealis)
This commit is contained in:
5
Dependencies/Python/Lib/test/test_importlib/source/__init__.py
vendored
Normal file
5
Dependencies/Python/Lib/test/test_importlib/source/__init__.py
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
import os
|
||||
from test.support import load_package_tests
|
||||
|
||||
def load_tests(*args):
|
||||
return load_package_tests(os.path.dirname(__file__), *args)
|
4
Dependencies/Python/Lib/test/test_importlib/source/__main__.py
vendored
Normal file
4
Dependencies/Python/Lib/test/test_importlib/source/__main__.py
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
from . import load_tests
|
||||
import unittest
|
||||
|
||||
unittest.main()
|
78
Dependencies/Python/Lib/test/test_importlib/source/test_case_sensitivity.py
vendored
Normal file
78
Dependencies/Python/Lib/test/test_importlib/source/test_case_sensitivity.py
vendored
Normal file
@ -0,0 +1,78 @@
|
||||
"""Test case-sensitivity (PEP 235)."""
|
||||
import sys
|
||||
|
||||
from test.test_importlib import util
|
||||
|
||||
importlib = util.import_importlib('importlib')
|
||||
machinery = util.import_importlib('importlib.machinery')
|
||||
|
||||
import os
|
||||
from test.support import os_helper
|
||||
import unittest
|
||||
|
||||
|
||||
@util.case_insensitive_tests
|
||||
class CaseSensitivityTest(util.CASEOKTestBase):
|
||||
|
||||
"""PEP 235 dictates that on case-preserving, case-insensitive file systems
|
||||
that imports are case-sensitive unless the PYTHONCASEOK environment
|
||||
variable is set."""
|
||||
|
||||
name = 'MoDuLe'
|
||||
assert name != name.lower()
|
||||
|
||||
def finder(self, path):
|
||||
return self.machinery.FileFinder(path,
|
||||
(self.machinery.SourceFileLoader,
|
||||
self.machinery.SOURCE_SUFFIXES),
|
||||
(self.machinery.SourcelessFileLoader,
|
||||
self.machinery.BYTECODE_SUFFIXES))
|
||||
|
||||
def sensitivity_test(self):
|
||||
"""Look for a module with matching and non-matching sensitivity."""
|
||||
sensitive_pkg = 'sensitive.{0}'.format(self.name)
|
||||
insensitive_pkg = 'insensitive.{0}'.format(self.name.lower())
|
||||
context = util.create_modules(insensitive_pkg, sensitive_pkg)
|
||||
with context as mapping:
|
||||
sensitive_path = os.path.join(mapping['.root'], 'sensitive')
|
||||
insensitive_path = os.path.join(mapping['.root'], 'insensitive')
|
||||
sensitive_finder = self.finder(sensitive_path)
|
||||
insensitive_finder = self.finder(insensitive_path)
|
||||
return self.find(sensitive_finder), self.find(insensitive_finder)
|
||||
|
||||
@unittest.skipIf(sys.flags.ignore_environment, 'ignore_environment flag was set')
|
||||
def test_sensitive(self):
|
||||
with os_helper.EnvironmentVarGuard() as env:
|
||||
env.unset('PYTHONCASEOK')
|
||||
self.caseok_env_changed(should_exist=False)
|
||||
sensitive, insensitive = self.sensitivity_test()
|
||||
self.assertIsNotNone(sensitive)
|
||||
self.assertIn(self.name, sensitive.get_filename(self.name))
|
||||
self.assertIsNone(insensitive)
|
||||
|
||||
@unittest.skipIf(sys.flags.ignore_environment, 'ignore_environment flag was set')
|
||||
def test_insensitive(self):
|
||||
with os_helper.EnvironmentVarGuard() as env:
|
||||
env.set('PYTHONCASEOK', '1')
|
||||
self.caseok_env_changed(should_exist=True)
|
||||
sensitive, insensitive = self.sensitivity_test()
|
||||
self.assertIsNotNone(sensitive)
|
||||
self.assertIn(self.name, sensitive.get_filename(self.name))
|
||||
self.assertIsNotNone(insensitive)
|
||||
self.assertIn(self.name, insensitive.get_filename(self.name))
|
||||
|
||||
|
||||
class CaseSensitivityTestPEP451(CaseSensitivityTest):
|
||||
def find(self, finder):
|
||||
found = finder.find_spec(self.name)
|
||||
return found.loader if found is not None else found
|
||||
|
||||
|
||||
(Frozen_CaseSensitivityTestPEP451,
|
||||
Source_CaseSensitivityTestPEP451
|
||||
) = util.test_both(CaseSensitivityTestPEP451, importlib=importlib,
|
||||
machinery=machinery)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
795
Dependencies/Python/Lib/test/test_importlib/source/test_file_loader.py
vendored
Normal file
795
Dependencies/Python/Lib/test/test_importlib/source/test_file_loader.py
vendored
Normal file
@ -0,0 +1,795 @@
|
||||
from test.test_importlib import abc, 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
|
||||
import py_compile
|
||||
import shutil
|
||||
import stat
|
||||
import sys
|
||||
import types
|
||||
import unittest
|
||||
import warnings
|
||||
|
||||
from test.support.import_helper import make_legacy_pyc, unload
|
||||
|
||||
from test.test_py_compile import without_source_date_epoch
|
||||
from test.test_py_compile import SourceDateEpochTestMeta
|
||||
|
||||
|
||||
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].
|
||||
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
self.name = 'spam'
|
||||
self.filepath = os.path.join('ham', self.name + '.py')
|
||||
self.loader = self.machinery.SourceFileLoader(self.name, self.filepath)
|
||||
|
||||
def test_load_module_API(self):
|
||||
class Tester(self.abc.FileLoader):
|
||||
def get_source(self, _): return 'attr = 42'
|
||||
def is_package(self, _): return False
|
||||
|
||||
loader = Tester('blah', 'blah.py')
|
||||
self.addCleanup(unload, 'blah')
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter('ignore', DeprecationWarning)
|
||||
module = loader.load_module() # Should not raise an exception.
|
||||
|
||||
def test_get_filename_API(self):
|
||||
# If fullname is not set then assume self.path is desired.
|
||||
class Tester(self.abc.FileLoader):
|
||||
def get_code(self, _): pass
|
||||
def get_source(self, _): pass
|
||||
def is_package(self, _): pass
|
||||
|
||||
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')
|
||||
|
||||
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)
|
||||
|
||||
# [basic]
|
||||
def test_module(self):
|
||||
with util.create_modules('_temp') as mapping:
|
||||
loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter('ignore', DeprecationWarning)
|
||||
module = loader.load_module('_temp')
|
||||
self.assertIn('_temp', sys.modules)
|
||||
check = {'__name__': '_temp', '__file__': mapping['_temp'],
|
||||
'__package__': ''}
|
||||
for attr, value in check.items():
|
||||
self.assertEqual(getattr(module, attr), value)
|
||||
|
||||
def test_package(self):
|
||||
with util.create_modules('_pkg.__init__') as mapping:
|
||||
loader = self.machinery.SourceFileLoader('_pkg',
|
||||
mapping['_pkg.__init__'])
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter('ignore', DeprecationWarning)
|
||||
module = loader.load_module('_pkg')
|
||||
self.assertIn('_pkg', sys.modules)
|
||||
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):
|
||||
with util.create_modules('_pkg.__init__', '_pkg.mod')as mapping:
|
||||
loader = self.machinery.SourceFileLoader('_pkg.mod',
|
||||
mapping['_pkg.mod'])
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter('ignore', DeprecationWarning)
|
||||
module = loader.load_module('_pkg.mod')
|
||||
self.assertIn('_pkg.mod', sys.modules)
|
||||
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):
|
||||
with util.create_modules('_temp') as mapping:
|
||||
loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter('ignore', DeprecationWarning)
|
||||
module = loader.load_module('_temp')
|
||||
module_id = id(module)
|
||||
module_dict_id = id(module.__dict__)
|
||||
with open(mapping['_temp'], 'w', encoding='utf-8') as file:
|
||||
file.write("testing_var = 42\n")
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter('ignore', DeprecationWarning)
|
||||
module = loader.load_module('_temp')
|
||||
self.assertIn('testing_var', module.__dict__,
|
||||
"'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'
|
||||
with util.create_modules(name) as mapping:
|
||||
orig_module = types.ModuleType(name)
|
||||
for attr in attributes:
|
||||
setattr(orig_module, attr, value)
|
||||
with open(mapping[name], 'w', encoding='utf-8') as file:
|
||||
file.write('+++ bad syntax +++')
|
||||
loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
|
||||
with self.assertRaises(SyntaxError):
|
||||
loader.exec_module(orig_module)
|
||||
for attr in attributes:
|
||||
self.assertEqual(getattr(orig_module, attr), value)
|
||||
with self.assertRaises(SyntaxError):
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter('ignore', DeprecationWarning)
|
||||
loader.load_module(name)
|
||||
for attr in attributes:
|
||||
self.assertEqual(getattr(orig_module, attr), value)
|
||||
|
||||
# [syntax error]
|
||||
def test_bad_syntax(self):
|
||||
with util.create_modules('_temp') as mapping:
|
||||
with open(mapping['_temp'], 'w', encoding='utf-8') as file:
|
||||
file.write('=')
|
||||
loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
|
||||
with self.assertRaises(SyntaxError):
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter('ignore', DeprecationWarning)
|
||||
loader.load_module('_temp')
|
||||
self.assertNotIn('_temp', sys.modules)
|
||||
|
||||
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.
|
||||
file_path = '_temp.py'
|
||||
with open(file_path, 'w', encoding='utf-8') as file:
|
||||
file.write("# test file for importlib")
|
||||
try:
|
||||
with util.uncache('_temp'):
|
||||
loader = self.machinery.SourceFileLoader('_temp', file_path)
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter('ignore', DeprecationWarning)
|
||||
mod = loader.load_module('_temp')
|
||||
self.assertEqual(file_path, mod.__file__)
|
||||
self.assertEqual(self.util.cache_from_source(file_path),
|
||||
mod.__cached__)
|
||||
finally:
|
||||
os.unlink(file_path)
|
||||
pycache = os.path.dirname(self.util.cache_from_source(file_path))
|
||||
if os.path.exists(pycache):
|
||||
shutil.rmtree(pycache)
|
||||
|
||||
@util.writes_bytecode_files
|
||||
def test_timestamp_overflow(self):
|
||||
# When a modification timestamp is larger than 2**32, it should be
|
||||
# truncated rather than raise an OverflowError.
|
||||
with util.create_modules('_temp') as mapping:
|
||||
source = mapping['_temp']
|
||||
compiled = self.util.cache_from_source(source)
|
||||
with open(source, 'w', encoding='utf-8') as f:
|
||||
f.write("x = 5")
|
||||
try:
|
||||
os.utime(source, (2 ** 33 - 5, 2 ** 33 - 5))
|
||||
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))
|
||||
loader = self.machinery.SourceFileLoader('_temp', mapping['_temp'])
|
||||
# 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
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter('ignore', DeprecationWarning)
|
||||
mod = loader.load_module('_temp')
|
||||
# Sanity checks.
|
||||
self.assertEqual(mod.__cached__, compiled)
|
||||
self.assertEqual(mod.x, 5)
|
||||
# The pyc file was created.
|
||||
self.assertTrue(os.path.exists(compiled))
|
||||
|
||||
def test_unloadable(self):
|
||||
loader = self.machinery.SourceFileLoader('good name', {})
|
||||
module = types.ModuleType('bad name')
|
||||
module.__spec__ = self.machinery.ModuleSpec('bad name', loader)
|
||||
with self.assertRaises(ImportError):
|
||||
loader.exec_module(module)
|
||||
with self.assertRaises(ImportError):
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter('ignore', DeprecationWarning)
|
||||
loader.load_module('bad name')
|
||||
|
||||
@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
|
||||
def test_overridden_checked_hash_based_pyc(self):
|
||||
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
|
||||
def test_overridden_unchecked_hash_based_pyc(self):
|
||||
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],
|
||||
)
|
||||
|
||||
|
||||
(Frozen_SimpleTest,
|
||||
Source_SimpleTest
|
||||
) = util.test_both(SimpleTest, importlib=importlib, machinery=machinery,
|
||||
abc=importlib_abc, util=importlib_util)
|
||||
|
||||
|
||||
class SourceDateEpochTestMeta(SourceDateEpochTestMeta,
|
||||
type(Source_SimpleTest)):
|
||||
pass
|
||||
|
||||
|
||||
class SourceDateEpoch_SimpleTest(Source_SimpleTest,
|
||||
metaclass=SourceDateEpochTestMeta,
|
||||
source_date_epoch=True):
|
||||
pass
|
||||
|
||||
|
||||
class BadBytecodeTest:
|
||||
|
||||
def import_(self, file, module_name):
|
||||
raise NotImplementedError
|
||||
|
||||
def manipulate_bytecode(self,
|
||||
name, mapping, manipulator, *,
|
||||
del_source=False,
|
||||
invalidation_mode=py_compile.PycInvalidationMode.TIMESTAMP):
|
||||
"""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
|
||||
py_compile.compile(mapping[name], invalidation_mode=invalidation_mode)
|
||||
if not del_source:
|
||||
bytecode_path = self.util.cache_from_source(mapping[name])
|
||||
else:
|
||||
os.unlink(mapping[name])
|
||||
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)
|
||||
return bytecode_path
|
||||
|
||||
def _test_empty_file(self, test, *, del_source=False):
|
||||
with util.create_modules('_temp') as mapping:
|
||||
bc_path = self.manipulate_bytecode('_temp', mapping,
|
||||
lambda bc: b'',
|
||||
del_source=del_source)
|
||||
test('_temp', mapping, bc_path)
|
||||
|
||||
@util.writes_bytecode_files
|
||||
def _test_partial_magic(self, test, *, del_source=False):
|
||||
# When their are less than 4 bytes to a .pyc, regenerate it if
|
||||
# possible, else raise ImportError.
|
||||
with util.create_modules('_temp') as mapping:
|
||||
bc_path = self.manipulate_bytecode('_temp', mapping,
|
||||
lambda bc: bc[:3],
|
||||
del_source=del_source)
|
||||
test('_temp', mapping, bc_path)
|
||||
|
||||
def _test_magic_only(self, test, *, del_source=False):
|
||||
with util.create_modules('_temp') as mapping:
|
||||
bc_path = self.manipulate_bytecode('_temp', mapping,
|
||||
lambda bc: bc[:4],
|
||||
del_source=del_source)
|
||||
test('_temp', mapping, bc_path)
|
||||
|
||||
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)
|
||||
|
||||
def _test_partial_timestamp(self, test, *, del_source=False):
|
||||
with util.create_modules('_temp') as mapping:
|
||||
bc_path = self.manipulate_bytecode('_temp', mapping,
|
||||
lambda bc: bc[:11],
|
||||
del_source=del_source)
|
||||
test('_temp', mapping, bc_path)
|
||||
|
||||
def _test_partial_size(self, test, *, del_source=False):
|
||||
with util.create_modules('_temp') as mapping:
|
||||
bc_path = self.manipulate_bytecode('_temp', mapping,
|
||||
lambda bc: bc[:15],
|
||||
del_source=del_source)
|
||||
test('_temp', mapping, bc_path)
|
||||
|
||||
def _test_no_marshal(self, *, del_source=False):
|
||||
with util.create_modules('_temp') as mapping:
|
||||
bc_path = self.manipulate_bytecode('_temp', mapping,
|
||||
lambda bc: bc[:16],
|
||||
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):
|
||||
with util.create_modules('_temp') as mapping:
|
||||
bytecode_path = self.manipulate_bytecode('_temp', mapping,
|
||||
lambda bc: bc[:16] + marshal.dumps(b'abcd'),
|
||||
del_source=del_source)
|
||||
file_path = mapping['_temp'] if not del_source else bytecode_path
|
||||
with self.assertRaises(ImportError) as cm:
|
||||
self.import_(file_path, '_temp')
|
||||
self.assertEqual(cm.exception.name, '_temp')
|
||||
self.assertEqual(cm.exception.path, bytecode_path)
|
||||
|
||||
def _test_bad_marshal(self, *, del_source=False):
|
||||
with util.create_modules('_temp') as mapping:
|
||||
bytecode_path = self.manipulate_bytecode('_temp', mapping,
|
||||
lambda bc: bc[:16] + b'<test>',
|
||||
del_source=del_source)
|
||||
file_path = mapping['_temp'] if not del_source else bytecode_path
|
||||
with self.assertRaises(EOFError):
|
||||
self.import_(file_path, '_temp')
|
||||
|
||||
def _test_bad_magic(self, test, *, del_source=False):
|
||||
with util.create_modules('_temp') as mapping:
|
||||
bc_path = self.manipulate_bytecode('_temp', mapping,
|
||||
lambda bc: b'\x00\x00\x00\x00' + bc[4:])
|
||||
test('_temp', mapping, bc_path)
|
||||
|
||||
|
||||
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)
|
||||
|
||||
|
||||
class BadBytecodeTestPEP302(BadBytecodeTest):
|
||||
|
||||
def import_(self, file, module_name):
|
||||
loader = self.loader(module_name, file)
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter('ignore', DeprecationWarning)
|
||||
module = loader.load_module(module_name)
|
||||
self.assertIn(module_name, sys.modules)
|
||||
|
||||
|
||||
class SourceLoaderBadBytecodeTest:
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.loader = cls.machinery.SourceFileLoader
|
||||
|
||||
@util.writes_bytecode_files
|
||||
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:
|
||||
self.assertGreater(len(file.read()), 16)
|
||||
|
||||
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:
|
||||
self.assertGreater(len(file.read()), 16)
|
||||
|
||||
self._test_partial_magic(test)
|
||||
|
||||
@util.writes_bytecode_files
|
||||
def test_magic_only(self):
|
||||
# When there is only the magic number, regenerate the .pyc if possible,
|
||||
# else raise EOFError.
|
||||
def test(name, mapping, bytecode_path):
|
||||
self.import_(mapping[name], name)
|
||||
with open(bytecode_path, 'rb') as file:
|
||||
self.assertGreater(len(file.read()), 16)
|
||||
|
||||
self._test_magic_only(test)
|
||||
|
||||
@util.writes_bytecode_files
|
||||
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:
|
||||
self.assertEqual(bytecode_file.read(4),
|
||||
self.util.MAGIC_NUMBER)
|
||||
|
||||
self._test_bad_magic(test)
|
||||
|
||||
@util.writes_bytecode_files
|
||||
def test_partial_timestamp(self):
|
||||
# When the timestamp 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_timestamp(test)
|
||||
|
||||
@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)
|
||||
|
||||
@util.writes_bytecode_files
|
||||
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:
|
||||
self.assertGreater(len(file.read()), 16)
|
||||
|
||||
self._test_partial_size(test)
|
||||
|
||||
@util.writes_bytecode_files
|
||||
def test_no_marshal(self):
|
||||
# When there is only the magic number and timestamp, raise EOFError.
|
||||
self._test_no_marshal()
|
||||
|
||||
@util.writes_bytecode_files
|
||||
def test_non_code_marshal(self):
|
||||
self._test_non_code_marshal()
|
||||
# XXX ImportError when sourceless
|
||||
|
||||
# [bad marshal]
|
||||
@util.writes_bytecode_files
|
||||
def test_bad_marshal(self):
|
||||
# Bad marshal data should raise a ValueError.
|
||||
self._test_bad_marshal()
|
||||
|
||||
# [bad timestamp]
|
||||
@util.writes_bytecode_files
|
||||
@without_source_date_epoch
|
||||
def test_old_timestamp(self):
|
||||
# When the timestamp is older than the source, bytecode should be
|
||||
# regenerated.
|
||||
zeros = b'\x00\x00\x00\x00'
|
||||
with util.create_modules('_temp') as mapping:
|
||||
py_compile.compile(mapping['_temp'])
|
||||
bytecode_path = self.util.cache_from_source(mapping['_temp'])
|
||||
with open(bytecode_path, 'r+b') as bytecode_file:
|
||||
bytecode_file.seek(8)
|
||||
bytecode_file.write(zeros)
|
||||
self.import_(mapping['_temp'], '_temp')
|
||||
source_mtime = os.path.getmtime(mapping['_temp'])
|
||||
source_timestamp = self.importlib._pack_uint32(source_mtime)
|
||||
with open(bytecode_path, 'rb') as bytecode_file:
|
||||
bytecode_file.seek(8)
|
||||
self.assertEqual(bytecode_file.read(4), source_timestamp)
|
||||
|
||||
# [bytecode read-only]
|
||||
@util.writes_bytecode_files
|
||||
def test_read_only_bytecode(self):
|
||||
# When bytecode is read-only but should be rewritten, fail silently.
|
||||
with util.create_modules('_temp') as mapping:
|
||||
# Create bytecode that will need to be re-created.
|
||||
py_compile.compile(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')
|
||||
# Make the bytecode read-only.
|
||||
os.chmod(bytecode_path,
|
||||
stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
|
||||
try:
|
||||
# Should not raise OSError!
|
||||
self.import_(mapping['_temp'], '_temp')
|
||||
finally:
|
||||
# Make writable for eventual clean-up.
|
||||
os.chmod(bytecode_path, stat.S_IWUSR)
|
||||
|
||||
|
||||
class SourceLoaderBadBytecodeTestPEP451(
|
||||
SourceLoaderBadBytecodeTest, BadBytecodeTestPEP451):
|
||||
pass
|
||||
|
||||
|
||||
(Frozen_SourceBadBytecodePEP451,
|
||||
Source_SourceBadBytecodePEP451
|
||||
) = util.test_both(SourceLoaderBadBytecodeTestPEP451, importlib=importlib,
|
||||
machinery=machinery, abc=importlib_abc,
|
||||
util=importlib_util)
|
||||
|
||||
|
||||
class SourceLoaderBadBytecodeTestPEP302(
|
||||
SourceLoaderBadBytecodeTest, BadBytecodeTestPEP302):
|
||||
pass
|
||||
|
||||
|
||||
(Frozen_SourceBadBytecodePEP302,
|
||||
Source_SourceBadBytecodePEP302
|
||||
) = util.test_both(SourceLoaderBadBytecodeTestPEP302, importlib=importlib,
|
||||
machinery=machinery, abc=importlib_abc,
|
||||
util=importlib_util)
|
||||
|
||||
|
||||
class SourcelessLoaderBadBytecodeTest:
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.loader = cls.machinery.SourcelessFileLoader
|
||||
|
||||
def test_empty_file(self):
|
||||
def test(name, mapping, bytecode_path):
|
||||
with self.assertRaises(ImportError) as cm:
|
||||
self.import_(bytecode_path, name)
|
||||
self.assertEqual(cm.exception.name, name)
|
||||
self.assertEqual(cm.exception.path, bytecode_path)
|
||||
|
||||
self._test_empty_file(test, del_source=True)
|
||||
|
||||
def test_partial_magic(self):
|
||||
def test(name, mapping, bytecode_path):
|
||||
with self.assertRaises(ImportError) as cm:
|
||||
self.import_(bytecode_path, name)
|
||||
self.assertEqual(cm.exception.name, name)
|
||||
self.assertEqual(cm.exception.path, bytecode_path)
|
||||
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):
|
||||
with self.assertRaises(ImportError) as cm:
|
||||
self.import_(bytecode_path, name)
|
||||
self.assertEqual(cm.exception.name, name)
|
||||
self.assertEqual(cm.exception.path, bytecode_path)
|
||||
|
||||
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)
|
||||
|
||||
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)
|
||||
|
||||
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)
|
||||
|
||||
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)
|
||||
|
||||
|
||||
class SourcelessLoaderBadBytecodeTestPEP451(SourcelessLoaderBadBytecodeTest,
|
||||
BadBytecodeTestPEP451):
|
||||
pass
|
||||
|
||||
|
||||
(Frozen_SourcelessBadBytecodePEP451,
|
||||
Source_SourcelessBadBytecodePEP451
|
||||
) = util.test_both(SourcelessLoaderBadBytecodeTestPEP451, importlib=importlib,
|
||||
machinery=machinery, abc=importlib_abc,
|
||||
util=importlib_util)
|
||||
|
||||
|
||||
class SourcelessLoaderBadBytecodeTestPEP302(SourcelessLoaderBadBytecodeTest,
|
||||
BadBytecodeTestPEP302):
|
||||
pass
|
||||
|
||||
|
||||
(Frozen_SourcelessBadBytecodePEP302,
|
||||
Source_SourcelessBadBytecodePEP302
|
||||
) = util.test_both(SourcelessLoaderBadBytecodeTestPEP302, importlib=importlib,
|
||||
machinery=machinery, abc=importlib_abc,
|
||||
util=importlib_util)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
212
Dependencies/Python/Lib/test/test_importlib/source/test_finder.py
vendored
Normal file
212
Dependencies/Python/Lib/test/test_importlib/source/test_finder.py
vendored
Normal file
@ -0,0 +1,212 @@
|
||||
from test.test_importlib import abc, util
|
||||
|
||||
machinery = util.import_importlib('importlib.machinery')
|
||||
|
||||
import errno
|
||||
import os
|
||||
import py_compile
|
||||
import stat
|
||||
import sys
|
||||
import tempfile
|
||||
from test.support.import_helper import make_legacy_pyc
|
||||
import unittest
|
||||
|
||||
|
||||
class FinderTests(abc.FinderTests):
|
||||
|
||||
"""For a top-level module, it should just be found directly in the
|
||||
directory being searched. This is true for a directory with source
|
||||
[top-level source], bytecode [top-level bc], or both [top-level both].
|
||||
There is also the possibility that it is a package [top-level package], in
|
||||
which case there will be a directory with the module name and an
|
||||
__init__.py file. If there is a directory without an __init__.py an
|
||||
ImportWarning is returned [empty dir].
|
||||
|
||||
For sub-modules and sub-packages, the same happens as above but only use
|
||||
the tail end of the name [sub module] [sub package] [sub empty].
|
||||
|
||||
When there is a conflict between a package and module having the same name
|
||||
in the same directory, the package wins out [package over module]. This is
|
||||
so that imports of modules within the package can occur rather than trigger
|
||||
an import error.
|
||||
|
||||
When there is a package and module with the same name, always pick the
|
||||
package over the module [package over module]. This is so that imports from
|
||||
the package have the possibility of succeeding.
|
||||
|
||||
"""
|
||||
|
||||
def get_finder(self, root):
|
||||
loader_details = [(self.machinery.SourceFileLoader,
|
||||
self.machinery.SOURCE_SUFFIXES),
|
||||
(self.machinery.SourcelessFileLoader,
|
||||
self.machinery.BYTECODE_SUFFIXES)]
|
||||
return self.machinery.FileFinder(root, *loader_details)
|
||||
|
||||
def import_(self, root, module):
|
||||
finder = self.get_finder(root)
|
||||
return self._find(finder, module, loader_only=True)
|
||||
|
||||
def run_test(self, test, create=None, *, compile_=None, unlink=None):
|
||||
"""Test the finding of 'test' with the creation of modules listed in
|
||||
'create'.
|
||||
|
||||
Any names listed in 'compile_' are byte-compiled. Modules
|
||||
listed in 'unlink' have their source files deleted.
|
||||
|
||||
"""
|
||||
if create is None:
|
||||
create = {test}
|
||||
with util.create_modules(*create) as mapping:
|
||||
if compile_:
|
||||
for name in compile_:
|
||||
py_compile.compile(mapping[name])
|
||||
if unlink:
|
||||
for name in unlink:
|
||||
os.unlink(mapping[name])
|
||||
try:
|
||||
make_legacy_pyc(mapping[name])
|
||||
except OSError as error:
|
||||
# Some tests do not set compile_=True so the source
|
||||
# module will not get compiled and there will be no
|
||||
# PEP 3147 pyc file to rename.
|
||||
if error.errno != errno.ENOENT:
|
||||
raise
|
||||
loader = self.import_(mapping['.root'], test)
|
||||
self.assertHasAttr(loader, 'load_module')
|
||||
return loader
|
||||
|
||||
def test_module(self):
|
||||
# [top-level source]
|
||||
self.run_test('top_level')
|
||||
# [top-level bc]
|
||||
self.run_test('top_level', compile_={'top_level'},
|
||||
unlink={'top_level'})
|
||||
# [top-level both]
|
||||
self.run_test('top_level', compile_={'top_level'})
|
||||
|
||||
# [top-level package]
|
||||
def test_package(self):
|
||||
# Source.
|
||||
self.run_test('pkg', {'pkg.__init__'})
|
||||
# Bytecode.
|
||||
self.run_test('pkg', {'pkg.__init__'}, compile_={'pkg.__init__'},
|
||||
unlink={'pkg.__init__'})
|
||||
# Both.
|
||||
self.run_test('pkg', {'pkg.__init__'}, compile_={'pkg.__init__'})
|
||||
|
||||
# [sub module]
|
||||
def test_module_in_package(self):
|
||||
with util.create_modules('pkg.__init__', 'pkg.sub') as mapping:
|
||||
pkg_dir = os.path.dirname(mapping['pkg.__init__'])
|
||||
loader = self.import_(pkg_dir, 'pkg.sub')
|
||||
self.assertHasAttr(loader, 'load_module')
|
||||
|
||||
# [sub package]
|
||||
def test_package_in_package(self):
|
||||
context = util.create_modules('pkg.__init__', 'pkg.sub.__init__')
|
||||
with context as mapping:
|
||||
pkg_dir = os.path.dirname(mapping['pkg.__init__'])
|
||||
loader = self.import_(pkg_dir, 'pkg.sub')
|
||||
self.assertHasAttr(loader, 'load_module')
|
||||
|
||||
# [package over modules]
|
||||
def test_package_over_module(self):
|
||||
name = '_temp'
|
||||
loader = self.run_test(name, {'{0}.__init__'.format(name), name})
|
||||
self.assertIn('__init__', loader.get_filename(name))
|
||||
|
||||
def test_failure(self):
|
||||
with util.create_modules('blah') as mapping:
|
||||
nothing = self.import_(mapping['.root'], 'sdfsadsadf')
|
||||
self.assertEqual(nothing, self.NOT_FOUND)
|
||||
|
||||
def test_empty_string_for_dir(self):
|
||||
# The empty string from sys.path means to search in the cwd.
|
||||
finder = self.machinery.FileFinder('', (self.machinery.SourceFileLoader,
|
||||
self.machinery.SOURCE_SUFFIXES))
|
||||
with open('mod.py', 'w', encoding='utf-8') as file:
|
||||
file.write("# test file for importlib")
|
||||
try:
|
||||
loader = self._find(finder, 'mod', loader_only=True)
|
||||
self.assertHasAttr(loader, 'load_module')
|
||||
finally:
|
||||
os.unlink('mod.py')
|
||||
|
||||
def test_invalidate_caches(self):
|
||||
# invalidate_caches() should reset the mtime.
|
||||
finder = self.machinery.FileFinder('', (self.machinery.SourceFileLoader,
|
||||
self.machinery.SOURCE_SUFFIXES))
|
||||
finder._path_mtime = 42
|
||||
finder.invalidate_caches()
|
||||
self.assertEqual(finder._path_mtime, -1)
|
||||
|
||||
# Regression test for http://bugs.python.org/issue14846
|
||||
def test_dir_removal_handling(self):
|
||||
mod = 'mod'
|
||||
with util.create_modules(mod) as mapping:
|
||||
finder = self.get_finder(mapping['.root'])
|
||||
found = self._find(finder, 'mod', loader_only=True)
|
||||
self.assertIsNotNone(found)
|
||||
found = self._find(finder, 'mod', loader_only=True)
|
||||
self.assertEqual(found, self.NOT_FOUND)
|
||||
|
||||
@unittest.skipUnless(sys.platform != 'win32',
|
||||
'os.chmod() does not support the needed arguments under Windows')
|
||||
def test_no_read_directory(self):
|
||||
# Issue #16730
|
||||
tempdir = tempfile.TemporaryDirectory()
|
||||
self.enterContext(tempdir)
|
||||
# Since we muck with the permissions, we want to set them back to
|
||||
# their original values to make sure the directory can be properly
|
||||
# cleaned up.
|
||||
original_mode = os.stat(tempdir.name).st_mode
|
||||
self.addCleanup(os.chmod, tempdir.name, original_mode)
|
||||
os.chmod(tempdir.name, stat.S_IWUSR | stat.S_IXUSR)
|
||||
finder = self.get_finder(tempdir.name)
|
||||
found = self._find(finder, 'doesnotexist')
|
||||
self.assertEqual(found, self.NOT_FOUND)
|
||||
|
||||
def test_ignore_file(self):
|
||||
# If a directory got changed to a file from underneath us, then don't
|
||||
# worry about looking for submodules.
|
||||
with tempfile.NamedTemporaryFile() as file_obj:
|
||||
finder = self.get_finder(file_obj.name)
|
||||
found = self._find(finder, 'doesnotexist')
|
||||
self.assertEqual(found, self.NOT_FOUND)
|
||||
|
||||
|
||||
class FinderTestsPEP451(FinderTests):
|
||||
|
||||
NOT_FOUND = None
|
||||
|
||||
def _find(self, finder, name, loader_only=False):
|
||||
spec = finder.find_spec(name)
|
||||
return spec.loader if spec is not None else spec
|
||||
|
||||
|
||||
(Frozen_FinderTestsPEP451,
|
||||
Source_FinderTestsPEP451
|
||||
) = util.test_both(FinderTestsPEP451, machinery=machinery)
|
||||
|
||||
|
||||
class FinderTestsPEP420(FinderTests):
|
||||
|
||||
NOT_FOUND = (None, [])
|
||||
|
||||
def _find(self, finder, name, loader_only=False):
|
||||
spec = finder.find_spec(name)
|
||||
if spec is None:
|
||||
return self.NOT_FOUND
|
||||
if loader_only:
|
||||
return spec.loader
|
||||
return spec.loader, spec.submodule_search_locations
|
||||
|
||||
|
||||
(Frozen_FinderTestsPEP420,
|
||||
Source_FinderTestsPEP420
|
||||
) = util.test_both(FinderTestsPEP420, machinery=machinery)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
32
Dependencies/Python/Lib/test/test_importlib/source/test_path_hook.py
vendored
Normal file
32
Dependencies/Python/Lib/test/test_importlib/source/test_path_hook.py
vendored
Normal file
@ -0,0 +1,32 @@
|
||||
from test.test_importlib import util
|
||||
|
||||
machinery = util.import_importlib('importlib.machinery')
|
||||
|
||||
import unittest
|
||||
|
||||
|
||||
class PathHookTest:
|
||||
|
||||
"""Test the path hook for source."""
|
||||
|
||||
def path_hook(self):
|
||||
return self.machinery.FileFinder.path_hook((self.machinery.SourceFileLoader,
|
||||
self.machinery.SOURCE_SUFFIXES))
|
||||
|
||||
def test_success(self):
|
||||
with util.create_modules('dummy') as mapping:
|
||||
self.assertHasAttr(self.path_hook()(mapping['.root']),
|
||||
'find_spec')
|
||||
|
||||
def test_empty_string(self):
|
||||
# The empty string represents the cwd.
|
||||
self.assertHasAttr(self.path_hook()(''), 'find_spec')
|
||||
|
||||
|
||||
(Frozen_PathHookTest,
|
||||
Source_PathHooktest
|
||||
) = util.test_both(PathHookTest, machinery=machinery)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
175
Dependencies/Python/Lib/test/test_importlib/source/test_source_encoding.py
vendored
Normal file
175
Dependencies/Python/Lib/test/test_importlib/source/test_source_encoding.py
vendored
Normal file
@ -0,0 +1,175 @@
|
||||
from test.test_importlib import util
|
||||
|
||||
machinery = util.import_importlib('importlib.machinery')
|
||||
|
||||
import codecs
|
||||
import importlib.util
|
||||
import re
|
||||
import types
|
||||
# Because sys.path gets essentially blanked, need to have unicodedata already
|
||||
# imported for the parser to use.
|
||||
import unicodedata
|
||||
import unittest
|
||||
import warnings
|
||||
|
||||
|
||||
CODING_RE = re.compile(r'^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)', re.ASCII)
|
||||
|
||||
|
||||
class EncodingTest:
|
||||
|
||||
"""PEP 3120 makes UTF-8 the default encoding for source code
|
||||
[default encoding].
|
||||
|
||||
PEP 263 specifies how that can change on a per-file basis. Either the first
|
||||
or second line can contain the encoding line [encoding first line]
|
||||
[encoding second line]. If the file has the BOM marker it is considered UTF-8
|
||||
implicitly [BOM]. If any encoding is specified it must be UTF-8, else it is
|
||||
an error [BOM and utf-8][BOM conflict].
|
||||
|
||||
"""
|
||||
|
||||
variable = '\u00fc'
|
||||
character = '\u00c9'
|
||||
source_line = "{0} = '{1}'\n".format(variable, character)
|
||||
module_name = '_temp'
|
||||
|
||||
def run_test(self, source):
|
||||
with util.create_modules(self.module_name) as mapping:
|
||||
with open(mapping[self.module_name], 'wb') as file:
|
||||
file.write(source)
|
||||
loader = self.machinery.SourceFileLoader(self.module_name,
|
||||
mapping[self.module_name])
|
||||
return self.load(loader)
|
||||
|
||||
def create_source(self, encoding):
|
||||
encoding_line = "# coding={0}".format(encoding)
|
||||
assert CODING_RE.match(encoding_line)
|
||||
source_lines = [encoding_line.encode('utf-8')]
|
||||
source_lines.append(self.source_line.encode(encoding))
|
||||
return b'\n'.join(source_lines)
|
||||
|
||||
def test_non_obvious_encoding(self):
|
||||
# Make sure that an encoding that has never been a standard one for
|
||||
# Python works.
|
||||
encoding_line = "# coding=koi8-r"
|
||||
assert CODING_RE.match(encoding_line)
|
||||
source = "{0}\na=42\n".format(encoding_line).encode("koi8-r")
|
||||
self.run_test(source)
|
||||
|
||||
# [default encoding]
|
||||
def test_default_encoding(self):
|
||||
self.run_test(self.source_line.encode('utf-8'))
|
||||
|
||||
# [encoding first line]
|
||||
def test_encoding_on_first_line(self):
|
||||
encoding = 'Latin-1'
|
||||
source = self.create_source(encoding)
|
||||
self.run_test(source)
|
||||
|
||||
# [encoding second line]
|
||||
def test_encoding_on_second_line(self):
|
||||
source = b"#/usr/bin/python\n" + self.create_source('Latin-1')
|
||||
self.run_test(source)
|
||||
|
||||
# [BOM]
|
||||
def test_bom(self):
|
||||
self.run_test(codecs.BOM_UTF8 + self.source_line.encode('utf-8'))
|
||||
|
||||
# [BOM and utf-8]
|
||||
def test_bom_and_utf_8(self):
|
||||
source = codecs.BOM_UTF8 + self.create_source('utf-8')
|
||||
self.run_test(source)
|
||||
|
||||
# [BOM conflict]
|
||||
def test_bom_conflict(self):
|
||||
source = codecs.BOM_UTF8 + self.create_source('latin-1')
|
||||
with self.assertRaises(SyntaxError):
|
||||
self.run_test(source)
|
||||
|
||||
|
||||
class EncodingTestPEP451(EncodingTest):
|
||||
|
||||
def load(self, loader):
|
||||
module = types.ModuleType(self.module_name)
|
||||
module.__spec__ = importlib.util.spec_from_loader(self.module_name, loader)
|
||||
loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
||||
(Frozen_EncodingTestPEP451,
|
||||
Source_EncodingTestPEP451
|
||||
) = util.test_both(EncodingTestPEP451, machinery=machinery)
|
||||
|
||||
|
||||
class EncodingTestPEP302(EncodingTest):
|
||||
|
||||
def load(self, loader):
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter('ignore', DeprecationWarning)
|
||||
return loader.load_module(self.module_name)
|
||||
|
||||
|
||||
(Frozen_EncodingTestPEP302,
|
||||
Source_EncodingTestPEP302
|
||||
) = util.test_both(EncodingTestPEP302, machinery=machinery)
|
||||
|
||||
|
||||
class LineEndingTest:
|
||||
|
||||
r"""Source written with the three types of line endings (\n, \r\n, \r)
|
||||
need to be readable [cr][crlf][lf]."""
|
||||
|
||||
def run_test(self, line_ending):
|
||||
module_name = '_temp'
|
||||
source_lines = [b"a = 42", b"b = -13", b'']
|
||||
source = line_ending.join(source_lines)
|
||||
with util.create_modules(module_name) as mapping:
|
||||
with open(mapping[module_name], 'wb') as file:
|
||||
file.write(source)
|
||||
loader = self.machinery.SourceFileLoader(module_name,
|
||||
mapping[module_name])
|
||||
return self.load(loader, module_name)
|
||||
|
||||
# [cr]
|
||||
def test_cr(self):
|
||||
self.run_test(b'\r')
|
||||
|
||||
# [crlf]
|
||||
def test_crlf(self):
|
||||
self.run_test(b'\r\n')
|
||||
|
||||
# [lf]
|
||||
def test_lf(self):
|
||||
self.run_test(b'\n')
|
||||
|
||||
|
||||
class LineEndingTestPEP451(LineEndingTest):
|
||||
|
||||
def load(self, loader, module_name):
|
||||
module = types.ModuleType(module_name)
|
||||
module.__spec__ = importlib.util.spec_from_loader(module_name, loader)
|
||||
loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
||||
(Frozen_LineEndingTestPEP451,
|
||||
Source_LineEndingTestPEP451
|
||||
) = util.test_both(LineEndingTestPEP451, machinery=machinery)
|
||||
|
||||
|
||||
class LineEndingTestPEP302(LineEndingTest):
|
||||
|
||||
def load(self, loader, module_name):
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter('ignore', DeprecationWarning)
|
||||
return loader.load_module(module_name)
|
||||
|
||||
|
||||
(Frozen_LineEndingTestPEP302,
|
||||
Source_LineEndingTestPEP302
|
||||
) = util.test_both(LineEndingTestPEP302, machinery=machinery)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Reference in New Issue
Block a user