diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2017-06-06 15:45:22 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-06 15:45:22 (GMT) |
commit | 5eb788bf7f54a8e04429e18fc332db858edd64b6 (patch) | |
tree | ff7ea360d2765c1ec18ad02fcfc1de473a1d7b65 /Python | |
parent | 5cefb6cfdd089d237ba6724bb5311ee4f04be59f (diff) | |
download | cpython-5eb788bf7f54a8e04429e18fc332db858edd64b6.zip cpython-5eb788bf7f54a8e04429e18fc332db858edd64b6.tar.gz cpython-5eb788bf7f54a8e04429e18fc332db858edd64b6.tar.bz2 |
bpo-30534: Fixed error messages when pass keyword arguments (#1901)
to functions implemented in C that don't support this.
Also unified error messages for functions that don't take positional or keyword
arguments.
Diffstat (limited to 'Python')
-rw-r--r-- | Python/getargs.c | 48 |
1 files changed, 32 insertions, 16 deletions
diff --git a/Python/getargs.c b/Python/getargs.c index 58c9a99..af1f2a2 100644 --- a/Python/getargs.c +++ b/Python/getargs.c @@ -1704,13 +1704,21 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format, break; } if (max < nargs) { - PyErr_Format(PyExc_TypeError, - "%.200s%s takes %s %d positional arguments" - " (%d given)", - (fname == NULL) ? "function" : fname, - (fname == NULL) ? "" : "()", - (min != INT_MAX) ? "at most" : "exactly", - max, nargs); + if (max == 0) { + PyErr_Format(PyExc_TypeError, + "%.200s%s takes no positional arguments", + (fname == NULL) ? "function" : fname, + (fname == NULL) ? "" : "()"); + } + else { + PyErr_Format(PyExc_TypeError, + "%.200s%s takes %s %d positional arguments" + " (%d given)", + (fname == NULL) ? "function" : fname, + (fname == NULL) ? "" : "()", + (min != INT_MAX) ? "at most" : "exactly", + max, nargs); + } return cleanreturn(0, &freelist); } } @@ -2079,12 +2087,20 @@ vgetargskeywordsfast_impl(PyObject **args, Py_ssize_t nargs, return cleanreturn(0, &freelist); } if (parser->max < nargs) { - PyErr_Format(PyExc_TypeError, - "%200s%s takes %s %d positional arguments (%d given)", - (parser->fname == NULL) ? "function" : parser->fname, - (parser->fname == NULL) ? "" : "()", - (parser->min != INT_MAX) ? "at most" : "exactly", - parser->max, nargs); + if (parser->max == 0) { + PyErr_Format(PyExc_TypeError, + "%200s%s takes no positional arguments", + (parser->fname == NULL) ? "function" : parser->fname, + (parser->fname == NULL) ? "" : "()"); + } + else { + PyErr_Format(PyExc_TypeError, + "%200s%s takes %s %d positional arguments (%d given)", + (parser->fname == NULL) ? "function" : parser->fname, + (parser->fname == NULL) ? "" : "()", + (parser->min != INT_MAX) ? "at most" : "exactly", + parser->max, nargs); + } return cleanreturn(0, &freelist); } @@ -2489,7 +2505,7 @@ _PyArg_NoKeywords(const char *funcname, PyObject *kwargs) return 1; } - PyErr_Format(PyExc_TypeError, "%.200s does not take keyword arguments", + PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments", funcname); return 0; } @@ -2506,7 +2522,7 @@ _PyArg_NoStackKeywords(const char *funcname, PyObject *kwnames) return 1; } - PyErr_Format(PyExc_TypeError, "%.200s does not take keyword arguments", + PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments", funcname); return 0; } @@ -2524,7 +2540,7 @@ _PyArg_NoPositional(const char *funcname, PyObject *args) if (PyTuple_GET_SIZE(args) == 0) return 1; - PyErr_Format(PyExc_TypeError, "%.200s does not take positional arguments", + PyErr_Format(PyExc_TypeError, "%.200s() takes no positional arguments", funcname); return 0; } |