Back to index

python3.2  3.2.2
Functions
importlib.test.source.util Namespace Reference

Functions

def writes_bytecode_files
def ensure_bytecode_path
def create_modules

Function Documentation

Temporarily create each named module with an attribute (named 'attr')
that contains the name passed into the context manager that caused the
creation of the module.

All files are created in a temporary directory returned by
tempfile.mkdtemp(). This directory is inserted at the beginning of
sys.path. When the context manager exits all created files (source and
bytecode) are explicitly deleted.

No magic is performed when creating packages! This means that if you create
a module within a package you must also create the package's __init__ as
well.

Definition at line 43 of file util.py.

00043 
00044 def create_modules(*names):
00045     """Temporarily create each named module with an attribute (named 'attr')
00046     that contains the name passed into the context manager that caused the
00047     creation of the module.
00048 
00049     All files are created in a temporary directory returned by
00050     tempfile.mkdtemp(). This directory is inserted at the beginning of
00051     sys.path. When the context manager exits all created files (source and
00052     bytecode) are explicitly deleted.
00053 
00054     No magic is performed when creating packages! This means that if you create
00055     a module within a package you must also create the package's __init__ as
00056     well.
00057 
00058     """
00059     source = 'attr = {0!r}'
00060     created_paths = []
00061     mapping = {}
00062     state_manager = None
00063     uncache_manager = None
00064     try:
00065         temp_dir = tempfile.mkdtemp()
00066         mapping['.root'] = temp_dir
00067         import_names = set()
00068         for name in names:
00069             if not name.endswith('__init__'):
00070                 import_name = name
00071             else:
00072                 import_name = name[:-len('.__init__')]
00073             import_names.add(import_name)
00074             if import_name in sys.modules:
00075                 del sys.modules[import_name]
00076             name_parts = name.split('.')
00077             file_path = temp_dir
00078             for directory in name_parts[:-1]:
00079                 file_path = os.path.join(file_path, directory)
00080                 if not os.path.exists(file_path):
00081                     os.mkdir(file_path)
00082                     created_paths.append(file_path)
00083             file_path = os.path.join(file_path, name_parts[-1] + '.py')
00084             with open(file_path, 'w') as file:
00085                 file.write(source.format(name))
00086             created_paths.append(file_path)
00087             mapping[name] = file_path
00088         uncache_manager = util.uncache(*import_names)
00089         uncache_manager.__enter__()
00090         state_manager = util.import_state(path=[temp_dir])
00091         state_manager.__enter__()
00092         yield mapping
00093     finally:
00094         if state_manager is not None:
00095             state_manager.__exit__(None, None, None)
00096         if uncache_manager is not None:
00097             uncache_manager.__exit__(None, None, None)
00098         support.rmtree(temp_dir)

Here is the call graph for this function:

Ensure that the __pycache__ directory for PEP 3147 pyc file exists.

:param bytecode_path: File system path to PEP 3147 pyc file.

Definition at line 30 of file util.py.

00030 
00031 def ensure_bytecode_path(bytecode_path):
00032     """Ensure that the __pycache__ directory for PEP 3147 pyc file exists.
00033 
00034     :param bytecode_path: File system path to PEP 3147 pyc file.
00035     """
00036     try:
00037         os.mkdir(os.path.dirname(bytecode_path))
00038     except OSError as error:
00039         if error.errno != errno.EEXIST:
00040             raise
00041 
00042 
@contextlib.contextmanager
Decorator to protect sys.dont_write_bytecode from mutation and to skip
tests that require it to be set to False.

Definition at line 13 of file util.py.

00013 
00014 def writes_bytecode_files(fxn):
00015     """Decorator to protect sys.dont_write_bytecode from mutation and to skip
00016     tests that require it to be set to False."""
00017     if sys.dont_write_bytecode:
00018         return lambda *args, **kwargs: None
00019     @functools.wraps(fxn)
00020     def wrapper(*args, **kwargs):
00021         original = sys.dont_write_bytecode
00022         sys.dont_write_bytecode = False
00023         try:
00024             to_return = fxn(*args, **kwargs)
00025         finally:
00026             sys.dont_write_bytecode = original
00027         return to_return
00028     return wrapper
00029 

Here is the call graph for this function: