]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-146092: Handle _PyFrame_GetFrameObject() failures properly (#146124)
authorVictor Stinner <vstinner@python.org>
Wed, 18 Mar 2026 17:27:52 +0000 (18:27 +0100)
committerGitHub <noreply@github.com>
Wed, 18 Mar 2026 17:27:52 +0000 (18:27 +0100)
* Fix _PyFrame_GetLocals() and _PyFrame_GetLocals() error handling.
* _PyEval_ExceptionGroupMatch() now fails on _PyFrame_GetLocals()
  error.

Objects/frameobject.c
Python/ceval.c

index 9a7abfc0ec26abc8979b7d7ab5950f3b5030564c..8911de6f2bfc5b7a040424f0cddfced85fe1506d 100644 (file)
@@ -2296,6 +2296,9 @@ _PyFrame_GetLocals(_PyInterpreterFrame *frame)
     }
 
     PyFrameObject* f = _PyFrame_GetFrameObject(frame);
+    if (f == NULL) {
+        return NULL;
+    }
 
     return _PyFrameLocalsProxy_New(f);
 }
index 29f81317722a80714ce5c723d9c41c5258973645..8a6895834cbb7e4c1a7570ba1264e96ceef7c51c 100644 (file)
@@ -2237,15 +2237,19 @@ _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) {
-                    Py_DECREF(wrapped);
-                    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) {
+                Py_DECREF(wrapped);
+                return -1;
             }
+            PyException_SetTraceback(wrapped, tb);
+            Py_DECREF(tb);
+
             *match = wrapped;
         }
         *rest = Py_NewRef(Py_None);
@@ -2620,6 +2624,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();