Skip to content

Commit 7026b7f

Browse files
committed
Clarify when these functions are useful
1 parent b725a08 commit 7026b7f

2 files changed

Lines changed: 34 additions & 6 deletions

File tree

Doc/c-api/frame.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,33 @@ Unless using :pep:`523`, you will not need this.
224224
225225
.. versionadded:: 3.11
226226
227+
The allocation-free raw-frame APIs added in Python 3.15 —
228+
:c:func:`PyUnstable_ThreadState_GetCurrentFrame`,
229+
:c:func:`PyUnstable_InterpreterFrame_GetCaller`,
230+
:c:func:`PyUnstable_InterpreterFrame_GetCodeBorrowed`, and
231+
:c:func:`PyUnstable_InterpreterFrame_GetLineChecked` — are intended for
232+
low-level observability and diagnostic tools that need to inspect the Python
233+
call stack without materializing Python frame objects. Examples include native
234+
profilers, crash reporters, custom allocator hooks, and C callbacks used with
235+
:mod:`sys.monitoring`.
236+
237+
Unlike :c:func:`PyThreadState_GetFrame`, :c:func:`PyFrame_GetBack`,
238+
:c:func:`PyFrame_GetCode`, and :c:func:`PyFrame_GetLineNumber`, these APIs do
239+
not allocate memory, do not create :c:type:`PyFrameObject` instances, do not
240+
change reference counts, do not set exceptions, do not call Python code, and do
241+
not acquire or release the GIL.
242+
243+
They provide best-effort, read-only access to raw interpreter frames. Returned
244+
frame and code object pointers are borrowed and must not be stored. If frame
245+
state appears invalid or concurrently torn down, these APIs may return ``NULL``
246+
or ``-1``. Freed-memory checks are heuristic and are not guaranteed to detect
247+
all races.
248+
249+
When used from a C callable registered with :mod:`sys.monitoring`, these APIs
250+
can be used to collect additional stack context beyond the code object and
251+
offset supplied to the callback. They do not make Python-level monitoring
252+
callbacks allocation-free or non-reentrant.
253+
227254
.. c:function:: PyObject* PyUnstable_InterpreterFrame_GetCode(struct _PyInterpreterFrame *frame);
228255
229256
Return a :term:`strong reference` to the code object for the frame.
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
Add four new ``PyUnstable_*`` C API functions for allocation-free, non-reentrant
2-
call-stack iteration: :c:func:`PyUnstable_ThreadState_GetCurrentFrame`,
1+
Add four new ``PyUnstable_*`` C API functions for best-effort,
2+
allocation-free, refcount-neutral stack inspection over raw interpreter frames:
3+
:c:func:`PyUnstable_ThreadState_GetCurrentFrame`,
34
:c:func:`PyUnstable_InterpreterFrame_GetCaller`,
45
:c:func:`PyUnstable_InterpreterFrame_GetCodeBorrowed`, and
5-
:c:func:`PyUnstable_InterpreterFrame_GetLineChecked`. These functions do not
6-
allocate memory, do not change reference counts, do not acquire the GIL, and do
7-
not re-enter the interpreter, making them suitable for use from custom memory
8-
allocator hooks and crash-reporting handlers.
6+
:c:func:`PyUnstable_InterpreterFrame_GetLineChecked`. These APIs avoid
7+
frame-object materialization and interpreter re-entry, making them useful for
8+
low-level observability and diagnostic tools, such as native profilers, crash
9+
reporters, custom allocator hooks, and C callbacks used with ``sys.monitoring``.

0 commit comments

Comments
 (0)