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