Fix 128-bit int regression on big-endian with Python <3.13 (#4291)

Fixes #4290.
This commit is contained in:
Ben Beasley 2024-06-26 15:21:31 -04:00 committed by GitHub
parent 7c2f5e80de
commit 8f7450e33d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 10 deletions

View File

@ -0,0 +1 @@
Fix 128-bit int regression on big-endian platforms with Python <3.13

View File

@ -238,15 +238,18 @@ mod fast_128bit_int_conversion {
unsafe { ffi::PyNumber_Index(ob.as_ptr()).assume_owned_or_err(ob.py())? }; unsafe { ffi::PyNumber_Index(ob.as_ptr()).assume_owned_or_err(ob.py())? };
let mut buffer = [0u8; std::mem::size_of::<$rust_type>()]; let mut buffer = [0u8; std::mem::size_of::<$rust_type>()];
#[cfg(not(Py_3_13))] #[cfg(not(Py_3_13))]
crate::err::error_on_minusone(ob.py(), unsafe { {
ffi::_PyLong_AsByteArray( crate::err::error_on_minusone(ob.py(), unsafe {
num.as_ptr() as *mut ffi::PyLongObject, ffi::_PyLong_AsByteArray(
buffer.as_mut_ptr(), num.as_ptr() as *mut ffi::PyLongObject,
buffer.len(), buffer.as_mut_ptr(),
1, buffer.len(),
$is_signed.into(), 1,
) $is_signed.into(),
})?; )
})?;
Ok(<$rust_type>::from_le_bytes(buffer))
}
#[cfg(Py_3_13)] #[cfg(Py_3_13)]
{ {
let mut flags = ffi::Py_ASNATIVEBYTES_NATIVE_ENDIAN; let mut flags = ffi::Py_ASNATIVEBYTES_NATIVE_ENDIAN;
@ -272,8 +275,8 @@ mod fast_128bit_int_conversion {
"Python int larger than 128 bits", "Python int larger than 128 bits",
)); ));
} }
Ok(<$rust_type>::from_ne_bytes(buffer))
} }
Ok(<$rust_type>::from_ne_bytes(buffer))
} }
#[cfg(feature = "experimental-inspect")] #[cfg(feature = "experimental-inspect")]