diff --git a/include/pybind11/detail/smart_holder_type_casters.h b/include/pybind11/detail/smart_holder_type_casters.h index 22758e101..15ab81c58 100644 --- a/include/pybind11/detail/smart_holder_type_casters.h +++ b/include/pybind11/detail/smart_holder_type_casters.h @@ -624,15 +624,18 @@ struct smart_holder_type_caster> : smart_holder_type_caster_l static handle cast(const std::shared_ptr &src, return_value_policy policy, handle parent) { if (policy != return_value_policy::automatic - && policy != return_value_policy::reference_internal) { + && policy != return_value_policy::reference_internal + && policy != return_value_policy::automatic_reference) { // SMART_HOLDER_WIP: IMPROVABLE: Error message. throw cast_error("Invalid return_value_policy for shared_ptr."); } + if (!src) + return none().release(); auto src_raw_ptr = src.get(); auto st = type_caster_base::src_and_type(src_raw_ptr); - if (st.first == nullptr) - return none().release(); // PyErr was set already. + if (st.second == nullptr) + return handle(); // no type info: error will be set already void *src_raw_void_ptr = static_cast(src_raw_ptr); const detail::type_info *tinfo = st.second; @@ -693,11 +696,13 @@ struct smart_holder_type_caster> : smart_holder_type_caste // SMART_HOLDER_WIP: IMPROVABLE: Error message. throw cast_error("Invalid return_value_policy for unique_ptr."); } + if (!src) + return none().release(); auto src_raw_ptr = src.get(); auto st = type_caster_base::src_and_type(src_raw_ptr); - if (st.first == nullptr) - return none().release(); // PyErr was set already. + if (st.second == nullptr) + return handle(); // no type info: error will be set already void *src_raw_void_ptr = static_cast(src_raw_ptr); const detail::type_info *tinfo = st.second;