Skip to content

Add more object functions to c-api#8042

Open
bschoenmaeckers wants to merge 3 commits into
RustPython:mainfrom
bschoenmaeckers:c-api-object
Open

Add more object functions to c-api#8042
bschoenmaeckers wants to merge 3 commits into
RustPython:mainfrom
bschoenmaeckers:c-api-object

Conversation

@bschoenmaeckers
Copy link
Copy Markdown
Contributor

@bschoenmaeckers bschoenmaeckers commented Jun 4, 2026

Summary by CodeRabbit

  • New Features
    • Expanded C-API for object introspection and operations: attribute access, type/module info, callable checks, rich comparisons, directory listing, weak-ref clearing, and generic dict get/set for objects.
  • Chores
    • Previously internal object helpers made public via the builtins surface for broader reuse and a new public method to clear object weak references.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 4, 2026

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: 40c88a66-ba33-465d-a036-d562e3d92809

📥 Commits

Reviewing files that changed from the base of the PR and between 0597510 and 9331963.

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

📝 Walkthrough

Walkthrough

Adds multiple C-API object/type helpers (type module-name, optional/generic attr access, rich compare, callable/dir, generic dict get/set, weakref clearing) and makes VM object dict utilities and a weakref-clear method public and re-exported.

Changes

C-API Object Introspection and Manipulation Functions

Layer / File(s) Summary
VM object utilities public API
crates/vm/src/builtins/object.rs, crates/vm/src/builtins/mod.rs, crates/vm/src/object/core.rs
object_get_dict and object_generic_set_dict changed from pub(crate) to pub and are re-exported; PyObject::clear_weak_refs() added to clear an object's weakref list.
C-API object and type introspection functions
crates/capi/src/object.rs
Adds PyType_GetModuleName, PyObject_GetOptionalAttr, PyObject_HasAttrWithError, PyObject_GenericGetAttr, PyObject_RichCompare, PyCallable_Check, PyObject_ClearWeakRefs, PyObject_Dir, PyObject_GenericGetDict, and PyObject_GenericSetDict; updates FFI imports and includes a disabled test module.

Sequence Diagram(s)

sequenceDiagram
  participant CCaller
  participant CApiLayer
  participant VMBuiltins
  participant VMCore
  CCaller->>CApiLayer: PyObject_GetOptionalAttr(obj, name, out)
  CApiLayer->>VMCore: generic_getattr / has_attr
  VMCore-->>CApiLayer: attribute pointer / NULL
  CCaller->>CApiLayer: PyObject_GenericSetDict(obj, value, ctx)
  CApiLayer->>VMBuiltins: object_generic_set_dict(obj, value, vm)
  VMBuiltins-->>CApiLayer: status (0/1)
  CCaller->>CApiLayer: PyObject_ClearWeakRefs(obj)
  CApiLayer->>VMCore: PyObject::clear_weak_refs()
  VMCore-->>CApiLayer: cleared
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • youknowone
  • ShaharNaveh

Poem

🐰 I hopped through code with nimble paws,

Exposed some attrs and cleared weak laws,
Dicts now open, comparisons sing,
C callers knock at every ring,
A rabbit cheers — the API claws!

🚥 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 multiple new object functions to the C-API export surface in crates/capi/src/object.rs.
Docstring Coverage ✅ Passed Docstring coverage is 84.21% 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: 2

🤖 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 273-279: PyObject_Dir currently dereferences obj unconditionally;
add a fast-path at the start of PyObject_Dir to check if obj.is_null() and
immediately return std::ptr::null_mut() (matching CPython semantics of returning
NULL without setting an exception) before calling with_vm or dereferencing; keep
the existing with_vm(... unsafe { &*obj }.to_owned().dir(vm).map(|list|
list.into_ref(&vm.ctx)) logic for the non-null path so behavior of to_owned(),
dir(), and into_ref() is unchanged.
- Around line 302-311: The function PyObject_GenericSetDict currently
unconditionally dereferences value causing UB when the descriptor protocol
passes NULL to signal deletion; change the code to check value with
NonNull::new(value) and map to PySetterValue::Delete when NULL (or
PySetterValue::Assign(non_null.to_owned()) when non-null) before calling
object_generic_set_dict so deletion flows through correctly and avoids
dereferencing a NULL pointer.
🪄 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: a7337516-4056-43f8-814e-250e06407d98

📥 Commits

Reviewing files that changed from the base of the PR and between 51c97b9 and 998109f.

📒 Files selected for processing (4)
  • crates/capi/src/object.rs
  • crates/vm/src/builtins/mod.rs
  • crates/vm/src/builtins/object.rs
  • crates/vm/src/object/core.rs

Comment thread crates/capi/src/object.rs
Comment thread crates/capi/src/object.rs
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.

1 participant