Skip to content

gh-141518: Add PyUnstable_InterpreterFrame_GetFrameObject() function - #141950

Closed
vstinner wants to merge 6 commits into
python:mainfrom
vstinner:interpframe
Closed

gh-141518: Add PyUnstable_InterpreterFrame_GetFrameObject() function#141950
vstinner wants to merge 6 commits into
python:mainfrom
vstinner:interpframe

Conversation

@vstinner

@vstinnervstinner commented Nov 25, 2025

Copy link
Copy Markdown
Member

…ction
Add also PyUnstable_InterpreterFrame type.
@vstinner

Copy link
Copy Markdown
MemberAuthor

See #141665 (review) for the rationale on adding this function.

Comment threadDoc/c-api/frame.rst
Comment threadDoc/c-api/frame.rst Outdated
Comment threadInclude/cpython/pyframe.h
Comment threadObjects/frameobject.c
vstinnerand others added 2 commits November 28, 2025 12:30
Co-authored-by: Mikhail Efimov <efimov.mikhail@gmail.com>
@markshannon

Copy link
Copy Markdown
Member

I don't think we should add PyUnstable_InterpreterFrame_GetFrameObject().

If someone wants a frame object, they can get it directly with sys._getframe() or the C API equivalent.
The whole point of using _PyInterpreterFrame is to avoid the overhead of creating a frame object.
Also, PyUnstable_InterpreterFrame_GetFrameObject() is not a simpler getter, but will often have to create the frame object.

@vstinner

Copy link
Copy Markdown
MemberAuthor

Well, @encukou requested such API for PyUnstable_InterpreterState_SetEvalFrameFunc(): #141665 (review)

@encukou

Copy link
Copy Markdown
Member

sys._getframe() won't work in a frame evaluation function -- until the new frame is set up, it returns the previous one.

Should it be PyUnstable_InterpreterFrame_AsFrameObject?

@markshannon

Copy link
Copy Markdown
Member

In a frame evaluation function, you don't want the frame object. It is just a bulkier, slower version of the frame you already have. The frame object contains no additional information, just a pointer back to the internal frame.

@vstinner

Copy link
Copy Markdown
MemberAuthor

In a frame evaluation function, you don't want the frame object

Yeah, you should use the internal C API to access the interpreter frame: pycore_interpframe.h provides many functions, such as:

  • _PyFrame_GetCode()
  • _PyFrame_GetBytecode()
  • _PyFrame_GetFunction()
  • _PyFrame_IsIncomplete()
  • etc.

I know that it's kind of unusual to have a public function to set a frame evalution function, but need the internal C API to implement it.

@vstinner
vstinner deleted the interpframe branch February 10, 2026 17:41
@vstinner

Copy link
Copy Markdown
MemberAuthor

The C API Working Group decided to not add a public function for this feature, but keep the _Py name instead: capi-workgroup/decisions#89.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants

@vstinner@markshannon@encukou@efimov-mikhail@emmatyping@nazeerali4325-commits