Skip to content
Back to top
View this page
Edit this page

# xonsh.imphooks

Import hooks for importing xonsh source files.

This module registers the hooks it defines when it is imported.

class xonsh.imphooks.XonshImportEventHook(*args, **kwargs)

  Implements the import hook for firing xonsh events on import.

  append_stack(fullname)

    A context manager for appending and then removing a name from the fullname stack.

  find_spec(fullname, path, target=None)

    Finds the spec for a xonsh module if it exists.

  invalidate_caches()

    An optional method for clearing the finder’s cache, if any. This method is used by importlib.invalidate_caches().

class xonsh.imphooks.XonshImportEventLoader(loader)

  A class that dispatches loader calls to another loader and fires relevant xonsh events.

  create_module(spec)

    Creates and returns the module object.

  exec_module(module)

    Executes the module in its own namespace.

  load_module(fullname)

    Return the loaded module.

    The module must be added to sys.modules and have import-related attributes set properly. The fullname is a str.

    ImportError is raised on failure.

    This method is deprecated in favor of loader.exec_module(). If exec_module() exists then it is used to provide a backwards-compatible functionality for this method.

class xonsh.imphooks.XonshImportHook(execer, *args, **kwargs)

  Implements the import hook for xonsh source files.

  create_module(spec)

    Create a xonsh module with the appropriate attributes.

  exec_module(module)

    Execute the module.

  find_spec(fullname, path, target=None)

    Finds the spec for a xonsh module if it exists.

  get_code(fullname)

    Gets the code object for a xonsh file.

  get_data(path)

    Gets the bytes for a path.

  get_filename(fullname)

    Returns the filename for a module’s fullname.

  get_source(fullname)

    Concrete implementation of InspectLoader.get_source.

  invalidate_caches()

    An optional method for clearing the finder’s cache, if any. This method is used by importlib.invalidate_caches().

  is_package(fullname)

    Concrete implementation of InspectLoader.is_package by checking if the path returned by get_filename has a filename of ‘__init__.py’.

  load_module(fullname)

    This method is deprecated.

  path_mtime(path)

    Return the (int) modification time for the path (str).

  path_stats(path)

    Return a metadata dict for the source pointed to by the path (str). Possible keys: - ‘mtime’ (mandatory) is the numeric timestamp of last source

    > code modification;

    - ‘size’ (optional) is the size in bytes of the source code.

  set_data(path, data)

    Write the bytes to the path (if possible).

    Accepts a str path and data as bytes.

    Any needed intermediary directories are to be created. If for some reason the file cannot be written because of permissions, fail silently.

  source_to_code(data, path, *, _optimize=-1)

    Return the code object compiled from source.

    The ‘data’ argument can be any object type that compile() supports.

xonsh.imphooks.find_source_encoding(src)

  Finds the source encoding given bytes representing a file by checking a special comment at either the first or second line of the source file. https://docs.python.org/3/howto/unicode.html#unicode-literals-in-python-source-code If no encoding is found, UTF-8 codec with BOM signature will be returned as it skips an optional UTF-8 encoded BOM at the start of the data and is otherwise the same as UTF-8 https://docs.python.org/3/library/codecs.html#module-encodings.utf_8_sig

xonsh.imphooks.install_hook(execer=<object object>)

  Install Xonsh import hooks in `sys.meta_path` in order for `.xsh` files to be importable and import events to be fired.

  Can safely be called many times, will be no-op if xonsh import hooks are already present.

xonsh.imphooks.install_import_hooks(execer=<object object>)

  Install Xonsh import hooks in `sys.meta_path` in order for `.xsh` files to be importable and import events to be fired.

  Can safely be called many times, will be no-op if xonsh import hooks are already present.