Skip to content

Commit 2a3e847

Browse files
committed
use posix error messages under 127
1 parent 49522e7 commit 2a3e847

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

  • crates/vm/src/stdlib

crates/vm/src/stdlib/io.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,30 @@ pub use _io::{OpenArgs, io_open as open};
2020
impl ToPyException for std::io::Error {
2121
fn to_pyexception(&self, vm: &VirtualMachine) -> PyBaseExceptionRef {
2222
let errno = self.posix_errno();
23+
#[cfg(windows)]
24+
let msg = 'msg: {
25+
// On Windows, use C runtime's strerror for POSIX errno values
26+
// For Windows-specific error codes, fall back to FormatMessage
27+
28+
// UCRT's strerror returns "Unknown error" for invalid errno values
29+
// Windows UCRT defines errno values 1-42 plus some more up to ~127
30+
const MAX_POSIX_ERRNO: i32 = 127;
31+
if errno > 0 && errno <= MAX_POSIX_ERRNO {
32+
let ptr = unsafe { libc::strerror(errno) };
33+
if !ptr.is_null() {
34+
let s = unsafe { std::ffi::CStr::from_ptr(ptr) }.to_string_lossy();
35+
if !s.starts_with("Unknown error") {
36+
break 'msg s.into_owned();
37+
}
38+
}
39+
}
40+
self.to_string()
41+
};
42+
#[cfg(not(windows))]
2343
let msg = self.to_string();
44+
2445
#[allow(clippy::let_and_return)]
2546
let exc = vm.new_errno_error(errno, msg);
26-
2747
#[cfg(windows)]
2848
{
2949
use crate::object::AsObject;

0 commit comments

Comments
 (0)