Skip to content

Add more int functions to the c-api#8159

Merged
youknowone merged 3 commits into
RustPython:mainfrom
bschoenmaeckers:c-api-more-long
Jun 30, 2026
Merged

Add more int functions to the c-api#8159
youknowone merged 3 commits 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 integer C API support with new PyLong_From* constructors for doubles, signed/unsigned 32- and 64-bit values, native-endian byte buffers, C strings (optional end-pointer), and void*/pointer-sized values.
    • Added a wider set of PyLong_As* conversions (typed signed/unsigned, size types, and mask variants), replacing PyLong_AsLong.
  • Bug Fixes
    • Improved unsigned mask extraction for negative integers using low-order digits.
  • Tests
    • Extended C/API conversion coverage for sentinel/error-value behavior and added large-integer roundtrip assertions.

@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: 9be578bf-0880-4423-b33e-377d1db93471

📥 Commits

Reviewing files that changed from the base of the PR and between 4ad78c4 and 18f4929.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (3)
  • .cspell.dict/cpython.txt
  • crates/capi/Cargo.toml
  • crates/capi/src/longobject.rs
🚧 Files skipped from review as they are similar to previous changes (2)
  • crates/capi/Cargo.toml
  • crates/capi/src/longobject.rs

📝 Walkthrough

Walkthrough

Expanded the PyLong C-API with new constructors and extractors, extended integer FfiResult outputs, and changed PyInt unsigned mask handling.

Changes

PyLong C-API expansion

Layer / File(s) Summary
Add PyLong constructors
crates/capi/Cargo.toml, crates/capi/src/longobject.rs, .cspell.dict/cpython.txt
Adds constructors from floating-point, fixed-width integers, pointers, native-byte buffers, and C strings, with native-byte flag parsing support and a new bigint dependency.
Add PyLong numeric extractors
crates/capi/src/longobject.rs
Adds floating-point, signed, unsigned, out-parameter, and pointer extraction APIs, plus the updated unsigned-long-long conversion path and disabled u128 round-trip test.
Extend FfiResult for integer outputs
crates/capi/src/util.rs
Adds direct-output FfiResult implementations for additional C integer types and adds sentinel-value tests for the new instantiations.
Fix u32/u64 digit masking in PyInt
crates/vm/src/builtins/int.rs
Changes as_u32_mask and as_u64_mask to derive masked values from leading digits and apply wrapping_neg() for negative values.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related issues

Possibly related PRs

Suggested reviewers

  • youknowone
  • ShaharNaveh

Poem

🐇 I hopped through ints both small and wide,
and pointer paws now fit just right.
Masked bits twinkle, neat and clear,
while bigint moons stay round and near.
Hoppity hop, the C-API sings!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 72.09% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 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 the C-API with more integer-related functions.
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.
✨ 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.

@bschoenmaeckers
bschoenmaeckers force-pushed the c-api-more-long branch 5 times, most recently from 6aa3a82 to 3d85bb2 Compare June 25, 2026 11:30
@bschoenmaeckers

Copy link
Copy Markdown
Contributor Author

@coderabbitai resume

@coderabbitai

coderabbitai Bot commented Jun 25, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Reviews resumed.

Comment thread crates/vm/src/builtins/int.rs Outdated
#[must_use]
pub fn as_u64_mask(&self) -> u64 {
let v = self.as_bigint();
v.to_u64()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I didn't investigate the detail yet, but trying to_u64 looks like redundant operation if the fallback still use the last digits

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed. Looking at the simplified logic this feels redundant.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've also applied this to the u32 function

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@crates/capi/src/longobject.rs`:
- Around line 92-106: The flag parsing in
AsNativeBytesFlags::from_bits_or_default currently accepts the reserved endian
value 2 because it passes through AsNativeBytesFlags::from_bits; add an explicit
rejection for that raw flag before the generic bitmask check so
PyLong_FromNativeBytes and PyLong_FromUnsignedNativeBytes raise ValueError for 2
while still preserving the existing default behavior for -1. Keep the Default
implementation unchanged, since Self::NATIVE_ENDIAN | Self::UNSIGNED_BUFFER is
correct, and ensure the new validation is localized to from_bits_or_default so
both call sites inherit it automatically.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: e0d68fea-f4dd-4ac0-9ab6-740c9eee9e6b

📥 Commits

Reviewing files that changed from the base of the PR and between b8835a8 and 4cf4334.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (2)
  • crates/capi/Cargo.toml
  • crates/capi/src/longobject.rs

Comment thread crates/capi/src/longobject.rs
@bschoenmaeckers

Copy link
Copy Markdown
Contributor Author

@coderabbitai resume

@coderabbitai

coderabbitai Bot commented Jun 26, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Reviews resumed.

@bschoenmaeckers
bschoenmaeckers force-pushed the c-api-more-long branch 2 times, most recently from 2bffd0c to 18e54a6 Compare June 26, 2026 07:40
@youknowone
youknowone merged commit 33b8c10 into RustPython:main Jun 30, 2026
26 checks passed
@bschoenmaeckers
bschoenmaeckers deleted the c-api-more-long branch June 30, 2026 15:12
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.

2 participants