compat.rs

Overview

The `compat.rs` file provides low-level compatibility utilities related to Python's C API internals, focusing primarily on reference counting and dictionary operations across multiple Python versions and build configurations. It contains constants and functions that help manage "immortal" Python objects (objects with special reference count handling) and exposes several unsafe extern "C" functions from the Python runtime to manipulate Python dictionaries and long integers efficiently.

These utilities are designed to abstract differences between Python versions (e.g., Python 3.12, 3.13, 3.14) and platform bit widths (32-bit vs 64-bit), enabling consistent behavior in a Rust environment interfacing with Python's native runtime (via `pyo3_ffi` bindings).


Detailed Explanation

Constants

The file defines several constants representing special reference count values used internally by CPython to mark "immortal" objects—Python objects that live for the entire program duration and have special reference counting semantics.


Function: _Py_IsImmortal

#[inline(always)]
#[allow(non_snake_case)]
pub(crate) unsafe fn _Py_IsImmortal(op: *mut pyo3_ffi::PyObject) -> core::ffi::c_int
unsafe {
    let is_immortal = _Py_IsImmortal(some_py_object_ptr);
    if is_immortal != 0 {
        println!("Object is immortal");
    }
}

Unsafe extern "C" function bindings

The file declares several external Python C API functions with conditional compilation based on Python version:

_PyDict_Next

pub fn _PyDict_Next(
    mp: *mut pyo3_ffi::PyObject,
    pos: *mut pyo3_ffi::Py_ssize_t,
    key: *mut *mut pyo3_ffi::PyObject,
    value: *mut *mut pyo3_ffi::PyObject,
    hash: *mut pyo3_ffi::Py_hash_t,
) -> core::ffi::c_int;

_PyDict_Contains_KnownHash (Python 3.10+)

pub fn _PyDict_Contains_KnownHash(
    op: *mut pyo3_ffi::PyObject,
    key: *mut pyo3_ffi::PyObject,
    hash: pyo3_ffi::Py_hash_t,
) -> core::ffi::c_int;

_PyDict_SetItem_KnownHash / _PyDict_SetItem_KnownHash_LockHeld

pub(crate) fn _PyDict_SetItem_KnownHash(
    mp: *mut pyo3_ffi::PyObject,
    name: *mut pyo3_ffi::PyObject,
    value: *mut pyo3_ffi::PyObject,
    hash: pyo3_ffi::Py_hash_t,
) -> core::ffi::c_int;
pub(crate) fn _PyDict_SetItem_KnownHash_LockHeld(
    mp: *mut pyo3_ffi::PyDictObject,
    name: *mut pyo3_ffi::PyObject,
    value: *mut pyo3_ffi::PyObject,
    hash: pyo3_ffi::Py_hash_t,
) -> core::ffi::c_int;

_PyLong_AsByteArray

pub(crate) fn _PyLong_AsByteArray(
    v: *mut pyo3_ffi::PyLongObject,
    bytes: *mut core::ffi::c_uchar,
    n: pyo3_ffi::Py_ssize_t,
    little_endian: core::ffi::c_int,
    is_signed: core::ffi::c_int,
    with_exceptions: core::ffi::c_int,
) -> core::ffi::c_int;
pub(crate) fn _PyLong_AsByteArray(
    v: *mut pyo3_ffi::PyLongObject,
    bytes: *mut core::ffi::c_uchar,
    n: pyo3_ffi::Py_ssize_t,
    little_endian: core::ffi::c_int,
    is_signed: core::ffi::c_int,
) -> core::ffi::c_int;

Implementation Details and Algorithms


Interaction with Other System Components


Visual Diagram

flowchart TD
    A[compat.rs] --> B[_Py_IsImmortal]
    A --> C[_PyDict_Next]
    A --> D[_PyDict_Contains_KnownHash]
    A --> E[_PyDict_SetItem_KnownHash / _LockHeld]
    A --> F[_PyLong_AsByteArray]

    subgraph Constants
        G[_Py_IMMORTAL_REFCNT_LOCAL]
        H[_Py_IMMORTAL_MINIMUM_REFCNT]
        I[_Py_IMMORTAL_REFCNT]
    end

    A --> Constants

    B -->|uses| G
    B -->|uses| H
    B -->|uses| I

    B -- Checks refcnt of --> J[PyObject]
    C -- Iterates --> K[PyDictObject]
    D -- Checks contains --> K
    E -- Sets item --> K
    F -- Converts --> L[PyLongObject]

Summary

The `compat.rs` file is a specialized compatibility layer bridging Rust code with Python's internal C API details. It mainly deals with immortal object reference counts and dictionary/long operations, providing version-aware, platform-specific constants and unsafe FFI function declarations. This enables safe and efficient interoperability with Python internals across multiple Python versions and configurations, crucial for projects embedding or extending Python in Rust.