From 94497408a3bc89593540532d5841450551f99a4e Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Wed, 30 Aug 2017 11:24:25 -0400 Subject: [PATCH 1/6] Simplify and fix functional policy and tests --- include/pybind11/cast.h | 13 +------------ tests/test_callbacks.cpp | 16 ++++++++++++++++ tests/test_callbacks.py | 9 +++++++++ 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 79e743d9bb..dbffafc612 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1744,18 +1744,7 @@ template ::value, int> = 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; - } - return reinterpret_steal( - detail::make_caster::cast(std::forward(value), policy, parent)); + return reinterpret_steal(detail::make_caster::cast(value, policy, parent)); } template diff --git a/tests/test_callbacks.cpp b/tests/test_callbacks.cpp index 9361451a18..b911af6d28 100644 --- a/tests/test_callbacks.cpp +++ b/tests/test_callbacks.cpp @@ -40,6 +40,17 @@ double apply_custom_transform(const py::object &src, double value) { } // namespace boost_histogram } // namespace test_callbacks +struct TestNotCopyable{ + TestNotCopyable() {}; + TestNotCopyable(const TestNotCopyable &obj) = delete; +}; + +TestNotCopyable* full_func_return_ptr() { + return new TestNotCopyable(); +} + +std::function func_return_ptr = &full_func_return_ptr; + int dummy_function(int i) { return i + 1; } TEST_SUBMODULE(callbacks, m) { @@ -299,4 +310,9 @@ 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); + 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..976b3b5911 100644 --- a/tests/test_callbacks.py +++ b/tests/test_callbacks.py @@ -246,3 +246,12 @@ 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() + + +@pytest.mark.xfail(reason="Currently auto policy is not inherited") +def test_full_func_return_ptr_inherit(): + m.func_return_ptr() From 1b15e8253eead03b025e2bc9e008e45cd0b5604c Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Wed, 28 Jul 2021 12:45:52 -0700 Subject: [PATCH 2/6] Update include/pybind11/cast.h --- include/pybind11/cast.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index dbffafc612..3dd1cafd5b 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1744,7 +1744,7 @@ template ::value, int> = object cast(T &&value, return_value_policy policy = return_value_policy::automatic_reference, handle parent = handle()) { - return reinterpret_steal(detail::make_caster::cast(value, policy, parent)); + return reinterpret_steal(detail::make_caster::cast(std::forward(value), policy, parent)); } template From 03e124e22853d7fb2abaa04d8baa91b7ecabb53f Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Wed, 28 Jul 2021 12:54:13 -0700 Subject: [PATCH 3/6] Update tests/test_callbacks.cpp --- tests/test_callbacks.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_callbacks.cpp b/tests/test_callbacks.cpp index b911af6d28..da6cc88313 100644 --- a/tests/test_callbacks.cpp +++ b/tests/test_callbacks.cpp @@ -41,7 +41,7 @@ double apply_custom_transform(const py::object &src, double value) { } // namespace test_callbacks struct TestNotCopyable{ - TestNotCopyable() {}; + TestNotCopyable() = default; TestNotCopyable(const TestNotCopyable &obj) = delete; }; From be7c9260df212d277f0c4b844e93588080469f9f Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Wed, 28 Jul 2021 13:09:50 -0700 Subject: [PATCH 4/6] Update tests/test_callbacks.py --- tests/test_callbacks.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_callbacks.py b/tests/test_callbacks.py index 976b3b5911..7730e371a5 100644 --- a/tests/test_callbacks.py +++ b/tests/test_callbacks.py @@ -252,6 +252,5 @@ def test_full_func_return_ptr(): m.full_func_return_ptr() -@pytest.mark.xfail(reason="Currently auto policy is not inherited") def test_full_func_return_ptr_inherit(): m.func_return_ptr() From 9501ae7d7dde7d7394a6f7d90e8b0d342f26ced8 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 12 Nov 2026 09:48:32 -0500 Subject: [PATCH 5/6] test: avoid leak in functional pointer test and format Assisted-by: ClaudeCode:claude-fable-5-2 --- include/pybind11/cast.h | 3 ++- tests/test_callbacks.cpp | 14 +++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 3dd1cafd5b..ec75847d3e 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1744,7 +1744,8 @@ template ::value, int> = object cast(T &&value, return_value_policy policy = return_value_policy::automatic_reference, handle parent = handle()) { - return reinterpret_steal(detail::make_caster::cast(std::forward(value), policy, parent)); + return reinterpret_steal( + detail::make_caster::cast(std::forward(value), policy, parent)); } template diff --git a/tests/test_callbacks.cpp b/tests/test_callbacks.cpp index da6cc88313..4ac4b9b1ac 100644 --- a/tests/test_callbacks.cpp +++ b/tests/test_callbacks.cpp @@ -40,16 +40,19 @@ double apply_custom_transform(const py::object &src, double value) { } // namespace boost_histogram } // namespace test_callbacks -struct TestNotCopyable{ +struct TestNotCopyable { TestNotCopyable() = default; TestNotCopyable(const TestNotCopyable &obj) = delete; }; -TestNotCopyable* full_func_return_ptr() { - return new TestNotCopyable(); -} +// Python takes ownership (automatic policy on a cpp_function) +TestNotCopyable *full_func_return_ptr() { return new TestNotCopyable(); } -std::function func_return_ptr = &full_func_return_ptr; +// 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; } @@ -313,6 +316,7 @@ TEST_SUBMODULE(callbacks, m) { // 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"); } From 48a4635adc061901e8ac31f96a20ef81547adc56 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Sat, 21 Nov 2026 13:27:07 -0500 Subject: [PATCH 6/6] fix: forward automatic policy from py::cast only for std::function py::cast() resolves the automatic policies again, so nested casters (for example an lvalue std::vector) keep the copy semantics. Only the std::function caster gets the unresolved policy. Assisted-by: ClaudeCode:claude-fable-5-2 --- include/pybind11/cast.h | 19 +++++++++++++++++++ include/pybind11/functional.h | 4 ++++ tests/test_stl.cpp | 17 +++++++++++++++++ tests/test_stl.py | 5 +++++ 4 files changed, 45 insertions(+) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index ec75847d3e..70dd7a1b3d 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1739,11 +1739,30 @@ 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 (!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_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]