Skip to main content

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