Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove os::errno_err
  • Loading branch information
youknowone committed Dec 9, 2025
commit eab4ba516efc14ca3c18948b8ccf9f0859aa61ed
14 changes: 7 additions & 7 deletions crates/stdlib/src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ mod fcntl {
}
let ret = unsafe { libc::fcntl(fd, cmd, buf.as_mut_ptr()) };
if ret < 0 {
return Err(os::errno_err(vm));
return Err(vm.new_last_errno_error());
}
return Ok(vm.ctx.new_bytes(buf[..arg_len].to_vec()).into());
}
Expand All @@ -84,7 +84,7 @@ mod fcntl {
};
let ret = unsafe { libc::fcntl(fd, cmd, int as i32) };
if ret < 0 {
return Err(os::errno_err(vm));
return Err(vm.new_last_errno_error());
}
Ok(vm.new_pyobj(ret))
}
Expand Down Expand Up @@ -117,7 +117,7 @@ mod fcntl {
let ret =
unsafe { libc::ioctl(fd, request as _, arg_buf.as_mut_ptr()) };
if ret < 0 {
return Err(os::errno_err(vm));
return Err(vm.new_last_errno_error());
}
return Ok(vm.ctx.new_int(ret).into());
}
Expand All @@ -128,14 +128,14 @@ mod fcntl {
};
let ret = unsafe { libc::ioctl(fd, request as _, buf.as_mut_ptr()) };
if ret < 0 {
return Err(os::errno_err(vm));
return Err(vm.new_last_errno_error());
}
Ok(vm.ctx.new_bytes(buf[..buf_len].to_vec()).into())
}
Either::B(i) => {
let ret = unsafe { libc::ioctl(fd, request as _, i) };
if ret < 0 {
return Err(os::errno_err(vm));
return Err(vm.new_last_errno_error());
}
Ok(vm.ctx.new_int(ret).into())
}
Expand All @@ -149,7 +149,7 @@ mod fcntl {
let ret = unsafe { libc::flock(fd, operation) };
// TODO: add support for platforms that don't have a builtin `flock` syscall
if ret < 0 {
return Err(os::errno_err(vm));
return Err(vm.new_last_errno_error());
}
Ok(vm.ctx.new_int(ret).into())
}
Expand Down Expand Up @@ -209,7 +209,7 @@ mod fcntl {
)
};
if ret < 0 {
return Err(os::errno_err(vm));
return Err(vm.new_last_errno_error());
}
Ok(vm.ctx.new_int(ret).into())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/stdlib/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ mod resource {
let rlimit = unsafe {
let mut rlimit = mem::MaybeUninit::<libc::rlimit>::uninit();
if libc::getrlimit(resource as _, rlimit.as_mut_ptr()) == -1 {
return Err(os::errno_err(vm));
return Err(vm.new_last_errno_error());
}
rlimit.assume_init()
};
Expand Down
2 changes: 1 addition & 1 deletion crates/stdlib/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2158,7 +2158,7 @@ mod _socket {
let mut buf = [0; c::IF_NAMESIZE + 1];
let ret = unsafe { c::if_indextoname(index, buf.as_mut_ptr()) };
if ret.is_null() {
Err(crate::vm::stdlib::os::errno_err(vm))
Err(vm.new_last_errno_error())
} else {
let buf = unsafe { ffi::CStr::from_ptr(buf.as_ptr() as _) };
Ok(buf.to_string_lossy().into_owned())
Expand Down
3 changes: 1 addition & 2 deletions crates/vm/src/stdlib/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3991,8 +3991,7 @@ mod _io {
// check file descriptor validity
#[cfg(unix)]
if let Ok(crate::ospath::OsPathOrFd::Fd(fd)) = file.clone().try_into_value(vm) {
nix::fcntl::fcntl(fd, nix::fcntl::F_GETFD)
.map_err(|_| crate::stdlib::os::errno_err(vm))?;
nix::fcntl::fcntl(fd, nix::fcntl::F_GETFD).map_err(|_| vm.new_last_errno_error())?;
}

// Construct a FileIO (subclass of RawIOBase)
Expand Down
5 changes: 2 additions & 3 deletions crates/vm/src/stdlib/msvcrt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ mod msvcrt {
builtins::{PyBytes, PyStrRef},
common::{crt_fd, suppress_iph},
convert::IntoPyException,
stdlib::os::errno_err,
};
use itertools::Itertools;
use std::os::windows::io::AsRawHandle;
Expand Down Expand Up @@ -82,7 +81,7 @@ mod msvcrt {
fn setmode(fd: crt_fd::Borrowed<'_>, flags: i32, vm: &VirtualMachine) -> PyResult<i32> {
let flags = unsafe { suppress_iph!(_setmode(fd, flags)) };
if flags == -1 {
Err(errno_err(vm))
Err(vm.new_last_errno_error())
} else {
Ok(flags)
}
Expand All @@ -92,7 +91,7 @@ mod msvcrt {
fn open_osfhandle(handle: isize, flags: i32, vm: &VirtualMachine) -> PyResult<i32> {
let ret = unsafe { suppress_iph!(libc::open_osfhandle(handle, flags)) };
if ret == -1 {
Err(errno_err(vm))
Err(vm.new_last_errno_error())
} else {
Ok(ret)
}
Expand Down
14 changes: 4 additions & 10 deletions crates/vm/src/stdlib/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::{
AsObject, Py, PyObjectRef, PyPayload, PyResult, TryFromObject, VirtualMachine,
builtins::{PyBaseExceptionRef, PyModule, PySet},
builtins::{PyModule, PySet},
common::crt_fd,
convert::{IntoPyException, ToPyException, ToPyObject},
function::{ArgumentError, FromArgs, FuncArgs},
Expand All @@ -22,24 +22,18 @@ pub(crate) fn fs_metadata<P: AsRef<Path>>(

#[cfg(unix)]
impl crate::convert::IntoPyException for nix::Error {
fn into_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef {
fn into_pyexception(self, vm: &VirtualMachine) -> crate::builtins::PyBaseExceptionRef {
io::Error::from(self).into_pyexception(vm)
}
}

#[cfg(unix)]
impl crate::convert::IntoPyException for rustix::io::Errno {
fn into_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef {
fn into_pyexception(self, vm: &VirtualMachine) -> crate::builtins::PyBaseExceptionRef {
io::Error::from(self).into_pyexception(vm)
}
}

/// Convert the error stored in the `errno` variable into an Exception
#[inline]
pub fn errno_err(vm: &VirtualMachine) -> PyBaseExceptionRef {
crate::common::os::errno_io_error().to_pyexception(vm)
}

#[allow(dead_code)]
#[derive(FromArgs, Default)]
pub struct TargetIsDirectory {
Expand Down Expand Up @@ -1484,7 +1478,7 @@ pub(super) mod _os {
)
};

usize::try_from(ret).map_err(|_| errno_err(vm))
usize::try_from(ret).map_err(|_| vm.new_last_errno_error())
}

#[pyfunction]
Expand Down
52 changes: 33 additions & 19 deletions crates/vm/src/stdlib/posix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ pub mod module {
convert::{IntoPyException, ToPyObject, TryFromObject},
function::{Either, KwArgs, OptionalArg},
ospath::{IOErrorBuilder, OsPath, OsPathOrFd},
stdlib::os::{
_os, DirFd, FollowSymlinks, SupportFunc, TargetIsDirectory, errno_err, fs_metadata,
},
stdlib::os::{_os, DirFd, FollowSymlinks, SupportFunc, TargetIsDirectory, fs_metadata},
types::{Constructor, Representable},
utils::ToCString,
};
Expand Down Expand Up @@ -467,7 +465,11 @@ pub mod module {
{
let [] = args.dir_fd.0;
let res = unsafe { libc::symlink(src.as_ptr(), dst.as_ptr()) };
if res < 0 { Err(errno_err(vm)) } else { Ok(()) }
if res < 0 {
Err(vm.new_last_errno_error())
} else {
Ok(())
}
}
}

Expand Down Expand Up @@ -715,14 +717,22 @@ pub mod module {
)
},
};
if ret != 0 { Err(errno_err(vm)) } else { Ok(()) }
if ret != 0 {
Err(vm.new_last_errno_error())
} else {
Ok(())
}
}

#[cfg(target_vendor = "apple")]
fn mknod(self, vm: &VirtualMachine) -> PyResult<()> {
let [] = self.dir_fd.0;
let ret = self._mknod(vm)?;
if ret != 0 { Err(errno_err(vm)) } else { Ok(()) }
if ret != 0 {
Err(vm.new_last_errno_error())
} else {
Ok(())
}
}
}

Expand All @@ -739,7 +749,7 @@ pub mod module {
Errno::clear();
let res = unsafe { libc::nice(increment) };
if res == -1 && Errno::last_raw() != 0 {
Err(errno_err(vm))
Err(vm.new_last_errno_error())
} else {
Ok(res)
}
Expand All @@ -750,7 +760,7 @@ pub mod module {
fn sched_get_priority_max(policy: i32, vm: &VirtualMachine) -> PyResult<i32> {
let max = unsafe { libc::sched_get_priority_max(policy) };
if max == -1 {
Err(errno_err(vm))
Err(vm.new_last_errno_error())
} else {
Ok(max)
}
Expand All @@ -761,7 +771,7 @@ pub mod module {
fn sched_get_priority_min(policy: i32, vm: &VirtualMachine) -> PyResult<i32> {
let min = unsafe { libc::sched_get_priority_min(policy) };
if min == -1 {
Err(errno_err(vm))
Err(vm.new_last_errno_error())
} else {
Ok(min)
}
Expand Down Expand Up @@ -852,7 +862,7 @@ pub mod module {
fn sched_getscheduler(pid: libc::pid_t, vm: &VirtualMachine) -> PyResult<i32> {
let policy = unsafe { libc::sched_getscheduler(pid) };
if policy == -1 {
Err(errno_err(vm))
Err(vm.new_last_errno_error())
} else {
Ok(policy)
}
Expand Down Expand Up @@ -886,7 +896,7 @@ pub mod module {
let libc_sched_param = args.sched_param_obj.try_to_libc(vm)?;
let policy = unsafe { libc::sched_setscheduler(args.pid, args.policy, &libc_sched_param) };
if policy == -1 {
Err(errno_err(vm))
Err(vm.new_last_errno_error())
} else {
Ok(policy)
}
Expand All @@ -902,7 +912,7 @@ pub mod module {
let param = unsafe {
let mut param = std::mem::MaybeUninit::uninit();
if -1 == libc::sched_getparam(pid, param.as_mut_ptr()) {
return Err(errno_err(vm));
return Err(vm.new_last_errno_error());
}
param.assume_init()
};
Expand Down Expand Up @@ -937,7 +947,7 @@ pub mod module {
let libc_sched_param = args.sched_param_obj.try_to_libc(vm)?;
let ret = unsafe { libc::sched_setparam(args.pid, &libc_sched_param) };
if ret == -1 {
Err(errno_err(vm))
Err(vm.new_last_errno_error())
} else {
Ok(ret)
}
Expand Down Expand Up @@ -1719,7 +1729,7 @@ pub mod module {
{
let ret = unsafe { libc::kill(pid, sig as i32) };
if ret == -1 {
Err(errno_err(vm))
Err(vm.new_last_errno_error())
} else {
Ok(())
}
Expand Down Expand Up @@ -1762,7 +1772,11 @@ pub mod module {
#[pyfunction]
fn _fcopyfile(in_fd: i32, out_fd: i32, flags: i32, vm: &VirtualMachine) -> PyResult<()> {
let ret = unsafe { fcopyfile(in_fd, out_fd, std::ptr::null_mut(), flags as u32) };
if ret < 0 { Err(errno_err(vm)) } else { Ok(()) }
if ret < 0 {
Err(vm.new_last_errno_error())
} else {
Ok(())
}
}

#[pyfunction]
Expand Down Expand Up @@ -1884,7 +1898,7 @@ pub mod module {
Errno::clear();
let retval = unsafe { libc::getpriority(which, who) };
if Errno::last_raw() != 0 {
Err(errno_err(vm))
Err(vm.new_last_errno_error())
} else {
Ok(vm.ctx.new_int(retval).into())
}
Expand All @@ -1900,7 +1914,7 @@ pub mod module {
) -> PyResult<()> {
let retval = unsafe { libc::setpriority(which, who, priority) };
if retval == -1 {
Err(errno_err(vm))
Err(vm.new_last_errno_error())
} else {
Ok(())
}
Expand Down Expand Up @@ -2325,7 +2339,7 @@ pub mod module {
fn sysconf(name: SysconfName, vm: &VirtualMachine) -> PyResult<libc::c_long> {
let r = unsafe { libc::sysconf(name.0) };
if r == -1 {
return Err(errno_err(vm));
return Err(vm.new_last_errno_error());
}
Ok(r)
}
Expand Down Expand Up @@ -2452,7 +2466,7 @@ pub mod module {
flags.unwrap_or(0),
)
.try_into()
.map_err(|_| errno_err(vm))?;
.map_err(|_| vm.new_last_os_error())?;
buf.set_len(len);
}
Ok(buf)
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/stdlib/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ pub(crate) mod _signal {
signal::assert_in_range(signum, vm)?;
let res = unsafe { siginterrupt(signum, flag) };
if res < 0 {
Err(crate::stdlib::os::errno_err(vm))
Err(vm.new_last_errno_error())
} else {
Ok(())
}
Expand Down