Import hooks

koskari.importer

The koskari import system. Implements the import hook and loader for ki and kio files via Python’s importlib machinery.

class koskari.importer.KoskariSourceFileLoader(fullname, path)

Bases: SourceFileLoader

Loader for .ki source files and precompiled .kio artifacts, on the machinery’s source loader for its file access; the base is also the abc’s FileLoader, which this module does not import, since importlib.abc pulls in importlib.resources and seventy modules behind it at every interpreter start the .pth triggers.

exec_module uses exec_module_demand by default (demand path).

create_module(spec)

The koskari Module the module’s code runs in is the module importlib places in sys.modules: what the body defines, what its functions rebind, what Python assigns and what a circular import reads are one namespace

exec_module(module)

Execute the module.

async exec_module_async(module)
exec_module_demand(module)

Execute the module synchronously via the demand path (demand_call).

Same steps as exec_module_async but uses load_module_demand so no event loop or await is required.

get_code(fullname)

The code object runpy executes for python -m and run_module: a Python shim that loads this Koskari module under the executing name through the launcher and, under __main__, runs its entry point and exits with the status. The import path never calls this; exec_module loads the module directly

get_source(fullname)

Concrete implementation of InspectLoader.get_source.

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_target(module)

Initialise the module from its spec and name the file to load

async koskari.importer.import_module_async(fullname)

Import fullname under a running event loop using the async loader path

When the module loader provides exec_module_async (koskari .ki/.kio sources), that path is used so module load may suspend on await. Pure Python modules fall back to exec_module.

Parameters:

fullname – dotted module name to import

Returns:

loaded module object

Raises:
koskari.importer.install()

Register the Koskari directory hook on sys.path_hooks, just ahead of Python’s own FileFinder hook, and drop the cached per-directory finders so every directory is re-resolved through it. Calling install() when the hook is already present is a no-op.

Suppressed when the environment variable KOSKARI_NOIMPORTER is set to a non-zero value, or when the -X KOSKARI_NOIMPORTER Python xoption is set.

koskari.importer.is_installed()

Return True if the Koskari directory hook is in sys.path_hooks.

Returns:

bool

koskari.importer.source_for_stale_kio(kio_path)

The sibling .ki to load in place of kio_path, or None when the .kio stands.

A .kio with no sibling source stands. Beside a source it stands only when its stamp matches the source’s current mtime and size; an unstamped, unreadable, or wrong-ABI .kio yields to the source.

Parameters:

kio_path – path ending in .kio

Returns:

the .ki path, or None

koskari.importer.uninstall()

Remove the Koskari directory hook from sys.path_hooks and drop the cached per-directory finders built through it.

Calling uninstall() when the hook is not present is a no-op.