File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -20,10 +20,30 @@ pub use _io::{OpenArgs, io_open as open};
2020impl 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 ;
You can’t perform that action at this time.
0 commit comments