Skip to content

Commit a88dc5c

Browse files
committed
Introduce return_value_policy::trampoline_reference
... to explicitly indicate a call from a trampoline dispatcher method. In this case, the C++ objects should be always passed by (modifyable) reference if required by the wrapped API.
1 parent 940eb01 commit a88dc5c

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

include/pybind11/detail/common.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,11 @@ enum class return_value_policy : uint8_t {
402402
collected while Python is still using the child. More advanced
403403
variations of this scheme are also possible using combinations of
404404
return_value_policy::reference and the keep_alive call policy */
405-
reference_internal
405+
reference_internal,
406+
407+
/* This internally-only used policy applies to C++ arguments passed
408+
to virtual methods overridden in Python to allow reference passing. */
409+
automatic_override
406410
};
407411

408412
PYBIND11_NAMESPACE_BEGIN(detail)

include/pybind11/detail/type_caster_base.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,8 @@ template <typename type> class type_caster_base : public type_caster_generic {
899899
}
900900

901901
static handle cast(const itype *src, return_value_policy policy, handle parent) {
902+
if (policy == return_value_policy::automatic_override)
903+
policy = return_value_policy::reference;
902904
auto st = src_and_type(src);
903905
return type_caster_generic::cast(
904906
st.first, policy, parent, st.second,

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::reference, typename... Args>
108+
template <return_value_policy policy = return_value_policy::automatic_override, typename... Args>
109109
object operator()(Args &&...args) const;
110-
template <return_value_policy policy = return_value_policy::reference, typename... Args>
110+
template <return_value_policy policy = return_value_policy::automatic_override, typename... Args>
111111
PYBIND11_DEPRECATED("call(...) was deprecated in favor of operator()(...)")
112112
object call(Args&&... args) const;
113113

0 commit comments

Comments
 (0)