]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-121058: Warn if `PyThreadState_Clear` is called with an exception set (gh-121343)
authorPeter Bierma <zintensitydev@gmail.com>
Wed, 20 Nov 2024 17:27:19 +0000 (12:27 -0500)
committerGitHub <noreply@github.com>
Wed, 20 Nov 2024 17:27:19 +0000 (10:27 -0700)
Misc/NEWS.d/next/C API/2024-07-03-13-39-13.gh-issue-121058.MKi1MV.rst [new file with mode: 0644]
Python/pystate.c

diff --git a/Misc/NEWS.d/next/C API/2024-07-03-13-39-13.gh-issue-121058.MKi1MV.rst b/Misc/NEWS.d/next/C API/2024-07-03-13-39-13.gh-issue-121058.MKi1MV.rst
new file mode 100644 (file)
index 0000000..133d8cb
--- /dev/null
@@ -0,0 +1,2 @@
+``PyThreadState_Clear()`` now warns (and calls ``sys.excepthook``) if the
+thread state still has an active exception.
index 01e54fc745de1fa341d297b52331991323baa739..44f55be5b5b7a8aaa57b0d8354deae105e59daad 100644 (file)
@@ -1649,6 +1649,11 @@ PyThreadState_Clear(PyThreadState *tstate)
           "PyThreadState_Clear: warning: thread still has a frame\n");
     }
 
+    if (verbose && tstate->current_exception != NULL) {
+        fprintf(stderr, "PyThreadState_Clear: warning: thread has an exception set\n");
+        _PyErr_Print(tstate);
+    }
+
     /* At this point tstate shouldn't be used any more,
        neither to run Python code nor for other uses.