mirror of
https://github.com/pybind/pybind11.git
synced 2026-05-12 09:17:42 +00:00
Show kwargs in failed method invocation
With the previous commit, output can be very confusing because you only see positional arguments in the "invoked with" line, but you can have a failure from kwargs as well (in particular, when a value is invalidly specified via both via positional and kwargs). This commits adds kwargs to the output, and updates the associated tests to match.
This commit is contained in:
committed by
Wenzel Jakob
parent
caa1379e92
commit
231e167854
@@ -650,11 +650,26 @@ protected:
|
||||
}
|
||||
msg += "\nInvoked with: ";
|
||||
auto args_ = reinterpret_borrow<tuple>(args_in);
|
||||
bool some_args = false;
|
||||
for (size_t ti = overloads->is_constructor ? 1 : 0; ti < args_.size(); ++ti) {
|
||||
if (!some_args) some_args = true;
|
||||
else msg += ", ";
|
||||
msg += pybind11::repr(args_[ti]);
|
||||
if ((ti + 1) != args_.size() )
|
||||
msg += ", ";
|
||||
}
|
||||
if (kwargs_in) {
|
||||
auto kwargs = reinterpret_borrow<dict>(kwargs_in);
|
||||
if (kwargs.size() > 0) {
|
||||
if (some_args) msg += "; ";
|
||||
msg += "kwargs: ";
|
||||
bool first = true;
|
||||
for (auto kwarg : kwargs) {
|
||||
if (first) first = false;
|
||||
else msg += ", ";
|
||||
msg += pybind11::str("{}={!r}").format(kwarg.first, kwarg.second);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PyErr_SetString(PyExc_TypeError, msg.c_str());
|
||||
return nullptr;
|
||||
} else if (!result) {
|
||||
|
||||
Reference in New Issue
Block a user