Skip to content

add PyLong* API (3.14+) - #6016

Merged
davidhewitt merged 60 commits into
PyO3:mainfrom
chirizxc:PyLongWriter
Jun 10, 2026
Merged

add PyLong* API (3.14+)#6016
davidhewitt merged 60 commits into
PyO3:mainfrom
chirizxc:PyLongWriter

Conversation

@chirizxc

@chirizxc chirizxc commented May 5, 2026

Copy link
Copy Markdown
Contributor

/close #6015

(Edit by @davidhewitt) - See codspeed comment for benchmark improvements.

@ngoldbaum

This comment was marked as resolved.

@chirizxc chirizxc closed this May 5, 2026
@chirizxc chirizxc reopened this May 5, 2026

@ngoldbaum ngoldbaum left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did a quick pass and spotted a couple issues.

Comment thread src/conversions/std/num.rs Outdated
Comment thread src/conversions/std/num.rs Outdated
@ngoldbaum

Copy link
Copy Markdown
Contributor

I looked over the FFI changes.

While we're touching longobject.rs, I'd appreciate it if you could make sure that your updates are ordered following CPython's Include/cpython/longobject.h as much as possible. For example, there are comments about skipping PyUnstable_Long_IsCompact and PyUnstable_Long_CompactValue, but those comments should follow the definition for PyLong_FromUnicodeObject because that's how the corresponding CPython header is organized in 3.14. Our bindings are also missing Py_ASNATIVEBYTES_ALLOW_INDEX, PyLong_IsPositive, PyLong_IsNegative, and PyLong_IsZero, which we should add.

It also looks like the PyLongLayout and PyLongExport structs are in cpython/longintrepr.h, so the definitions you're adding here should go into a new cpython/longintrepr.rs rust module, along with the C API definitions that are in the corresponding header.

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).

@chirizxc

chirizxc commented May 6, 2026

Copy link
Copy Markdown
Contributor Author

I looked over the FFI changes.

While we're touching longobject.rs, I'd appreciate it if you could make sure that your updates are ordered following CPython's Include/cpython/longobject.h as much as possible. For example, there are comments about skipping PyUnstable_Long_IsCompact and PyUnstable_Long_CompactValue, but those comments should follow the definition for PyLong_FromUnicodeObject because that's how the corresponding CPython header is organized in 3.14. Our bindings are also missing Py_ASNATIVEBYTES_ALLOW_INDEX, PyLong_IsPositive, PyLong_IsNegative, and PyLong_IsZero, which we should add.

It also looks like the PyLongLayout and PyLongExport structs are in cpython/longintrepr.h, so the definitions you're adding here should go into a new cpython/longintrepr.rs rust module, along with the C API definitions that are in the corresponding header.

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, PyLong_FromNativeBytes, Py_ASNATIVEBYTES_BIG_ENDIAN, etc. , were moved from cpython/Include/longobject.h to cpython/longobject.h (see: 3.13* | 3.14*). Should I move them as well? I mean, do I need to fully synchronize the current files with branch 3.14?

@ngoldbaum

Copy link
Copy Markdown
Contributor

Should I move them as well? I mean, do I need to fully synchronize the current files with 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.

@chirizxc

chirizxc commented May 6, 2026

Copy link
Copy Markdown
Contributor Author

I split up some of the extern_libpython! blocks so that the order remains the same as in the *.h files

@ngoldbaum ngoldbaum left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I spotted some issues, see below.

Comment thread pyo3-ffi/src/cpython/longobject.rs
Comment thread pyo3-ffi/src/cpython/longobject.rs
Comment thread src/conversions/std/num.rs Outdated
Comment thread src/conversions/std/num.rs Outdated
Comment thread pyo3-ffi/src/longobject.rs
Comment thread pyo3-benches/benches/bench_int128.rs Outdated
Comment thread pyo3-ffi/src/cpython/longobject.rs Outdated
Comment thread src/conversions/std/num.rs Outdated
@chirizxc

chirizxc commented Jun 5, 2026

Copy link
Copy Markdown
Contributor Author

@davidhewitt can we simply add a new PyLong* API to FFI via a separate PR until a definitive decision is made regarding the error type?

@Tpt

Tpt commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

@davidhewitt can we simply add a new PyLong* API to FFI via a separate PR until a definitive decision is made regarding the error type?

Not @davidhewitt but it sounds like a great idea to me

@davidhewitt

Copy link
Copy Markdown
Member

Sorry for the delay. I've pushed a commit which just keeps the existing inconsistency for now where 3.13+ uses ValueError, and opened #6116 to fix this in the future.

I think let's not delay this further and proceed to ship it as part of 0.29.

@davidhewitt
davidhewitt enabled auto-merge June 9, 2026 09:39
auto-merge was automatically disabled June 9, 2026 14:27

Head branch was pushed to by a user without write access

@chirizxc chirizxc closed this Jun 9, 2026
@chirizxc chirizxc reopened this Jun 9, 2026
@davidhewitt
davidhewitt added this pull request to the merge queue Jun 9, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jun 9, 2026
@davidhewitt davidhewitt added CI-build-full CI-no-fail-fast If one job fails, allow the rest to keep testing labels Jun 9, 2026
@ngoldbaum

Copy link
Copy Markdown
Contributor

There seem to be test failures on a debug Python build:

conversions::std::num::test_128bit_integers::test_u128_negative stdout ----
  
  thread 'conversions::std::num::test_128bit_integers::test_u128_negative' (6298) panicked at src/conversions/std/num.rs:937:13:
  assertion failed: err.is_instance_of::<exceptions::PyValueError>(py)

@davidhewitt

Copy link
Copy Markdown
Member

I think it might just be an abi3 issue related to #6116, skipped the test on it for now and will re-attempt merge.

@davidhewitt
davidhewitt enabled auto-merge June 10, 2026 08:04
@davidhewitt
davidhewitt added this pull request to the merge queue Jun 10, 2026
Merged via the queue into PyO3:main with commit fe0fdd5 Jun 10, 2026
166 of 173 checks passed
@chirizxc

Copy link
Copy Markdown
Contributor Author

thanks

@chirizxc
chirizxc deleted the PyLongWriter branch June 10, 2026 12:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI-build-full CI-no-fail-fast If one job fails, allow the rest to keep testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pyo3-ffi: Add PyLong_* fixed-width integer conversion APIs (3.14+)

5 participants