From bfc3d8d77ff39eea84b5154f14e2e7a94be27bf4 Mon Sep 17 00:00:00 2001 From: Dino Viehland Date: Wed, 7 Jan 2026 13:26:19 -0800 Subject: [PATCH] gh-143531: Use macro to check if PEP 523 is hooked (#143532) Use macro to check if PEP 523 is hooked --- Python/bytecodes.c | 14 ++++----- Python/ceval_macros.h | 2 ++ Python/executor_cases.c.h | 10 +++--- Python/generated_cases.c.h | 64 +++++++++++++++++++------------------- 4 files changed, 46 insertions(+), 44 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 8db1de6ca384..300683a94027 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -1349,7 +1349,7 @@ dummy_func( PyObject *receiver_o = PyStackRef_AsPyObjectBorrow(receiver); PyObject *retval_o; assert(frame->owner != FRAME_OWNED_BY_INTERPRETER); - if ((tstate->interp->eval_frame == NULL) && + if (!IS_PEP523_HOOKED(tstate) && (Py_TYPE(receiver_o) == &PyGen_Type || Py_TYPE(receiver_o) == &PyCoro_Type) && gen_try_set_executing((PyGenObject *)receiver_o)) { @@ -2596,7 +2596,7 @@ dummy_func( PyObject *owner_o = PyStackRef_AsPyObjectBorrow(owner); assert((oparg & 1) == 0); - DEOPT_IF(tstate->interp->eval_frame); + DEOPT_IF(IS_PEP523_HOOKED(tstate)); PyTypeObject *cls = Py_TYPE(owner_o); assert(type_version != 0); DEOPT_IF(FT_ATOMIC_LOAD_UINT_RELAXED(cls->tp_version_tag) != type_version); @@ -3746,7 +3746,7 @@ dummy_func( } // Check if the call can be inlined or not if (Py_TYPE(callable_o) == &PyFunction_Type && - tstate->interp->eval_frame == NULL && + !IS_PEP523_HOOKED(tstate) && ((PyFunctionObject *)callable_o)->vectorcall == _PyFunction_Vectorcall) { int code_flags = ((PyCodeObject*)PyFunction_GET_CODE(callable_o))->co_flags; @@ -3938,7 +3938,7 @@ dummy_func( } op(_CHECK_PEP_523, (--)) { - DEOPT_IF(tstate->interp->eval_frame); + DEOPT_IF(IS_PEP523_HOOKED(tstate)); } op(_CHECK_FUNCTION_EXACT_ARGS, (callable, self_or_null, unused[oparg] -- callable, self_or_null, unused[oparg])) { @@ -3974,7 +3974,7 @@ dummy_func( } op(_PUSH_FRAME, (new_frame -- )) { - assert(tstate->interp->eval_frame == NULL); + assert(!IS_PEP523_HOOKED(tstate)); _PyInterpreterFrame *temp = PyStackRef_Unwrap(new_frame); DEAD(new_frame); SYNC_SP(); @@ -4607,7 +4607,7 @@ dummy_func( int positional_args = total_args - (int)PyTuple_GET_SIZE(kwnames_o); // Check if the call can be inlined or not if (Py_TYPE(callable_o) == &PyFunction_Type && - tstate->interp->eval_frame == NULL && + !IS_PEP523_HOOKED(tstate) && ((PyFunctionObject *)callable_o)->vectorcall == _PyFunction_Vectorcall) { int code_flags = ((PyCodeObject*)PyFunction_GET_CODE(callable_o))->co_flags; @@ -4851,7 +4851,7 @@ dummy_func( } else { if (Py_TYPE(func) == &PyFunction_Type && - tstate->interp->eval_frame == NULL && + !IS_PEP523_HOOKED(tstate) && ((PyFunctionObject *)func)->vectorcall == _PyFunction_Vectorcall) { PyObject *callargs = PyStackRef_AsPyObjectSteal(callargs_st); assert(PyTuple_CheckExact(callargs)); diff --git a/Python/ceval_macros.h b/Python/ceval_macros.h index 697466d14a68..c70d6f4ba006 100644 --- a/Python/ceval_macros.h +++ b/Python/ceval_macros.h @@ -489,6 +489,8 @@ do { \ #define CHECK_CURRENT_CACHED_VALUES(N) ((void)0) #endif +#define IS_PEP523_HOOKED(tstate) (tstate->interp->eval_frame != NULL) + static inline int check_periodics(PyThreadState *tstate) { _Py_CHECK_EMSCRIPTEN_SIGNALS_PERIODICALLY(); diff --git a/Python/executor_cases.c.h b/Python/executor_cases.c.h index e78c37611ce6..fb8a919336bc 100644 --- a/Python/executor_cases.c.h +++ b/Python/executor_cases.c.h @@ -13100,7 +13100,7 @@ case _CHECK_PEP_523_r00: { CHECK_CURRENT_CACHED_VALUES(0); assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE()); - if (tstate->interp->eval_frame) { + if (IS_PEP523_HOOKED(tstate)) { UOP_STAT_INC(uopcode, miss); SET_CURRENT_CACHED_VALUES(0); JUMP_TO_JUMP_TARGET(); @@ -13114,7 +13114,7 @@ CHECK_CURRENT_CACHED_VALUES(1); assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE()); _PyStackRef _stack_item_0 = _tos_cache0; - if (tstate->interp->eval_frame) { + if (IS_PEP523_HOOKED(tstate)) { UOP_STAT_INC(uopcode, miss); _tos_cache0 = _stack_item_0; SET_CURRENT_CACHED_VALUES(1); @@ -13131,7 +13131,7 @@ assert(WITHIN_STACK_BOUNDS_IGNORING_CACHE()); _PyStackRef _stack_item_0 = _tos_cache0; _PyStackRef _stack_item_1 = _tos_cache1; - if (tstate->interp->eval_frame) { + if (IS_PEP523_HOOKED(tstate)) { UOP_STAT_INC(uopcode, miss); _tos_cache1 = _stack_item_1; _tos_cache0 = _stack_item_0; @@ -13151,7 +13151,7 @@ _PyStackRef _stack_item_0 = _tos_cache0; _PyStackRef _stack_item_1 = _tos_cache1; _PyStackRef _stack_item_2 = _tos_cache2; - if (tstate->interp->eval_frame) { + if (IS_PEP523_HOOKED(tstate)) { UOP_STAT_INC(uopcode, miss); _tos_cache2 = _stack_item_2; _tos_cache1 = _stack_item_1; @@ -13457,7 +13457,7 @@ _PyStackRef new_frame; _PyStackRef _stack_item_0 = _tos_cache0; new_frame = _stack_item_0; - assert(tstate->interp->eval_frame == NULL); + assert(!IS_PEP523_HOOKED(tstate)); _PyInterpreterFrame *temp = PyStackRef_Unwrap(new_frame); _PyFrame_SetStackPointer(frame, stack_pointer); assert(temp->previous == frame || temp->previous->previous == frame); diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index ece1146924bc..4de75b54c9e0 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -680,7 +680,7 @@ /* Skip 5 cache entries */ // _CHECK_PEP_523 { - if (tstate->interp->eval_frame) { + if (IS_PEP523_HOOKED(tstate)) { UPDATE_MISS_STATS(BINARY_OP); assert(_PyOpcode_Deopt[opcode] == (BINARY_OP)); JUMP_TO_PREDICTED(BINARY_OP); @@ -730,7 +730,7 @@ } // _PUSH_FRAME { - assert(tstate->interp->eval_frame == NULL); + assert(!IS_PEP523_HOOKED(tstate)); _PyInterpreterFrame *temp = PyStackRef_Unwrap(new_frame); stack_pointer += -2; ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); @@ -1750,7 +1750,7 @@ total_args++; } if (Py_TYPE(callable_o) == &PyFunction_Type && - tstate->interp->eval_frame == NULL && + !IS_PEP523_HOOKED(tstate) && ((PyFunctionObject *)callable_o)->vectorcall == _PyFunction_Vectorcall) { int code_flags = ((PyCodeObject*)PyFunction_GET_CODE(callable_o))->co_flags; @@ -1827,7 +1827,7 @@ /* Skip 1 cache entry */ // _CHECK_PEP_523 { - if (tstate->interp->eval_frame) { + if (IS_PEP523_HOOKED(tstate)) { UPDATE_MISS_STATS(CALL); assert(_PyOpcode_Deopt[opcode] == (CALL)); JUMP_TO_PREDICTED(CALL); @@ -1913,7 +1913,7 @@ // _PUSH_FRAME { new_frame = init_frame; - assert(tstate->interp->eval_frame == NULL); + assert(!IS_PEP523_HOOKED(tstate)); _PyInterpreterFrame *temp = PyStackRef_Unwrap(new_frame); _PyFrame_SetStackPointer(frame, stack_pointer); assert(temp->previous == frame || temp->previous->previous == frame); @@ -1946,7 +1946,7 @@ /* Skip 1 cache entry */ // _CHECK_PEP_523 { - if (tstate->interp->eval_frame) { + if (IS_PEP523_HOOKED(tstate)) { UPDATE_MISS_STATS(CALL); assert(_PyOpcode_Deopt[opcode] == (CALL)); JUMP_TO_PREDICTED(CALL); @@ -2054,7 +2054,7 @@ } // _PUSH_FRAME { - assert(tstate->interp->eval_frame == NULL); + assert(!IS_PEP523_HOOKED(tstate)); _PyInterpreterFrame *temp = PyStackRef_Unwrap(new_frame); stack_pointer += -2 - oparg; ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); @@ -2089,7 +2089,7 @@ /* Skip 1 cache entry */ // _CHECK_PEP_523 { - if (tstate->interp->eval_frame) { + if (IS_PEP523_HOOKED(tstate)) { UPDATE_MISS_STATS(CALL); assert(_PyOpcode_Deopt[opcode] == (CALL)); JUMP_TO_PREDICTED(CALL); @@ -2184,7 +2184,7 @@ } // _PUSH_FRAME { - assert(tstate->interp->eval_frame == NULL); + assert(!IS_PEP523_HOOKED(tstate)); _PyInterpreterFrame *temp = PyStackRef_Unwrap(new_frame); _PyFrame_SetStackPointer(frame, stack_pointer); assert(temp->previous == frame || temp->previous->previous == frame); @@ -2626,7 +2626,7 @@ /* Skip 1 cache entry */ // _CHECK_PEP_523 { - if (tstate->interp->eval_frame) { + if (IS_PEP523_HOOKED(tstate)) { UPDATE_MISS_STATS(CALL_FUNCTION_EX); assert(_PyOpcode_Deopt[opcode] == (CALL_FUNCTION_EX)); JUMP_TO_PREDICTED(CALL_FUNCTION_EX); @@ -2713,7 +2713,7 @@ // _PUSH_FRAME { new_frame = ex_frame; - assert(tstate->interp->eval_frame == NULL); + assert(!IS_PEP523_HOOKED(tstate)); _PyInterpreterFrame *temp = PyStackRef_Unwrap(new_frame); _PyFrame_SetStackPointer(frame, stack_pointer); assert(temp->previous == frame || temp->previous->previous == frame); @@ -2842,7 +2842,7 @@ } else { if (Py_TYPE(func) == &PyFunction_Type && - tstate->interp->eval_frame == NULL && + !IS_PEP523_HOOKED(tstate) && ((PyFunctionObject *)func)->vectorcall == _PyFunction_Vectorcall) { PyObject *callargs = PyStackRef_AsPyObjectSteal(callargs_st); assert(PyTuple_CheckExact(callargs)); @@ -3122,7 +3122,7 @@ } int positional_args = total_args - (int)PyTuple_GET_SIZE(kwnames_o); if (Py_TYPE(callable_o) == &PyFunction_Type && - tstate->interp->eval_frame == NULL && + !IS_PEP523_HOOKED(tstate) && ((PyFunctionObject *)callable_o)->vectorcall == _PyFunction_Vectorcall) { int code_flags = ((PyCodeObject*)PyFunction_GET_CODE(callable_o))->co_flags; @@ -3193,7 +3193,7 @@ /* Skip 1 cache entry */ // _CHECK_PEP_523 { - if (tstate->interp->eval_frame) { + if (IS_PEP523_HOOKED(tstate)) { UPDATE_MISS_STATS(CALL_KW); assert(_PyOpcode_Deopt[opcode] == (CALL_KW)); JUMP_TO_PREDICTED(CALL_KW); @@ -3289,7 +3289,7 @@ } // _PUSH_FRAME { - assert(tstate->interp->eval_frame == NULL); + assert(!IS_PEP523_HOOKED(tstate)); _PyInterpreterFrame *temp = PyStackRef_Unwrap(new_frame); _PyFrame_SetStackPointer(frame, stack_pointer); assert(temp->previous == frame || temp->previous->previous == frame); @@ -3399,7 +3399,7 @@ /* Skip 1 cache entry */ // _CHECK_PEP_523 { - if (tstate->interp->eval_frame) { + if (IS_PEP523_HOOKED(tstate)) { UPDATE_MISS_STATS(CALL_KW); assert(_PyOpcode_Deopt[opcode] == (CALL_KW)); JUMP_TO_PREDICTED(CALL_KW); @@ -3476,7 +3476,7 @@ } // _PUSH_FRAME { - assert(tstate->interp->eval_frame == NULL); + assert(!IS_PEP523_HOOKED(tstate)); _PyInterpreterFrame *temp = PyStackRef_Unwrap(new_frame); _PyFrame_SetStackPointer(frame, stack_pointer); assert(temp->previous == frame || temp->previous->previous == frame); @@ -4147,7 +4147,7 @@ /* Skip 1 cache entry */ // _CHECK_PEP_523 { - if (tstate->interp->eval_frame) { + if (IS_PEP523_HOOKED(tstate)) { UPDATE_MISS_STATS(CALL); assert(_PyOpcode_Deopt[opcode] == (CALL)); JUMP_TO_PREDICTED(CALL); @@ -4226,7 +4226,7 @@ } // _PUSH_FRAME { - assert(tstate->interp->eval_frame == NULL); + assert(!IS_PEP523_HOOKED(tstate)); _PyInterpreterFrame *temp = PyStackRef_Unwrap(new_frame); stack_pointer += -2 - oparg; ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); @@ -4260,7 +4260,7 @@ /* Skip 1 cache entry */ // _CHECK_PEP_523 { - if (tstate->interp->eval_frame) { + if (IS_PEP523_HOOKED(tstate)) { UPDATE_MISS_STATS(CALL); assert(_PyOpcode_Deopt[opcode] == (CALL)); JUMP_TO_PREDICTED(CALL); @@ -4328,7 +4328,7 @@ } // _PUSH_FRAME { - assert(tstate->interp->eval_frame == NULL); + assert(!IS_PEP523_HOOKED(tstate)); _PyInterpreterFrame *temp = PyStackRef_Unwrap(new_frame); _PyFrame_SetStackPointer(frame, stack_pointer); assert(temp->previous == frame || temp->previous->previous == frame); @@ -5804,7 +5804,7 @@ /* Skip 1 cache entry */ // _CHECK_PEP_523 { - if (tstate->interp->eval_frame) { + if (IS_PEP523_HOOKED(tstate)) { UPDATE_MISS_STATS(FOR_ITER); assert(_PyOpcode_Deopt[opcode] == (FOR_ITER)); JUMP_TO_PREDICTED(FOR_ITER); @@ -5836,7 +5836,7 @@ // _PUSH_FRAME { new_frame = gen_frame; - assert(tstate->interp->eval_frame == NULL); + assert(!IS_PEP523_HOOKED(tstate)); _PyInterpreterFrame *temp = PyStackRef_Unwrap(new_frame); _PyFrame_SetStackPointer(frame, stack_pointer); assert(temp->previous == frame || temp->previous->previous == frame); @@ -6432,7 +6432,7 @@ total_args++; } if (Py_TYPE(callable_o) == &PyFunction_Type && - tstate->interp->eval_frame == NULL && + !IS_PEP523_HOOKED(tstate) && ((PyFunctionObject *)callable_o)->vectorcall == _PyFunction_Vectorcall) { int code_flags = ((PyCodeObject*)PyFunction_GET_CODE(callable_o))->co_flags; @@ -6583,7 +6583,7 @@ } else { if (Py_TYPE(func) == &PyFunction_Type && - tstate->interp->eval_frame == NULL && + !IS_PEP523_HOOKED(tstate) && ((PyFunctionObject *)func)->vectorcall == _PyFunction_Vectorcall) { PyObject *callargs = PyStackRef_AsPyObjectSteal(callargs_st); assert(PyTuple_CheckExact(callargs)); @@ -6728,7 +6728,7 @@ } int positional_args = total_args - (int)PyTuple_GET_SIZE(kwnames_o); if (Py_TYPE(callable_o) == &PyFunction_Type && - tstate->interp->eval_frame == NULL && + !IS_PEP523_HOOKED(tstate) && ((PyFunctionObject *)callable_o)->vectorcall == _PyFunction_Vectorcall) { int code_flags = ((PyCodeObject*)PyFunction_GET_CODE(callable_o))->co_flags; @@ -8028,7 +8028,7 @@ PyObject *getattribute = read_obj(&this_instr[6].cache); PyObject *owner_o = PyStackRef_AsPyObjectBorrow(owner); assert((oparg & 1) == 0); - if (tstate->interp->eval_frame) { + if (IS_PEP523_HOOKED(tstate)) { UPDATE_MISS_STATS(LOAD_ATTR); assert(_PyOpcode_Deopt[opcode] == (LOAD_ATTR)); JUMP_TO_PREDICTED(LOAD_ATTR); @@ -8528,7 +8528,7 @@ /* Skip 1 cache entry */ // _CHECK_PEP_523 { - if (tstate->interp->eval_frame) { + if (IS_PEP523_HOOKED(tstate)) { UPDATE_MISS_STATS(LOAD_ATTR); assert(_PyOpcode_Deopt[opcode] == (LOAD_ATTR)); JUMP_TO_PREDICTED(LOAD_ATTR); @@ -8590,7 +8590,7 @@ } // _PUSH_FRAME { - assert(tstate->interp->eval_frame == NULL); + assert(!IS_PEP523_HOOKED(tstate)); _PyInterpreterFrame *temp = PyStackRef_Unwrap(new_frame); stack_pointer += -1; ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); @@ -10476,7 +10476,7 @@ PyObject *receiver_o = PyStackRef_AsPyObjectBorrow(receiver); PyObject *retval_o; assert(frame->owner != FRAME_OWNED_BY_INTERPRETER); - if ((tstate->interp->eval_frame == NULL) && + if (!IS_PEP523_HOOKED(tstate) && (Py_TYPE(receiver_o) == &PyGen_Type || Py_TYPE(receiver_o) == &PyCoro_Type) && gen_try_set_executing((PyGenObject *)receiver_o)) { @@ -10561,7 +10561,7 @@ /* Skip 1 cache entry */ // _CHECK_PEP_523 { - if (tstate->interp->eval_frame) { + if (IS_PEP523_HOOKED(tstate)) { UPDATE_MISS_STATS(SEND); assert(_PyOpcode_Deopt[opcode] == (SEND)); JUMP_TO_PREDICTED(SEND); @@ -10595,7 +10595,7 @@ // _PUSH_FRAME { new_frame = gen_frame; - assert(tstate->interp->eval_frame == NULL); + assert(!IS_PEP523_HOOKED(tstate)); _PyInterpreterFrame *temp = PyStackRef_Unwrap(new_frame); stack_pointer += -1; ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); -- 2.47.3