Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use CStr for COMPILER/COPYRIGHT/PLATFORM
  • Loading branch information
bschoenmaeckers committed Jul 7, 2026
commit 7881dfdeac4736b59d962cf334e419634f894856
15 changes: 3 additions & 12 deletions crates/capi/src/pylifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,26 +91,17 @@ pub extern "C" fn Py_GetBuildInfo() -> *const c_char {

#[unsafe(no_mangle)]
pub extern "C" fn Py_GetCompiler() -> *const c_char {
static COMPILER: LazyLock<CString> = LazyLock::new(|| {
CString::new(sys::COMPILER).expect("compiler must not contain interior NULs")
});
COMPILER.as_ptr()
sys::COMPILER.as_ptr()
}

#[unsafe(no_mangle)]
pub extern "C" fn Py_GetCopyright() -> *const c_char {
static COPYRIGHT: LazyLock<CString> = LazyLock::new(|| {
CString::new(sys::COPYRIGHT).expect("copyright must not contain interior NULs")
});
COPYRIGHT.as_ptr()
sys::COPYRIGHT.as_ptr()
}

#[unsafe(no_mangle)]
pub extern "C" fn Py_GetPlatform() -> *const c_char {
static PLATFORM: LazyLock<CString> = LazyLock::new(|| {
CString::new(sys::PLATFORM).expect("platform must not contain interior NULs")
});
PLATFORM.as_ptr()
sys::PLATFORM.as_ptr()
}

Comment thread
bschoenmaeckers marked this conversation as resolved.
#[cfg(test)]
Expand Down
7 changes: 7 additions & 0 deletions crates/vm/src/builtins/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use crate::{
use alloc::{borrow::Cow, fmt};
use ascii::{AsciiChar, AsciiStr, AsciiString};
use bstr::ByteSlice;
use core::ffi::CStr;
use core::{char, mem, ops::Range};
use itertools::Itertools;
use num_traits::ToPrimitive;
Expand Down Expand Up @@ -1868,6 +1869,12 @@ impl ToPyObject for &String {
}
}

impl ToPyObject for &CStr {
fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
vm.ctx.new_str(self.to_string_lossy()).into()
Comment thread
bschoenmaeckers marked this conversation as resolved.
Outdated
}
}

impl ToPyObject for &Wtf8 {
fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
vm.ctx.new_str(self).into()
Expand Down
23 changes: 12 additions & 11 deletions crates/vm/src/stdlib/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub mod sys {
version,
vm::{Settings, VirtualMachine},
};
use core::ffi::CStr;
use core::sync::atomic::Ordering;
use num_traits::ToPrimitive;
use std::{
Expand Down Expand Up @@ -223,7 +224,7 @@ pub mod sys {
#[pyattr(name = "api_version")]
const API_VERSION: u32 = 0x0; // what C api?
#[pyattr(name = "copyright")]
pub const COPYRIGHT: &str = "Copyright (c) 2019 RustPython Team";
pub const COPYRIGHT: &CStr = c"Copyright (c) 2019 RustPython Team";
#[pyattr(name = "float_repr_style")]
const FLOAT_REPR_STYLE: &str = "short";
#[pyattr(name = "_framework")]
Expand All @@ -236,14 +237,14 @@ pub mod sys {
const MAXUNICODE: u32 = core::char::MAX as u32;

#[pyattr(name = "platform")]
pub const PLATFORM: &str = cfg_select! {
target_os = "linux" => "linux",
target_os = "android" => "android",
target_os = "macos" => "darwin",
target_os = "ios" => "ios",
windows => "win32",
target_os = "wasi" => "wasi",
_ => "unknown"
pub const PLATFORM: &CStr = cfg_select! {
target_os = "linux" => c"linux",
target_os = "android" => c"android",
target_os = "macos" => c"darwin",
target_os = "ios" => c"ios",
windows => c"win32",
target_os = "wasi" => c"wasi",
_ => c"unknown"
};

#[pyattr(name = "ps1")]
Expand Down Expand Up @@ -713,7 +714,7 @@ pub mod sys {

pub const BUILD_INFO: &str = version::RUSTPYTHON_BUILD_INFO;
Comment thread
bschoenmaeckers marked this conversation as resolved.
Outdated

pub const COMPILER: &str = "[Rust]";
pub const COMPILER: &CStr = c"[Rust]";

// Note: This is Python DLL version in CPython, but we arbitrary fill it for compatibility
#[cfg(windows)]
Expand Down Expand Up @@ -1916,7 +1917,7 @@ pub(crate) fn sysconfigdata_name() -> String {
format!(
"_sysconfigdata_{}_{}_{}",
sys::ABIFLAGS,
sys::PLATFORM,
sys::PLATFORM.to_string_lossy(),
sys::multiarch()
)
}
Loading