From 384b19e5ff859477923e6ea607873c385a97601b Mon Sep 17 00:00:00 2001 From: ZeroIntensity Date: Wed, 3 Jul 2024 13:37:27 -0400 Subject: [PATCH 1/2] Warn if an exception is set during call to PyThreadState_Clear() --- Python/pystate.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Python/pystate.c b/Python/pystate.c index 602b13e18c71ae5..cc6139b3f1b59b1 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -1687,6 +1687,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. From f7d678d03245dfe33eb26139954b288ec862e1a3 Mon Sep 17 00:00:00 2001 From: ZeroIntensity Date: Wed, 3 Jul 2024 13:39:24 -0400 Subject: [PATCH 2/2] Add news entry --- .../next/C API/2024-07-03-13-39-13.gh-issue-121058.MKi1MV.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/C API/2024-07-03-13-39-13.gh-issue-121058.MKi1MV.rst 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 000000000000000..133d8cb6fe4b9e6 --- /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.