Browse Source
Issue #19721: Consolidate test_importlib utility code into a single
Issue #19721: Consolidate test_importlib utility code into a single
module.pull/9921/head
25 changed files with 249 additions and 270 deletions
-
19Lib/test/test_importlib/builtin/test_finder.py
-
25Lib/test/test_importlib/builtin/test_loader.py
-
7Lib/test/test_importlib/builtin/util.py
-
7Lib/test/test_importlib/extension/test_case_sensitivity.py
-
11Lib/test/test_importlib/extension/test_finder.py
-
31Lib/test/test_importlib/extension/test_loader.py
-
9Lib/test/test_importlib/extension/test_path_hook.py
-
19Lib/test/test_importlib/extension/util.py
-
5Lib/test/test_importlib/import_/test___loader__.py
-
7Lib/test/test_importlib/import_/test___package__.py
-
5Lib/test/test_importlib/import_/test_api.py
-
5Lib/test/test_importlib/import_/test_caching.py
-
5Lib/test/test_importlib/import_/test_fromlist.py
-
7Lib/test/test_importlib/import_/test_meta_path.py
-
3Lib/test/test_importlib/import_/test_packages.py
-
5Lib/test/test_importlib/import_/test_path.py
-
3Lib/test/test_importlib/import_/test_relative_imports.py
-
20Lib/test/test_importlib/import_/util.py
-
3Lib/test/test_importlib/source/test_case_sensitivity.py
-
61Lib/test/test_importlib/source/test_file_loader.py
-
11Lib/test/test_importlib/source/test_finder.py
-
3Lib/test/test_importlib/source/test_path_hook.py
-
5Lib/test/test_importlib/source/test_source_encoding.py
-
96Lib/test/test_importlib/source/util.py
-
147Lib/test/test_importlib/util.py
@ -1,7 +0,0 @@ |
|||
import sys |
|||
|
|||
assert 'errno' in sys.builtin_module_names |
|||
NAME = 'errno' |
|||
|
|||
assert 'importlib' not in sys.builtin_module_names |
|||
BAD_NAME = 'importlib' |
|||
@ -1,19 +0,0 @@ |
|||
from importlib import machinery |
|||
import os |
|||
import sys |
|||
|
|||
PATH = None |
|||
EXT = None |
|||
FILENAME = None |
|||
NAME = '_testcapi' |
|||
try: |
|||
for PATH in sys.path: |
|||
for EXT in machinery.EXTENSION_SUFFIXES: |
|||
FILENAME = NAME + EXT |
|||
FILEPATH = os.path.join(PATH, FILENAME) |
|||
if os.path.exists(os.path.join(PATH, FILENAME)): |
|||
raise StopIteration |
|||
else: |
|||
PATH = EXT = FILENAME = FILEPATH = None |
|||
except StopIteration: |
|||
pass |
|||
@ -1,20 +0,0 @@ |
|||
from .. import util |
|||
|
|||
frozen_importlib, source_importlib = util.import_importlib('importlib') |
|||
|
|||
import builtins |
|||
import functools |
|||
import importlib |
|||
import unittest |
|||
|
|||
|
|||
__import__ = staticmethod(builtins.__import__), staticmethod(source_importlib.__import__) |
|||
|
|||
|
|||
def mock_path_hook(*entries, importer): |
|||
"""A mock sys.path_hooks entry.""" |
|||
def hook(entry): |
|||
if entry not in entries: |
|||
raise ImportError |
|||
return importer |
|||
return hook |
|||
@ -1,96 +0,0 @@ |
|||
from .. import util |
|||
import contextlib |
|||
import errno |
|||
import functools |
|||
import os |
|||
import os.path |
|||
import sys |
|||
import tempfile |
|||
from test import support |
|||
|
|||
|
|||
def writes_bytecode_files(fxn): |
|||
"""Decorator to protect sys.dont_write_bytecode from mutation and to skip |
|||
tests that require it to be set to False.""" |
|||
if sys.dont_write_bytecode: |
|||
return lambda *args, **kwargs: None |
|||
@functools.wraps(fxn) |
|||
def wrapper(*args, **kwargs): |
|||
original = sys.dont_write_bytecode |
|||
sys.dont_write_bytecode = False |
|||
try: |
|||
to_return = fxn(*args, **kwargs) |
|||
finally: |
|||
sys.dont_write_bytecode = original |
|||
return to_return |
|||
return wrapper |
|||
|
|||
|
|||
def ensure_bytecode_path(bytecode_path): |
|||
"""Ensure that the __pycache__ directory for PEP 3147 pyc file exists. |
|||
|
|||
:param bytecode_path: File system path to PEP 3147 pyc file. |
|||
""" |
|||
try: |
|||
os.mkdir(os.path.dirname(bytecode_path)) |
|||
except OSError as error: |
|||
if error.errno != errno.EEXIST: |
|||
raise |
|||
|
|||
|
|||
@contextlib.contextmanager |
|||
def create_modules(*names): |
|||
"""Temporarily create each named module with an attribute (named 'attr') |
|||
that contains the name passed into the context manager that caused the |
|||
creation of the module. |
|||
|
|||
All files are created in a temporary directory returned by |
|||
tempfile.mkdtemp(). This directory is inserted at the beginning of |
|||
sys.path. When the context manager exits all created files (source and |
|||
bytecode) are explicitly deleted. |
|||
|
|||
No magic is performed when creating packages! This means that if you create |
|||
a module within a package you must also create the package's __init__ as |
|||
well. |
|||
|
|||
""" |
|||
source = 'attr = {0!r}' |
|||
created_paths = [] |
|||
mapping = {} |
|||
state_manager = None |
|||
uncache_manager = None |
|||
try: |
|||
temp_dir = tempfile.mkdtemp() |
|||
mapping['.root'] = temp_dir |
|||
import_names = set() |
|||
for name in names: |
|||
if not name.endswith('__init__'): |
|||
import_name = name |
|||
else: |
|||
import_name = name[:-len('.__init__')] |
|||
import_names.add(import_name) |
|||
if import_name in sys.modules: |
|||
del sys.modules[import_name] |
|||
name_parts = name.split('.') |
|||
file_path = temp_dir |
|||
for directory in name_parts[:-1]: |
|||
file_path = os.path.join(file_path, directory) |
|||
if not os.path.exists(file_path): |
|||
os.mkdir(file_path) |
|||
created_paths.append(file_path) |
|||
file_path = os.path.join(file_path, name_parts[-1] + '.py') |
|||
with open(file_path, 'w') as file: |
|||
file.write(source.format(name)) |
|||
created_paths.append(file_path) |
|||
mapping[name] = file_path |
|||
uncache_manager = util.uncache(*import_names) |
|||
uncache_manager.__enter__() |
|||
state_manager = util.import_state(path=[temp_dir]) |
|||
state_manager.__enter__() |
|||
yield mapping |
|||
finally: |
|||
if state_manager is not None: |
|||
state_manager.__exit__(None, None, None) |
|||
if uncache_manager is not None: |
|||
uncache_manager.__exit__(None, None, None) |
|||
support.rmtree(temp_dir) |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue