From c6a87e88976ff41625f9ebc85b9314d575bd5c0f Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Tue, 9 Jul 2024 15:07:10 -0700 Subject: [PATCH] Call new load_helper.get_void_ptr_or_nullptr() instead of calling type_caster_generic::load_value() in shared_ptr and unique_ptr casters. --- include/pybind11/cast.h | 4 ++-- include/pybind11/detail/type_caster_base.h | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 34e44f027..5c7b25da5 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -903,7 +903,7 @@ protected: void load_value(value_and_holder &&v_h) { if (typeinfo->default_holder) { sh_load_helper.loaded_v_h = v_h; - type_caster_generic::load_value(std::move(v_h)); // NOLINT(performance-move-const-arg) + value = sh_load_helper.get_void_ptr_or_nullptr(); return; } if (v_h.holder_constructed()) { @@ -1028,7 +1028,7 @@ public: if (typeinfo->default_holder) { sh_load_helper.loaded_v_h = v_h; sh_load_helper.loaded_v_h.type = get_type_info(typeid(type)); - type_caster_generic::load_value(std::move(v_h)); // NOLINT(performance-move-const-arg) + value = sh_load_helper.get_void_ptr_or_nullptr(); return; } throw std::runtime_error("BAKEIN_WIP: What is the best behavior here (load_value)?"); diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index abc9ce8f3..d77a973b6 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -617,6 +617,16 @@ printf("\nLOOOK throw(Python instance was disowned.)\n"); fflush(stdout); // NO throw value_error("Python instance is currently owned by a std::shared_ptr."); } } + + void *get_void_ptr_or_nullptr() const { + if (have_holder()) { + auto &hld = holder(); + if (hld.is_populated && hld.has_pointee()) { + return hld.template as_raw_ptr_unowned(); + } + } + return nullptr; + } }; template