Skip to content

Commit 8f7450e

Browse files
Fix 128-bit int regression on big-endian with Python <3.13 (#4291)
Fixes #4290.
1 parent 7c2f5e8 commit 8f7450e

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

newsfragments/4291.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix 128-bit int regression on big-endian platforms with Python <3.13

src/conversions/std/num.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -238,15 +238,18 @@ mod fast_128bit_int_conversion {
238238
unsafe { ffi::PyNumber_Index(ob.as_ptr()).assume_owned_or_err(ob.py())? };
239239
let mut buffer = [0u8; std::mem::size_of::<$rust_type>()];
240240
#[cfg(not(Py_3_13))]
241-
crate::err::error_on_minusone(ob.py(), unsafe {
242-
ffi::_PyLong_AsByteArray(
243-
num.as_ptr() as *mut ffi::PyLongObject,
244-
buffer.as_mut_ptr(),
245-
buffer.len(),
246-
1,
247-
$is_signed.into(),
248-
)
249-
})?;
241+
{
242+
crate::err::error_on_minusone(ob.py(), unsafe {
243+
ffi::_PyLong_AsByteArray(
244+
num.as_ptr() as *mut ffi::PyLongObject,
245+
buffer.as_mut_ptr(),
246+
buffer.len(),
247+
1,
248+
$is_signed.into(),
249+
)
250+
})?;
251+
Ok(<$rust_type>::from_le_bytes(buffer))
252+
}
250253
#[cfg(Py_3_13)]
251254
{
252255
let mut flags = ffi::Py_ASNATIVEBYTES_NATIVE_ENDIAN;
@@ -272,8 +275,8 @@ mod fast_128bit_int_conversion {
272275
"Python int larger than 128 bits",
273276
));
274277
}
278+
Ok(<$rust_type>::from_ne_bytes(buffer))
275279
}
276-
Ok(<$rust_type>::from_ne_bytes(buffer))
277280
}
278281

279282
#[cfg(feature = "experimental-inspect")]

0 commit comments

Comments
 (0)