Skip to content
Open
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
7 changes: 5 additions & 2 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,7 @@ class cpp_function : public function {
// replaced later
if (func.has_args && call.args.size() == func.nargs_pos) {
call.args.push_back(none());
call.args_convert.push_back(false);
}

call.args.push_back(value);
Expand Down Expand Up @@ -1187,10 +1188,11 @@ class cpp_function : public function {
}
if (call.args.size() <= func.nargs_pos) {
call.args.push_back(call.args_ref);
call.args_convert.push_back(false);
} else {
// The stub pushed in step 2 already has its convert flag.
call.args[func.nargs_pos] = call.args_ref;
}
call.args_convert.push_back(false);
}

// 4b. If we have a py::kwargs, pass on any remaining kwargs
Expand Down Expand Up @@ -1246,7 +1248,8 @@ class cpp_function : public function {
// The (overloaded) call failed; if the call has at least one argument that
// permits conversion (i.e. it hasn't been explicitly specified `.noconvert()`)
// then add this call to the list of second pass overloads to try.
for (size_t i = func.is_method ? 1 : 0; i < pos_args; i++) {
// Keyword-only arguments count too, so scan all of the flags.
for (size_t i = func.is_method ? 1 : 0; i < second_pass_convert.size(); i++) {
if (second_pass_convert[i]) {
// Found one: swap the converting flags back in and store the call for
// the second pass.
Expand Down
17 changes: 17 additions & 0 deletions tests/test_kwargs_and_defaults.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,23 @@ TEST_SUBMODULE(kwargs_and_defaults, m) {
"j"_a = 3.14159,
"z"_a = 42);

// test_args_kwonly_noconvert
m.def(
"args_kwonly_noconvert",
[](const py::args &args, bool x, bool y) { return py::make_tuple(args, x, y); },
py::arg("x").noconvert(),
py::arg("y"));

// test_args_kwonly_second_pass
m.def(
"args_kwonly_second_pass",
[](const py::args &, const std::string &s) { return "str: " + s; },
"s"_a);
m.def(
"args_kwonly_second_pass",
[](const py::args &, bool b) { return std::string(b ? "bool: True" : "bool: False"); },
"b"_a);

// test_args_refcount
// PyPy needs a garbage collection to get the reference count values to match CPython's behaviour
// PyPy uses the top few bits for REFCNT_FROM_PYPY & REFCNT_FROM_PYPY_LIGHT, so truncate
Expand Down
14 changes: 14 additions & 0 deletions tests/test_kwargs_and_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,20 @@ def test_mixed_args_and_kwargs(msg):
assert m.args_kwonly_kwargs_defaults(5, 6, 7, m=8, z=9) == (5, 6, (7,), 9, {"m": 8})


def test_args_kwonly_noconvert():
# The noconvert flag must stay on x, the argument it was given for.
assert m.args_kwonly_noconvert(1, 2, x=True, y=1) == ((1, 2), True, True)
with pytest.raises(TypeError):
m.args_kwonly_noconvert(x=1, y=True)


def test_args_kwonly_second_pass():
# Only the keyword-only argument can convert, so the second pass must run.
assert m.args_kwonly_second_pass(s="a") == "str: a"
assert m.args_kwonly_second_pass(b=True) == "bool: True"
assert m.args_kwonly_second_pass(b=1) == "bool: True"


def test_keyword_only_args(msg):
assert m.kw_only_all(i=1, j=2) == (1, 2)
assert m.kw_only_all(j=1, i=2) == (2, 1)
Expand Down
Loading