From b67d07eea6b6091854d70b13d2fde65fc984f2d9 Mon Sep 17 00:00:00 2001 From: Justen Di Ruscio Date: Wed, 13 Aug 2025 21:21:42 -0300 Subject: [PATCH 1/2] check for current exception, not uncaught_exceptions --- include/pybind11/subinterpreter.h | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/include/pybind11/subinterpreter.h b/include/pybind11/subinterpreter.h index 1bdcc26a3c..664933420e 100644 --- a/include/pybind11/subinterpreter.h +++ b/include/pybind11/subinterpreter.h @@ -281,24 +281,18 @@ inline subinterpreter_scoped_activate::~subinterpreter_scoped_activate() { PyGILState_Release(gil_state_); } else { #if defined(PYBIND11_DETAILED_ERROR_MESSAGES) - bool has_active_exception; -# if defined(__cpp_lib_uncaught_exceptions) - has_active_exception = std::uncaught_exceptions() > 0; -# else - // removed in C++20, replaced with uncaught_exceptions - has_active_exception = std::uncaught_exception(); -# endif - if (has_active_exception) { - try { - std::rethrow_exception(std::current_exception()); - } catch (error_already_set &) { - // Because error_already_set holds python objects and what() acquires the GIL, it - // is basically never OK to let these exceptions propagate outside the current - // active interpreter. - pybind11_fail("~subinterpreter_scoped_activate: cannot propagate Python " - "exceptions outside of their owning interpreter"); - } catch (...) { + try { + const auto ex = std::current_exception(); + if (ex) { + std::rethrow_exception(ex); } + } catch (error_already_set &) { + // Because error_already_set holds python objects and what() acquires the GIL, it + // is basically never OK to let these exceptions propagate outside the current + // active interpreter. + pybind11_fail("~subinterpreter_scoped_activate: cannot propagate Python " + "exceptions outside of their owning interpreter"); + } catch (...) { } #endif From 4f38128348012bbbc302c621a78704aed83a08c1 Mon Sep 17 00:00:00 2001 From: Justen Di Ruscio Date: Thu, 14 Aug 2025 07:03:36 -0300 Subject: [PATCH 2/2] remove all in-flight exception handling from ~subinterpreter_scoped_activate --- include/pybind11/subinterpreter.h | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/include/pybind11/subinterpreter.h b/include/pybind11/subinterpreter.h index 664933420e..4b208ed9b6 100644 --- a/include/pybind11/subinterpreter.h +++ b/include/pybind11/subinterpreter.h @@ -280,22 +280,6 @@ inline subinterpreter_scoped_activate::~subinterpreter_scoped_activate() { // We were on this interpreter already, so just make sure the GIL goes back as it was PyGILState_Release(gil_state_); } else { -#if defined(PYBIND11_DETAILED_ERROR_MESSAGES) - try { - const auto ex = std::current_exception(); - if (ex) { - std::rethrow_exception(ex); - } - } catch (error_already_set &) { - // Because error_already_set holds python objects and what() acquires the GIL, it - // is basically never OK to let these exceptions propagate outside the current - // active interpreter. - pybind11_fail("~subinterpreter_scoped_activate: cannot propagate Python " - "exceptions outside of their owning interpreter"); - } catch (...) { - } -#endif - if (tstate_) { #if defined(PYBIND11_DETAILED_ERROR_MESSAGES) if (detail::get_thread_state_unchecked() != tstate_) {