diff -r 203407036e46 Lib/test/test_extcall.py --- a/Lib/test/test_extcall.py Fri Jun 17 21:10:21 2011 +0200 +++ b/Lib/test/test_extcall.py Fri Jun 17 19:48:49 2011 -0500 @@ -61,27 +61,27 @@ Verify clearing of SF bug #733667 >>> e(c=4) Traceback (most recent call last): ... TypeError: e() got an unexpected keyword argument 'c' >>> g() Traceback (most recent call last): ... - TypeError: g() takes at least 1 positional argument but 0 were given + TypeError: g() missing 1 required positional argument: 'x' >>> g(*()) Traceback (most recent call last): ... - TypeError: g() takes at least 1 positional argument but 0 were given + TypeError: g() missing 1 required positional argument: 'x' >>> g(*(), **{}) Traceback (most recent call last): ... - TypeError: g() takes at least 1 positional argument but 0 were given + TypeError: g() missing 1 required positional argument: 'x' >>> g(1) 1 () {} >>> g(1, 2) 1 (2,) {} >>> g(1, 2, 3) 1 (2, 3) {} >>> g(1, 2, 3, *(4, 5)) @@ -258,101 +258,90 @@ the function call setup. See >> x = {Name("a"):1, Name("b"):2} >>> def f(a, b): ... print(a,b) >>> f(**x) 1 2 -Some additional tests about positional argument errors: +Too many arguments: - >>> def f(a, b): - ... pass - >>> f(b=1) + >>> def f(): pass + >>> f(1) Traceback (most recent call last): ... - TypeError: f() takes 2 positional arguments but 1 was given - - >>> def f(a): - ... pass - >>> f(6, a=4, *(1, 2, 3)) + TypeError: f() takes 0 positional arguments but 1 was given + >>> def f(a): pass + >>> f(1, 2) Traceback (most recent call last): ... - TypeError: f() got multiple values for argument 'a' - >>> def f(a, *, kw): - ... pass - >>> f(6, 4, kw=4) + TypeError: f() takes 1 positional argument but 2 were given + >>> def f(a, b=1): pass + >>> f(1, 2, 3) Traceback (most recent call last): ... - TypeError: f() takes 1 positional argument but 2 positional arguments (and 1 keyword-only argument) were given + TypeError: f() takes from 1 to 2 positional arguments but 3 were given + >>> def f(*, kw): pass + >>> f(1, kw=3) + Traceback (most recent call last): + ... + TypeError: f() takes 0 positional arguments but 1 positional argument (and 1 keyword-only argument) were given + >>> def f(*, kw, b): pass + >>> f(1, 2, 3, b=3, kw=3) + Traceback (most recent call last): + ... + TypeError: f() takes 0 positional arguments but 3 positional arguments (and 2 keyword-only arguments) were given + >>> def f(a, b=2, *, kw): pass + >>> f(2, 3, 4, kw=4) + Traceback (most recent call last): + ... + TypeError: f() takes from 1 to 2 positional arguments but 3 positional arguments (and 1 keyword-only argument) were given - >>> def f(a): - ... pass +Too few and missing arguments: + + >>> def f(a): pass >>> f() Traceback (most recent call last): ... - TypeError: f() takes 1 positional argument but 0 were given - - >>> def f(a, b): - ... pass - >>> f(1) - Traceback (most recent call last): - ... - TypeError: f() takes 2 positional arguments but 1 was given - - >>> def f(a, *b): - ... pass + TypeError: f() missing 1 required positional argument: 'a' + >>> def f(a, b): pass >>> f() Traceback (most recent call last): ... - TypeError: f() takes at least 1 positional argument but 0 were given - - >>> def f(a, *, kw=4): - ... pass - >>> f(kw=4) - Traceback (most recent call last): - ... - TypeError: f() takes 1 positional argument but 0 positional arguments (and 1 keyword-only argument) were given - - >>> def f(a, b=2): - ... pass + TypeError: f() missing 2 required positional arguments: 'a' and 'b' + >>> def f(a, b, c): pass >>> f() Traceback (most recent call last): ... - TypeError: f() takes from 1 to 2 positional arguments but 0 were given - - >>> def f(a, *b): - ... pass + TypeError: f() missing 3 required positional arguments: 'a', 'b', and 'c' + >>> def f(a, b, c, d, e): pass >>> f() Traceback (most recent call last): ... - TypeError: f() takes at least 1 positional argument but 0 were given - - >>> def f(*, kw): - ... pass - >>> f(3, kw=4) + TypeError: f() missing 5 required positional arguments: 'a', 'b', 'c', 'd', and 'e' + >>> def f(a, b=4, c=5, d=5): pass + >>> f(c=12, b=9) Traceback (most recent call last): ... - TypeError: f() takes 0 positional arguments but 1 positional argument (and 1 keyword-only argument) were given + TypeError: f() missing 1 required positional argument: 'a' - >>> def f(a, c=3, *b, kw): - ... pass +Same with keyword only args: + + >>> def f(*, w): pass >>> f() Traceback (most recent call last): - ... - TypeError: f() takes at least 1 positional argument but 0 were given - >>> f(kw=3) + ... + TypeError: f() missing 1 required keyword-only argument: 'w' + >>> def f(*, a, b, c, d, e): pass + >>> f() Traceback (most recent call last): - ... - TypeError: f() takes at least 1 positional argument but 0 positional arguments (and 1 keyword-only argument) were given - >>> f(kw=3, c=4) - Traceback (most recent call last): - ... - TypeError: f() takes at least 1 positional argument but 1 positional argument (and 1 keyword-only argument) were given + ... + TypeError: f() missing 5 required keyword-only arguments: 'a', 'b', 'c', 'd', and 'e' + """ import sys from test import support def test_main(): support.run_doctest(sys.modules[__name__], True) diff -r 203407036e46 Python/ceval.c --- a/Python/ceval.c Fri Jun 17 21:10:21 2011 +0200 +++ b/Python/ceval.c Fri Jun 17 19:48:49 2011 -0500 @@ -3041,38 +3041,128 @@ fast_yield: exit_eval_frame: Py_LeaveRecursiveCall(); tstate->frame = f->f_back; return retval; } static void -positional_argument_error(PyCodeObject *co, int given, int defcount, PyObject **fastlocals) +format_missing(const char *kind, PyCodeObject *co, PyObject *names) +{ + int err; + Py_ssize_t len = PyList_GET_SIZE(names); + PyObject *name_str, *comma, *tail, *tmp; + + assert(PyList_CheckExact(names)); + assert(len >= 1); + /* Deal with the joys of natural language. */ + switch (len) { + case 1: + name_str = PyList_GET_ITEM(names, 0); + Py_INCREF(name_str); + break; + case 2: + name_str = PyUnicode_FromFormat("%U and %U", + PyList_GET_ITEM(names, len - 2), + PyList_GET_ITEM(names, len - 1)); + break; + default: + tail = PyUnicode_FromFormat(", %U, and %U", + PyList_GET_ITEM(names, len - 2), + PyList_GET_ITEM(names, len - 1)); + /* Chop off the last two objects in the list. This shouldn't actually + fail, but we can't be too careful. */ + err = PyList_SetSlice(names, len - 2, len, NULL); + if (err == -1) { + Py_DECREF(tail); + return; + } + /* Stitch everything up into a nice comma-separated list. */ + comma = PyUnicode_FromString(", "); + if (comma == NULL) { + Py_DECREF(tail); + return; + } + tmp = PyUnicode_Join(comma, names); + Py_DECREF(comma); + if (tmp == NULL) { + Py_DECREF(tail); + return; + } + name_str = PyUnicode_Concat(tmp, tail); + Py_DECREF(tmp); + Py_DECREF(tail); + break; + } + if (name_str == NULL) + return; + PyErr_Format(PyExc_TypeError, + "%U() missing %i required %s argument%s: %U", + co->co_name, + len, + kind, + len == 1 ? "" : "s", + name_str); + Py_DECREF(name_str); +} + +static void +missing_arguments(PyCodeObject *co, int missing, int defcount, + PyObject **fastlocals) +{ + int i, j = 0; + int start, end; + int positional = defcount != -1; + const char *kind = positional ? "positional" : "keyword-only"; + PyObject *missing_names; + + /* Compute the names of the arguments that are missing. */ + missing_names = PyList_New(missing); + if (missing_names == NULL) + return; + if (positional) { + start = 0; + end = co->co_argcount - defcount; + } + else { + start = co->co_argcount; + end = start + co->co_kwonlyargcount; + } + for (i = start; i < end; i++) { + if (GETLOCAL(i) == NULL) { + PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i); + PyObject *name = PyObject_Repr(raw); + if (name == NULL) { + Py_DECREF(missing_names); + return; + } + PyList_SET_ITEM(missing_names, j++, name); + } + } + assert(j == missing); + format_missing(kind, co, missing_names); + Py_DECREF(missing_names); +} + +static void +too_many_positional(PyCodeObject *co, int given, int defcount, PyObject **fastlocals) { int plural; int kwonly_given = 0; - int atleast = co->co_argcount - defcount; int i; PyObject *sig, *kwonly_sig; - if (given == -1) { - given = 0; - for (i = 0; i < co->co_argcount; i++) - if (GETLOCAL(i)) - given++; - } + assert((co->co_flags & CO_VARARGS) == 0); + /* Count missing keyword-only args. */ for (i = co->co_argcount; i < co->co_argcount + co->co_kwonlyargcount; i++) - if (GETLOCAL(i)) + if (GETLOCAL(i) != NULL) kwonly_given++; - if (co->co_flags & CO_VARARGS) { - plural = atleast != 1; - sig = PyUnicode_FromFormat("at least %d", atleast); - } - else if (defcount) { + if (defcount) { + int atleast = co->co_argcount - defcount; plural = 1; sig = PyUnicode_FromFormat("from %d to %d", atleast, co->co_argcount); } else { plural = co->co_argcount != 1; sig = PyUnicode_FromFormat("%d", co->co_argcount); } if (sig == NULL) @@ -3084,16 +3174,17 @@ positional_argument_error(PyCodeObject * if (kwonly_sig == NULL) { Py_DECREF(sig); return; } } else { /* This will not fail. */ kwonly_sig = PyUnicode_FromString(""); + assert(kwonly_sig != NULL); } PyErr_Format(PyExc_TypeError, "%U() takes %U positional argument%s but %d%U %s given", co->co_name, sig, plural ? "s" : "", given, kwonly_sig, @@ -3212,56 +3303,60 @@ PyEval_EvalCodeEx(PyObject *_co, PyObjec co->co_name, keyword); goto fail; } Py_INCREF(value); SETLOCAL(j, value); } if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) { - positional_argument_error(co, argcount, defcount, fastlocals); + too_many_positional(co, argcount, defcount, fastlocals); goto fail; } if (argcount < co->co_argcount) { int m = co->co_argcount - defcount; - for (i = argcount; i < m; i++) { - if (GETLOCAL(i) == NULL) { - positional_argument_error(co, -1, defcount, fastlocals); - goto fail; - } + int missing = 0; + for (i = argcount; i < m; i++) + if (GETLOCAL(i) == NULL) + missing++; + if (missing) { + missing_arguments(co, missing, defcount, fastlocals); + goto fail; } if (n > m) i = n - m; else i = 0; for (; i < defcount; i++) { if (GETLOCAL(m+i) == NULL) { PyObject *def = defs[i]; Py_INCREF(def); SETLOCAL(m+i, def); } } } if (co->co_kwonlyargcount > 0) { + int missing = 0; for (i = co->co_argcount; i < total_args; i++) { PyObject *name; if (GETLOCAL(i) != NULL) continue; name = PyTuple_GET_ITEM(co->co_varnames, i); if (kwdefs != NULL) { PyObject *def = PyDict_GetItem(kwdefs, name); if (def) { Py_INCREF(def); SETLOCAL(i, def); continue; } } - PyErr_Format(PyExc_TypeError, - "%U() requires keyword-only argument '%S'", - co->co_name, name); + missing++; + } + if (missing) { + missing_arguments(co, missing, -1, fastlocals); goto fail; } } /* Allocate and initialize storage for cell vars, and copy free vars into frame. This isn't too efficient right now. */ if (PyTuple_GET_SIZE(co->co_cellvars)) { int i, j, nargs, found;