Skip to content

Commit f1e2e55

Browse files
authored
[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
1 parent b644446 commit f1e2e55

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

include/pybind11/detail/smart_holder_type_casters.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,7 @@ struct smart_holder_type_caster<std::unique_ptr<T, D>> : smart_holder_type_caste
965965

966966
static handle cast(std::unique_ptr<T, D> &&src, return_value_policy policy, handle parent) {
967967
if (policy != return_value_policy::automatic
968+
&& policy != return_value_policy::automatic_reference
968969
&& policy != return_value_policy::reference_internal
969970
&& policy != return_value_policy::move
970971
&& policy != return_value_policy::_clif_automatic) {

tests/test_class_sh_basic.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ TEST_SUBMODULE(class_sh_basic, m) {
160160
m.def("args_shared_ptr_const", [](std::shared_ptr<atyp const> p) { return p; });
161161
m.def("args_unique_ptr", [](std::unique_ptr<atyp> p) { return p; });
162162
m.def("args_unique_ptr_const", [](std::unique_ptr<atyp const> p) { return p; });
163+
164+
// Make sure unique_ptr type caster accept automatic_reference return value policy.
165+
m.def(
166+
"rtrn_uq_automatic_reference",
167+
[]() { return std::unique_ptr<atyp>(new atyp("rtrn_uq_automatic_reference")); },
168+
pybind11::return_value_policy::automatic_reference);
163169
}
164170

165171
} // namespace class_sh_basic

tests/test_class_sh_basic.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,7 @@ def test_function_signatures(doc):
181181
doc(m.args_unique_ptr_const)
182182
== "args_unique_ptr_const(arg0: m.class_sh_basic.atyp) -> m.class_sh_basic.atyp"
183183
)
184+
185+
186+
def test_unique_ptr_return_value_policy_automatic_reference():
187+
assert m.get_mtxt(m.rtrn_uq_automatic_reference()) == "rtrn_uq_automatic_reference"

0 commit comments

Comments
 (0)