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