Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/4291.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix 128-bit int regression on big-endian platforms with Python <3.13
23 changes: 13 additions & 10 deletions src/conversions/std/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")]
Expand Down
Loading