Add list support to c-api by bschoenmaeckers · Pull Request #7944 · RustPython/RustPython
Note
Reviews paused
It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.
Use the following commands to manage reviews:
@coderabbitai resumeto resume automatic reviews.@coderabbitai reviewto trigger a single review.
Use the checkboxes below for quick actions:
-
▶️ Resume reviews - 🔍 Trigger review
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: afee3a35-704e-4f72-95d2-08d90133071c
📒 Files selected for processing (2)
crates/capi/src/lib.rscrates/capi/src/listobject.rs
📝 Walkthrough
Walkthrough
Adds a public listobject module and implements C-API list functions (New, Size, GetItemRef, SetItem, Append, Insert, Reverse) that run inside the VM, downcast to RustPython lists, perform bounds-checked reads/mutations, and return Python index errors on invalid indices.
Changes
C-API List Object Support
| Layer / File(s) | Summary |
|---|---|
Module declaration and list helpers crates/capi/src/lib.rs, crates/capi/src/listobject.rs |
Module listobject is publicly exported; PyList_Check/PyList_CheckExact helpers are defined. |
List construction and query operations crates/capi/src/listobject.rs |
PyList_New creates a list with reserved capacity; PyList_Size returns length; PyList_GetItemRef retrieves items by index with bounds checking and VM index errors. |
List mutation operations crates/capi/src/listobject.rs |
PyList_SetItem assigns items (rejects negative indices; includes capacity-based fill special-case), PyList_Append appends, PyList_Insert inserts with normalized/clamped index handling, and PyList_Reverse reverses in place; functions return c_int status and use VM-created errors on failure. |
Unit tests (disabled) crates/capi/src/listobject.rs |
Disabled #[cfg(false)] PyO3 test module covering creation, replacement, bounds errors, append, insert, and reverse. |
Sequence Diagram
sequenceDiagram
participant CCaller as C Caller
participant PyList_CAPI as PyList C-API
participant VM as VM
participant PyList as PyList
CCaller->>PyList_CAPI: call PyList_New(size) / PyList_* APIs
PyList_CAPI->>VM: with_vm (enter VM context)
VM->>PyList: allocate or downcast to PyList and operate on Vec
PyList->>VM: return results/errors
VM->>PyList_CAPI: propagate return value / error
PyList_CAPI->>CCaller: return pointer or status
Estimated code review effort
🎯 3 (Moderate) | ⏱️ ~20 minutes
Possibly related PRs
- RustPython/RustPython#7871: Introduced the
define_py_check!macro used forPyList_Check/PyList_CheckExact.
Suggested reviewers
- ShaharNaveh
- youknowone
Poem
🐇 I nibbled bytes and stitched a list so neat,
New, Size, Get, Set — each hop a small feat,
Append and Insert, then Reverse on repeat,
A tiny rabbit patch where C and VM greet,
Soft paws pushing items in tidy beat.
🚥 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 list support to c-api' directly and clearly summarizes the main change: adding C-API functions for list operations to the RustPython project. |
| 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.
Comment @coderabbitai help to get the list of available commands and usage tips.