When calling a Python callback function that consumes std::unique_ptr in C++, we see RuntimeError: Invalid return_value_policy for unique_ptr. This is because when calling Python functions manually from C++ , we use return_value_policy::automatic_reference for function arguments, and the type caster of std::unique_ptr rejects this return value policy.
Description
When calling a Python callback function that consumes
std::unique_ptr
in C++, we seeRuntimeError: Invalid return_value_policy for unique_ptr.
This is because when calling Python functions manually from C++ , we usereturn_value_policy::automatic_reference
for function arguments, and the type caster ofstd::unique_ptr
rejects this return value policy.Pybind11 always override
return_value_policy::automatic
andreturn_value_policy::automatic_reference
withreturn_value_policy::move
forstd::unique_ptr
: https://github.com/pybind/pybind11/blob/b6444460eeddc2965ab1a49c6c50c83073779489/include/pybind11/cast.h#L1138, soreturn_value_policy::automatic
andreturn_value_policy::automatic_reference
does not make any difference forstd::unique_ptr
. We should make the type caster accept both.