Skip to content

Commit b67d07e

Browse files
committed
check for current exception, not uncaught_exceptions
1 parent 90bc05c commit b67d07e

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

include/pybind11/subinterpreter.h

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -281,24 +281,18 @@ inline subinterpreter_scoped_activate::~subinterpreter_scoped_activate() {
281281
PyGILState_Release(gil_state_);
282282
} else {
283283
#if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
284-
bool has_active_exception;
285-
# if defined(__cpp_lib_uncaught_exceptions)
286-
has_active_exception = std::uncaught_exceptions() > 0;
287-
# else
288-
// removed in C++20, replaced with uncaught_exceptions
289-
has_active_exception = std::uncaught_exception();
290-
# endif
291-
if (has_active_exception) {
292-
try {
293-
std::rethrow_exception(std::current_exception());
294-
} catch (error_already_set &) {
295-
// Because error_already_set holds python objects and what() acquires the GIL, it
296-
// is basically never OK to let these exceptions propagate outside the current
297-
// active interpreter.
298-
pybind11_fail("~subinterpreter_scoped_activate: cannot propagate Python "
299-
"exceptions outside of their owning interpreter");
300-
} catch (...) {
284+
try {
285+
const auto ex = std::current_exception();
286+
if (ex) {
287+
std::rethrow_exception(ex);
301288
}
289+
} catch (error_already_set &) {
290+
// Because error_already_set holds python objects and what() acquires the GIL, it
291+
// is basically never OK to let these exceptions propagate outside the current
292+
// active interpreter.
293+
pybind11_fail("~subinterpreter_scoped_activate: cannot propagate Python "
294+
"exceptions outside of their owning interpreter");
295+
} catch (...) {
302296
}
303297
#endif
304298

0 commit comments

Comments
 (0)