Skip to content

Commit 415f3d3

Browse files
authored
Match CPython unsigned socket conversion errors (#8257)
Assisted-by: Codex:gpt-5.4
1 parent a9c2c52 commit 415f3d3

8 files changed

Lines changed: 18 additions & 23 deletions

File tree

Lib/test/test_lzma.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,6 @@ def test_init_bad_check(self):
656656
with self.assertRaises(ValueError):
657657
LZMAFile(BytesIO(COMPRESSED_XZ), check=lzma.CHECK_UNKNOWN)
658658

659-
@unittest.expectedFailure # TODO: RUSTPYTHON; OverflowError: Python int too large to convert to Rust u32
660659
def test_init_bad_preset(self):
661660
with self.assertRaises(TypeError):
662661
LZMAFile(BytesIO(), "w", preset=4.39)

Lib/test/test_memoryio.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -934,10 +934,6 @@ def test_flags(self):
934934
def test_write(self):
935935
return super().test_write()
936936

937-
@unittest.expectedFailure # TODO: RUSTPYTHON; OverflowError: Python int too large to convert to Rust u64
938-
def test_seek(self):
939-
return super().test_seek()
940-
941937
class CStringIOTest(PyStringIOTest):
942938
ioclass = io.StringIO
943939
UnsupportedOperation = io.UnsupportedOperation
@@ -1030,10 +1026,6 @@ def test_flags(self):
10301026
def test_newlines_property(self):
10311027
return super().test_newlines_property()
10321028

1033-
@unittest.expectedFailure # TODO: RUSTPYTHON; OverflowError: Python int too large to convert to Rust u64
1034-
def test_seek(self):
1035-
return super().test_seek()
1036-
10371029
@unittest.expectedFailure # TODO: RUSTPYTHON; d
10381030
def test_newline_cr(self):
10391031
return super().test_newline_cr()

Lib/test/test_resource.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ def expected(cur):
151151
resource.setrlimit(resource.RLIMIT_FSIZE, (2**64-5, max))
152152
self.assertIn(resource.getrlimit(resource.RLIMIT_FSIZE), expected(2**64-5))
153153

154-
@unittest.expectedFailure # TODO: RUSTPYTHON; OverflowError: Python int too large to convert to Rust u64
155154
@unittest.skipIf(sys.platform == "vxworks",
156155
"setting RLIMIT_FSIZE is not supported on VxWorks")
157156
@unittest.skipUnless(hasattr(resource, 'RLIMIT_FSIZE'), 'requires resource.RLIMIT_FSIZE')

Lib/test/test_socket.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,7 +1181,6 @@ def testInterfaceNameIndex(self):
11811181
self.assertIsInstance(_name, str)
11821182
self.assertEqual(name, _name)
11831183

1184-
@unittest.expectedFailure # TODO: RUSTPYTHON; OverflowError: Python int too large to convert to Rust u32
11851184
@unittest.skipUnless(hasattr(socket, 'if_indextoname'),
11861185
'socket.if_indextoname() not available.')
11871186
@support.skip_android_selinux('if_indextoname')
@@ -1249,7 +1248,6 @@ def testNtoH(self):
12491248
self.assertEqual(swapped & mask, mask)
12501249
self.assertRaises(OverflowError, func, 1<<34)
12511250

1252-
@unittest.expectedFailure # TODO: RUSTPYTHON; OverflowError: Python int too large to convert to Rust u16
12531251
def testNtoHErrors(self):
12541252
s_good_values = [0, 1, 2, 0xffff]
12551253
l_good_values = s_good_values + [0xffffffff]

crates/stdlib/src/array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ pub mod array {
498498
($($t:ty,)*) => {$(
499499
impl ArrayElement for $t {
500500
fn try_into_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
501-
obj.try_index(vm)?.try_to_primitive(vm)
501+
obj.try_index(vm)?.try_to_primitive_raw(vm)
502502
}
503503
fn byteswap(self) -> Self {
504504
<$t>::swap_bytes(self)

crates/stdlib/src/socket.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2230,7 +2230,7 @@ mod _socket {
22302230
let addr = Self::from_tuple(tuple, vm)?;
22312231
let flowinfo = tuple
22322232
.get(2)
2233-
.map(|obj| u32::try_from_borrowed_object(vm, obj))
2233+
.map(|obj| obj.clone().try_index(vm)?.try_to_primitive_raw(vm))
22342234
.transpose()?
22352235
.unwrap_or(0);
22362236
let scopeid = tuple
@@ -3141,14 +3141,16 @@ mod _socket {
31413141

31423142
#[cfg(all(unix, not(target_os = "redox")))]
31433143
#[pyfunction(name = "CMSG_LEN")]
3144-
fn cmsg_len(length: usize, vm: &VirtualMachine) -> PyResult<usize> {
3144+
fn cmsg_len(length: PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {
3145+
let length = length.try_index(vm)?.try_to_primitive_raw(vm)?;
31453146
host_socket::checked_cmsg_len(length)
31463147
.ok_or_else(|| vm.new_overflow_error("CMSG_LEN() argument out of range"))
31473148
}
31483149

31493150
#[cfg(all(unix, not(target_os = "redox")))]
31503151
#[pyfunction(name = "CMSG_SPACE")]
3151-
fn cmsg_space(length: usize, vm: &VirtualMachine) -> PyResult<usize> {
3152+
fn cmsg_space(length: PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {
3153+
let length = length.try_index(vm)?.try_to_primitive_raw(vm)?;
31523154
host_socket::checked_cmsg_space(length)
31533155
.ok_or_else(|| vm.new_overflow_error("CMSG_SPACE() argument out of range"))
31543156
}

crates/vm/src/builtins/int.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -343,13 +343,17 @@ impl PyInt {
343343
where
344344
I: PrimInt + TryFrom<&'a BigInt>,
345345
{
346-
// TODO: Python 3.14+: ValueError for negative int to unsigned type
347-
// See stdlib_socket.py socket.htonl(-1)
348-
//
349-
// if I::min_value() == I::zero() && self.as_bigint().sign() == Sign::Minus {
350-
// return Err(vm.new_value_error("Cannot convert negative int".to_owned()));
351-
// }
346+
if I::min_value() == I::zero() && self.as_bigint().sign() == Sign::Minus {
347+
return Err(vm.new_value_error("can't convert negative number to unsigned"));
348+
}
349+
350+
self.try_to_primitive_raw(vm)
351+
}
352352

353+
pub fn try_to_primitive_raw<'a, I>(&'a self, vm: &VirtualMachine) -> PyResult<I>
354+
where
355+
I: PrimInt + TryFrom<&'a BigInt>,
356+
{
353357
I::try_from(self.as_bigint()).map_err(|_| {
354358
vm.new_overflow_error(format!(
355359
"Python int too large to convert to Rust {}",

crates/vm/src/stdlib/os.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1870,7 +1870,8 @@ pub(super) mod _os {
18701870

18711871
#[cfg(windows)]
18721872
#[pyfunction]
1873-
fn waitstatus_to_exitcode(status: u64, vm: &VirtualMachine) -> PyResult<u32> {
1873+
fn waitstatus_to_exitcode(status: PyObjectRef, vm: &VirtualMachine) -> PyResult<u32> {
1874+
let status = status.try_index(vm)?.try_to_primitive_raw::<u64>(vm)?;
18741875
let exitcode = status >> 8;
18751876
// ExitProcess() accepts an UINT type:
18761877
// reject exit code which doesn't fit in an UINT

0 commit comments

Comments
 (0)