mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
Fix multiprocessing closesocket, set_errno (#6388)
* Fix multiprocessing closesocket * set_errno
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -3007,6 +3007,7 @@ dependencies = [
|
||||
"malachite-base",
|
||||
"malachite-bigint",
|
||||
"malachite-q",
|
||||
"nix 0.30.1",
|
||||
"num-complex",
|
||||
"num-traits",
|
||||
"once_cell",
|
||||
|
||||
@@ -35,6 +35,9 @@ lock_api = "0.4"
|
||||
siphasher = "1"
|
||||
num-complex.workspace = true
|
||||
|
||||
[target.'cfg(unix)'.dependencies]
|
||||
nix = { workspace = true }
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
widestring = { workspace = true }
|
||||
windows-sys = { workspace = true, features = [
|
||||
|
||||
@@ -48,6 +48,20 @@ pub fn get_errno() -> i32 {
|
||||
std::io::Error::last_os_error().posix_errno()
|
||||
}
|
||||
|
||||
/// Set errno to the specified value.
|
||||
#[cfg(windows)]
|
||||
pub fn set_errno(value: i32) {
|
||||
unsafe extern "C" {
|
||||
fn _set_errno(value: i32) -> i32;
|
||||
}
|
||||
unsafe { suppress_iph!(_set_errno(value)) };
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
pub fn set_errno(value: i32) {
|
||||
nix::errno::Errno::from_raw(value).set();
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
pub fn bytes_as_os_str(b: &[u8]) -> Result<&std::ffi::OsStr, Utf8Error> {
|
||||
use std::os::unix::ffi::OsStrExt;
|
||||
|
||||
@@ -9,7 +9,7 @@ mod _multiprocessing {
|
||||
#[pyfunction]
|
||||
fn closesocket(socket: usize, vm: &VirtualMachine) -> PyResult<()> {
|
||||
let res = unsafe { WinSock::closesocket(socket as SOCKET) };
|
||||
if res == 0 {
|
||||
if res != 0 {
|
||||
Err(vm.new_last_os_error())
|
||||
} else {
|
||||
Ok(())
|
||||
|
||||
@@ -2143,10 +2143,11 @@ mod _socket {
|
||||
#[pyfunction]
|
||||
fn if_nametoindex(name: FsPath, vm: &VirtualMachine) -> PyResult<IfIndex> {
|
||||
let name = name.to_cstring(vm)?;
|
||||
|
||||
// in case 'if_nametoindex' does not set errno
|
||||
crate::common::os::set_errno(libc::ENODEV);
|
||||
let ret = unsafe { c::if_nametoindex(name.as_ptr() as _) };
|
||||
if ret == 0 {
|
||||
Err(vm.new_os_error("no interface with this name".to_owned()))
|
||||
Err(vm.new_last_errno_error())
|
||||
} else {
|
||||
Ok(ret)
|
||||
}
|
||||
@@ -2156,6 +2157,8 @@ mod _socket {
|
||||
#[pyfunction]
|
||||
fn if_indextoname(index: IfIndex, vm: &VirtualMachine) -> PyResult<String> {
|
||||
let mut buf = [0; c::IF_NAMESIZE + 1];
|
||||
// in case 'if_indextoname' does not set errno
|
||||
crate::common::os::set_errno(libc::ENXIO);
|
||||
let ret = unsafe { c::if_indextoname(index, buf.as_mut_ptr()) };
|
||||
if ret.is_null() {
|
||||
Err(vm.new_last_errno_error())
|
||||
|
||||
@@ -2337,8 +2337,9 @@ pub mod module {
|
||||
|
||||
#[pyfunction]
|
||||
fn sysconf(name: SysconfName, vm: &VirtualMachine) -> PyResult<libc::c_long> {
|
||||
crate::common::os::set_errno(0);
|
||||
let r = unsafe { libc::sysconf(name.0) };
|
||||
if r == -1 {
|
||||
if r == -1 && crate::common::os::get_errno() != 0 {
|
||||
return Err(vm.new_last_errno_error());
|
||||
}
|
||||
Ok(r)
|
||||
|
||||
Reference in New Issue
Block a user