Skip to content

Add more object c-api's#8170

Merged
youknowone merged 1 commit into
RustPython:mainfrom
bschoenmaeckers:c-api-more-object
Jun 26, 2026
Merged

Add more object c-api's#8170
youknowone merged 1 commit into
RustPython:mainfrom
bschoenmaeckers:c-api-more-object

Conversation

@bschoenmaeckers

@bschoenmaeckers bschoenmaeckers commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • New Features

    • Expanded C API surface for object operations, including ASCII/bytes conversion, attribute deletion, generic attribute setting, and richer attribute-existence APIs.
    • Added additional object helpers for negation, hashing, self-iteration, identity checks, and repr enter/leave recursion tracking.
  • Bug Fixes

    • Improved robustness for invalid attribute name text: malformed UTF-8 now raises a proper ValueError instead of triggering unexpected failures.
    • Attribute existence checks now fail safely (returning false while reporting the underlying error) rather than erroring unpredictably.
  • Other

    • Improved rich-compare operator handling and constant lookup behavior.

@coderabbitai

coderabbitai Bot commented Jun 25, 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: 51ac15d2-af59-4882-a1f1-c22381247742

📥 Commits

Reviewing files that changed from the base of the PR and between f90995c and 640375f.

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

📝 Walkthrough

Walkthrough

This PR expands the object C-API with new attribute, deletion, comparison, hash, identity, iterator, and repr-recursion entry points. It also changes string attribute APIs to validate UTF-8 and adds isize FFI output support.

Changes

Object C-API updates

Layer / File(s) Summary
Constant lookup and support imports
crates/capi/src/object.rs
PyComparisonOp and hash_not_implemented are imported, and Py_GetConstantBorrowed/Py_GetConstant now share a helper over runtime singletons.
String attribute access and conversions
crates/capi/src/object.rs
PyObject_GetAttrString, PyObject_GetOptionalAttrString, and PyObject_SetAttrString map invalid UTF-8 to ValueError, and PyObject_ASCII/PyObject_Bytes are exported.
Attribute deletion and generic set
crates/capi/src/object.rs
PyObject_DelAttr, PyObject_DelAttrString, and PyObject_GenericSetAttr add delete support and route null values to deletion.
Attribute existence APIs
crates/capi/src/object.rs
PyObject_HasAttr and PyObject_HasAttrString report failures through vm.run_unraisable(...), while PyObject_HasAttrStringWithError propagates decoding and lookup errors.
Rich compare, hash, and repr exports
crates/capi/src/object.rs
PyObject_RichCompare now uses parse_richcompare_op to map operator IDs, and PyObject_Not, PyObject_Hash, PyObject_HashNotImplemented, PyObject_SelfIter, Py_Is, Py_IsNone, Py_ReprEnter, and Py_ReprLeave are added.
isize FFI result support
crates/capi/src/util.rs
isize gets an FfiResult implementation with -1 as the error sentinel.

Sequence Diagram(s)

sequenceDiagram
  participant Caller as C API caller
  participant HasAttr as PyObject_HasAttrString
  participant Obj as obj.has_attr
  participant Unraisable as vm.run_unraisable

  Caller->>HasAttr: attr_name
  HasAttr->>Obj: decode UTF-8 and look up attribute
  alt lookup succeeds
    Obj-->>HasAttr: bool
    HasAttr-->>Caller: 0 or 1
  else decode or lookup fails
    HasAttr->>Unraisable: report error
    HasAttr-->>Caller: false
  end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

Suggested reviewers

  • ShaharNaveh
  • youknowone

Poem

A rabbit hopped through C-API lace,
With hash and repr and attrs in place.
No UTF-8 tumble, just ValueError’s glow,
And -1 hops home when hash winds blow. 🐇
Thump-thump, the object forest sings!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Title check ❓ Inconclusive The title is related, but it is too generic to clearly describe the main API additions and behavior changes. Use a more specific title, such as adding object-related C-API functions and attribute/hash/repr helpers.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed Docstring coverage is 94.29% 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.

@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/object.rs`:
- Around line 324-326: The UTF-8 decode failure in the attribute-name handling
path is being swallowed by returning false directly, so invalid input is not
reported through the unraisable error flow. Update the attr-name conversion
logic in PyObject_HasAttrString/object lookup handling to detect the to_str
failure, invoke vm.run_unraisable with the conversion error, and then return
false so it matches the forgiving HasAttr behavior and does not silently drop
the error.
🪄 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: 59a6987b-4a8a-45aa-af6e-46a55d8f57c3

📥 Commits

Reviewing files that changed from the base of the PR and between c876709 and f90995c.

📒 Files selected for processing (2)
  • crates/capi/src/object.rs
  • crates/capi/src/util.rs

Comment thread crates/capi/src/object.rs
@bschoenmaeckers
bschoenmaeckers force-pushed the c-api-more-object branch 2 times, most recently from 7df5737 to 0eccf4b Compare June 25, 2026 19:30
@youknowone
youknowone merged commit c449564 into RustPython:main Jun 26, 2026
26 checks passed
@bschoenmaeckers
bschoenmaeckers deleted the c-api-more-object branch June 26, 2026 11:44
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