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:
Jason Rhinelander
2017-02-23 21:04:46 -05:00
committed by Wenzel Jakob
parent caa1379e92
commit 231e167854
2 changed files with 32 additions and 15 deletions

View File

@@ -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) {