From 963896ae5acc5bc189f9c74aa8e26b5df4d82db2 Mon Sep 17 00:00:00 2001 From: Sam Gross Date: Thu, 12 Sep 2024 22:25:50 +0000 Subject: [PATCH] Fix tier2 optimizer --- Include/internal/pycore_frame.h | 6 ++++++ Modules/_testinternalcapi.c | 2 +- Objects/frameobject.c | 2 +- Python/optimizer.c | 2 +- Python/optimizer_analysis.c | 2 +- 5 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Include/internal/pycore_frame.h b/Include/internal/pycore_frame.h index 4738d0ca4cd06b..c9ac3819d0390b 100644 --- a/Include/internal/pycore_frame.h +++ b/Include/internal/pycore_frame.h @@ -84,6 +84,12 @@ static inline PyCodeObject *_PyFrame_GetCode(_PyInterpreterFrame *f) { return (PyCodeObject *)executable; } +static inline PyFunctionObject *_PyFrame_GetFunction(_PyInterpreterFrame *f) { + PyObject *func = PyStackRef_AsPyObjectBorrow(f->f_funcobj); + assert(PyFunction_Check(func)); + return (PyFunctionObject *)func; +} + static inline _PyStackRef *_PyFrame_Stackbase(_PyInterpreterFrame *f) { return (f->localsplus + _PyFrame_GetCode(f)->co_nlocalsplus); } diff --git a/Modules/_testinternalcapi.c b/Modules/_testinternalcapi.c index 246bf8f483a1ae..c403075fbb2501 100644 --- a/Modules/_testinternalcapi.c +++ b/Modules/_testinternalcapi.c @@ -682,7 +682,7 @@ static PyObject * record_eval(PyThreadState *tstate, struct _PyInterpreterFrame *f, int exc) { if (PyStackRef_FunctionCheck(f->f_funcobj)) { - PyFunctionObject *func = (PyFunctionObject *)PyStackRef_AsPyObjectBorrow(f->f_funcobj); + PyFunctionObject *func = _PyFrame_GetFunction(f); PyObject *module = _get_current_module(); assert(module != NULL); module_state *state = get_module_state(module); diff --git a/Objects/frameobject.c b/Objects/frameobject.c index 035818becabdae..9f1c031dcb9a9d 100644 --- a/Objects/frameobject.c +++ b/Objects/frameobject.c @@ -1868,7 +1868,7 @@ frame_init_get_vars(_PyInterpreterFrame *frame) } /* Free vars have not been initialized -- Do that */ - PyFunctionObject *func = (PyFunctionObject *)PyStackRef_AsPyObjectBorrow(frame->f_funcobj); + PyFunctionObject *func = _PyFrame_GetFunction(frame); PyObject *closure = func->func_closure; int offset = PyUnstable_Code_GetFirstFree(co); for (int i = 0; i < co->co_nfreevars; ++i) { diff --git a/Python/optimizer.c b/Python/optimizer.c index 9198e410627dd4..bb7a90b3204f40 100644 --- a/Python/optimizer.c +++ b/Python/optimizer.c @@ -533,7 +533,7 @@ translate_bytecode_to_trace( { bool first = true; PyCodeObject *code = _PyFrame_GetCode(frame); - PyFunctionObject *func = (PyFunctionObject *)frame->f_funcobj; + PyFunctionObject *func = _PyFrame_GetFunction(frame); assert(PyFunction_Check(func)); PyCodeObject *initial_code = code; _Py_BloomFilter_Add(dependencies, initial_code); diff --git a/Python/optimizer_analysis.c b/Python/optimizer_analysis.c index f7adb44c9e09ef..b202b58a8b7214 100644 --- a/Python/optimizer_analysis.c +++ b/Python/optimizer_analysis.c @@ -145,7 +145,7 @@ remove_globals(_PyInterpreterFrame *frame, _PyUOpInstruction *buffer, return 1; } PyObject *globals = frame->f_globals; - PyFunctionObject *function = (PyFunctionObject *)frame->f_funcobj; + PyFunctionObject *function = _PyFrame_GetFunction(frame); assert(PyFunction_Check(function)); assert(function->func_builtins == builtins); assert(function->func_globals == globals);