Skip to content

Add descriptor support to c-api - #8283

Merged
youknowone merged 4 commits into
RustPython:mainfrom
bschoenmaeckers:c-api-desc
Jul 15, 2026
Merged

Add descriptor support to c-api#8283
youknowone merged 4 commits into
RustPython:mainfrom
bschoenmaeckers:c-api-desc

Conversation

@bschoenmaeckers

@bschoenmaeckers bschoenmaeckers commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • New Features
    • Added C API constructors to create method, class method, get/set, member, wrapper, and mapping-proxy descriptors.
    • Improved validation and error handling for descriptor definitions (including UTF-8 fields and read-only behavior).
    • Re-exported descriptor-related types in the VM built-ins for simpler access.
  • Tests
    • Added a unit test verifying correct read behavior through mapping proxies.

@coderabbitai

coderabbitai Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

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 resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Adds C ABI definitions and exported constructors for RustPython method, class-method, get/set, member, mapping-proxy, and wrapper descriptors, with validation, VM execution, callback handling, and exception translation.

Changes

Descriptor FFI integration

Layer / File(s) Summary
Descriptor ABI contracts and VM exports
crates/capi/src/descrobject.rs, crates/vm/src/builtins/mod.rs
Defines PyGetSetDef and PyMemberDef, implements callback-backed get/set behavior, and re-exports the VM descriptor types required by the C API.
Method and mapping constructors
crates/capi/src/descrobject.rs
Adds PyDictProxy_New, PyDescr_NewMethod, and PyDescr_NewClassMethod, executing construction within the VM context.
Get/set, member, and wrapper behavior
crates/capi/src/descrobject.rs
Adds UTF-8 and metadata validation, member descriptor construction, native error translation, wrapper binding through __get__, and mapping-proxy coverage.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant C_API_Caller
  participant DescriptorConstructor
  participant RustPython_VM
  participant Python_Descriptor
  C_API_Caller->>DescriptorConstructor: call PyDescr_NewGetSet or PyDescr_NewMember
  DescriptorConstructor->>RustPython_VM: execute construction through with_vm
  RustPython_VM->>Python_Descriptor: construct descriptor with callbacks and metadata
  Python_Descriptor-->>C_API_Caller: return PyObject or translated exception
  C_API_Caller->>DescriptorConstructor: call PyWrapper_New
  DescriptorConstructor->>RustPython_VM: call descriptor __get__ with obj and obj.class()
  RustPython_VM-->>C_API_Caller: return bound object
Loading

Possibly related PRs

Suggested reviewers: youknowone

🚥 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 summarizes the main change: adding descriptor-related C-API support.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.

@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: 3

🤖 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/descrobject.rs`:
- Around line 82-184: In the descriptor construction flow around the four `match
(getset.get, getset.set)` arms, extract shared `wrap_native_getter` and
`wrap_native_setter` helpers and have every arm reuse them. Make the getter
wrapper convert a NULL native return into the raised exception or a
`SystemError` via `vm.take_raised_exception().unwrap_or_else(...)` instead of
panicking, while preserving the existing unreadable-attribute behavior for
missing getters and setters.
- Around line 48-68: Update PyDescr_NewClassMethod to construct the descriptor
with build_classmethod instead of build_method, while preserving its existing
type and method handling; leave PyDescr_NewMethod unchanged.
- Around line 70-184: Update PyDescr_NewGetSet to propagate getset.doc into
every descriptor constructor, preserving the C extension docstring for both
readable and setter-only descriptors. In all getter closures, replace the
NULL-return expect panic with a SystemError when no exception is set, while
continuing to propagate an already-raised exception.
🪄 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: defaults

Review profile: CHILL

Plan: Pro

Run ID: 35e2d182-0118-411d-a8dc-2dfdcd287ec9

📥 Commits

Reviewing files that changed from the base of the PR and between a9c2c52 and 6a68731.

📒 Files selected for processing (2)
  • crates/capi/src/descrobject.rs
  • crates/vm/src/builtins/mod.rs

Comment thread crates/capi/src/descrobject.rs Outdated
Comment thread crates/capi/src/descrobject.rs Outdated
Comment thread crates/capi/src/descrobject.rs Outdated
use rustpython_vm::{Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine};

#[repr(C)]
pub struct PyGetSetDef {

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.

this is part of api... painful

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.

Yes it is...

@youknowone
youknowone merged commit c85e31e into RustPython:main Jul 15, 2026
26 checks passed
@bschoenmaeckers
bschoenmaeckers deleted the c-api-desc branch July 15, 2026 14:46
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