Skip to content

Add more eval functions to c-api#8244

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

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

Conversation

@bschoenmaeckers

@bschoenmaeckers bschoenmaeckers commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • New Features
    • Extended the C evaluation/frame API with new helpers to evaluate frames and fetch the current frame’s globals, locals, and builtins.
    • Added callable introspection via a new function descriptor helper.
  • Bug Fixes
    • Improved FFI return handling for strings and raw pointers, including clearer “no current frame” behavior.
  • Compatibility / API Updates
    • Updated exported frame handle types to provide more reliable access to frame code objects and line numbers.
    • Removed the older builtins accessor in favor of current-frame accessors.

@coderabbitai

coderabbitai Bot commented Jul 8, 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: 948f8770-5ef2-497f-b694-51e17105cff0

📥 Commits

Reviewing files that changed from the base of the PR and between ddf148d and a01eae7.

📒 Files selected for processing (1)
  • crates/capi/src/ceval.rs

📝 Walkthrough

Walkthrough

This PR extends the C-ABI eval and frame surface with frame execution, current-frame accessors, callable descriptor lookup, typed frame/code handles, and new FFI result conversions.

Changes

C-API eval/frame extension

Layer / File(s) Summary
Import and FFI support
crates/capi/src/ceval.rs, crates/capi/src/util.rs
Updates ceval imports and adds FfiResult conversions for raw Py pointers and C strings.
Frame evaluation entry points
crates/capi/src/ceval.rs
Adds PyEval_EvalFrame and PyEval_EvalFrameEx, forwarding execution through vm.run_frame.
Frame accessors
crates/capi/src/ceval.rs
Replaces PyEval_GetBuiltins with current-frame accessors for frame, builtins, globals, locals, and globals/locals pointers derived from the active frame.
Callable descriptor helper
crates/capi/src/ceval.rs
Adds PyEval_GetFuncDesc, which inspects callable runtime classes and returns the matching descriptor suffix.
PyFrameObject FFI handle
crates/capi/src/pyframe.rs
Adds typed PyFrameObject and PyCodeObject handles and updates frame accessors to use them directly.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Possibly related PRs

  • RustPython/RustPython#7927: Overlaps on crates/capi/src/ceval.rs and the PyEval_GetBuiltins surface that this PR replaces with frame-oriented accessors.
  • RustPython/RustPython#8215: Related to the crates/capi/src/pyframe.rs FFI frame accessors that this PR retypes to PyFrameObject.

Suggested reviewers: youknowone, ShaharNaveh

🚥 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: expanding the C API with additional eval-related functions.
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: 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/ceval.rs`:
- Around line 118-124: `PyEval_GetLocals` is returning the frame’s locals
mapping without first synchronizing fast locals, so update the
`PyEval_GetLocals` implementation in `ceval.rs` to use the same synchronization
path as `frame.locals(vm)` before converting to a raw `PyObject`. Keep the
existing `with_vm`/`current_frame` flow, but fetch the synced locals mapping
from the frame rather than calling `frame.locals.as_object(vm)` directly so the
behavior matches CPython.
- Around line 127-150: PyEval_GetFuncName is returning borrowed string pointers
that may not be stable or NUL-terminated, especially for __name__ and cls.name()
paths. Update PyEval_GetFuncName to construct and return a stable C string
(matching the approach used by PyEval_GetFuncDesc) by converting the chosen name
into a CString or equivalent CStr-backed value before returning, and keep the
recursive bound_method_type path consistent with that same ownership model.
🪄 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: 0e718ea3-ba8f-4190-9854-61beccd59be6

📥 Commits

Reviewing files that changed from the base of the PR and between 5c36d5c and 00c0b14.

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

Comment thread crates/capi/src/ceval.rs
Comment thread crates/capi/src/ceval.rs Outdated

@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/pyframe.rs`:
- Around line 8-16: `PyFrame_GetLineNumber` currently converts the `f_lineno()`
result from `usize` to `c_int` with a blind cast, which can truncate large
values. Update the `PyFrame_GetLineNumber` implementation in `pyframe.rs` to use
a checked conversion or explicit bounds handling before returning
`core::ffi::c_int`, and keep `PyFrame_GetCode` unchanged since its ownership is
already correct.
🪄 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: 6ac3a184-adae-42c0-8fb0-bc3796bc0001

📥 Commits

Reviewing files that changed from the base of the PR and between 00c0b14 and 0dfff5f.

📒 Files selected for processing (3)
  • crates/capi/src/ceval.rs
  • crates/capi/src/pyframe.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/ceval.rs

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