@@ -336,6 +336,7 @@ TEST_CASE("Restart the interpreter") {
336
336
REQUIRE (py_widget.attr (" the_message" ).cast <std::string>() == " Hello after restart" );
337
337
}
338
338
339
+ #if defined(PYBIND11_SUBINTERPRETER_SUPPORT)
339
340
TEST_CASE (" Subinterpreter" ) {
340
341
py::module_::import (" external_module" ); // in the main interpreter
341
342
@@ -347,6 +348,10 @@ TEST_CASE("Subinterpreter") {
347
348
348
349
REQUIRE (m.attr (" add" )(1 , 2 ).cast <int >() == 3 );
349
350
}
351
+
352
+ auto main_int
353
+ = py::module_::import (" external_module" ).attr (" internals_at" )().cast <uintptr_t >();
354
+
350
355
REQUIRE (has_state_dict_internals_obj ());
351
356
REQUIRE (has_pybind11_internals_static ());
352
357
@@ -359,7 +364,6 @@ TEST_CASE("Subinterpreter") {
359
364
// Subinterpreters get their own copy of builtins.
360
365
REQUIRE_FALSE (has_state_dict_internals_obj ());
361
366
362
- #if defined(PYBIND11_SUBINTERPRETER_SUPPORT) && PY_VERSION_HEX >= 0x030C0000
363
367
// internals hasn't been populated yet, but will be different for the subinterpreter
364
368
REQUIRE_FALSE (has_pybind11_internals_static ());
365
369
@@ -369,14 +373,12 @@ TEST_CASE("Subinterpreter") {
369
373
py::detail::get_internals ();
370
374
REQUIRE (has_pybind11_internals_static ());
371
375
REQUIRE (get_details_as_uintptr () == ext_int);
372
- #else
373
- // This static is still defined
374
- REQUIRE (has_pybind11_internals_static ());
375
- #endif
376
+ REQUIRE (main_int != ext_int);
376
377
377
378
// Modules tags should be gone.
378
379
REQUIRE_FALSE (py::hasattr (py::module_::import (" __main__" ), " tag" ));
379
380
{
381
+ REQUIRE_NOTHROW (py::module_::import (" widget_module" ));
380
382
auto m = py::module_::import (" widget_module" );
381
383
REQUIRE_FALSE (py::hasattr (m, " extension_module_tag" ));
382
384
@@ -397,7 +399,6 @@ TEST_CASE("Subinterpreter") {
397
399
REQUIRE (has_state_dict_internals_obj ());
398
400
}
399
401
400
- #if defined(PYBIND11_SUBINTERPRETER_SUPPORT)
401
402
TEST_CASE (" Multiple Subinterpreters" ) {
402
403
// Make sure the module is in the main interpreter and save its pointer
403
404
auto *main_ext = py::module_::import (" external_module" ).ptr ();
@@ -527,13 +528,7 @@ TEST_CASE("Per-Subinterpreter GIL") {
527
528
T_REQUIRE (caught);
528
529
529
530
// widget_module did provide the per_interpreter_gil tag, so it this does not throw
530
- try {
531
- py::module_::import (" widget_module" );
532
- caught = false ;
533
- } catch (pybind11::error_already_set &) {
534
- caught = true ;
535
- }
536
- T_REQUIRE (!caught);
531
+ py::module_::import (" widget_module" );
537
532
538
533
T_REQUIRE (!py::hasattr (py::module_::import (" external_module" ), " multi_interp" ));
539
534
py::module_::import (" external_module" ).attr (" multi_interp" ) = std::to_string (num);
@@ -557,8 +552,8 @@ TEST_CASE("Per-Subinterpreter GIL") {
557
552
558
553
Py_EndInterpreter (sub);
559
554
560
- PyThreadState_Swap (
561
- main_tstate); // switch back so the scoped_acquire can release the GIL properly
555
+ // switch back so the scoped_acquire can release the GIL properly
556
+ PyThreadState_Swap ( main_tstate);
562
557
};
563
558
564
559
std::thread t1 (thread_main, 1 );
0 commit comments