From caa1379e92e4c50c62a91e3c1a5c1b95134e90ed Mon Sep 17 00:00:00 2001 From: Jason Rhinelander Date: Thu, 23 Feb 2017 19:54:53 -0500 Subject: [PATCH] Make bad kwarg arguments try next overload Fixes #688. My (commented) assumption that such an error is "highly likely to be a caller mistake" was proven false by #688. --- include/pybind11/pybind11.h | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index cc468e48..74c3138c 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -444,21 +444,19 @@ protected: size_t args_copied = 0; // 1. Copy any position arguments given. + bool bad_kwarg = false; for (; args_copied < args_to_copy; ++args_copied) { - // If we find a given positional argument that also has a named kwargs argument, - // raise a TypeError like Python does. (We could also continue with the next - // overload, but this seems highly likely to be a caller mistake rather than a - // legitimate overload). - if (kwargs_in && args_copied < func.args.size() && func.args[args_copied].name) { - handle value = PyDict_GetItemString(kwargs_in, func.args[args_copied].name); - if (value) - throw type_error(std::string(func.name) + "(): got multiple values for argument '" + - std::string(func.args[args_copied].name) + "'"); + if (kwargs_in && args_copied < func.args.size() && func.args[args_copied].name + && PyDict_GetItemString(kwargs_in, func.args[args_copied].name)) { + bad_kwarg = true; + break; } call.args.push_back(PyTuple_GET_ITEM(args_in, args_copied)); call.args_convert.push_back(args_copied < func.args.size() ? func.args[args_copied].convert : true); } + if (bad_kwarg) + continue; // Maybe it was meant for another overload (issue #688) // We'll need to copy this if we steal some kwargs for defaults dict kwargs = reinterpret_borrow(kwargs_in);