loki.jit_build.jit

Utilities to facilitate Just-in-Time compilation for testing purposes.

Functions

clean_test(filepath)

Clean test directory based on JIT'ed source file.

jit_compile(source[, filepath, objname])

Generate, Just-in-Time compile and load a given item for interactive execution.

jit_compile_lib(sources, path, name[, wrap, ...])

Generate, just-in-time compile and load a set of items into a library and import dynamically into the Python runtime.

run_isolated(target, *args[, ...])

Execute target in a short-lived subprocess and return its result.

jit_compile(source, filepath=None, objname=None)

Generate, Just-in-Time compile and load a given item for interactive execution.

Parameters:
  • source (Sourcefile or Module or Subroutine) – The item to compile and load

  • filepath (str or Path, optional) – Path of the source file to write (default: hashed name in gettempdir())

  • objname (str, optional) – Return a specific object (module or subroutine) in source

jit_compile_lib(sources, path, name, wrap=None, builder=None)

Generate, just-in-time compile and load a set of items into a library and import dynamically into the Python runtime.

Parameters:
  • source (list) – Source items or filepaths to compile and add to lib

  • path (str or Path) – Basepath for on-the-fly creation of source files

  • name (str) – Name of created lib

  • wrap (list, optional) – File names to pass to f90wrap. Defaults to list of source files.

  • builder (Builder, optional) – Builder object to use for lib compilation and linking

run_isolated(target, *args, multiprocessing_context='fork', exit_after_result=False, **kwargs)

Execute target in a short-lived subprocess and return its result.

This is useful for tests that JIT-compile and import native extension modules, where unloading all linked Fortran/Python wrapper state from the current process is not reliable. Python exceptions raised by target are re-raised as RuntimeError with the child traceback; native crashes or explicit non-zero exits are reported via the child process exit code.

Parameters:
  • target (callable) – The callable to execute in the child process.

  • multiprocessing_context (str, optional) – Multiprocessing start method. Use 'fork' for low-overhead isolation when inherited process state is safe, or 'spawn' when the child must start without inherited native extension modules.

  • exit_after_result (bool, optional) – Exit the child process immediately after reporting the result, bypassing interpreter shutdown. This is useful for native extension tests where finalizers can crash after successful execution.

clean_test(filepath)

Clean test directory based on JIT’ed source file.