Skip to content
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

py::arg and py::arg_v member function chaining fixes #30155

Merged
merged 3 commits into from
Aug 29, 2024
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
18 changes: 18 additions & 0 deletions include/pybind11/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -1943,6 +1943,18 @@ struct arg : detail::arg_base {
template <typename T>
arg_v operator=(T &&value) const;

/// Same as `arg_base::noconvert()`, but returns *this as arg&, not arg_base&
arg &noconvert(bool flag = true) {
arg_base::noconvert(flag);
return *this;
}

/// Same as `arg_base::noconvert()`, but returns *this as arg&, not arg_base&
arg &none(bool flag = true) {
arg_base::none(flag);
return *this;
}

arg &policies(const from_python_policies &policies) {
m_policies = policies;
return *this;
Expand Down Expand Up @@ -2013,6 +2025,12 @@ struct arg_v : arg {
return *this;
}

/// Same as `arg::policies()`, but returns *this as arg_v&, not arg&
arg_v &policies(const from_python_policies &policies) {
arg::policies(policies);
return *this;
}

/// The default value
object value;
bool value_is_nullptr = false;
Expand Down
46 changes: 46 additions & 0 deletions tests/test_return_value_policy_pack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,4 +436,50 @@ TEST_SUBMODULE(return_value_policy_pack, m) {
py::class_<IntOwner>(m, "IntOwner").def_readonly("val", &IntOwner::val);

m.def("call_callback_pass_int_owner_const_ptr", call_callback_pass_int_owner_const_ptr);

// Ensure chaining py::arg() member functions works.
m.def(
"arg_chaining_noconvert_policies",
[](const std::function<std::string(std::string)> &cb) {
return cb("\x80"
"ArgNoconvertPolicies");
},
py::arg("cb").noconvert().policies(py::return_value_policy_pack(rvpb)),
rvpb);
m.def(
"arg_chaining_none_policies",
[](const std::function<std::string(std::string)> &cb) {
return cb("\x80"
"ArgNonePolicies");
},
py::arg("cb").none().policies(py::return_value_policy_pack(rvpb)),
rvpb);
m.def(
"arg_chaining_policies_noconvert",
[](const std::function<std::string(std::string)> &cb) {
return cb("\x80"
"ArgPoliciesNoconvert");
},
py::arg("cb").policies(py::return_value_policy_pack(rvpb)).noconvert(),
rvpb);

// Ensure chaining py::arg_v() member functions works.
// This does not look very useful, but .policies() chaining was added because
// chaining for .noconvert() and .none() existed already.
m.def(
"arg_v_chaining_noconvert",
[](int num) { return num + 10; },
(py::arg("num") = 2).noconvert());
m.def("arg_v_chaining_none", [](int num) { return num + 20; }, (py::arg("num") = 3).none());
m.def(
"arg_v_chaining_none_policies",
[](const std::function<std::string(std::string)> &cb) {
if (cb) {
return cb("\x80"
"ArgvNonePolicies");
}
return std::string("<NONE>");
},
(py::arg("cb") = py::none()).policies(py::return_value_policy_pack(rvpb)),
rvpb);
}
30 changes: 30 additions & 0 deletions tests/test_return_value_policy_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,3 +337,33 @@ def cb(int_owner):
return int_owner.val + 40

assert m.call_callback_pass_int_owner_const_ptr(cb) == 543


@pytest.mark.parametrize(
("fn", "expected"),
[
(m.arg_chaining_noconvert_policies, b"\x80ArgNoconvertPolicies"),
(m.arg_chaining_none_policies, b"\x80ArgNonePolicies"),
(m.arg_chaining_policies_noconvert, b"\x80ArgPoliciesNoconvert"),
],
)
def test_arg_chaining(fn, expected):
assert fn(lambda arg: arg + b"Extra") == expected + b"Extra"


def test_arg_v_chaining_noconvert():
assert m.arg_v_chaining_noconvert() == 12
assert m.arg_v_chaining_noconvert(4) == 14


def test_arg_v_chaining_none():
assert m.arg_v_chaining_none() == 23
assert m.arg_v_chaining_none(5) == 25


def test_arg_v_chaining_none_policies():
assert m.arg_v_chaining_none_policies() == b"<NONE>"
assert (
m.arg_v_chaining_none_policies(lambda arg: arg + b"Extra")
== b"\x80ArgvNonePoliciesExtra"
)