spack.util.imp package

Submodules

spack.util.imp.imp_importer module

Implementation of Spack imports that uses imp underneath.

imp is deprecated in newer versions of Python, but is the only option in Python 2.6.

spack.util.imp.imp_importer.import_lock()
spack.util.imp.imp_importer.load_source(full_name, path, prepend=None)

Import a Python module from source.

Load the source file and add it to sys.modules.

Parameters:
  • full_name (str) – full name of the module to be loaded
  • path (str) – path to the file that should be loaded
  • prepend (str, optional) – some optional code to prepend to the loaded module; e.g., can be used to inject import statements
Returns:

the loaded module

Return type:

(ModuleType)

spack.util.imp.imp_importer.prepend_open(f, *args, **kwargs)

Open a file for reading, but prepend with some text prepended

Arguments are same as for open(), with one keyword argument, text, specifying the text to prepend.

We have to write and read a tempfile for the imp-based importer, as the file argument to imp.load_source() requires a low-level file handle.

See the importlib-based importer for a faster way to do this in later versions of python.

spack.util.imp.importlib_importer module

Implementation of Spack imports that uses importlib underneath.

importlib is only fully implemented in Python 3.

class spack.util.imp.importlib_importer.PrependFileLoader(full_name, path, prepend=None)

Bases: _frozen_importlib_external.SourceFileLoader

get_data(path)

Return the data from path as raw bytes.

spack.util.imp.importlib_importer.load_source(full_name, path, prepend=None)

Import a Python module from source.

Load the source file and add it to sys.modules.

Parameters:
  • full_name (str) – full name of the module to be loaded
  • path (str) – path to the file that should be loaded
  • prepend (str, optional) – some optional code to prepend to the loaded module; e.g., can be used to inject import statements
Returns:

the loaded module

Return type:

(ModuleType)

Module contents

Consolidated module for all imports done by Spack.

Many parts of Spack have to import Python code. This utility package wraps Spack’s interface with Python’s import system.

We do this because Python’s import system is confusing and changes from Python version to Python version, and we should be able to adapt our approach to the underlying implementation.

Currently, this uses importlib.machinery where available and imp when importlib is not completely usable.