Skip to content

Commit c567388

Browse files
Add more build-info functions to c-api
1 parent 3f9d80b commit c567388

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

‎crates/capi/src/pylifecycle.rs‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::pystate::ensure_thread_has_vm_attached;
44
use alloc::ffi::CString;
55
use core::ffi::{c_char, c_int, c_ulong};
66
use rustpython_vm::common::rc::PyRc;
7+
use rustpython_vm::stdlib::sys;
78
use rustpython_vm::version::{MAJOR, MICRO, MINOR, VERSION_HEX};
89
use rustpython_vm::vm::thread::ThreadedVirtualMachine;
910
use rustpython_vm::{Context, Interpreter};
@@ -80,6 +81,38 @@ pub extern "C" fn Py_GetVersion() -> *const c_char {
8081
VERSION.as_ptr()
8182
}
8283

84+
#[unsafe(no_mangle)]
85+
pub extern "C" fn Py_GetBuildInfo() -> *const c_char {
86+
static BUILD_INFO: LazyLock<CString> = LazyLock::new(|| {
87+
CString::new(sys::BUILD_INFO).expect("build info must not contain interior NULs")
88+
});
89+
BUILD_INFO.as_ptr()
90+
}
91+
92+
#[unsafe(no_mangle)]
93+
pub extern "C" fn Py_GetCompiler() -> *const c_char {
94+
static COMPILER: LazyLock<CString> = LazyLock::new(|| {
95+
CString::new(sys::COMPILER).expect("compiler must not contain interior NULs")
96+
});
97+
COMPILER.as_ptr()
98+
}
99+
100+
#[unsafe(no_mangle)]
101+
pub extern "C" fn Py_GetCopyright() -> *const c_char {
102+
static COPYRIGHT: LazyLock<CString> = LazyLock::new(|| {
103+
CString::new(sys::COPYRIGHT).expect("copyright must not contain interior NULs")
104+
});
105+
COPYRIGHT.as_ptr()
106+
}
107+
108+
#[unsafe(no_mangle)]
109+
pub extern "C" fn Py_GetPlatform() -> *const c_char {
110+
static PLATFORM: LazyLock<CString> = LazyLock::new(|| {
111+
CString::new(sys::PLATFORM).expect("platform must not contain interior NULs")
112+
});
113+
PLATFORM.as_ptr()
114+
}
115+
83116
#[cfg(test)]
84117
mod tests {
85118
use pyo3::prelude::*;

‎crates/vm/src/stdlib/sys.rs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::{Py, PyPayload, PyResult, VirtualMachine, builtins::PyModule, convert
44

55
#[cfg(all(not(feature = "host_env"), feature = "stdio"))]
66
pub(crate) use sys::SandboxStdio;
7+
pub use sys::{BUILD_INFO, COMPILER, COPYRIGHT, PLATFORM, VERSION};
78
pub(crate) use sys::{DOC, MAXSIZE, RUST_MULTIARCH, UnraisableHookArgsData, module_def, multiarch};
89

910
#[pymodule(name = "_jit")]
@@ -222,7 +223,7 @@ pub mod sys {
222223
#[pyattr(name = "api_version")]
223224
const API_VERSION: u32 = 0x0; // what C api?
224225
#[pyattr(name = "copyright")]
225-
const COPYRIGHT: &str = "Copyright (c) 2019 RustPython Team";
226+
pub const COPYRIGHT: &str = "Copyright (c) 2019 RustPython Team";
226227
#[pyattr(name = "float_repr_style")]
227228
const FLOAT_REPR_STYLE: &str = "short";
228229
#[pyattr(name = "_framework")]
@@ -708,7 +709,11 @@ pub mod sys {
708709
}
709710

710711
#[pyattr(name = "version")]
711-
const VERSION: &str = version::RUSTPYTHON_VERSION;
712+
pub const VERSION: &str = version::RUSTPYTHON_VERSION;
713+
714+
pub const BUILD_INFO: &str = version::RUSTPYTHON_BUILD_INFO;
715+
716+
pub const COMPILER: &str = "[Rust]";
712717

713718
// Note: This is Python DLL version in CPython, but we arbitrary fill it for compatibility
714719
#[cfg(windows)]

0 commit comments

Comments
 (0)