@@ -78,11 +78,10 @@ class subinterpreter {
78
78
// / interpreter and its GIL are not required to be held prior to calling this function.
79
79
static inline subinterpreter create (PyInterpreterConfig const &cfg) {
80
80
error_scope err_scope;
81
- subinterpreter_scoped_activate main_guard (main ());
82
81
subinterpreter result;
83
82
{
84
83
// we must hold the main GIL in order to create a subinterpreter
85
- gil_scoped_acquire gil ;
84
+ subinterpreter_scoped_activate main_guard ( main ()) ;
86
85
87
86
auto prev_tstate = PyThreadState_Get ();
88
87
@@ -264,6 +263,28 @@ inline subinterpreter_scoped_activate::~subinterpreter_scoped_activate() {
264
263
// We were on this interpreter already, so just make sure the GIL goes back as it was
265
264
PyGILState_Release (gil_state_);
266
265
} else {
266
+ #if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
267
+ bool has_active_exception;
268
+ # if defined(__cpp_lib_uncaught_exceptions)
269
+ has_active_exception = std::uncaught_exceptions () > 0 ;
270
+ # else
271
+ // removed in C++20, replaced with uncaught_exceptions
272
+ has_active_exception = std::uncaught_exception ();
273
+ # endif
274
+ if (has_active_exception) {
275
+ try {
276
+ std::rethrow_exception (std::current_exception ());
277
+ } catch (error_already_set &e) {
278
+ // Because error_already_set holds python objects and what() acquires the GIL, it
279
+ // is basically never OK to let these exceptions propagate outside the current
280
+ // active interpreter.
281
+ pybind11_fail (" ~subinterpreter_scoped_activate: cannot propagate Python "
282
+ " exceptions outside of their owning interpreter" );
283
+ } catch (...) {
284
+ }
285
+ }
286
+ #endif
287
+
267
288
if (tstate_) {
268
289
#if defined(PYBIND11_DETAILED_ERROR_MESSAGES)
269
290
if (detail::get_thread_state_unchecked () != tstate_) {
0 commit comments