Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_importlib/source/util.py @ 79691:05c8928362ee
Merge __next__ method link fixes with 3.3.
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Fri, 12 Oct 2012 13:46:37 +0300 |
| parents | 4cebcfc97774 |
| children | 5e8b377942f7 |
| rev | line source |
|---|---|
|
51236
d85ef26efd10
Rename importlib.test.support to importlib.test.util.
Brett Cannon <bcannon@gmail.com>
parents:
51234
diff
changeset
|
1 from .. import util |
|
51234
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
2 import contextlib |
| 60648 | 3 import errno |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
52077
diff
changeset
|
4 import functools |
|
51234
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
5 import imp |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
6 import os |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
7 import os.path |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
8 import sys |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
9 import tempfile |
|
51236
d85ef26efd10
Rename importlib.test.support to importlib.test.util.
Brett Cannon <bcannon@gmail.com>
parents:
51234
diff
changeset
|
10 from test import support |
|
51234
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
11 |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
12 |
|
53996
0495bc47cd08
Tests for case-senstivity were not being skipped for darwin when installed on a
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
13 def writes_bytecode_files(fxn): |
|
0495bc47cd08
Tests for case-senstivity were not being skipped for darwin when installed on a
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
14 """Decorator to protect sys.dont_write_bytecode from mutation and to skip |
|
0495bc47cd08
Tests for case-senstivity were not being skipped for darwin when installed on a
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
15 tests that require it to be set to False.""" |
|
0495bc47cd08
Tests for case-senstivity were not being skipped for darwin when installed on a
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
16 if sys.dont_write_bytecode: |
|
0495bc47cd08
Tests for case-senstivity were not being skipped for darwin when installed on a
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
17 return lambda *args, **kwargs: None |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
52077
diff
changeset
|
18 @functools.wraps(fxn) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
52077
diff
changeset
|
19 def wrapper(*args, **kwargs): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
52077
diff
changeset
|
20 original = sys.dont_write_bytecode |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
52077
diff
changeset
|
21 sys.dont_write_bytecode = False |
|
53996
0495bc47cd08
Tests for case-senstivity were not being skipped for darwin when installed on a
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
22 try: |
|
0495bc47cd08
Tests for case-senstivity were not being skipped for darwin when installed on a
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
23 to_return = fxn(*args, **kwargs) |
|
0495bc47cd08
Tests for case-senstivity were not being skipped for darwin when installed on a
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
24 finally: |
|
0495bc47cd08
Tests for case-senstivity were not being skipped for darwin when installed on a
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
25 sys.dont_write_bytecode = original |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
52077
diff
changeset
|
26 return to_return |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
52077
diff
changeset
|
27 return wrapper |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
52077
diff
changeset
|
28 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
52077
diff
changeset
|
29 |
| 60648 | 30 def ensure_bytecode_path(bytecode_path): |
| 31 """Ensure that the __pycache__ directory for PEP 3147 pyc file exists. | |
| 32 | |
| 33 :param bytecode_path: File system path to PEP 3147 pyc file. | |
| 34 """ | |
| 35 try: | |
| 36 os.mkdir(os.path.dirname(bytecode_path)) | |
| 37 except OSError as error: | |
| 38 if error.errno != errno.EEXIST: | |
| 39 raise | |
|
51234
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
40 |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
41 |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
42 @contextlib.contextmanager |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
43 def create_modules(*names): |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
44 """Temporarily create each named module with an attribute (named 'attr') |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
45 that contains the name passed into the context manager that caused the |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
46 creation of the module. |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
47 |
|
57116
93c752875033
Merged revisions 76113-76114 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
53996
diff
changeset
|
48 All files are created in a temporary directory returned by |
|
93c752875033
Merged revisions 76113-76114 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
53996
diff
changeset
|
49 tempfile.mkdtemp(). This directory is inserted at the beginning of |
|
51234
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
50 sys.path. When the context manager exits all created files (source and |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
51 bytecode) are explicitly deleted. |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
52 |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
53 No magic is performed when creating packages! This means that if you create |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
54 a module within a package you must also create the package's __init__ as |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
55 well. |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
56 |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
57 """ |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
58 source = 'attr = {0!r}' |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
59 created_paths = [] |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
60 mapping = {} |
|
57116
93c752875033
Merged revisions 76113-76114 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
53996
diff
changeset
|
61 state_manager = None |
|
93c752875033
Merged revisions 76113-76114 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
53996
diff
changeset
|
62 uncache_manager = None |
|
51234
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
63 try: |
|
57116
93c752875033
Merged revisions 76113-76114 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
53996
diff
changeset
|
64 temp_dir = tempfile.mkdtemp() |
|
51234
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
65 mapping['.root'] = temp_dir |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
66 import_names = set() |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
67 for name in names: |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
68 if not name.endswith('__init__'): |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
69 import_name = name |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
70 else: |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
71 import_name = name[:-len('.__init__')] |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
72 import_names.add(import_name) |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
73 if import_name in sys.modules: |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
74 del sys.modules[import_name] |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
75 name_parts = name.split('.') |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
76 file_path = temp_dir |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
77 for directory in name_parts[:-1]: |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
78 file_path = os.path.join(file_path, directory) |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
79 if not os.path.exists(file_path): |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
80 os.mkdir(file_path) |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
81 created_paths.append(file_path) |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
82 file_path = os.path.join(file_path, name_parts[-1] + '.py') |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
83 with open(file_path, 'w') as file: |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
84 file.write(source.format(name)) |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
85 created_paths.append(file_path) |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
86 mapping[name] = file_path |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
87 uncache_manager = util.uncache(*import_names) |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
88 uncache_manager.__enter__() |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
89 state_manager = util.import_state(path=[temp_dir]) |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
90 state_manager.__enter__() |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
91 yield mapping |
|
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
92 finally: |
|
57116
93c752875033
Merged revisions 76113-76114 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
53996
diff
changeset
|
93 if state_manager is not None: |
|
93c752875033
Merged revisions 76113-76114 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
53996
diff
changeset
|
94 state_manager.__exit__(None, None, None) |
|
93c752875033
Merged revisions 76113-76114 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
53996
diff
changeset
|
95 if uncache_manager is not None: |
|
93c752875033
Merged revisions 76113-76114 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
53996
diff
changeset
|
96 uncache_manager.__exit__(None, None, None) |
|
93c752875033
Merged revisions 76113-76114 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
53996
diff
changeset
|
97 support.rmtree(temp_dir) |
