Skip to content

Commit 77a15e1

Browse files
authored
Merge pull request #30032 from rwgk/from_python_policies_automatic_reference_fix
`from_python_policies` `automatic_reference` bug fix
2 parents 47b097b + 0f03f22 commit 77a15e1

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

include/pybind11/detail/common.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,8 @@ struct from_python_policies {
606606
: rvpp(return_value_policy::automatic_reference), convert(true), none(true) {}
607607

608608
// NOLINTNEXTLINE(google-explicit-constructor)
609-
from_python_policies(bool convert, bool none = true) : convert(convert), none(none) {}
609+
from_python_policies(bool convert, bool none = true)
610+
: rvpp(return_value_policy::automatic_reference), convert(convert), none(none) {}
610611

611612
// NOLINTNEXTLINE(google-explicit-constructor)
612613
from_python_policies(const return_value_policy_pack &rvpp)

tests/test_return_value_policy_pack.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
# include <variant>
1919
#endif
2020

21-
namespace {
21+
namespace pybind11_tests {
22+
namespace return_value_policy_pack {
2223

2324
using PairString = std::pair<std::string, std::string>;
2425

@@ -223,7 +224,19 @@ int call_virtual_override(const VirtualBase &base, const std::string &which) {
223224
return -99; // Invalid which.
224225
}
225226

226-
} // namespace
227+
struct IntOwner {
228+
int val = 3;
229+
};
230+
231+
int call_callback_pass_int_owner_const_ptr(const std::function<int(const IntOwner *)> &cb) {
232+
IntOwner int_owner;
233+
return cb(&int_owner) + 500;
234+
}
235+
236+
} // namespace return_value_policy_pack
237+
} // namespace pybind11_tests
238+
239+
using namespace pybind11_tests::return_value_policy_pack;
227240

228241
TEST_SUBMODULE(return_value_policy_pack, m) {
229242
static constexpr auto rvpc = py::return_value_policy::_clif_automatic;
@@ -422,4 +435,8 @@ TEST_SUBMODULE(return_value_policy_pack, m) {
422435
.def("py_nonp_bs", &VirtualBase::nonp_bs, py::arg("a0"), py::arg("a1"));
423436

424437
m.def("call_virtual_override", call_virtual_override);
438+
439+
py::class_<IntOwner>(m, "IntOwner").def_readonly("val", &IntOwner::val);
440+
441+
m.def("call_callback_pass_int_owner_const_ptr", call_callback_pass_int_owner_const_ptr);
425442
}

tests/test_return_value_policy_pack.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,3 +328,10 @@ def test_virtual_overrides_base(which, expected):
328328
)
329329
else:
330330
assert m.call_virtual_override(b, which) == -expected
331+
332+
333+
def test_call_callback_pass_int_owner():
334+
def cb(int_owner):
335+
return int_owner.val + 40
336+
337+
assert m.call_callback_pass_int_owner_const_ptr(cb) == 543

0 commit comments

Comments
 (0)