[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
This commit is contained in:
Xiaofei Wang 2023-08-03 18:33:16 -07:00 committed by GitHub
parent b6444460ee
commit f1e2e55bda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 0 deletions

View File

@ -965,6 +965,7 @@ struct smart_holder_type_caster<std::unique_ptr<T, D>> : smart_holder_type_caste
static handle cast(std::unique_ptr<T, D> &&src, return_value_policy policy, handle parent) { static handle cast(std::unique_ptr<T, D> &&src, return_value_policy policy, handle parent) {
if (policy != return_value_policy::automatic if (policy != return_value_policy::automatic
&& policy != return_value_policy::automatic_reference
&& policy != return_value_policy::reference_internal && policy != return_value_policy::reference_internal
&& policy != return_value_policy::move && policy != return_value_policy::move
&& policy != return_value_policy::_clif_automatic) { && policy != return_value_policy::_clif_automatic) {

View File

@ -160,6 +160,12 @@ TEST_SUBMODULE(class_sh_basic, m) {
m.def("args_shared_ptr_const", [](std::shared_ptr<atyp const> p) { return p; }); m.def("args_shared_ptr_const", [](std::shared_ptr<atyp const> p) { return p; });
m.def("args_unique_ptr", [](std::unique_ptr<atyp> p) { return p; }); m.def("args_unique_ptr", [](std::unique_ptr<atyp> p) { return p; });
m.def("args_unique_ptr_const", [](std::unique_ptr<atyp const> p) { return p; }); m.def("args_unique_ptr_const", [](std::unique_ptr<atyp const> 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<atyp>(new atyp("rtrn_uq_automatic_reference")); },
pybind11::return_value_policy::automatic_reference);
} }
} // namespace class_sh_basic } // namespace class_sh_basic

View File

@ -181,3 +181,7 @@ def test_function_signatures(doc):
doc(m.args_unique_ptr_const) doc(m.args_unique_ptr_const)
== "args_unique_ptr_const(arg0: m.class_sh_basic.atyp) -> m.class_sh_basic.atyp" == "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"