Skip to content

bpo-38500: Add PyInterpreterState_SetEvalFrameFunc() #17352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Doc/c-api/init.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,35 @@ All of the following functions must be called after :c:func:`Py_Initialize`.

.. versionadded:: 3.8

.. c:type:: PyObject* (*PyFrameEvalFunction)(PyThreadState *tstate, PyFrameObject *frame, int throwflag)

Type of a frame evaluation function.

The *throwflag* parameter is used by the ``throw()`` method of generators:
if non-zero, handle the current exception.

.. versionadded:: 3.8.1

.. versionchanged:: 3.8.1
Add *tstate* parameter.


.. c:function:: PyFrameEvalFunction PyInterpreterState_GetEvalFrameFunc(PyInterpreterState *interp)

Get the frame evaluation function.

See the :pep:`523` "Adding a frame evaluation API to CPython".

.. versionadded:: 3.8.1

.. c:function:: void PyInterpreterState_SetEvalFrameFunc(PyInterpreterState *interp, PyFrameEvalFunction eval_frame);

Set the frame evaluation function.

See the :pep:`523` "Adding a frame evaluation API to CPython".

.. versionadded:: 3.8.1


.. c:function:: PyObject* PyThreadState_GetDict()

Expand Down
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.9.rst
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ Build and C API Changes
removed in Python 3.3.
(Contributed by Victor Stinner in :issue:`38896`.)

* ``PyInterpreterState.eval_frame`` (:pep:`523`) now requires a new mandatory
*tstate* parameter (``PyThreadState*``).
(Contributed by Victor Stinner in :issue:`38818`.)


Deprecated
==========
Expand Down
5 changes: 4 additions & 1 deletion Include/cpython/ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ PyAPI_FUNC(PyObject *) _PyEval_GetBuiltinId(_Py_Identifier *);
flag was set, else return 0. */
PyAPI_FUNC(int) PyEval_MergeCompilerFlags(PyCompilerFlags *cf);

PyAPI_FUNC(PyObject *) _PyEval_EvalFrameDefault(struct _frame *f, int exc);
PyAPI_FUNC(PyObject *) _PyEval_EvalFrameDefault(
PyThreadState *tstate,
struct _frame *f,
int exc);

PyAPI_FUNC(void) _PyEval_SetSwitchInterval(unsigned long microseconds);
PyAPI_FUNC(unsigned long) _PyEval_GetSwitchInterval(void);
Expand Down
10 changes: 10 additions & 0 deletions Include/cpython/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,16 @@ PyAPI_FUNC(void) PyThreadState_DeleteCurrent(void);

typedef struct _frame *(*PyThreadFrameGetter)(PyThreadState *self_);

/* Frame evaluation API */

typedef PyObject* (*PyFrameEvalFunction)(PyThreadState *tstate, struct _frame *, int);

PyAPI_FUNC(PyFrameEvalFunction) PyInterpreterState_GetEvalFrameFunc(
PyInterpreterState *interp);
PyAPI_FUNC(void) PyInterpreterState_SetEvalFrameFunc(
PyInterpreterState *interp,
PyFrameEvalFunction eval_frame);

/* cross-interpreter data */

struct _xid;
Expand Down
2 changes: 1 addition & 1 deletion Include/internal/pycore_ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void _PyEval_Fini(void);
static inline PyObject*
_PyEval_EvalFrame(PyThreadState *tstate, struct _frame *f, int throwflag)
{
return tstate->interp->eval_frame(f, throwflag);
return tstate->interp->eval_frame(tstate, f, throwflag);
}

extern PyObject *_PyEval_EvalCode(
Expand Down
4 changes: 1 addition & 3 deletions Include/internal/pycore_pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ struct _ceval_runtime_state {

/* interpreter state */

typedef PyObject* (*_PyFrameEvalFunction)(struct _frame *, int);

// The PyInterpreterState typedef is in Include/pystate.h.
struct _is {

Expand Down Expand Up @@ -113,7 +111,7 @@ struct _is {
PyObject *builtins_copy;
PyObject *import_func;
/* Initialized to PyEval_EvalFrameDefault(). */
_PyFrameEvalFunction eval_frame;
PyFrameEvalFunction eval_frame;

Py_ssize_t co_extra_user_count;
freefunc co_extra_freefuncs[MAX_CO_EXTRA_USERS];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
``PyInterpreterState.eval_frame`` (:pep:`523`) now requires a new mandatory
*tstate* parameter (``PyThreadState*``).
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add a public API to get and set the frame evaluation function: add
:c:func:`PyInterpreterState_GetEvalFrameFunc` and
:c:func:`PyInterpreterState_SetEvalFrameFunc` C functions
3 changes: 1 addition & 2 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
}

PyObject* _Py_HOT_FUNCTION
_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
{
#ifdef DXPAIRS
int lastopcode = 0;
Expand All @@ -755,7 +755,6 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
PyObject **fastlocals, **freevars;
PyObject *retval = NULL; /* Return value */
_PyRuntimeState * const runtime = &_PyRuntime;
PyThreadState * const tstate = _PyRuntimeState_GetThreadState(runtime);
struct _ceval_runtime_state * const ceval = &runtime->ceval;
_Py_atomic_int * const eval_breaker = &ceval->eval_breaker;
PyCodeObject *co;
Expand Down
14 changes: 14 additions & 0 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1721,6 +1721,20 @@ _register_builtins_for_crossinterpreter_data(struct _xidregistry *xidregistry)
}


PyFrameEvalFunction
PyInterpreterState_GetEvalFrameFunc(PyInterpreterState *interp)
{
return interp->eval_frame;
}


void
PyInterpreterState_SetEvalFrameFunc(PyInterpreterState *interp,
PyFrameEvalFunction eval_frame)
{
interp->eval_frame = eval_frame;
}

#ifdef __cplusplus
}
#endif