From f1e2e55bdabfcab0506d8bd8d970dd47aba829be Mon Sep 17 00:00:00 2001 From: Xiaofei Wang <6218006+wangxf123456@users.noreply.github.com> Date: Thu, 3 Aug 2023 18:33:16 -0700 Subject: [PATCH] [smart_holder] Make smart holder type caster of `unique_ptr` accept `automatic_reference` (#4775) * Also accept automatic_reference * Add a test case * Remove the test case * Add another test case * Fix test case --- include/pybind11/detail/smart_holder_type_casters.h | 1 + tests/test_class_sh_basic.cpp | 6 ++++++ tests/test_class_sh_basic.py | 4 ++++ 3 files changed, 11 insertions(+) diff --git a/include/pybind11/detail/smart_holder_type_casters.h b/include/pybind11/detail/smart_holder_type_casters.h index 67d7c4f13..bd9483a02 100644 --- a/include/pybind11/detail/smart_holder_type_casters.h +++ b/include/pybind11/detail/smart_holder_type_casters.h @@ -965,6 +965,7 @@ struct smart_holder_type_caster> : smart_holder_type_caste static handle cast(std::unique_ptr &&src, return_value_policy policy, handle parent) { if (policy != return_value_policy::automatic + && policy != return_value_policy::automatic_reference && policy != return_value_policy::reference_internal && policy != return_value_policy::move && policy != return_value_policy::_clif_automatic) { diff --git a/tests/test_class_sh_basic.cpp b/tests/test_class_sh_basic.cpp index 502f48a2c..7582cfa04 100644 --- a/tests/test_class_sh_basic.cpp +++ b/tests/test_class_sh_basic.cpp @@ -160,6 +160,12 @@ TEST_SUBMODULE(class_sh_basic, m) { m.def("args_shared_ptr_const", [](std::shared_ptr p) { return p; }); m.def("args_unique_ptr", [](std::unique_ptr p) { return p; }); m.def("args_unique_ptr_const", [](std::unique_ptr p) { return p; }); + + // Make sure unique_ptr type caster accept automatic_reference return value policy. + m.def( + "rtrn_uq_automatic_reference", + []() { return std::unique_ptr(new atyp("rtrn_uq_automatic_reference")); }, + pybind11::return_value_policy::automatic_reference); } } // namespace class_sh_basic diff --git a/tests/test_class_sh_basic.py b/tests/test_class_sh_basic.py index 95460d9dc..268e79398 100644 --- a/tests/test_class_sh_basic.py +++ b/tests/test_class_sh_basic.py @@ -181,3 +181,7 @@ def test_function_signatures(doc): doc(m.args_unique_ptr_const) == "args_unique_ptr_const(arg0: m.class_sh_basic.atyp) -> m.class_sh_basic.atyp" ) + + +def test_unique_ptr_return_value_policy_automatic_reference(): + assert m.get_mtxt(m.rtrn_uq_automatic_reference()) == "rtrn_uq_automatic_reference"