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

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

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