Skip to main content

A small Python library to seamlessly introspect interpreter features at runtime

Project description

runtime_introspect

PyPI Supported Python Versions

A small Python library to introspect interpreter features in a couple of portable lines of code. The core functionality is to produce diagnostics for which optional features are (un)available, the state they are in (enabled, disabled, active, or inactive), and why. It utilizes implementation specific APIs to abstract away the pain of writing portable code that runs on any configuration.

Scope and development status

This project is currently in alpha, with a flexible scope; Only CPython interpreter features (Free-threading and JIT) are supported at the moment. However, the library design leaves open the possibility to add support for other Python implementations. If you spot something missing please open a feature request or a pull request, contributions are always welcome !

Installation

python -m pip install runtime-introspect

Usage

Create diagnostics

Here's how to produce a complete diagnostic

from runtime_introspect import runtime_feature_set

fs = runtime_feature_set()
print("\n".join(fs.diagnostics()))

example output:

free-threading: unavailable (this interpreter was built without free-threading support)
JIT: disabled (envvar PYTHON_JIT is unset)

Since runtime-introspect 0.3.0, FeatureSet.snapshot and FeatureSet.diagnostics will also accept a features list to fine-tune exactly which features to inspect and report on.

Inspect specific features

To make code conditional on the availability of a specific feature, use FeatureSet.supports. For instance

from runtime_introspect import runtime_feature_set

fs = runtime_feature_set()
if fs.supports("free-threading"):
    ... # cool multi-threading stuff
else:
    ... # also cool, but single-threaded stuff

As of runtime-introspect 0.4.0, supported feature names include

  • 'free-threading'
  • 'JIT'

Build a pytest header

You can use this library to customize pytest so that test session headers showcase the runtime feature set at startup. For instance

# conftest.py
import sys
import textwrap
from runtime_introspect import runtime_feature_set

# ...

def pytest_report_header(config, start_path) -> list[str]:
    fs = runtime_feature_set()
    if diagnostics := fs.diagnostics():
        return [
            "Runtime optional features state (snapshot):",
            textwrap.indent("\n".join(diagnostics), "  "),
        ]
    else:
        return []

example output (truncated)

===================================== test session starts ======================================
platform darwin -- Python 3.13.6, pytest-8.4.1, pluggy-1.6.0
Runtime optional features state (snapshot):
  free-threading: unavailable (this interpreter was built without free-threading support)
  JIT: undetermined (no introspection API known for Python 3.13)
...

Command Line Interface (CLI) examples

Outputs may (really, should) vary depending on which python interpreter is active and how it was invoked

❯ python3.13 -m runtime_introspect
free-threading: unavailable (this interpreter was built without free-threading support)
JIT: undetermined (no introspection API known for Python 3.13)
❯ PYTHON_JIT=1 python3.14 -m runtime_introspect
free-threading: unavailable (this interpreter was built without free-threading support)
JIT: enabled (by envvar PYTHON_JIT=1)
❯ python3.14t -X gil=0 -m runtime_introspect
free-threading: enabled (forced by command line option -Xgil=0)
JIT: unavailable (this interpreter was built without JIT compilation support)

Run python -m runtime_introspect --help to browse additional options.

Additional resources

Many more details can be inspected with the standard library sysconfig CLI (python -m sysconfig).

Also refer to python-introspection for a similar tool with a different approach and focus.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

runtime_introspect-0.4.1.tar.gz (24.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

runtime_introspect-0.4.1-py3-none-any.whl (20.8 kB view details)

Uploaded Python 3

File details

Details for the file runtime_introspect-0.4.1.tar.gz.

File metadata

  • Download URL: runtime_introspect-0.4.1.tar.gz
  • Upload date:
  • Size: 24.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for runtime_introspect-0.4.1.tar.gz
Algorithm Hash digest
SHA256 e1c6932c4021d432c81ed577424af20e4cb1e20eed93d45b2950cf8be2da9b45
MD5 7bffbe2eb663fc5cdf0b725e0e3a5c77
BLAKE2b-256 77b9d4feffda2f30e904b44d00cfc186227923108c7c74b51d7cc74dca746022

See more details on using hashes here.

Provenance

The following attestation bundles were made for runtime_introspect-0.4.1.tar.gz:

Publisher: cd.yml on neutrinoceros/runtime-introspect

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file runtime_introspect-0.4.1-py3-none-any.whl.

File metadata

File hashes

Hashes for runtime_introspect-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 22b3703ef9f0f3e5207339b549b2d785505cb6511d7f090eb2fa268589c7f0f0
MD5 9cb49909756822e037bce7233899bda2
BLAKE2b-256 e48deae181160558368a946f185a7d422f448433617d90af74cf899f5d0be2f5

See more details on using hashes here.

Provenance

The following attestation bundles were made for runtime_introspect-0.4.1-py3-none-any.whl:

Publisher: cd.yml on neutrinoceros/runtime-introspect

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page