Skip to content

capi: add PyFrame_GetLocals, GetGlobals, GetBuiltins - #8761

Merged
youknowone merged 1 commit into
RustPython:mainfrom
youknowone:capi-pyframe-getters
Sep 21, 2026
Merged

youknowone merged 1 commit into
RustPython:mainfrom
youknowone:capi-pyframe-getters

Conversation

@youknowone

@youknowone youknowone commented Sep 21, 2026 •

Copy link
Copy Markdown
Member
  • Closes #xxxx

One of checkbox below must be checked.

  • I did not use AI to write the code of this patch.
  • This PR follows our AI policy

Summary

Add PyFrame_GetLocals, PyFrame_GetGlobals, and PyFrame_GetBuiltins as new-reference C-API getters for frame.f_locals, f_globals, and f_builtins.

PyFrame_GetLocals returns the write-through mapping (a FrameLocalsProxy on optimized frames). PyEval_GetFrameLocals remains the independent locals() snapshot.

Tests

  • cd crates/capi && cargo test -- get_locals_globals_builtins
  • cd crates/capi && cargo clippy --all-targets -- -D warnings

Summary by CodeRabbit

  • New Features
    • Added C API access to a frame’s local variables, global namespace, and built-in namespace.
    • Applications using the C API can now retrieve these frame contexts directly.
    • Local-variable access supports updates through the standard locals interface, while snapshot access remains non-writing.
    • Global and built-in namespace retrieval preserves object identity for consistent access.

Wire the C-API getters to frame.f_locals, f_globals, and f_builtins,
and cover write-through vs snapshot locals in a capi test.

Assisted-by: Grok:4.6
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@coderabbitai

coderabbitai Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

Understand this PR’s impact

Explore downstream dependencies and potential security impact with Blast Radius.

View blast radius →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository: RustPython/RustPython/.coderabbit.yml

Review profile: CHILL

Plan: Advanced

Run ID: d3ed91fa-0dd8-4f91-911e-a66d2a39e417

📥 Commits

Reviewing files that changed from the base of the PR and between 53a906c and 3664693.

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

Included review availability: Your plan provides up to 10 included reviews per hour; 8 remain after this review.


📝 Walkthrough

Walkthrough

The change exposes frame locals, globals, and builtins through public VM accessors and adds corresponding C API functions. Tests cover locals write behavior, locals snapshots, and globals or builtins object identity.

Changes

Frame accessors

Layer / File(s) Summary
Public VM frame accessors
crates/vm/src/builtins/frame.rs
The f_globals, f_builtins, and f_locals accessors are made public. Their behavior remains unchanged.
C API bindings and validation
crates/capi/src/pyframe.rs
Adds PyFrame_GetLocals, PyFrame_GetGlobals, and PyFrame_GetBuiltins. Tests cover locals writes, locals snapshots, and globals or builtins identity.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Feature

Suggested reviewers: bschoenmaeckers, shaharnaveh

Sequence Diagram(s)

sequenceDiagram
  participant CAPI as C API caller
  participant FrameAPI as Frame C API
  participant Frame as FrameObject
  participant Objects as Frame objects
  CAPI->>FrameAPI: Call a frame getter
  FrameAPI->>Frame: Read locals, globals, or builtins
  Frame-->>FrameAPI: Return the corresponding object
  FrameAPI-->>CAPI: Return PyObject pointer
Loading
🚥 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 clearly and concisely identifies the three new C API getters added by the pull request.
Docstring Coverage ✅ Passed Docstring coverage is 85.71% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 14 functions across 2 files.
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 a new PR

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.

@youknowone
youknowone merged commit 8035209 into RustPython:main Sep 21, 2026
21 checks passed
@youknowone
youknowone deleted the capi-pyframe-getters branch September 21, 2026 10:38
@bschoenmaeckers

Copy link
Copy Markdown
Contributor

These are not part of the limited api. Do you need them for something?

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