@@ -21,6 +21,13 @@ pub(crate) mod module {
2121 ospath:: OsPath ,
2222 stdlib:: os:: { _os, DirFd , FollowSymlinks , SupportFunc , TargetIsDirectory , errno_err} ,
2323 } ;
24+
25+ /// Convert the error stored in the C runtime `errno` variable into an Exception.
26+ /// Use this for CRT functions like _dup, _dup2, _fstat, etc. which set errno, not GetLastError().
27+ #[ inline]
28+ fn crt_err ( vm : & VirtualMachine ) -> crate :: builtins:: PyBaseExceptionRef {
29+ crate :: common:: os:: last_crt_error ( ) . to_pyexception ( vm)
30+ }
2431 use libc:: intptr_t;
2532 use std:: os:: windows:: io:: AsRawHandle ;
2633 use std:: {
@@ -876,7 +883,7 @@ pub(crate) mod module {
876883 fn dup ( fd : i32 , vm : & VirtualMachine ) -> PyResult < i32 > {
877884 let fd2 = unsafe { suppress_iph ! ( _dup( fd) ) } ;
878885 if fd2 < 0 {
879- return Err ( errno_err ( vm) ) ;
886+ return Err ( crt_err ( vm) ) ;
880887 }
881888 // Set the new fd as non-inheritable
882889 let borrowed = unsafe { crt_fd:: Borrowed :: borrow_raw ( fd2) } ;
@@ -900,7 +907,7 @@ pub(crate) mod module {
900907 fn dup2 ( args : Dup2Args , vm : & VirtualMachine ) -> PyResult < i32 > {
901908 let result = unsafe { suppress_iph ! ( _dup2( args. fd, args. fd2) ) } ;
902909 if result < 0 {
903- return Err ( errno_err ( vm) ) ;
910+ return Err ( crt_err ( vm) ) ;
904911 }
905912 if !args. inheritable {
906913 let borrowed = unsafe { crt_fd:: Borrowed :: borrow_raw ( args. fd2 ) } ;
0 commit comments