Skip to content

Commit 25ffaac

Browse files
committed
Fix test, after internals is free'd it can come back at the same address
So instead, just make sure it was zero'd and don't try to compare the addresses. Also a little code cleanup
1 parent b53ff38 commit 25ffaac

2 files changed

Lines changed: 24 additions & 19 deletions

File tree

include/pybind11/detail/internals.h

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ template <typename InternalsType>
478478
class internals_pp_manager {
479479
public:
480480
using on_fetch_function = void(InternalsType *);
481-
explicit internals_pp_manager(char const *id, on_fetch_function *on_fetch = nullptr)
481+
internals_pp_manager(char const *id, on_fetch_function *on_fetch)
482482
: holder_id_(id), on_fetch_(on_fetch) {}
483483

484484
/// Get the current pointer-to-pointer, allocating it if it does not already exist. May
@@ -571,29 +571,35 @@ class internals_pp_manager {
571571
std::unique_ptr<InternalsType> *internals_singleton_pp_;
572572
};
573573

574-
inline void on_internals_fetch(internals *internals_ptr) {
575-
// If We loaded the internals through `state_dict`, our `error_already_set`
576-
// and `builtin_exception` may be different local classes than the ones set up in the
577-
// initial exception translator, below, so add another for our local exception classes.
578-
//
579-
// libstdc++ doesn't require this (types there are identified only by name)
580-
// libc++ with CPython doesn't require this (types are explicitly exported)
581-
// libc++ with PyPy still need it, awaiting further investigation
582-
if (internals_ptr) {
583574
#if !defined(__GLIBCXX__)
575+
// If We loaded the internals through `state_dict`, our `error_already_set`
576+
// and `builtin_exception` may be different local classes than the ones set up in the
577+
// initial exception translator, below, so add another for our local exception classes.
578+
//
579+
// libstdc++ doesn't require this (types there are identified only by name)
580+
// libc++ with CPython doesn't require this (types are explicitly exported)
581+
// libc++ with PyPy still need it, awaiting further investigation
582+
inline void check_internals_local_exception_translator(internals *internals_ptr) {
583+
if (internals_ptr) {
584584
for (auto et : internals_ptr->registered_exception_translators) {
585585
if (et == &translate_local_exception) {
586586
return;
587587
}
588588
}
589589
internals_ptr->registered_exception_translators.push_front(&translate_local_exception);
590-
#endif
591590
}
592591
}
592+
#endif
593593

594594
inline internals_pp_manager<internals> &get_internals_pp_manager() {
595+
#if defined(__GLIBCXX__)
596+
# define ON_FETCH_FN nullptr
597+
#else
598+
# define ON_FETCH_FN &check_internals_local_exception_translator
599+
#endif
595600
static internals_pp_manager<internals> internals_pp_manager(PYBIND11_INTERNALS_ID,
596-
&on_internals_fetch);
601+
ON_FETCH_FN);
602+
#undef ON_FETCH_FN
597603
return internals_pp_manager;
598604
}
599605

@@ -623,7 +629,7 @@ inline internals_pp_manager<local_internals> &get_local_internals_pp_manager() {
623629
= PYBIND11_MODULE_LOCAL_ID
624630
+ std::to_string(reinterpret_cast<uintptr_t>(&this_module_idstr));
625631
static internals_pp_manager<local_internals> local_internals_pp_manager(
626-
this_module_idstr.c_str());
632+
this_module_idstr.c_str(), nullptr);
627633
return local_internals_pp_manager;
628634
}
629635

tests/test_embed/test_interpreter.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,11 @@ TEST_CASE("Restart the interpreter") {
272272
// Verify pre-restart state.
273273
REQUIRE(py::module_::import("widget_module").attr("add")(1, 2).cast<int>() == 3);
274274
REQUIRE(has_state_dict_internals_obj());
275-
auto internals_addr = get_details_as_uintptr();
276275
REQUIRE(py::module_::import("external_module").attr("A")(123).attr("value").cast<int>()
277276
== 123);
278277

279278
// local and foreign module internals should point to the same internals:
280-
REQUIRE(internals_addr
279+
REQUIRE(get_details_as_uintptr()
281280
== py::module_::import("external_module").attr("internals_at")().cast<uintptr_t>());
282281

283282
// Restart the interpreter.
@@ -289,11 +288,11 @@ TEST_CASE("Restart the interpreter") {
289288

290289
// Internals are deleted after a restart.
291290
REQUIRE_FALSE(has_state_dict_internals_obj());
291+
REQUIRE(get_details_as_uintptr() == 0);
292292
pybind11::detail::get_internals();
293293
REQUIRE(has_state_dict_internals_obj());
294-
REQUIRE(internals_addr != get_details_as_uintptr());
295-
internals_addr = get_details_as_uintptr();
296-
REQUIRE(internals_addr
294+
REQUIRE(get_details_as_uintptr() != 0);
295+
REQUIRE(get_details_as_uintptr()
297296
== py::module_::import("external_module").attr("internals_at")().cast<uintptr_t>());
298297

299298
// Make sure that an interpreter with no get_internals() created until finalize still gets the
@@ -313,7 +312,7 @@ TEST_CASE("Restart the interpreter") {
313312
REQUIRE(ran);
314313
py::initialize_interpreter();
315314
REQUIRE_FALSE(has_state_dict_internals_obj());
316-
REQUIRE(internals_addr != get_details_as_uintptr());
315+
REQUIRE(get_details_as_uintptr() == 0);
317316

318317
// C++ modules can be reloaded.
319318
auto cpp_module = py::module_::import("widget_module");

0 commit comments

Comments
 (0)