Skip to content

Commit 5583036

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 798ce6d commit 5583036

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
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/pytypes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ class object_api : public pyobject_tag {
105105
function will throw a `cast_error` exception. When the Python function
106106
call fails, a `error_already_set` exception is thrown.
107107
\endrst */
108-
template <return_value_policy policy = return_value_policy::automatic_reference, typename... Args>
108+
template <return_value_policy policy = return_value_policy::reference, typename... Args>
109109
object operator()(Args &&...args) const;
110-
template <return_value_policy policy = return_value_policy::automatic_reference, typename... Args>
110+
template <return_value_policy policy = return_value_policy::reference, typename... Args>
111111
PYBIND11_DEPRECATED("call(...) was deprecated in favor of operator()(...)")
112112
object call(Args&&... args) const;
113113

0 commit comments

Comments
 (0)