Skip to content

Commit 1691a84

Browse files
committed
Pass arguments from trampoline methods via return_value_policy::reference
Otherwise, modifications applied by Python-coded method overrides would be applied to copies, even though the parameters were passed by pointer or reference.
1 parent c0dab64 commit 1691a84

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

include/pybind11/detail/smart_holder_type_casters.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,9 @@ struct smart_holder_type_caster<std::unique_ptr<T, D>> : smart_holder_type_caste
724724
return none().release();
725725
if (policy == return_value_policy::automatic)
726726
policy = return_value_policy::reference_internal;
727-
if (policy != return_value_policy::reference_internal)
727+
else if (policy == return_value_policy::reference && !parent)
728+
; // passing from trampoline dispatcher: no parent available
729+
else if (policy != return_value_policy::reference_internal)
728730
throw cast_error(
729731
"Invalid return_value_policy: unique_ptr const& expects reference_internal");
730732
return smart_holder_type_caster<T>::cast(src.get(), policy, parent);

include/pybind11/pybind11.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2243,7 +2243,7 @@ template <class T> function get_override(const T *this_ptr, const char *name) {
22432243
pybind11::gil_scoped_acquire gil; \
22442244
pybind11::function override = pybind11::get_override(static_cast<const cname *>(this), name); \
22452245
if (override) { \
2246-
auto o = override(__VA_ARGS__); \
2246+
auto o = override.operator()<pybind11::return_value_policy::reference>(__VA_ARGS__); \
22472247
if (pybind11::detail::cast_is_temporary_value_reference<ret_type>::value) { \
22482248
static pybind11::detail::override_caster_t<ret_type> caster; \
22492249
return pybind11::detail::cast_ref<ret_type>(std::move(o), caster); \

0 commit comments

Comments
 (0)