From: Victor Stinner Date: Wed, 18 Mar 2026 17:57:08 +0000 (+0100) Subject: [3.14] gh-146092: Handle _PyFrame_GetFrameObject() failures properly (#146124) (... X-Git-Tag: v3.14.4~113 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=8eeb800faf5562e6ce3805416f656ab09243c9a6;p=thirdparty%2FPython%2Fcpython.git [3.14] gh-146092: Handle _PyFrame_GetFrameObject() failures properly (#146124) (#146132) gh-146092: Handle _PyFrame_GetFrameObject() failures properly (#146124) * Fix _PyFrame_GetLocals() and _PyFrame_GetLocals() error handling. * _PyEval_ExceptionGroupMatch() now fails on _PyFrame_GetLocals() error. (cherry picked from commit e1e4852133ea548479bc9b975420a32331df7cee) --- diff --git a/Objects/frameobject.c b/Objects/frameobject.c index 7c2773085f4b..51d3e6c8d317 100644 --- a/Objects/frameobject.c +++ b/Objects/frameobject.c @@ -2288,6 +2288,9 @@ _PyFrame_GetLocals(_PyInterpreterFrame *frame) } PyFrameObject* f = _PyFrame_GetFrameObject(frame); + if (f == NULL) { + return NULL; + } return _PyFrameLocalsProxy_New(f); } diff --git a/Python/ceval.c b/Python/ceval.c index e6d82f249ef5..b48939c0547b 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -2293,14 +2293,17 @@ _PyEval_ExceptionGroupMatch(_PyInterpreterFrame *frame, PyObject* exc_value, return -1; } PyFrameObject *f = _PyFrame_GetFrameObject(frame); - if (f != NULL) { - PyObject *tb = _PyTraceBack_FromFrame(NULL, f); - if (tb == NULL) { - return -1; - } - PyException_SetTraceback(wrapped, tb); - Py_DECREF(tb); + if (f == NULL) { + Py_DECREF(wrapped); + return -1; + } + + PyObject *tb = _PyTraceBack_FromFrame(NULL, f); + if (tb == NULL) { + return -1; } + PyException_SetTraceback(wrapped, tb); + Py_DECREF(tb); *match = wrapped; } *rest = Py_NewRef(Py_None); @@ -2778,6 +2781,11 @@ PyEval_GetLocals(void) if (PyFrameLocalsProxy_Check(locals)) { PyFrameObject *f = _PyFrame_GetFrameObject(current_frame); + if (f == NULL) { + Py_DECREF(locals); + return NULL; + } + PyObject *ret = f->f_locals_cache; if (ret == NULL) { ret = PyDict_New();