From 8f7450e33d9edcab790d5b2ad303cbb81a86536e Mon Sep 17 00:00:00 2001 From: Ben Beasley Date: Wed, 26 Jun 2024 15:21:31 -0400 Subject: [PATCH] Fix 128-bit int regression on big-endian with Python <3.13 (#4291) Fixes #4290. --- newsfragments/4291.fixed.md | 1 + src/conversions/std/num.rs | 23 +++++++++++++---------- 2 files changed, 14 insertions(+), 10 deletions(-) create mode 100644 newsfragments/4291.fixed.md diff --git a/newsfragments/4291.fixed.md b/newsfragments/4291.fixed.md new file mode 100644 index 00000000..38242ff7 --- /dev/null +++ b/newsfragments/4291.fixed.md @@ -0,0 +1 @@ +Fix 128-bit int regression on big-endian platforms with Python <3.13 diff --git a/src/conversions/std/num.rs b/src/conversions/std/num.rs index effe7c7c..1304e73e 100644 --- a/src/conversions/std/num.rs +++ b/src/conversions/std/num.rs @@ -238,15 +238,18 @@ mod fast_128bit_int_conversion { unsafe { ffi::PyNumber_Index(ob.as_ptr()).assume_owned_or_err(ob.py())? }; let mut buffer = [0u8; std::mem::size_of::<$rust_type>()]; #[cfg(not(Py_3_13))] - crate::err::error_on_minusone(ob.py(), unsafe { - ffi::_PyLong_AsByteArray( - num.as_ptr() as *mut ffi::PyLongObject, - buffer.as_mut_ptr(), - buffer.len(), - 1, - $is_signed.into(), - ) - })?; + { + crate::err::error_on_minusone(ob.py(), unsafe { + ffi::_PyLong_AsByteArray( + num.as_ptr() as *mut ffi::PyLongObject, + buffer.as_mut_ptr(), + buffer.len(), + 1, + $is_signed.into(), + ) + })?; + Ok(<$rust_type>::from_le_bytes(buffer)) + } #[cfg(Py_3_13)] { let mut flags = ffi::Py_ASNATIVEBYTES_NATIVE_ENDIAN; @@ -272,8 +275,8 @@ mod fast_128bit_int_conversion { "Python int larger than 128 bits", )); } + Ok(<$rust_type>::from_ne_bytes(buffer)) } - Ok(<$rust_type>::from_ne_bytes(buffer)) } #[cfg(feature = "experimental-inspect")]