Skip to content

Commit 74c07df

Browse files
committed
WindowsError
1 parent b3141d1 commit 74c07df

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

crates/vm/src/exceptions.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,6 +1501,14 @@ pub(super) mod types {
15011501
#[cfg(windows)]
15021502
if let Some(winerror) = new_args.args.get(3) {
15031503
zelf.set_attr("winerror", winerror.clone(), vm)?;
1504+
// Convert winerror to errno and replace args[0] (CPython behavior)
1505+
if let Some(winerror_int) = winerror
1506+
.downcast_ref::<crate::builtins::PyInt>()
1507+
.and_then(|w| w.try_to_primitive::<i32>(vm).ok())
1508+
{
1509+
let errno = crate::common::os::winerror_to_errno(winerror_int);
1510+
new_args.args[0] = vm.new_pyobj(errno);
1511+
}
15041512
}
15051513
if let Some(filename2) = new_args.args.get(4) {
15061514
zelf.set_attr("filename2", filename2.clone(), vm)?;
@@ -1521,19 +1529,29 @@ pub(super) mod types {
15211529
let errno = exc.get_arg(0).unwrap().str(vm)?;
15221530
let msg = exc.get_arg(1).unwrap().str(vm)?;
15231531

1532+
// On Windows, use [WinError X] format when winerror is set
1533+
#[cfg(windows)]
1534+
let (label, code) = match obj.get_attr("winerror", vm) {
1535+
Ok(winerror) if !vm.is_none(&winerror) => ("WinError", winerror.str(vm)?),
1536+
_ => ("Errno", errno.clone()),
1537+
};
1538+
#[cfg(not(windows))]
1539+
let (label, code) = ("Errno", errno.clone());
1540+
15241541
let s = match obj.get_attr("filename", vm) {
15251542
Ok(filename) if !vm.is_none(&filename) => match obj.get_attr("filename2", vm) {
15261543
Ok(filename2) if !vm.is_none(&filename2) => format!(
1527-
"[Errno {}] {}: '{}' -> '{}'",
1528-
errno,
1544+
"[{} {}] {}: '{}' -> '{}'",
1545+
label,
1546+
code,
15291547
msg,
15301548
filename.str(vm)?,
15311549
filename2.str(vm)?
15321550
),
1533-
_ => format!("[Errno {}] {}: '{}'", errno, msg, filename.str(vm)?),
1551+
_ => format!("[{} {}] {}: '{}'", label, code, msg, filename.str(vm)?),
15341552
},
15351553
_ => {
1536-
format!("[Errno {errno}] {msg}")
1554+
format!("[{label} {code}] {msg}")
15371555
}
15381556
};
15391557
vm.ctx.new_str(s)

crates/vm/src/stdlib/builtins.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,4 +1187,10 @@ pub fn init_module(vm: &VirtualMachine, module: &Py<PyModule>) {
11871187
extend_module!(vm, module, {
11881188
"JitError" => ctx.exceptions.jit_error.to_owned(),
11891189
});
1190+
1191+
#[cfg(windows)]
1192+
extend_module!(vm, module, {
1193+
// OSError alias for Windows
1194+
"WindowsError" => ctx.exceptions.os_error.to_owned(),
1195+
});
11901196
}

0 commit comments

Comments
 (0)