Skip to content

Add object protocol methods to c-api#7882

Merged
youknowone merged 1 commit into
RustPython:mainfrom
bschoenmaeckers:c-api-abstract
May 17, 2026
Merged

Add object protocol methods to c-api#7882
youknowone merged 1 commit into
RustPython:mainfrom
bschoenmaeckers:c-api-abstract

Conversation

@bschoenmaeckers
Copy link
Copy Markdown
Contributor

@bschoenmaeckers bschoenmaeckers commented May 16, 2026

Summary by CodeRabbit

  • New Features
    • Extended the C API surface with new object operations for getting, setting, and deleting items, and added subclass/instance checks—improving compatibility with C extensions and interoperability with code relying on these fundamental object behaviors.
    • Exposed the new API group at crate/module level so it’s available to C consumers.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 16, 2026

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: 9a2da496-c73c-4ba0-b221-9349fd1a1841

📥 Commits

Reviewing files that changed from the base of the PR and between 8cc3821 and 4357f78.

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

📝 Walkthrough

Walkthrough

A new FFI module adds five C-ABI extern functions that wrap PyObject operations (GetItem, SetItem, DelItem, IsSubclass, IsInstance), converting raw *mut PyObject pointers to Rust references and executing the underlying methods inside with_vm.

Changes

PyObject FFI Wrappers

Layer / File(s) Summary
Module declaration and imports
crates/capi/src/lib.rs, crates/capi/src/abstract_.rs
Module abstract_ is declared in the crate root. Imports for PyObject, with_vm, and c_int are established in the new module.
Item access FFI wrappers
crates/capi/src/abstract_.rs
PyObject_GetItem, PyObject_SetItem, and PyObject_DelItem expose subscript-like operations as C-ABI wrappers, converting raw pointers and forwarding to underlying PyObject methods within a VM context.
Type checking FFI wrappers
crates/capi/src/abstract_.rs
PyObject_IsSubclass and PyObject_IsInstance expose type relationship checks as C-ABI wrappers, dereferencing raw class/instance pointers and forwarding to the corresponding PyObject methods.
sequenceDiagram
  participant Caller
  participant PyObject_FFI
  participant VM
  participant PyObject
  Caller->>PyObject_FFI: PyObject_GetItem(obj, key)
  PyObject_FFI->>VM: with_vm(call get_item on obj)
  VM->>PyObject: get_item(key)
  PyObject-->>VM: result PyObject*
  VM-->>PyObject_FFI: returned pointer
  PyObject_FFI-->>Caller: result pointer
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 I hopped through FFI and found,

Pointers wrapped and calls unbound.
Get and Set beneath the VM moon,
Delete and check — a quick buffoon.
Rust carrots, C-ABI tune.

🚥 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 accurately describes the main change: adding FFI functions for object protocol methods (get_item, set_item, del_item, is_subclass, is_instance) to the c-api module.
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.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

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_.rs`:
- Around line 5-11: The exported FFI function(s) like PyObject_GetItem (and the
other four exported functions in this module) currently have safe fn signatures
but dereference raw pointers (unsafe { &*obj }), so change their signatures to
pub unsafe extern "C" fn ... to reflect the required caller-safety contract;
update all five exported functions' declarations (keeping extern "C" and return
types) to be unsafe, and ensure any internal unsafe blocks remain as needed so
the functions properly express that callers must provide valid, non-null,
aligned pointers.
🪄 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: 29e4e1e8-0de0-495c-b69c-06e5355b891e

📥 Commits

Reviewing files that changed from the base of the PR and between 0871bc8 and 6a28e4e.

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

Comment thread crates/capi/src/abstract_.rs Outdated
@youknowone youknowone merged commit 3a56f37 into RustPython:main May 17, 2026
26 checks passed
@bschoenmaeckers bschoenmaeckers deleted the c-api-abstract branch May 17, 2026 11:34
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