Skip to content

[smart_holder] Make smart holder type caster of unique_ptr accept automatic_reference #4775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/pybind11/detail/smart_holder_type_casters.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
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) {
Expand Down
6 changes: 6 additions & 0 deletions tests/test_class_sh_basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_unique_ptr", [](std::unique_ptr<atyp> 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
Expand Down
4 changes: 4 additions & 0 deletions tests/test_class_sh_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"