bpo-39947: Add PyThreadState_GetFrame() function (GH-19092)

Add PyThreadState_GetFrame() function: get the current frame
of a Python thread state.
This commit is contained in:
Victor Stinner 2020-03-20 15:51:45 +01:00 committed by GitHub
parent d83168854e
commit fd1e1a18fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 33 additions and 5 deletions

View File

@ -1072,6 +1072,18 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
to :c:func:`PyThreadState_Clear`. to :c:func:`PyThreadState_Clear`.
.. c:function:: PyFrameObject* PyThreadState_GetFrame(PyThreadState *tstate)
Get the current frame of the Python thread state *tstate*. It can be
``NULL`` if no frame is currently executing.
See also :c:func:`PyEval_GetFrame`.
*tstate* must not be ``NULL``.
.. versionadded:: 3.9
.. c:function:: PyInterpreterState* PyThreadState_GetInterpreter(PyThreadState *tstate) .. c:function:: PyInterpreterState* PyThreadState_GetInterpreter(PyThreadState *tstate)
Get the interpreter of the Python thread state *tstate*. Get the interpreter of the Python thread state *tstate*.

View File

@ -5,29 +5,31 @@
Reflection Reflection
========== ==========
.. c:function:: PyObject* PyEval_GetBuiltins() .. c:function:: PyObject* PyEval_GetBuiltins(void)
Return a dictionary of the builtins in the current execution frame, Return a dictionary of the builtins in the current execution frame,
or the interpreter of the thread state if no frame is currently executing. or the interpreter of the thread state if no frame is currently executing.
.. c:function:: PyObject* PyEval_GetLocals() .. c:function:: PyObject* PyEval_GetLocals(void)
Return a dictionary of the local variables in the current execution frame, Return a dictionary of the local variables in the current execution frame,
or ``NULL`` if no frame is currently executing. or ``NULL`` if no frame is currently executing.
.. c:function:: PyObject* PyEval_GetGlobals() .. c:function:: PyObject* PyEval_GetGlobals(void)
Return a dictionary of the global variables in the current execution frame, Return a dictionary of the global variables in the current execution frame,
or ``NULL`` if no frame is currently executing. or ``NULL`` if no frame is currently executing.
.. c:function:: PyFrameObject* PyEval_GetFrame() .. c:function:: PyFrameObject* PyEval_GetFrame(void)
Return the current thread state's frame, which is ``NULL`` if no frame is Return the current thread state's frame, which is ``NULL`` if no frame is
currently executing. currently executing.
See also :c:func:`PyThreadState_GetFrame`.
.. c:function:: int PyFrame_GetLineNumber(PyFrameObject *frame) .. c:function:: int PyFrame_GetLineNumber(PyFrameObject *frame)

View File

@ -427,6 +427,9 @@ Build and C API Changes
* New :c:func:`PyThreadState_GetInterpreter` and * New :c:func:`PyThreadState_GetInterpreter` and
:c:func:`PyInterpreterState_Get` functions to get the interpreter. :c:func:`PyInterpreterState_Get` functions to get the interpreter.
New :c:func:`PyThreadState_GetFrame` function to get the current frame of a
Python thread state.
(Contributed by Victor Stinner in :issue:`39947`.)
* Add ``--with-platlibdir`` option to the ``configure`` script: name of the * Add ``--with-platlibdir`` option to the ``configure`` script: name of the
platform-specific library directory, stored in the new :attr:`sys.platlibdir` platform-specific library directory, stored in the new :attr:`sys.platlibdir`

View File

@ -89,7 +89,8 @@ PyAPI_FUNC(int) PyThreadState_SetAsyncExc(unsigned long, PyObject *);
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000
/* New in 3.9 */ /* New in 3.9 */
PyAPI_FUNC(PyInterpreterState *) PyThreadState_GetInterpreter(PyThreadState *tstate); PyAPI_FUNC(PyInterpreterState*) PyThreadState_GetInterpreter(PyThreadState *tstate);
PyAPI_FUNC(struct _frame*) PyThreadState_GetFrame(PyThreadState *tstate);
#endif #endif
typedef typedef

View File

@ -0,0 +1,2 @@
Add :c:func:`PyThreadState_GetFrame` function: get the current frame of a
Python thread state.

View File

@ -1007,6 +1007,14 @@ PyThreadState_GetInterpreter(PyThreadState *tstate)
} }
struct _frame*
PyThreadState_GetFrame(PyThreadState *tstate)
{
assert(tstate != NULL);
return _PyThreadState_GetFrame(tstate);
}
/* Asynchronously raise an exception in a thread. /* Asynchronously raise an exception in a thread.
Requested by Just van Rossum and Alex Martelli. Requested by Just van Rossum and Alex Martelli.
To prevent naive misuse, you must write your own extension To prevent naive misuse, you must write your own extension