nautilus_core/python/version.rs
1// -------------------------------------------------------------------------------------------------
2// Copyright (C) 2015-2025 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//! Functions for introspecting the running Python interpreter & installed packages.
17
18#![allow(
19 clippy::manual_let_else,
20 reason = "Prefer explicit control flow for error handling"
21)]
22use pyo3::{prelude::*, types::PyTuple};
23
24/// Retrieves the Python interpreter version as a string.
25///
26/// # Panics
27///
28/// Panics if `version_info` cannot be downcast to a tuple or if tuple elements are missing.
29#[must_use]
30pub fn get_python_version() -> String {
31 Python::attach(|py| {
32 let sys = match py.import("sys") {
33 Ok(mod_sys) => mod_sys,
34 Err(_) => return "Unavailable (failed to import sys)".to_string(),
35 };
36
37 let version_info = match sys.getattr("version_info") {
38 Ok(info) => info,
39 Err(_) => return "Unavailable (version_info not found)".to_string(),
40 };
41
42 let version_tuple: &Bound<'_, PyTuple> = version_info
43 .cast::<PyTuple>()
44 .expect("Failed to extract version_info");
45
46 let major = version_tuple
47 .get_item(0)
48 .expect("Failed to get major version")
49 .extract::<i32>()
50 .unwrap_or(-1);
51 let minor = version_tuple
52 .get_item(1)
53 .expect("Failed to get minor version")
54 .extract::<i32>()
55 .unwrap_or(-1);
56 let micro = version_tuple
57 .get_item(2)
58 .expect("Failed to get micro version")
59 .extract::<i32>()
60 .unwrap_or(-1);
61
62 if major == -1 || minor == -1 || micro == -1 {
63 "Unavailable (failed to extract version components)".to_string()
64 } else {
65 format!("{major}.{minor}.{micro}")
66 }
67 })
68}
69
70#[must_use]
71/// Attempt to retrieve the `__version__` attribute of a *Python* package.
72///
73/// When the requested package cannot be imported, or when it does not define a `__version__`
74/// attribute, the function returns a human-readable fallback string that starts with
75/// `"Unavailable"` so that downstream code can distinguish *real* version strings from error
76/// cases.
77///
78/// This helper is primarily intended for diagnostic/logging purposes inside the NautilusTrader
79/// Python bindings.
80pub fn get_python_package_version(package_name: &str) -> String {
81 Python::attach(|py| match py.import(package_name) {
82 Ok(package) => match package.getattr("__version__") {
83 Ok(version_attr) => match version_attr.extract::<String>() {
84 Ok(version) => version,
85 Err(_) => "Unavailable (failed to extract version)".to_string(),
86 },
87 Err(_) => "Unavailable (__version__ attribute not found)".to_string(),
88 },
89 Err(_) => "Unavailable (failed to import package)".to_string(),
90 })
91}