Skip to content

Add weakref support to c-api#8006

Merged
youknowone merged 1 commit into
RustPython:mainfrom
bschoenmaeckers:c-api-weakref
Jun 2, 2026
Merged

Add weakref support to c-api#8006
youknowone merged 1 commit into
RustPython:mainfrom
bschoenmaeckers:c-api-weakref

Conversation

@bschoenmaeckers
Copy link
Copy Markdown
Contributor

@bschoenmaeckers bschoenmaeckers commented Jun 1, 2026

Summary by CodeRabbit

  • New Features

    • Full weak-reference support: create and manage weak references and proxies, upgrade weakrefs to their targets, and create proxies with optional callbacks.
    • Exposed FFI-level weakref APIs for interoperability with external/native callers.
  • Refactor

    • Centralized weak-proxy construction for more consistent behavior and reuse.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 1, 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: 1ddf3942-bec1-44ab-bcad-9ef7406f5724

📥 Commits

Reviewing files that changed from the base of the PR and between 9edf71d and c85af7d.

📒 Files selected for processing (3)
  • crates/capi/src/lib.rs
  • crates/capi/src/weakrefobject.rs
  • crates/vm/src/builtins/weakproxy.rs
🚧 Files skipped from review as they are similar to previous changes (3)
  • crates/capi/src/lib.rs
  • crates/capi/src/weakrefobject.rs
  • crates/vm/src/builtins/weakproxy.rs

📝 Walkthrough

Walkthrough

This PR refactors VM weakproxy construction and exposes a new capi weakrefobject module that implements three C-ABI functions: PyWeakref_NewRef, PyWeakref_NewProxy, and PyWeakref_GetRef to create and upgrade weak references/proxies.

Changes

Weakref FFI Support

Layer / File(s) Summary
PyWeakProxy refactoring and public APIs
crates/vm/src/builtins/weakproxy.rs
py_new delegates weak creation to Self::new_weak(...). Adds public new_weakproxy(referent, callback, vm) constructor and get_weak() accessor for the stored PyRef<PyWeak>.
FFI weakref module and C-ABI functions
crates/capi/src/lib.rs, crates/capi/src/weakrefobject.rs
Exports weakrefobject in capi and adds three exported functions: PyWeakref_GetRef (upgrade weakref/proxy into target, write pointer or NULL), PyWeakref_NewProxy (create weak proxy with optional callback), and PyWeakref_NewRef (create weak reference with optional callback). Includes type-check helpers and a disabled test module.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Suggested reviewers

  • youknowone

Poem

🐇 I hop with tiny paws through reference and thread,
I tuck callbacks softly where the weak ones tread.
From Rust-bound core to C-ABI I guide,
Proxies and refs now stride side by side.
Hooray — a whispering hop for weakref pride.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 72.73% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Add weakref support to c-api' accurately and concisely summarizes the main change: adding weakref (weak reference) functionality to the C API module.
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.

@bschoenmaeckers bschoenmaeckers force-pushed the c-api-weakref branch 2 times, most recently from f0dd96c to 9edf71d Compare June 1, 2026 19:31
@youknowone youknowone merged commit 2c46b69 into RustPython:main Jun 2, 2026
26 checks passed
@bschoenmaeckers bschoenmaeckers deleted the c-api-weakref branch June 2, 2026 07:24
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