]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-146092: Handle _PyFrame_GetFrameObject() failures properly (GH-146124)...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 18 Mar 2026 20:23:26 +0000 (21:23 +0100)
committerGitHub <noreply@github.com>
Wed, 18 Mar 2026 20:23:26 +0000 (21:23 +0100)
[3.14] gh-146092: Handle _PyFrame_GetFrameObject() failures properly (GH-146124) (GH-146132)

gh-146092: Handle _PyFrame_GetFrameObject() failures properly (GH-146124)

* Fix _PyFrame_GetLocals() and _PyFrame_GetLocals() error handling.
* _PyEval_ExceptionGroupMatch() now fails on _PyFrame_GetLocals()
  error.
(cherry picked from commit 8eeb800faf5562e6ce3805416f656ab09243c9a6)

(cherry picked from commit e1e4852133ea548479bc9b975420a32331df7cee)

Co-authored-by: Victor Stinner <vstinner@python.org>
Objects/frameobject.c
Python/ceval.c

index d947f655d573ba62721a629726a3addd03cdb335..467c3fe56e98f206423d2e150f7319286d24f42f 100644 (file)
@@ -2093,6 +2093,9 @@ _PyFrame_GetLocals(_PyInterpreterFrame *frame)
     }
 
     PyFrameObject* f = _PyFrame_GetFrameObject(frame);
+    if (f == NULL) {
+        return NULL;
+    }
 
     return _PyFrameLocalsProxy_New(f);
 }
index ca07bfbaaf6a38dd72f40a7c1e87240f9213aacd..aafb4dd0fd97d37788dec0a1d62ebfd100b5914f 100644 (file)
@@ -2031,14 +2031,17 @@ _PyEval_ExceptionGroupMatch(PyObject* exc_value, PyObject *match_type,
             PyThreadState *tstate = _PyThreadState_GET();
             _PyInterpreterFrame *frame = _PyThreadState_GetFrame(tstate);
             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);
@@ -2505,6 +2508,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();