improve error message from passing inadequate number of keyword arguments #6474

Note this removes the "non-keyword" or "keyword" phrases from these messages.
This commit is contained in:
Benjamin Peterson 2010-03-21 20:21:00 +00:00
parent 54bc22e9f3
commit 965458931f
3 changed files with 19 additions and 5 deletions

View file

@ -270,6 +270,15 @@ the function call setup. See <http://bugs.python.org/issue2016>.
... print a,b ... print a,b
>>> f(**x) >>> f(**x)
1 2 1 2
A obscure message:
>>> def f(a, b):
... pass
>>> f(b=1)
Traceback (most recent call last):
...
TypeError: f() takes exactly 2 arguments (1 given)
""" """
import unittest import unittest

View file

@ -12,6 +12,9 @@ What's New in Python 2.7 beta 1?
Core and Builtins Core and Builtins
----------------- -----------------
- Issue #6474: Make error message from passing an inadequate number of keyword
arguments to a function correct.
- Issue #8164: Don't allow lambda functions to have a docstring. - Issue #8164: Don't allow lambda functions to have a docstring.
- Issue #3137: Don't ignore errors at startup, especially a keyboard interrupt - Issue #3137: Don't ignore errors at startup, especially a keyboard interrupt

View file

@ -3055,11 +3055,10 @@ PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
if (!(co->co_flags & CO_VARARGS)) { if (!(co->co_flags & CO_VARARGS)) {
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"%.200s() takes %s %d " "%.200s() takes %s %d "
"%sargument%s (%d given)", "argument%s (%d given)",
PyString_AsString(co->co_name), PyString_AsString(co->co_name),
defcount ? "at most" : "exactly", defcount ? "at most" : "exactly",
co->co_argcount, co->co_argcount,
kwcount ? "non-keyword " : "",
co->co_argcount == 1 ? "" : "s", co->co_argcount == 1 ? "" : "s",
argcount); argcount);
goto fail; goto fail;
@ -3150,15 +3149,18 @@ PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
int m = co->co_argcount - defcount; int m = co->co_argcount - defcount;
for (i = argcount; i < m; i++) { for (i = argcount; i < m; i++) {
if (GETLOCAL(i) == NULL) { if (GETLOCAL(i) == NULL) {
int j, given = 0;
for (j = 0; j < co->co_argcount; j++)
if (GETLOCAL(j))
given++;
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"%.200s() takes %s %d " "%.200s() takes %s %d "
"%sargument%s (%d given)", "argument%s (%d given)",
PyString_AsString(co->co_name), PyString_AsString(co->co_name),
((co->co_flags & CO_VARARGS) || ((co->co_flags & CO_VARARGS) ||
defcount) ? "at least" defcount) ? "at least"
: "exactly", : "exactly",
m, kwcount ? "non-keyword " : "", m, m == 1 ? "" : "s", given);
m == 1 ? "" : "s", i);
goto fail; goto fail;
} }
} }