Skip to content

Add more int functions to the c-api#8159

Open
bschoenmaeckers wants to merge 1 commit into
RustPython:mainfrom
bschoenmaeckers:c-api-more-long
Open

Add more int functions to the c-api#8159
bschoenmaeckers wants to merge 1 commit into
RustPython:mainfrom
bschoenmaeckers:c-api-more-long

Conversation

@bschoenmaeckers

@bschoenmaeckers bschoenmaeckers commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • New Features
    • Expanded the C API for converting between Python integers and more C integer widths (32/64, signed/unsigned, size/signed-size), including additional unsigned masking helpers and pointer-based conversions.
    • Added new constructors for creating Python integers from doubles, fixed-width integers, and raw pointers.
  • Bug Fixes
    • Improved unsigned-masking behavior for large and negative integers to produce more reliable masked results.
  • Tests
    • Added tests covering extraction of very large integer values and validation of conversion sentinel values.

@coderabbitai

coderabbitai Bot commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 9191289c-969b-49b6-b082-5bbf35f301f6

📥 Commits

Reviewing files that changed from the base of the PR and between 11de64d and 5c916a4.

📒 Files selected for processing (4)
  • crates/capi/src/lib.rs
  • crates/capi/src/longobject.rs
  • crates/capi/src/util.rs
  • crates/vm/src/builtins/int.rs
✅ Files skipped from review due to trivial changes (1)
  • crates/capi/src/lib.rs
🚧 Files skipped from review as they are similar to previous changes (2)
  • crates/vm/src/builtins/int.rs
  • crates/capi/src/longobject.rs

📝 Walkthrough

Walkthrough

Expands the C-API integer conversion surface with new PyLong_From* and PyLong_As* functions, updates PyInt unsigned masking behavior, and extends integer FfiResult support and tests.

Changes

PyLong C-API expansion

Layer / File(s) Summary
Fix u32/u64 digit masking in PyInt
crates/vm/src/builtins/int.rs
as_u32_mask and as_u64_mask now derive masked values directly from leading digits, and negative handling uses wrapping_neg().
Extend FfiResult for integer outputs
crates/capi/src/lib.rs, crates/capi/src/util.rs
Adds direct-output FfiResult implementations for c_int, usize, isize, c_ulong, and Windows-only c_longlong/c_ulonglong, with tests for the new sentinel values.
Add PyLong_From* constructors
crates/capi/src/longobject.rs
Adds PyLong_FromDouble, fixed-width signed/unsigned constructors, and PyLong_FromVoidPtr, wired through with_vm.
Add PyLong_As* converters
crates/capi/src/longobject.rs
Replaces PyLong_AsLong with integer-width converters, unsigned mask helpers, PyLong_AsVoidPtr, and a disabled large-u128 round-trip test.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • ShaharNaveh
  • youknowone

Poem

🐇 Hop, hop—new integers gleam,
u32 and u64 flow downstream,
From pointer paws to bigint bright,
The bunny hops with C-API delight,
And u128 bounces back just right.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: expanding C-API integer functions.
Docstring Coverage ✅ Passed Docstring coverage is 86.21% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant