File tree Expand file tree Collapse file tree 6 files changed +26
-4
lines changed
Expand file tree Collapse file tree 6 files changed +26
-4
lines changed Original file line number Diff line number Diff line change @@ -35,6 +35,9 @@ lock_api = "0.4"
3535siphasher = " 1"
3636num-complex.workspace = true
3737
38+ [target .'cfg(unix)' .dependencies ]
39+ nix = { workspace = true }
40+
3841[target .'cfg(windows)' .dependencies ]
3942widestring = { workspace = true }
4043windows-sys = { workspace = true , features = [
Original file line number Diff line number Diff line change @@ -48,6 +48,20 @@ pub fn get_errno() -> i32 {
4848 std:: io:: Error :: last_os_error ( ) . posix_errno ( )
4949}
5050
51+ /// Set errno to the specified value.
52+ #[ cfg( windows) ]
53+ pub fn set_errno ( value : i32 ) {
54+ unsafe extern "C" {
55+ fn _set_errno ( value : i32 ) -> i32 ;
56+ }
57+ unsafe { suppress_iph ! ( _set_errno( value) ) } ;
58+ }
59+
60+ #[ cfg( unix) ]
61+ pub fn set_errno ( value : i32 ) {
62+ nix:: errno:: Errno :: from_raw ( value) . set ( ) ;
63+ }
64+
5165#[ cfg( unix) ]
5266pub fn bytes_as_os_str ( b : & [ u8 ] ) -> Result < & std:: ffi:: OsStr , Utf8Error > {
5367 use std:: os:: unix:: ffi:: OsStrExt ;
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ mod _multiprocessing {
99 #[ pyfunction]
1010 fn closesocket ( socket : usize , vm : & VirtualMachine ) -> PyResult < ( ) > {
1111 let res = unsafe { WinSock :: closesocket ( socket as SOCKET ) } ;
12- if res = = 0 {
12+ if res ! = 0 {
1313 Err ( vm. new_last_os_error ( ) )
1414 } else {
1515 Ok ( ( ) )
Original file line number Diff line number Diff line change @@ -2143,10 +2143,11 @@ mod _socket {
21432143 #[ pyfunction]
21442144 fn if_nametoindex ( name : FsPath , vm : & VirtualMachine ) -> PyResult < IfIndex > {
21452145 let name = name. to_cstring ( vm) ?;
2146-
2146+ // in case 'if_nametoindex' does not set errno
2147+ crate :: common:: os:: set_errno ( libc:: ENODEV ) ;
21472148 let ret = unsafe { c:: if_nametoindex ( name. as_ptr ( ) as _ ) } ;
21482149 if ret == 0 {
2149- Err ( vm. new_os_error ( "no interface with this name" . to_owned ( ) ) )
2150+ Err ( vm. new_last_errno_error ( ) )
21502151 } else {
21512152 Ok ( ret)
21522153 }
@@ -2156,6 +2157,8 @@ mod _socket {
21562157 #[ pyfunction]
21572158 fn if_indextoname ( index : IfIndex , vm : & VirtualMachine ) -> PyResult < String > {
21582159 let mut buf = [ 0 ; c:: IF_NAMESIZE + 1 ] ;
2160+ // in case 'if_indextoname' does not set errno
2161+ crate :: common:: os:: set_errno ( libc:: ENXIO ) ;
21592162 let ret = unsafe { c:: if_indextoname ( index, buf. as_mut_ptr ( ) ) } ;
21602163 if ret. is_null ( ) {
21612164 Err ( vm. new_last_errno_error ( ) )
Original file line number Diff line number Diff line change @@ -2337,8 +2337,9 @@ pub mod module {
23372337
23382338 #[ pyfunction]
23392339 fn sysconf ( name : SysconfName , vm : & VirtualMachine ) -> PyResult < libc:: c_long > {
2340+ crate :: common:: os:: set_errno ( 0 ) ;
23402341 let r = unsafe { libc:: sysconf ( name. 0 ) } ;
2341- if r == -1 {
2342+ if r == -1 && crate :: common :: os :: get_errno ( ) != 0 {
23422343 return Err ( vm. new_last_errno_error ( ) ) ;
23432344 }
23442345 Ok ( r)
You can’t perform that action at this time.
0 commit comments