ยปCore Development>Code coverage>Lib/test/test_importlib/source/util.py

Python code coverage for Lib/test/test_importlib/source/util.py

#countcontent
1n/afrom .. import util
2n/aimport contextlib
3n/aimport errno
4n/aimport functools
5n/aimport os
6n/aimport os.path
7n/aimport sys
8n/aimport tempfile
9n/afrom test import support
10n/a
11n/a
12n/adef writes_bytecode_files(fxn):
13n/a """Decorator to protect sys.dont_write_bytecode from mutation and to skip
14n/a tests that require it to be set to False."""
15n/a if sys.dont_write_bytecode:
16n/a return lambda *args, **kwargs: None
17n/a @functools.wraps(fxn)
18n/a def wrapper(*args, **kwargs):
19n/a original = sys.dont_write_bytecode
20n/a sys.dont_write_bytecode = False
21n/a try:
22n/a to_return = fxn(*args, **kwargs)
23n/a finally:
24n/a sys.dont_write_bytecode = original
25n/a return to_return
26n/a return wrapper
27n/a
28n/a
29n/adef ensure_bytecode_path(bytecode_path):
30n/a """Ensure that the __pycache__ directory for PEP 3147 pyc file exists.
31n/a
32n/a :param bytecode_path: File system path to PEP 3147 pyc file.
33n/a """
34n/a try:
35n/a os.mkdir(os.path.dirname(bytecode_path))
36n/a except OSError as error:
37n/a if error.errno != errno.EEXIST:
38n/a raise
39n/a
40n/a
41n/a@contextlib.contextmanager
42n/adef create_modules(*names):
43n/a """Temporarily create each named module with an attribute (named 'attr')
44n/a that contains the name passed into the context manager that caused the
45n/a creation of the module.
46n/a
47n/a All files are created in a temporary directory returned by
48n/a tempfile.mkdtemp(). This directory is inserted at the beginning of
49n/a sys.path. When the context manager exits all created files (source and
50n/a bytecode) are explicitly deleted.
51n/a
52n/a No magic is performed when creating packages! This means that if you create
53n/a a module within a package you must also create the package's __init__ as
54n/a well.
55n/a
56n/a """
57n/a source = 'attr = {0!r}'
58n/a created_paths = []
59n/a mapping = {}
60n/a state_manager = None
61n/a uncache_manager = None
62n/a try:
63n/a temp_dir = tempfile.mkdtemp()
64n/a mapping['.root'] = temp_dir
65n/a import_names = set()
66n/a for name in names:
67n/a if not name.endswith('__init__'):
68n/a import_name = name
69n/a else:
70n/a import_name = name[:-len('.__init__')]
71n/a import_names.add(import_name)
72n/a if import_name in sys.modules:
73n/a del sys.modules[import_name]
74n/a name_parts = name.split('.')
75n/a file_path = temp_dir
76n/a for directory in name_parts[:-1]:
77n/a file_path = os.path.join(file_path, directory)
78n/a if not os.path.exists(file_path):
79n/a os.mkdir(file_path)
80n/a created_paths.append(file_path)
81n/a file_path = os.path.join(file_path, name_parts[-1] + '.py')
82n/a with open(file_path, 'w') as file:
83n/a file.write(source.format(name))
84n/a created_paths.append(file_path)
85n/a mapping[name] = file_path
86n/a uncache_manager = util.uncache(*import_names)
87n/a uncache_manager.__enter__()
88n/a state_manager = util.import_state(path=[temp_dir])
89n/a state_manager.__enter__()
90n/a yield mapping
91n/a finally:
92n/a if state_manager is not None:
93n/a state_manager.__exit__(None, None, None)
94n/a if uncache_manager is not None:
95n/a uncache_manager.__exit__(None, None, None)
96n/a support.rmtree(temp_dir)