add PyLong* API (3.14+)#6016
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
ngoldbaum
left a comment
There was a problem hiding this comment.
Did a quick pass and spotted a couple issues.
|
I looked over the FFI changes. While we're touching It also looks like the The principle you should keep in mind when touching PyO3's FFI bindings is that it should be organized exactly like the upstream headers, as of the newest Python we support (3.14 right now, but it'll be 3.15 in the next few weeks). |
In branch 3.14, |
Yes, that's generally what we do. It's hard to keep the FFI bindings perfectly up-to-date, so it's a chore for whoever needs to touch them. |
|
I split up some of the |
ngoldbaum
left a comment
There was a problem hiding this comment.
I spotted some issues, see below.
|
Should we apply this to the |
Good idea. It might be possible to avoid an intermediate allocation on the to-python direction, on the from-python direction I think the |
Yes, but as I alluded to above IMO further work to add more conversions should happen in followups, this PR has already had a bit of scope creep (mea culpa on that). |
Sorry missed your comment. |
davidhewitt
left a comment
There was a problem hiding this comment.
In which case let's move to merge this, and leave an issue to follow up with the num_bigint conversions later.
Merge conflict has arisen, sorry.
| @@ -0,0 +1 @@ | |||
| Changed `IntoPyObject` for `i128`, `u128`, and references to them to return `PyErr`, allowing Python integer creation failures to be propagated. No newline at end of file | |||
There was a problem hiding this comment.
I guess we still need a decision here. Would you be amenable to leaving these implementations to panic for now, and maybe we open an issue to decide if we change this for all implementations separately?
|
https://github.com/search?q=repo%3APyO3%2Fpyo3%20libc%3A%3Asize_t&type=code Should there be another alias for Something like |
|
No, we don't like to create symbols that look like they're exposed by Python unless they actually are. |
davidhewitt
left a comment
There was a problem hiding this comment.
One tiny last thing, otherwise LGTM. Thanks, and also noted you created the follow up issues 👍
Co-authored-by: David Hewitt <mail@davidhewitt.dev>
|
I think I forgot to change the test after 3fdcb06 |
|
|
||
| #[test] | ||
| fn test_u128_negative() { | ||
| Python::attach(|py| { |
There was a problem hiding this comment.
#[cfg(Py_3_13)]
{
let mut flags = ffi::Py_ASNATIVEBYTES_NATIVE_ENDIAN;
if !$is_signed {
flags |= ffi::Py_ASNATIVEBYTES_UNSIGNED_BUFFER
| ffi::Py_ASNATIVEBYTES_REJECT_NEGATIVE;
}On CPython 3.13 / 3.14 / 3.15 Py_ASNATIVEBYTES_REJECT_NEGATIVE flag raises ValueError:
PyErr_SetString(PyExc_ValueError, "Cannot convert negative int");There was a problem hiding this comment.
It seems to me that OverflowError is semantically more appropriate for "the number does not fit the type"
< 3.13: ffi::_PyLong_AsByteArray -> OverflowError
= 3.13: ffi::PyLong_AsNativeBytes + ffi::Py_ASNATIVEBYTES_REJECT_NEGATIVE -> ValueError
≥ 3.14: pylong_visit_digits -> OverflowError
Maybe we should change error type for =3.13?
There was a problem hiding this comment.
Related - #5179
I am not entirely decided whether OverflowError or ValueError is better. I think in other places in this file we use ValueError for the negative case already?
There was a problem hiding this comment.
Related - #5179
I am not entirely decided whether
OverflowErrororValueErroris better. I think in other places in this file we useValueErrorfor the negative case already?
Overall, I don't really care the main thing is that it's consistent and that the same type of error occurs across different versions.
/close #6015
main (3.13):
this PR (3.14):
cmd:
into_u128_zerointo_u128_smallinto_u128_u32_maxinto_u128_u64_maxinto_u128_maxinto_i128_zerointo_i128_small_posinto_i128_small_neginto_i128_pos_maxinto_i128_neg_minextract_u128_zeroextract_u128_smallextract_u128_u32_maxextract_u128_u64_maxextract_u128_maxextract_i128_zeroextract_i128_small_posextract_i128_small_negextract_i128_pos_maxextract_i128_neg_min