Skip to main content

nautilus_core/python/
mod.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16#![allow(clippy::doc_markdown, reason = "Python docstrings")]
17
18//! Python bindings and interoperability built using [`PyO3`](https://pyo3.rs).
19
20#![allow(
21    deprecated,
22    reason = "pyo3-stub-gen currently relies on PyO3 initialization helpers marked as deprecated"
23)]
24//!
25//! This sub-module groups together the Rust code that is *only* required when compiling the
26//! `python` feature flag. It provides thin adapters so that NautilusTrader functionality can be
27//! consumed from the `nautilus_trader` Python package without sacrificing type-safety or
28//! performance.
29
30pub mod casing;
31pub mod datetime;
32pub mod enums;
33pub mod params;
34pub mod parsing;
35pub mod serialization;
36/// String manipulation utilities for Python.
37pub mod string;
38pub mod uuid;
39pub mod version;
40
41use std::fmt::Display;
42
43use pyo3::{
44    Py,
45    conversion::IntoPyObjectExt,
46    exceptions::{
47        PyException, PyKeyError, PyNotImplementedError, PyRuntimeError, PyTypeError, PyValueError,
48    },
49    prelude::*,
50    types::PyString,
51    wrap_pyfunction,
52};
53use pyo3_stub_gen::derive::gen_stub_pyfunction;
54
55use crate::{
56    UUID4,
57    consts::{NAUTILUS_USER_AGENT, NAUTILUS_VERSION},
58    datetime::{
59        MILLISECONDS_IN_SECOND, NANOSECONDS_IN_MICROSECOND, NANOSECONDS_IN_MILLISECOND,
60        NANOSECONDS_IN_SECOND,
61    },
62};
63
64/// Safely clones a Python object by acquiring the GIL and properly managing reference counts.
65///
66/// This function exists to break reference cycles between Rust and Python that can occur
67/// when using `Arc<Py<PyAny>>` in callback-holding structs. The original design wrapped
68/// Python callbacks in `Arc` for thread-safe sharing, but this created circular references:
69///
70/// 1. Rust `Arc` holds Python objects → increases Python reference count.
71/// 2. Python objects might reference Rust objects → creates cycles.
72/// 3. Neither side can be garbage collected → memory leak.
73///
74/// By using plain `Py<PyAny>` with GIL-based cloning instead of `Arc<Py<PyAny>>`, we:
75/// - Avoid circular references between Rust and Python memory management.
76/// - Ensure proper Python reference counting under the GIL.
77/// - Allow both Rust and Python garbage collectors to work correctly.
78#[must_use]
79pub fn clone_py_object(obj: &Py<PyAny>) -> Py<PyAny> {
80    Python::attach(|py| obj.clone_ref(py))
81}
82
83/// Calls a Python callback with a single argument, logging any errors.
84pub fn call_python(py: Python, callback: &Py<PyAny>, py_obj: Py<PyAny>) {
85    if let Err(e) = callback.call1(py, (py_obj,)) {
86        log::error!("Error calling Python: {e}");
87    }
88}
89
90/// Schedules a Python callback on the event loop thread via `call_soon_threadsafe`.
91///
92/// This must be used instead of [`call_python`] when invoking Python callbacks
93/// from Tokio worker threads, since Python callbacks that enter the kernel
94/// (e.g. via `MessageBus.send`) must run on the asyncio event loop thread.
95pub fn call_python_threadsafe(
96    py: Python,
97    call_soon: &Py<PyAny>,
98    callback: &Py<PyAny>,
99    py_obj: Py<PyAny>,
100) {
101    if let Err(e) = call_soon.call1(py, (callback, py_obj)) {
102        log::error!("Error scheduling Python callback on event loop: {e}");
103    }
104}
105
106/// Extend `IntoPyObjectExt` helper trait to unwrap `Py<PyAny>` after conversion.
107pub trait IntoPyObjectNautilusExt<'py>: IntoPyObjectExt<'py> {
108    /// Convert `self` into a [`Py<PyAny>`] while *panicking* if the conversion fails.
109    ///
110    /// This is a convenience wrapper around [`IntoPyObjectExt::into_py_any`] that avoids the
111    /// cumbersome `Result` handling when we are certain that the conversion cannot fail (for
112    /// instance when we are converting primitives or other types that already implement the
113    /// necessary PyO3 traits).
114    #[inline]
115    fn into_py_any_unwrap(self, py: Python<'py>) -> Py<PyAny> {
116        self.into_py_any(py)
117            .expect("Failed to convert type to Py<PyAny>")
118    }
119}
120
121impl<'py, T> IntoPyObjectNautilusExt<'py> for T where T: IntoPyObjectExt<'py> {}
122
123/// Gets the type name for the given Python `obj`.
124///
125/// # Errors
126///
127/// Returns a error if accessing the type name fails.
128pub fn get_pytype_name<'py>(obj: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyString>> {
129    obj.get_type().name()
130}
131
132/// Converts any type that implements `Display` to a Python `ValueError`.
133pub fn to_pyvalue_err(e: impl Display) -> PyErr {
134    PyValueError::new_err(e.to_string())
135}
136
137/// Converts any type that implements `Display` to a Python `TypeError`.
138pub fn to_pytype_err(e: impl Display) -> PyErr {
139    PyTypeError::new_err(e.to_string())
140}
141
142/// Converts any type that implements `Display` to a Python `RuntimeError`.
143pub fn to_pyruntime_err(e: impl Display) -> PyErr {
144    PyRuntimeError::new_err(e.to_string())
145}
146
147/// Converts any type that implements `Display` to a Python `KeyError`.
148pub fn to_pykey_err(e: impl Display) -> PyErr {
149    PyKeyError::new_err(e.to_string())
150}
151
152/// Converts any type that implements `Display` to a Python `Exception`.
153pub fn to_pyexception(e: impl Display) -> PyErr {
154    PyException::new_err(e.to_string())
155}
156
157/// Converts any type that implements `Display` to a Python `NotImplementedError`.
158pub fn to_pynotimplemented_err(e: impl Display) -> PyErr {
159    PyNotImplementedError::new_err(e.to_string())
160}
161
162/// Return a value indicating whether the `obj` is a `PyCapsule`.
163///
164/// Parameters
165/// ----------
166/// obj : Any
167///     The object to check.
168///
169/// Returns
170/// -------
171/// bool
172#[gen_stub_pyfunction(module = "nautilus_trader.core")]
173#[pyfunction(name = "is_pycapsule")]
174#[allow(
175    clippy::needless_pass_by_value,
176    reason = "Python FFI requires owned types"
177)]
178#[allow(unsafe_code)]
179fn py_is_pycapsule(obj: Py<PyAny>) -> bool {
180    // SAFETY: obj.as_ptr() returns a valid Python object pointer
181    unsafe {
182        // PyCapsule_CheckExact checks if the object is exactly a PyCapsule
183        pyo3::ffi::PyCapsule_CheckExact(obj.as_ptr()) != 0
184    }
185}
186
187/// Loaded as `nautilus_pyo3.core`.
188///
189/// # Errors
190///
191/// Returns a `PyErr` if registering any module components fails.
192#[pymodule]
193#[rustfmt::skip]
194pub fn core(_: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
195    m.add(stringify!(NAUTILUS_VERSION), NAUTILUS_VERSION)?;
196    m.add(stringify!(NAUTILUS_USER_AGENT), NAUTILUS_USER_AGENT)?;
197    m.add(stringify!(MILLISECONDS_IN_SECOND), MILLISECONDS_IN_SECOND)?;
198    m.add(stringify!(NANOSECONDS_IN_SECOND), NANOSECONDS_IN_SECOND)?;
199    m.add(stringify!(NANOSECONDS_IN_MILLISECOND), NANOSECONDS_IN_MILLISECOND)?;
200    m.add(stringify!(NANOSECONDS_IN_MICROSECOND), NANOSECONDS_IN_MICROSECOND)?;
201    m.add_class::<UUID4>()?;
202    m.add_function(wrap_pyfunction!(py_is_pycapsule, m)?)?;
203    m.add_function(wrap_pyfunction!(casing::py_convert_to_snake_case, m)?)?;
204    m.add_function(wrap_pyfunction!(string::py_mask_api_key, m)?)?;
205    m.add_function(wrap_pyfunction!(datetime::py_secs_to_nanos, m)?)?;
206    m.add_function(wrap_pyfunction!(datetime::py_secs_to_millis, m)?)?;
207    m.add_function(wrap_pyfunction!(datetime::py_millis_to_nanos, m)?)?;
208    m.add_function(wrap_pyfunction!(datetime::py_micros_to_nanos, m)?)?;
209    m.add_function(wrap_pyfunction!(datetime::py_nanos_to_secs, m)?)?;
210    m.add_function(wrap_pyfunction!(datetime::py_nanos_to_millis, m)?)?;
211    m.add_function(wrap_pyfunction!(datetime::py_nanos_to_micros, m)?)?;
212    m.add_function(wrap_pyfunction!(datetime::py_unix_nanos_to_iso8601, m)?)?;
213    m.add_function(wrap_pyfunction!(datetime::py_last_weekday_nanos, m)?)?;
214    m.add_function(wrap_pyfunction!(datetime::py_is_within_last_24_hours, m)?)?;
215    Ok(())
216}