https://github.com/python/cpython/commit/6c1a4fb6d400827155fd70e48d682e35397731a1
commit: 6c1a4fb6d400827155fd70e48d682e35397731a1
branch: main
author: Peter Bierma <[email protected]>
committer: ericsnowcurrently <[email protected]>
date: 2024-11-20T10:27:19-07:00
summary:

gh-121058: Warn if `PyThreadState_Clear` is called with an exception set 
(gh-121343)

files:
A Misc/NEWS.d/next/C API/2024-07-03-13-39-13.gh-issue-121058.MKi1MV.rst
M 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
index 00000000000000..133d8cb6fe4b9e
--- /dev/null
+++ b/Misc/NEWS.d/next/C API/2024-07-03-13-39-13.gh-issue-121058.MKi1MV.rst     
@@ -0,0 +1,2 @@
+``PyThreadState_Clear()`` now warns (and calls ``sys.excepthook``) if the
+thread state still has an active exception.
diff --git a/Python/pystate.c b/Python/pystate.c
index 01e54fc745de1f..44f55be5b5b7a8 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -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.
 

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to