loki.jit_build.jit module

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

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_and_run(source, *args, filepath=None, objname=None, isolated=True, multiprocessing_context='fork', exit_after_result=False, timeout=10, **kwargs)

JIT-compile a source item and execute the selected entry point.

The complete compile, load, and execution cycle can optionally run in a short-lived subprocess to isolate native extension module state from the parent test process. See run_isolated for available options for arguments.

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

  • *args (tuple) – Positional arguments passed to the compiled entry point.

  • filepath (str or Path, optional) – Path of the source file to write.

  • objname (str, optional) – Name of the compiled object to execute. Defaults to source.name when available.

  • isolated (bool, optional) – Run the compile-and-execute cycle via run_isolated when enabled (default: True).

  • multiprocessing_context (str, optional) – Multiprocessing start method used for isolated execution.

  • exit_after_result (bool, optional) – Exit the isolated child immediately after reporting the result.

  • timeout (int or float, optional) – Maximum time in seconds to wait for isolated execution (default: 10).

  • **kwargs (dict) – Keyword arguments passed to the compiled entry point.

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, timeout=None, **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.

  • timeout (int or float, optional) – Maximum time in seconds to wait for the isolated child process.

clean_test(filepath)

Clean test directory based on JIT’ed source file.