Skip to content

Commit 22ea6ad

Browse files
committed
Fix sysconfigdata platform-specific alias name registration
- Register _sysconfigdata under both short name and platform-specific name - Refactor sysconfigdata_name() to be reusable across modules - Add platform-specific name to sys.builtin_module_names - Eliminate code duplication in sysconfigdata name formatting This fixes the CI failure where sysconfig module imports '_sysconfigdata_t_darwin_aarch64-apple-darwin' but RustPython only registered '_sysconfigdata'. PyGlobalState
1 parent 8b7c26a commit 22ea6ad

File tree

17 files changed

+211
-176
lines changed

17 files changed

+211
-176
lines changed

crates/common/src/crt_fd.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ use alloc::fmt;
55
use core::cmp;
66
use std::{ffi, io};
77

8+
#[cfg(unix)]
9+
use std::os::fd::AsFd;
810
#[cfg(not(windows))]
9-
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
11+
use std::os::fd::{AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
1012
#[cfg(windows)]
1113
use std::os::windows::io::BorrowedHandle;
1214

crates/vm/src/import.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,11 @@ pub fn import_builtin(vm: &VirtualMachine, module_name: &str) -> PyResult {
103103
sys_modules.set_item(module_name, module.clone().into(), vm)?;
104104

105105
// Phase 2: Call exec slot (can safely import other modules now)
106-
def.exec_module(vm, &module)?;
106+
// If exec fails, remove the partially-initialized module from sys.modules
107+
if let Err(e) = def.exec_module(vm, &module) {
108+
let _ = sys_modules.del_item(module_name, vm);
109+
return Err(e);
110+
}
107111

108112
return Ok(module.into());
109113
}

crates/vm/src/stdlib/errno.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ mod errno_mod {
1616
for (name, code) in super::ERROR_CODES {
1717
let name = vm.ctx.intern_str(*name);
1818
let code = vm.new_pyobj(*code);
19-
errorcode
20-
.set_item(&*code, name.to_owned().into(), vm)
21-
.unwrap();
22-
module.set_attr(name, code, vm).unwrap();
19+
errorcode.set_item(&*code, name.to_owned().into(), vm)?;
20+
module.set_attr(name, code, vm)?;
2321
}
2422
Ok(())
2523
}

crates/vm/src/stdlib/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,7 @@ pub fn builtin_module_defs(ctx: &Context) -> Vec<&'static PyModuleDef> {
9393
#[cfg(windows)]
9494
nt::module_def(ctx),
9595
operator::module_def(ctx),
96-
#[cfg(unix)]
97-
posix::module_def(ctx),
98-
#[cfg(target_os = "wasi")]
96+
#[cfg(any(unix, target_os = "wasi"))]
9997
posix::module_def(ctx),
10098
#[cfg(all(
10199
any(not(target_arch = "wasm32"), target_os = "wasi"),

crates/vm/src/stdlib/nt.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
// spell-checker:disable
22

3-
use crate::{PyRef, VirtualMachine, builtins::PyModule};
4-
53
pub(crate) use module::module_def;
64
pub use module::raw_set_handle_inheritable;
75

86
#[pymodule(name = "nt", with(super::os::_os))]
97
pub(crate) mod module {
108
use crate::{
11-
PyResult, TryFromObject, VirtualMachine,
9+
Py, PyResult, TryFromObject, VirtualMachine,
1210
builtins::{PyBaseExceptionRef, PyDictRef, PyListRef, PyStrRef, PyTupleRef},
1311
common::{crt_fd, suppress_iph, windows::ToWideString},
1412
convert::ToPyException,

crates/vm/src/stdlib/os.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ pub(super) mod _os {
149149
use super::{DirFd, FollowSymlinks, SupportFunc};
150150
#[cfg(windows)]
151151
use crate::common::windows::ToWideString;
152+
#[cfg(any(unix, windows))]
153+
use crate::utils::ToCString;
152154
use crate::{
153155
AsObject, Py, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject,
154156
builtins::{
@@ -168,7 +170,6 @@ pub(super) mod _os {
168170
protocol::PyIterReturn,
169171
recursion::ReprGuard,
170172
types::{IterNext, Iterable, PyStructSequence, Representable, SelfIter},
171-
utils::ToCString,
172173
vm::VirtualMachine,
173174
};
174175
use core::time::Duration;

crates/vm/src/stdlib/posix_compat.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// spell-checker:disable
22

33
//! `posix` compatible module for `not(any(unix, windows))`
4-
use crate::{PyRef, VirtualMachine, builtins::PyModule};
54
65
pub(crate) use module::module_def;
76

crates/vm/src/stdlib/stat.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
pub(crate) use stat::module_def;
1+
pub(crate) use _stat::module_def;
22

33
#[pymodule]
4-
mod stat {
4+
mod _stat {
55
// Use libc::mode_t for Mode to match the system's definition
66
#[cfg(unix)]
77
type Mode = libc::mode_t;

crates/vm/src/stdlib/symtable.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
pub(crate) use symtable::module_def;
1+
pub(crate) use _symtable::module_def;
22

33
#[pymodule]
4-
mod symtable {
4+
mod _symtable {
55
use crate::{
66
PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
77
builtins::{PyDictRef, PyStrRef},

crates/vm/src/stdlib/sys.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ mod sys {
7878
#[pyattr(name = "abiflags")]
7979
const ABIFLAGS_ATTR: &str = "t"; // 't' for free-threaded (no GIL)
8080
// Internal constant used for sysconfigdata_name
81-
pub(crate) const ABIFLAGS: &str = "t";
81+
pub const ABIFLAGS: &str = "t";
8282
#[pyattr(name = "api_version")]
8383
const API_VERSION: u32 = 0x0; // what C api?
8484
#[pyattr(name = "copyright")]
@@ -94,7 +94,7 @@ mod sys {
9494
#[pyattr(name = "maxunicode")]
9595
const MAXUNICODE: u32 = core::char::MAX as u32;
9696
#[pyattr(name = "platform")]
97-
pub(crate) const PLATFORM: &str = {
97+
pub const PLATFORM: &str = {
9898
cfg_if::cfg_if! {
9999
if #[cfg(target_os = "linux")] {
100100
"linux"
@@ -166,9 +166,11 @@ mod sys {
166166

167167
#[pyattr]
168168
fn builtin_module_names(vm: &VirtualMachine) -> PyTupleRef {
169-
let mut module_names: Vec<&str> = vm.state.module_defs.keys().copied().collect();
170-
module_names.push("sys");
171-
module_names.push("builtins");
169+
let mut module_names: Vec<String> =
170+
vm.state.module_defs.keys().map(|&s| s.to_owned()).collect();
171+
module_names.push("sys".to_owned());
172+
module_names.push("builtins".to_owned());
173+
172174
module_names.sort();
173175
vm.ctx.new_tuple(
174176
module_names

0 commit comments

Comments
 (0)