Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1655)

Side by Side Diff: Lib/importlib/test/source/util.py

Issue 842043: PEP 3147 implementation (Closed) Base URL: http://svn.python.org/view/*checkout*/python/branches/py3k/
Patch Set: Created 13 years, 12 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 from .. import util 1 from .. import util
2 import contextlib 2 import contextlib
3 import errno
3 import functools 4 import functools
4 import imp 5 import imp
5 import os 6 import os
6 import os.path 7 import os.path
7 import sys 8 import sys
8 import tempfile 9 import tempfile
9 from test import support 10 from test import support
10 11
11 12
12 def writes_bytecode_files(fxn): 13 def writes_bytecode_files(fxn):
13 """Decorator to protect sys.dont_write_bytecode from mutation and to skip 14 """Decorator to protect sys.dont_write_bytecode from mutation and to skip
14 tests that require it to be set to False.""" 15 tests that require it to be set to False."""
15 if sys.dont_write_bytecode: 16 if sys.dont_write_bytecode:
16 return lambda *args, **kwargs: None 17 return lambda *args, **kwargs: None
17 @functools.wraps(fxn) 18 @functools.wraps(fxn)
18 def wrapper(*args, **kwargs): 19 def wrapper(*args, **kwargs):
19 original = sys.dont_write_bytecode 20 original = sys.dont_write_bytecode
20 sys.dont_write_bytecode = False 21 sys.dont_write_bytecode = False
21 try: 22 try:
22 to_return = fxn(*args, **kwargs) 23 to_return = fxn(*args, **kwargs)
23 finally: 24 finally:
24 sys.dont_write_bytecode = original 25 sys.dont_write_bytecode = original
25 return to_return 26 return to_return
26 return wrapper 27 return wrapper
27 28
28 29
29 def bytecode_path(source_path): 30 def bytecode_path(source_path):
Antoine Pitrou 2010/04/04 15:55:30 Shouldn't it be removed, if it's just an empty wra
barry 2010/04/06 23:05:09 Done.
30 for suffix, _, type_ in imp.get_suffixes(): 31 return imp.cache_from_source(source_path)
31 if type_ == imp.PY_COMPILED: 32
32 bc_suffix = suffix 33
33 break 34 def ensure_bytecode_path(bytecode_path):
Antoine Pitrou 2010/04/04 15:55:30 How about a docstring?
barry 2010/04/06 23:05:09 Done.
34 else: 35 try:
35 raise ValueError("no bytecode suffix is defined") 36 os.mkdir(os.path.dirname(bytecode_path))
36 return os.path.splitext(source_path)[0] + bc_suffix 37 except OSError as error:
38 if error.errno != errno.EEXIST:
39 raise
37 40
38 41
39 @contextlib.contextmanager 42 @contextlib.contextmanager
40 def create_modules(*names): 43 def create_modules(*names):
41 """Temporarily create each named module with an attribute (named 'attr') 44 """Temporarily create each named module with an attribute (named 'attr')
42 that contains the name passed into the context manager that caused the 45 that contains the name passed into the context manager that caused the
43 creation of the module. 46 creation of the module.
44 47
45 All files are created in a temporary directory returned by 48 All files are created in a temporary directory returned by
46 tempfile.mkdtemp(). This directory is inserted at the beginning of 49 tempfile.mkdtemp(). This directory is inserted at the beginning of
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 uncache_manager.__enter__() 88 uncache_manager.__enter__()
86 state_manager = util.import_state(path=[temp_dir]) 89 state_manager = util.import_state(path=[temp_dir])
87 state_manager.__enter__() 90 state_manager.__enter__()
88 yield mapping 91 yield mapping
89 finally: 92 finally:
90 if state_manager is not None: 93 if state_manager is not None:
91 state_manager.__exit__(None, None, None) 94 state_manager.__exit__(None, None, None)
92 if uncache_manager is not None: 95 if uncache_manager is not None:
93 uncache_manager.__exit__(None, None, None) 96 uncache_manager.__exit__(None, None, None)
94 support.rmtree(temp_dir) 97 support.rmtree(temp_dir)
OLDNEW

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b