nautilus_core/python/
version.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Posei Systems Pty Ltd. All rights reserved.
3//  https://poseitrader.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::manual_let_else)]
17use pyo3::{prelude::*, types::PyTuple};
18
19/// Retrieves the Python interpreter version as a string.
20///
21/// # Panics
22///
23/// Panics if `version_info` cannot be downcast to a tuple or if tuple elements are missing.
24#[must_use]
25pub fn get_python_version() -> String {
26    Python::with_gil(|py| {
27        let sys = match py.import("sys") {
28            Ok(mod_sys) => mod_sys,
29            Err(_) => return "Unavailable (failed to import sys)".to_string(),
30        };
31
32        let version_info = match sys.getattr("version_info") {
33            Ok(info) => info,
34            Err(_) => return "Unavailable (version_info not found)".to_string(),
35        };
36
37        let version_tuple: &Bound<'_, PyTuple> = version_info
38            .downcast::<PyTuple>()
39            .expect("Failed to extract version_info");
40
41        let major = version_tuple
42            .get_item(0)
43            .expect("Failed to get major version")
44            .extract::<i32>()
45            .unwrap_or(-1);
46        let minor = version_tuple
47            .get_item(1)
48            .expect("Failed to get minor version")
49            .extract::<i32>()
50            .unwrap_or(-1);
51        let micro = version_tuple
52            .get_item(2)
53            .expect("Failed to get micro version")
54            .extract::<i32>()
55            .unwrap_or(-1);
56
57        if major == -1 || minor == -1 || micro == -1 {
58            "Unavailable (failed to extract version components)".to_string()
59        } else {
60            format!("{major}.{minor}.{micro}")
61        }
62    })
63}
64
65#[must_use]
66pub fn get_python_package_version(package_name: &str) -> String {
67    Python::with_gil(|py| match py.import(package_name) {
68        Ok(package) => match package.getattr("__version__") {
69            Ok(version_attr) => match version_attr.extract::<String>() {
70                Ok(version) => version,
71                Err(_) => "Unavailable (failed to extract version)".to_string(),
72            },
73            Err(_) => "Unavailable (__version__ attribute not found)".to_string(),
74        },
75        Err(_) => "Unavailable (failed to import package)".to_string(),
76    })
77}