obtype.rs


Overview

The `obtype.rs` file defines functionality to classify Python objects into a well-defined internal enumeration of object types (`ObType`). It provides efficient runtime type inspection and categorization of Python objects interfaced via the Python C API (through `pyo3_ffi`). This classification is critical for serialization and processing workflows where behavior depends on the Python object type.

The core of the file is the `ObType` enum representing various Python data types and the functions `pyobject_to_obtype` and `pyobject_to_obtype_unlikely` which determine the appropriate `ObType` for a given Python object pointer (`*mut PyObject`).


Detailed Documentation

Enum: ObType

#[repr(u32)]
pub(crate) enum ObType {
    Str,
    Int,
    Bool,
    None,
    Float,
    List,
    Dict,
    Datetime,
    Date,
    Time,
    Tuple,
    Uuid,
    Dataclass,
    NumpyScalar,
    NumpyArray,
    Enum,
    StrSubclass,
    Fragment,
    Unknown,
}

Function: pyobject_to_obtype

pub(crate) fn pyobject_to_obtype(obj: *mut pyo3_ffi::PyObject, opts: Opt) -> ObType
let py_obj: *mut pyo3_ffi::PyObject = /* obtained from Python */;
let opts = Opt::default();
let obj_type = pyobject_to_obtype(py_obj, opts);
match obj_type {
    ObType::Int => println!("Object is an integer"),
    ObType::Str => println!("Object is a string"),
    _ => println!("Object type is other"),
}

Function: pyobject_to_obtype_unlikely

#[cfg_attr(feature = "optimize", optimize(size))]
#[inline(never)]
pub(crate) fn pyobject_to_obtype_unlikely(
    ob_type: *mut pyo3_ffi::PyTypeObject,
    opts: Opt,
) -> ObType

Implementation Details and Algorithms


Interaction with Other Parts of the System


Visual Diagram: Structure of obtype.rs

classDiagram
    class ObType {
        <<enum>>
        +Str
        +Int
        +Bool
        +None
        +Float
        +List
        +Dict
        +Datetime
        +Date
        +Time
        +Tuple
        +Uuid
        +Dataclass
        +NumpyScalar
        +NumpyArray
        +Enum
        +StrSubclass
        +Fragment
        +Unknown
    }

    class pyobject_to_obtype {
        +obj: *mut PyObject
        +opts: Opt
        +-> ObType
    }

    class pyobject_to_obtype_unlikely {
        +ob_type: *mut PyTypeObject
        +opts: Opt
        +-> ObType
    }

    pyobject_to_obtype --> ObType : returns
    pyobject_to_obtype_unlikely --> ObType : returns
    pyobject_to_obtype "1" --> "1" pyobject_to_obtype_unlikely : calls on miss

Summary

The `obtype.rs` file is a utility module that classifies Python objects into an internal `ObType` enum by analyzing Python type pointers and options flags. It supports both common and specialized Python types, including subclass detection, dataclasses, enums, and NumPy types. Critical to serialization and runtime type-dependent logic, this module balances performance via a fast path and extensibility with a comprehensive slow path. It interfaces closely with Python internals through the `pyo3_ffi` bindings and coordinates with other modules handling options and serialization specifics.