diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 0262296a..0cba8a12 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -871,14 +871,14 @@ struct copyable_holder_caster< pybind11_fail("Passing `std::shared_ptr *` from Python to C++ is not supported " "(inherently unsafe)."); } - return std::addressof(shared_ptr_holder); + return std::addressof(shared_ptr_storage); } explicit operator std::shared_ptr &() { if (typeinfo->holder_enum_v == detail::holder_enum_t::smart_holder) { - shared_ptr_holder = sh_load_helper.load_as_shared_ptr(value); + shared_ptr_storage = sh_load_helper.load_as_shared_ptr(value); } - return shared_ptr_holder; + return shared_ptr_storage; } static handle @@ -924,7 +924,7 @@ struct copyable_holder_caster< } if (v_h.holder_constructed()) { value = v_h.value_ptr(); - shared_ptr_holder = v_h.template holder>(); + shared_ptr_storage = v_h.template holder>(); return; } throw cast_error("Unable to cast from non-held to held instance (T& to Holder) " @@ -953,8 +953,8 @@ struct copyable_holder_caster< if (typeinfo->holder_enum_v == detail::holder_enum_t::smart_holder) { sh_load_helper.loaded_v_h = sub_caster.sh_load_helper.loaded_v_h; } else { - shared_ptr_holder - = std::shared_ptr(sub_caster.shared_ptr_holder, (type *) value); + shared_ptr_storage + = std::shared_ptr(sub_caster.shared_ptr_storage, (type *) value); } return true; } @@ -964,8 +964,8 @@ struct copyable_holder_caster< static bool try_direct_conversions(handle) { return false; } - std::shared_ptr shared_ptr_holder; smart_holder_type_caster_support::load_helper> sh_load_helper; // Const2Mutbl + std::shared_ptr shared_ptr_storage; }; #endif // PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT @@ -1040,7 +1040,7 @@ struct move_only_holder_caster< policy = return_value_policy::reference_internal; } if (policy != return_value_policy::reference_internal) { - throw cast_error("Invalid return_value_policy for unique_ptr&"); + throw cast_error("Invalid return_value_policy for const unique_ptr&"); } return type_caster_base::cast(src.get(), policy, parent); } diff --git a/tests/test_class_sh_trampoline_shared_from_this.cpp b/tests/test_class_sh_trampoline_shared_from_this.cpp index e5d688af..f664cac5 100644 --- a/tests/test_class_sh_trampoline_shared_from_this.cpp +++ b/tests/test_class_sh_trampoline_shared_from_this.cpp @@ -87,7 +87,9 @@ long pass_shared_ptr(const std::shared_ptr &obj) { return sft.use_count(); } -void pass_unique_ptr(const std::unique_ptr &) {} +void pass_unique_ptr_cref(const std::unique_ptr &) { + throw std::runtime_error("Expected to not be reached."); +} Sft *make_pure_cpp_sft_raw_ptr(const std::string &history_seed) { return new Sft{history_seed}; } @@ -135,7 +137,7 @@ TEST_SUBMODULE(class_sh_trampoline_shared_from_this, m) { m.def("use_count", use_count); m.def("pass_shared_ptr", pass_shared_ptr); - m.def("pass_unique_ptr", pass_unique_ptr); + m.def("pass_unique_ptr_cref", pass_unique_ptr_cref); m.def("make_pure_cpp_sft_raw_ptr", make_pure_cpp_sft_raw_ptr); m.def("make_pure_cpp_sft_unq_ptr", make_pure_cpp_sft_unq_ptr); m.def("make_pure_cpp_sft_shd_ptr", make_pure_cpp_sft_shd_ptr); diff --git a/tests/test_class_sh_trampoline_shared_from_this.py b/tests/test_class_sh_trampoline_shared_from_this.py index a074b20a..b112bb5d 100644 --- a/tests/test_class_sh_trampoline_shared_from_this.py +++ b/tests/test_class_sh_trampoline_shared_from_this.py @@ -138,10 +138,11 @@ def test_pass_released_shared_ptr_as_unique_ptr(): stash1 = m.SftSharedPtrStash(1) stash1.Add(obj) # Releases shared_ptr to C++. with pytest.raises(ValueError) as exc_info: - m.pass_unique_ptr(obj) + m.pass_unique_ptr_cref(obj) assert str(exc_info.value) == ( "Python instance is currently owned by a std::shared_ptr." ) + assert obj.history == "PySft_Stash1Add" @pytest.mark.parametrize(