@@ -199,8 +199,7 @@ public:
199199 template <typename T_, \
200200 ::pybind11::detail::enable_if_t < \
201201 std::is_same<type, ::pybind11::detail::remove_cv_t <T_>>::value, \
202- int > \
203- = 0 > \
202+ int > = 0 > \
204203 static ::pybind11::handle cast ( \
205204 T_ *src, ::pybind11::return_value_policy policy, ::pybind11::handle parent) { \
206205 if (!src) \
@@ -1020,7 +1019,19 @@ struct copyable_holder_caster<
10201019 return smart_holder_type_caster_support::smart_holder_from_shared_ptr (
10211020 src, policy, parent, srcs.result );
10221021 }
1023- return type_caster_base<type>::cast_holder (srcs, &src);
1022+
1023+ auto *tinfo = srcs.result .tinfo ;
1024+ if (tinfo != nullptr && tinfo->holder_enum_v == holder_enum_t ::std_shared_ptr) {
1025+ return type_caster_base<type>::cast_holder (srcs, &src);
1026+ }
1027+
1028+ if (parent) {
1029+ return type_caster_base<type>::cast (
1030+ srcs, return_value_policy::reference_internal, parent);
1031+ }
1032+
1033+ throw cast_error (" Unable to convert std::shared_ptr<T> to Python when the bound type "
1034+ " does not use std::shared_ptr or py::smart_holder as its holder type" );
10241035 }
10251036
10261037 // This function will succeed even if the `responsible_parent` does not own the
@@ -1662,8 +1673,7 @@ PYBIND11_NAMESPACE_END(detail)
16621673template <typename T,
16631674 detail::enable_if_t<!detail::is_pyobject<T>::value
16641675 && !detail::is_same_ignoring_cvref<T, PyObject *>::value,
1665- int>
1666- = 0>
1676+ int> = 0>
16671677T cast(const handle &handle) {
16681678 using namespace detail ;
16691679 constexpr bool is_enum_cast = type_uses_type_caster_enum_type<intrinsic_t <T>>::value;
@@ -1698,8 +1708,7 @@ template <typename T,
16981708 typename Handle,
16991709 detail::enable_if_t <detail::is_same_ignoring_cvref<T, PyObject *>::value
17001710 && detail::is_same_ignoring_cvref<Handle, handle>::value,
1701- int >
1702- = 0 >
1711+ int > = 0 >
17031712T cast (Handle &&handle) {
17041713 return handle.inc_ref ().ptr ();
17051714}
@@ -1708,8 +1717,7 @@ template <typename T,
17081717 typename Object,
17091718 detail::enable_if_t <detail::is_same_ignoring_cvref<T, PyObject *>::value
17101719 && detail::is_same_ignoring_cvref<Object, object>::value,
1711- int >
1712- = 0 >
1720+ int > = 0 >
17131721T cast (Object &&obj) {
17141722 return obj.release ().ptr ();
17151723}
@@ -2243,8 +2251,13 @@ class unpacking_collector {
22432251 if (m_names) {
22442252 nargs -= m_names.size ();
22452253 }
2246- PyObject *result = _PyObject_Vectorcall (
2247- ptr, m_args.data () + 1 , nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, m_names.ptr ());
2254+ PyObject *result =
2255+ #if PY_VERSION_HEX >= 0x03090000
2256+ PyObject_Vectorcall (
2257+ #else
2258+ _PyObject_Vectorcall (
2259+ #endif
2260+ ptr, m_args.data () + 1 , nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, m_names.ptr ());
22482261 if (!result) {
22492262 throw error_already_set ();
22502263 }
0 commit comments