diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 79e743d9bb..70dd7a1b3d 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1739,20 +1739,29 @@ T cast(Object &&obj) { return obj.release().ptr(); } +PYBIND11_NAMESPACE_BEGIN(detail) +// Specialize to true for a type whose caster must get the automatic policies unresolved from +// pybind11::cast() (std::function passes the policy on to the return value of the callable). +template +struct cast_forwards_automatic_policy : std::false_type {}; +PYBIND11_NAMESPACE_END(detail) + // C++ type -> py::object template ::value, int> = 0> object cast(T &&value, return_value_policy policy = return_value_policy::automatic_reference, handle parent = handle()) { using no_ref_T = typename std::remove_reference::type; - if (policy == return_value_policy::automatic) { - policy = std::is_pointer::value ? return_value_policy::take_ownership - : std::is_lvalue_reference::value ? return_value_policy::copy - : return_value_policy::move; - } else if (policy == return_value_policy::automatic_reference) { - policy = std::is_pointer::value ? return_value_policy::reference - : std::is_lvalue_reference::value ? return_value_policy::copy - : return_value_policy::move; + if (!detail::cast_forwards_automatic_policy>::value) { + if (policy == return_value_policy::automatic) { + policy = std::is_pointer::value ? return_value_policy::take_ownership + : std::is_lvalue_reference::value ? return_value_policy::copy + : return_value_policy::move; + } else if (policy == return_value_policy::automatic_reference) { + policy = std::is_pointer::value ? return_value_policy::reference + : std::is_lvalue_reference::value ? return_value_policy::copy + : return_value_policy::move; + } } return reinterpret_steal( detail::make_caster::cast(std::forward(value), policy, parent)); diff --git a/include/pybind11/functional.h b/include/pybind11/functional.h index 3c169cb842..9c26ea5c7c 100644 --- a/include/pybind11/functional.h +++ b/include/pybind11/functional.h @@ -51,6 +51,10 @@ struct func_wrapper : func_wrapper_base { PYBIND11_NAMESPACE_END(type_caster_std_function_specializations) +// The policy applies to the return value of the callable, not to the std::function. +template +struct cast_forwards_automatic_policy> : std::true_type {}; + template struct type_caster> { using type = std::function; diff --git a/tests/test_callbacks.cpp b/tests/test_callbacks.cpp index 9361451a18..4ac4b9b1ac 100644 --- a/tests/test_callbacks.cpp +++ b/tests/test_callbacks.cpp @@ -40,6 +40,20 @@ double apply_custom_transform(const py::object &src, double value) { } // namespace boost_histogram } // namespace test_callbacks +struct TestNotCopyable { + TestNotCopyable() = default; + TestNotCopyable(const TestNotCopyable &obj) = delete; +}; + +// Python takes ownership (automatic policy on a cpp_function) +TestNotCopyable *full_func_return_ptr() { return new TestNotCopyable(); } + +// Static: a cast std::function gets the reference policy, so Python must not own it +TestNotCopyable *static_func_return_ptr() { + static TestNotCopyable obj; + return &obj; +} + int dummy_function(int i) { return i + 1; } TEST_SUBMODULE(callbacks, m) { @@ -299,4 +313,10 @@ TEST_SUBMODULE(callbacks, m) { test_callbacks::boost_histogram::custom_transform_int); m.def("boost_histogram_apply_custom_transform", test_callbacks::boost_histogram::apply_custom_transform); + + // Testing returning pointers to non-copyable objects + m.attr("full_func_return_ptr") = py::cpp_function(full_func_return_ptr); + std::function func_return_ptr = &static_func_return_ptr; + m.attr("func_return_ptr") = func_return_ptr; + py::class_(m, "TestNotCopyable"); } diff --git a/tests/test_callbacks.py b/tests/test_callbacks.py index 1c3117041b..7730e371a5 100644 --- a/tests/test_callbacks.py +++ b/tests/test_callbacks.py @@ -246,3 +246,11 @@ def test_boost_histogram_apply_custom_transform(): assert apply(lambda value: value, 9) == -200 assert apply({}, 0) == -100 assert apply("", 0) == -100 + + +def test_full_func_return_ptr(): + m.full_func_return_ptr() + + +def test_full_func_return_ptr_inherit(): + m.func_return_ptr() diff --git a/tests/test_stl.cpp b/tests/test_stl.cpp index b5d1979d04..ceddd8d992 100644 --- a/tests/test_stl.cpp +++ b/tests/test_stl.cpp @@ -618,6 +618,23 @@ TEST_SUBMODULE(stl, m) { }, py::return_value_policy::take_ownership); + // py::cast() of an lvalue container copies the pointees, also for the automatic policies + auto cast_lvalue_ptr_vector_copies = [](py::return_value_policy policy) { + std::unique_ptr owner(new NoAssign(7)); + std::vector vec{owner.get()}; + auto lst = py::cast(vec, policy).cast(); + bool copied = lst[0].cast() != owner.get(); + if (!copied && policy == py::return_value_policy::automatic) { + owner.release(); // Python took ownership; avoid a double free + } + return copied; + }; + m.def("cast_lvalue_ptr_vector_copies_automatic", + [=]() { return cast_lvalue_ptr_vector_copies(py::return_value_policy::automatic); }); + m.def("cast_lvalue_ptr_vector_copies_automatic_reference", [=]() { + return cast_lvalue_ptr_vector_copies(py::return_value_policy::automatic_reference); + }); + m.def("array_cast_sequence", [](std::array x) { return x; }); /// test_issue_1561 diff --git a/tests/test_stl.py b/tests/test_stl.py index f16a5dc743..bda9455562 100644 --- a/tests/test_stl.py +++ b/tests/test_stl.py @@ -581,6 +581,11 @@ def test_stl_ownership(): assert cstats.alive() == 0 +def test_cast_lvalue_ptr_vector_copies(): + assert m.cast_lvalue_ptr_vector_copies_automatic() + assert m.cast_lvalue_ptr_vector_copies_automatic_reference() + + def test_array_cast_sequence(): assert m.array_cast_sequence((1, 2, 3)) == [1, 2, 3]