Skip to content

Commit 7ee8bdf

Browse files
committed
Align overlapped constructor handle defaults
Assisted-by: dot:frontier-intelligence
1 parent 04ec60a commit 7ee8bdf

1 file changed

Lines changed: 27 additions & 7 deletions

File tree

‎crates/stdlib/src/overlapped.rs‎

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ mod _overlapped {
1010
use crate::vm::{
1111
AsObject, Py, PyObjectRef, PyPayload, PyResult, VirtualMachine,
1212
builtins::{
13-
PyBaseExceptionRef, PyBytesRef, PyModule, PyStrRef, PyTuple, PyTupleRef, PyType,
13+
PyBaseExceptionRef, PyBytesRef, PyInt, PyModule, PyStrRef, PyTuple, PyTupleRef, PyType,
1414
},
1515
common::lock::PyMutex,
1616
convert::{ToPyException, ToPyObject},
@@ -54,7 +54,7 @@ mod _overlapped {
5454
const INFINITE: u32 = host_winapi::INFINITE_TIMEOUT;
5555

5656
#[pyattr]
57-
const INVALID_HANDLE_VALUE: isize = host_overlapped::INVALID_HANDLE_VALUE_ISIZE;
57+
const INVALID_HANDLE_VALUE: usize = usize::MAX;
5858

5959
#[pyattr]
6060
const NULL: isize = 0;
@@ -1114,13 +1114,33 @@ mod _overlapped {
11141114
}
11151115
}
11161116

1117-
impl Constructor for Overlapped {
1118-
type Args = (OptionalArg<isize>,);
1117+
#[derive(FromArgs)]
1118+
struct OverlappedArgs {
1119+
#[pyarg(any, optional, py_default = "_overlapped.INVALID_HANDLE_VALUE")]
1120+
event: OptionalArg<PyObjectRef>,
1121+
}
11191122

1120-
fn py_new(_cls: &Py<PyType>, (event,): Self::Args, vm: &VirtualMachine) -> PyResult<Self> {
1121-
let mut event = event.unwrap_or(INVALID_HANDLE_VALUE);
1123+
impl Constructor for Overlapped {
1124+
type Args = OverlappedArgs;
1125+
1126+
fn py_new(_cls: &Py<PyType>, args: Self::Args, vm: &VirtualMachine) -> PyResult<Self> {
1127+
// PyLong_AsVoidPtr accepts actual integers, not arbitrary __index__ objects.
1128+
// Borrow the integer payload to avoid boxing int subclasses.
1129+
let mut event = match &args.event {
1130+
OptionalArg::Missing => host_overlapped::INVALID_HANDLE_VALUE_ISIZE,
1131+
OptionalArg::Present(event) => {
1132+
let int = event
1133+
.downcast_ref::<PyInt>()
1134+
.ok_or_else(|| vm.new_type_error("an integer is required"))?;
1135+
if int.as_bigint().sign() == malachite_bigint::Sign::Minus {
1136+
int.try_to_primitive_in_range::<isize>(vm)?
1137+
} else {
1138+
int.try_to_primitive_in_range::<usize>(vm)? as isize
1139+
}
1140+
}
1141+
};
11221142

1123-
if event == INVALID_HANDLE_VALUE {
1143+
if event == host_overlapped::INVALID_HANDLE_VALUE_ISIZE {
11241144
event = host_winapi::create_event_w(true, false, None)
11251145
.map(|handle| handle as isize)
11261146
.map_err(|err| {

0 commit comments

Comments
 (0)