diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 1c4973b74..05aef0e99 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1021,7 +1021,19 @@ public: return smart_holder_type_caster_support::smart_holder_from_shared_ptr( src, policy, parent, srcs.result); } - return type_caster_base::cast_holder(srcs, &src); + + auto *tinfo = srcs.result.tinfo; + if (tinfo != nullptr && tinfo->holder_enum_v == holder_enum_t::std_shared_ptr) { + return type_caster_base::cast_holder(srcs, &src); + } + + if (parent) { + return type_caster_base::cast( + srcs, return_value_policy::reference_internal, parent); + } + + throw cast_error("Unable to convert std::shared_ptr to Python when the bound type " + "does not use std::shared_ptr or py::smart_holder as its holder type"); } // This function will succeed even if the `responsible_parent` does not own the