From eae174b4919a661e863313345a501bef0dd34620 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Sun, 6 Jun 2021 16:12:22 -0700 Subject: [PATCH] Revert "Experiment: effectively undoing all shared_from_this modifications." This reverts commit d72d54ebb0b7784f5616edc02910dbd9cce0b2a4. --- .../detail/smart_holder_type_casters.h | 31 ++++++------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/include/pybind11/detail/smart_holder_type_casters.h b/include/pybind11/detail/smart_holder_type_casters.h index 050a6c124..366474d2e 100644 --- a/include/pybind11/detail/smart_holder_type_casters.h +++ b/include/pybind11/detail/smart_holder_type_casters.h @@ -26,11 +26,6 @@ #include #include -#ifdef JUNK -# include -inline void to_cout(std::string msg) { std::cout << msg << std::endl; } -#endif - PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) using pybindit::memory::smart_holder; @@ -282,7 +277,6 @@ struct smart_holder_type_caster_class_hooks : smart_holder_type_caster_base_tag return false; } -#ifdef JUNK template static bool try_initialization_using_shared_from_this( holder_type *uninitialized_location, @@ -296,7 +290,6 @@ struct smart_holder_type_caster_class_hooks : smart_holder_type_caster_base_tag new (uninitialized_location) holder_type(holder_type::from_shared_ptr(shd_ptr)); return true; } -#endif template static void init_instance_for_type(detail::instance *inst, const void *holder_const_void_ptr) { @@ -309,26 +302,26 @@ struct smart_holder_type_caster_class_hooks : smart_holder_type_caster_base_tag register_instance(inst, v_h.value_ptr(), v_h.type); v_h.set_instance_registered(); } + auto uninitialized_location = std::addressof(v_h.holder()); + auto value_ptr_w_t = v_h.value_ptr(); if (holder_void_ptr) { // Note: inst->owned ignored. auto holder_ptr = static_cast(holder_void_ptr); - new (std::addressof(v_h.holder())) holder_type(std::move(*holder_ptr)); + new (uninitialized_location) holder_type(std::move(*holder_ptr)); } else { if (!try_initialization_using_shared_from_this( - std::addressof(v_h.holder()), - v_h.value_ptr(), - v_h.value_ptr())) { + uninitialized_location, value_ptr_w_t, value_ptr_w_t)) { if (inst->owned) { - new (std::addressof(v_h.holder())) holder_type( - holder_type::from_raw_ptr_take_ownership(v_h.value_ptr())); + new (uninitialized_location) + holder_type(holder_type::from_raw_ptr_take_ownership(value_ptr_w_t)); } else { - new (std::addressof(v_h.holder())) holder_type( - holder_type::from_raw_ptr_unowned(v_h.value_ptr())); + new (uninitialized_location) + holder_type(holder_type::from_raw_ptr_unowned(value_ptr_w_t)); } } } v_h.holder().pointee_depends_on_holder_owner - = dynamic_raw_ptr_cast_if_possible(v_h.value_ptr()) != nullptr; + = dynamic_raw_ptr_cast_if_possible(value_ptr_w_t) != nullptr; v_h.set_holder_constructed(); } @@ -398,20 +391,14 @@ struct smart_holder_type_caster_load { auto type_raw_ptr = convert_type(void_raw_ptr); if (holder().pointee_depends_on_holder_owner) { // Tie lifetime of trampoline Python part to C++ part (PR #2839). -#ifdef JUNK - to_cout(""); - to_cout("LOOOK " + std::to_string(__LINE__) + " " + __FILE__); -#endif return std::shared_ptr( type_raw_ptr, shared_ptr_dec_ref_deleter{ handle((PyObject *) load_impl.loaded_v_h.inst).inc_ref()}); } -#ifdef JUNK if (holder().vptr_is_using_noop_deleter) { throw std::runtime_error("Non-owning holder (loaded_as_shared_ptr)."); } -#endif std::shared_ptr void_shd_ptr = holder().template as_shared_ptr(); return std::shared_ptr(void_shd_ptr, type_raw_ptr); }