From c5673883254401deac9ad931916d533d5976184a Mon Sep 17 00:00:00 2001 From: Bas Schoenmaeckers Date: Mon, 6 Jul 2026 14:57:33 +0200 Subject: [PATCH] Add more build-info functions to c-api --- crates/capi/src/pylifecycle.rs | 33 +++++++++++++++++++++++++++++++++ crates/vm/src/stdlib/sys.rs | 9 +++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/crates/capi/src/pylifecycle.rs b/crates/capi/src/pylifecycle.rs index de673b33f0a..5a51f4a31bf 100644 --- a/crates/capi/src/pylifecycle.rs +++ b/crates/capi/src/pylifecycle.rs @@ -4,6 +4,7 @@ use crate::pystate::ensure_thread_has_vm_attached; use alloc::ffi::CString; use core::ffi::{c_char, c_int, c_ulong}; use rustpython_vm::common::rc::PyRc; +use rustpython_vm::stdlib::sys; use rustpython_vm::version::{MAJOR, MICRO, MINOR, VERSION_HEX}; use rustpython_vm::vm::thread::ThreadedVirtualMachine; use rustpython_vm::{Context, Interpreter}; @@ -80,6 +81,38 @@ pub extern "C" fn Py_GetVersion() -> *const c_char { VERSION.as_ptr() } +#[unsafe(no_mangle)] +pub extern "C" fn Py_GetBuildInfo() -> *const c_char { + static BUILD_INFO: LazyLock = LazyLock::new(|| { + CString::new(sys::BUILD_INFO).expect("build info must not contain interior NULs") + }); + BUILD_INFO.as_ptr() +} + +#[unsafe(no_mangle)] +pub extern "C" fn Py_GetCompiler() -> *const c_char { + static COMPILER: LazyLock = LazyLock::new(|| { + CString::new(sys::COMPILER).expect("compiler must not contain interior NULs") + }); + COMPILER.as_ptr() +} + +#[unsafe(no_mangle)] +pub extern "C" fn Py_GetCopyright() -> *const c_char { + static COPYRIGHT: LazyLock = LazyLock::new(|| { + CString::new(sys::COPYRIGHT).expect("copyright must not contain interior NULs") + }); + COPYRIGHT.as_ptr() +} + +#[unsafe(no_mangle)] +pub extern "C" fn Py_GetPlatform() -> *const c_char { + static PLATFORM: LazyLock = LazyLock::new(|| { + CString::new(sys::PLATFORM).expect("platform must not contain interior NULs") + }); + PLATFORM.as_ptr() +} + #[cfg(test)] mod tests { use pyo3::prelude::*; diff --git a/crates/vm/src/stdlib/sys.rs b/crates/vm/src/stdlib/sys.rs index b8fe578f238..9fcd40c7888 100644 --- a/crates/vm/src/stdlib/sys.rs +++ b/crates/vm/src/stdlib/sys.rs @@ -4,6 +4,7 @@ use crate::{Py, PyPayload, PyResult, VirtualMachine, builtins::PyModule, convert #[cfg(all(not(feature = "host_env"), feature = "stdio"))] pub(crate) use sys::SandboxStdio; +pub use sys::{BUILD_INFO, COMPILER, COPYRIGHT, PLATFORM, VERSION}; pub(crate) use sys::{DOC, MAXSIZE, RUST_MULTIARCH, UnraisableHookArgsData, module_def, multiarch}; #[pymodule(name = "_jit")] @@ -222,7 +223,7 @@ pub mod sys { #[pyattr(name = "api_version")] const API_VERSION: u32 = 0x0; // what C api? #[pyattr(name = "copyright")] - const COPYRIGHT: &str = "Copyright (c) 2019 RustPython Team"; + pub const COPYRIGHT: &str = "Copyright (c) 2019 RustPython Team"; #[pyattr(name = "float_repr_style")] const FLOAT_REPR_STYLE: &str = "short"; #[pyattr(name = "_framework")] @@ -708,7 +709,11 @@ pub mod sys { } #[pyattr(name = "version")] - const VERSION: &str = version::RUSTPYTHON_VERSION; + pub const VERSION: &str = version::RUSTPYTHON_VERSION; + + pub const BUILD_INFO: &str = version::RUSTPYTHON_BUILD_INFO; + + pub const COMPILER: &str = "[Rust]"; // Note: This is Python DLL version in CPython, but we arbitrary fill it for compatibility #[cfg(windows)]