Skip to content

Commit 03cd9d6

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 b994121 commit 03cd9d6

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
@@ -716,7 +716,9 @@ struct smart_holder_type_caster<std::unique_ptr<T, D>> : smart_holder_type_caste
716716
return none().release();
717717
if (policy == return_value_policy::automatic)
718718
policy = return_value_policy::reference_internal;
719-
if (policy != return_value_policy::reference_internal)
719+
else if (policy == return_value_policy::reference && !parent)
720+
; // passing from trampoline dispatcher: no parent available
721+
else if (policy != return_value_policy::reference_internal)
720722
throw cast_error(
721723
"Invalid return_value_policy: unique_ptr const& expects reference_internal");
722724
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)