8000 Fix test_poll::test_poll3 by youknowone · Pull Request #5718 · RustPython/RustPython · GitHub
[go: up one dir, main page]

Skip to content

Fix test_poll::test_poll3 #5718

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 28, 2025
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
Next Next commit
test_poll3
  • Loading branch information
youknowone committed Apr 20, 2025
commit 05f982974668092b74bf74c20a8615c2cbdaa026
2 changes: 0 additions & 2 deletions Lib/test/test_poll.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,6 @@ def test_poll2(self):
else:
self.fail('Unexpected return value from select.poll: %s' % fdlist)

# TODO: RUSTPYTHON int overflow
@unittest.expectedFailure
def test_poll3(self):
# test int overflow
pollster = select.poll()
Expand Down
56 changes: 45 additions & 11 deletions stdlib/src/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,15 +330,18 @@ mod decl {
use super::*;
use crate::vm::{
AsObject, PyPayload,
builtins::PyFloat,
builtins::{PyFloat, PyIntRef},
common::lock::PyMutex,
convert::{IntoPyException, ToPyObject},
function::OptionalArg,
stdlib::io::Fildes,
};
use libc::pollfd;
use num_traits::{Signed, ToPrimitive};
use std::time::{Duration, Instant};
use std::{
convert::TryFrom,
time::{Duration, Instant},
};

#[derive(Default)]
pub(super) struct TimeoutArg<const MILLIS: bool>(pub Option<Duration>);
Expand Down Expand Up @@ -422,20 +425,51 @@ mod decl {
#[pyclass]
impl PyPoll {
#[pymethod]
fn register(&self, Fildes(fd): Fildes, eventmask: OptionalArg<u16>) {
insert_fd(
&mut self.fds.lock(),
fd,
eventmask.map_or(DEFAULT_EVENTS, |e| e as i16),
)
fn register(
&self,
Fildes(fd): Fildes,
eventmask: OptionalArg<PyIntRef>,
vm: &VirtualMachine,
) -> PyResult<()> {
let mask = match eventmask {
OptionalArg::Present(int_ref) => {
let val = int_ref.as_bigint();
if val.is_negative() {
return Err(vm.new_value_error("negative event mask".to_owned()));
}
// Try converting to i16, should raise OverflowError if too large
i16::try_from(val).map_err(|_| {
vm.new_overflow_error("event mask value out of range".to_owned())
})?
}
OptionalArg::Missing => DEFAULT_EVENTS,
};
insert_fd(&mut self.fds.lock(), fd, mask);
Ok(())
}

#[pymethod]
fn modify(&self, Fildes(fd): Fildes, eventmask: u16) -> io::Result<()> {
fn modify(
&self,
Fildes(fd): Fildes,
eventmask: PyIntRef,
vm: &VirtualMachine,
) -> PyResult<()> {
let mask = {
let val = eventmask.as_bigint();
if val.is_negative() {
return Err(vm.new_value_error("negative event mask".to_owned()));
}
// Try converting to i16, should raise OverflowError if too large
i16::try_from(val).map_err(|_| {
vm.new_overflow_error("event mask value out of range".to_owned())
})?
};
let mut fds = self.fds.lock();
// CPython raises KeyError if fd is not registered, match that behavior
let pfd = get_fd_mut(&mut fds, fd)
.ok_or_else(|| io::Error::from_raw_os_error(libc::ENOENT))?;
pfd.events = eventmask as i16;
.ok_or_else(|| vm.new_key_error(vm.ctx.new_int(fd).into()))?;
pfd.events = mask;
Ok(())
}

Expand Down
0