Skip to content

Commit fd1e1a1

Browse files
authored
bpo-39947: Add PyThreadState_GetFrame() function (GH-19092)
Add PyThreadState_GetFrame() function: get the current frame of a Python thread state.
1 parent d831688 commit fd1e1a1

File tree

6 files changed

+33
-5
lines changed

6 files changed

+33
-5
lines changed

Doc/c-api/init.rst

+12
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,18 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
10721072
to :c:func:`PyThreadState_Clear`.
10731073
10741074
1075+
.. c:function:: PyFrameObject* PyThreadState_GetFrame(PyThreadState *tstate)
1076+
1077+
Get the current frame of the Python thread state *tstate*. It can be
1078+
``NULL`` if no frame is currently executing.
1079+
1080+
See also :c:func:`PyEval_GetFrame`.
1081+
1082+
*tstate* must not be ``NULL``.
1083+
1084+
.. versionadded:: 3.9
1085+
1086+
10751087
.. c:function:: PyInterpreterState* PyThreadState_GetInterpreter(PyThreadState *tstate)
10761088
10771089
Get the interpreter of the Python thread state *tstate*.

Doc/c-api/reflection.rst

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,31 @@
55
Reflection
66
==========
77

8-
.. c:function:: PyObject* PyEval_GetBuiltins()
8+
.. c:function:: PyObject* PyEval_GetBuiltins(void)
99
1010
Return a dictionary of the builtins in the current execution frame,
1111
or the interpreter of the thread state if no frame is currently executing.
1212
1313
14-
.. c:function:: PyObject* PyEval_GetLocals()
14+
.. c:function:: PyObject* PyEval_GetLocals(void)
1515
1616
Return a dictionary of the local variables in the current execution frame,
1717
or ``NULL`` if no frame is currently executing.
1818
1919
20-
.. c:function:: PyObject* PyEval_GetGlobals()
20+
.. c:function:: PyObject* PyEval_GetGlobals(void)
2121
2222
Return a dictionary of the global variables in the current execution frame,
2323
or ``NULL`` if no frame is currently executing.
2424
2525
26-
.. c:function:: PyFrameObject* PyEval_GetFrame()
26+
.. c:function:: PyFrameObject* PyEval_GetFrame(void)
2727
2828
Return the current thread state's frame, which is ``NULL`` if no frame is
2929
currently executing.
3030
31+
See also :c:func:`PyThreadState_GetFrame`.
32+
3133
3234
.. c:function:: int PyFrame_GetLineNumber(PyFrameObject *frame)
3335

Doc/whatsnew/3.9.rst

+3
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,9 @@ Build and C API Changes
427427

428428
* New :c:func:`PyThreadState_GetInterpreter` and
429429
:c:func:`PyInterpreterState_Get` functions to get the interpreter.
430+
New :c:func:`PyThreadState_GetFrame` function to get the current frame of a
431+
Python thread state.
432+
(Contributed by Victor Stinner in :issue:`39947`.)
430433

431434
* Add ``--with-platlibdir`` option to the ``configure`` script: name of the
432435
platform-specific library directory, stored in the new :attr:`sys.platlibdir`

Include/pystate.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ PyAPI_FUNC(int) PyThreadState_SetAsyncExc(unsigned long, PyObject *);
8989

9090
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000
9191
/* New in 3.9 */
92-
PyAPI_FUNC(PyInterpreterState *) PyThreadState_GetInterpreter(PyThreadState *tstate);
92+
PyAPI_FUNC(PyInterpreterState*) PyThreadState_GetInterpreter(PyThreadState *tstate);
93+
PyAPI_FUNC(struct _frame*) PyThreadState_GetFrame(PyThreadState *tstate);
9394
#endif
9495

9596
typedef
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add :c:func:`PyThreadState_GetFrame` function: get the current frame of a
2+
Python thread state.

Python/pystate.c

+8
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,14 @@ PyThreadState_GetInterpreter(PyThreadState *tstate)
10071007
}
10081008

10091009

1010+
struct _frame*
1011+
PyThreadState_GetFrame(PyThreadState *tstate)
1012+
{
1013+
assert(tstate != NULL);
1014+
return _PyThreadState_GetFrame(tstate);
1015+
}
1016+
1017+
10101018
/* Asynchronously raise an exception in a thread.
10111019
Requested by Just van Rossum and Alex Martelli.
10121020
To prevent naive misuse, you must write your own extension

0 commit comments

Comments
 (0)