Skip to content

Add iter support to c-api#8035

Merged
youknowone merged 1 commit into
RustPython:mainfrom
bschoenmaeckers:c-api-iter
Jun 4, 2026
Merged

Add iter support to c-api#8035
youknowone merged 1 commit into
RustPython:mainfrom
bschoenmaeckers:c-api-iter

Conversation

@bschoenmaeckers

@bschoenmaeckers bschoenmaeckers commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • New Features
    • Added a C API to compute an object's size.
    • Added C API support for the iterator protocol: checking iterability, obtaining an iterator, and advancing/consuming iterator items.
    • Added C API support for sending values into generators/coroutines and returning yielded or final values.

@coderabbitai

coderabbitai Bot commented Jun 3, 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: a3753766-ca68-4005-af5b-67132ae21efa

📥 Commits

Reviewing files that changed from the base of the PR and between 39772a7 and 21fffc5.

📒 Files selected for processing (2)
  • crates/capi/src/abstract_.rs
  • crates/capi/src/abstract_/iter.rs
🚧 Files skipped from review as they are similar to previous changes (2)
  • crates/capi/src/abstract_.rs
  • crates/capi/src/abstract_/iter.rs

📝 Walkthrough

Walkthrough

Adds an iter submodule re-export and C-ABI FFI bindings for iterator operations (check, get-iter, next-item, next, send) plus a new PyObject_Size function that returns an object's length via obj.length(vm).

Changes

Iterator and Object Measurement C API Extension

Layer / File(s) Summary
Iterator module declaration and re-export
crates/capi/src/abstract_.rs
The iter submodule is declared and its public functions are re-exported via pub use iter::*;.
Core iterator FFI interface
crates/capi/src/abstract_/iter.rs
Adds FFI imports and implements PyIter_Check, PyObject_GetIter, PyIter_NextItem, and PyIter_Next, translating iterator returns and StopIteration to C-visible results and out-pointers.
Generator coroutine send semantics
crates/capi/src/abstract_/iter.rs
Implements PyIter_Send to downcast iterators to PyGenerator, call send, and return yielded values or StopIteration results via the out-pointer and boolean return.
Object size measurement
crates/capi/src/abstract_.rs
Adds PyObject_Size, which invokes obj.length(vm) inside with_vm and returns the resulting size as isize.
Iterator and generator validation tests
crates/capi/src/abstract_/iter.rs
Adds a compile-disabled (#[cfg(false)]) test module covering iterator next and generator send behavior using PyO3 test utilities.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Suggested reviewers

  • youknowone

Poem

A rabbit hops through FFI fields, 🐰
Yielding values, sending seals,
Sizes tallied, iterators stride,
C and Rust now walk side-by-side. ✨

🚥 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 'Add iter support to c-api' directly and clearly summarizes the main change: adding iterator-related C-ABI functions to the C API layer.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% 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 and usage tips.

@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/abstract_/iter.rs`:
- Around line 65-68: In PyIter_Send, change the current hard downcast to
PyGenerator so it matches CPython: first check if arg_obj.is_none(); if so,
attempt to downcast iter_obj to PyGenerator and call as_coro().send(...) when it
is a generator, but if it's not a generator call PyIter::new(iter_obj).next(vm)
to advance the iterator; if arg_obj is not None and iter_obj is not a
PyGenerator, raise a TypeError. Use the existing methods
try_downcast_ref::<PyGenerator>, PyGenerator::as_coro().send, and
PyIter::new(iter_obj).next(vm) to implement the fallback.
🪄 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: 6cd14683-2403-4826-83dd-15a54a1a8120

📥 Commits

Reviewing files that changed from the base of the PR and between 57d1695 and 39772a7.

📒 Files selected for processing (2)
  • crates/capi/src/abstract_.rs
  • crates/capi/src/abstract_/iter.rs

Comment thread crates/capi/src/abstract_/iter.rs Outdated
@youknowone youknowone merged commit dec9942 into RustPython:main Jun 4, 2026
26 checks passed
@bschoenmaeckers bschoenmaeckers deleted the c-api-iter branch June 4, 2026 07:33
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