]> granicus.if.org Git - python/commitdiff
improve error message from passing inadequate number of keyword arguments #6474
authorBenjamin Peterson <benjamin@python.org>
Sun, 21 Mar 2010 20:21:00 +0000 (20:21 +0000)
committerBenjamin Peterson <benjamin@python.org>
Sun, 21 Mar 2010 20:21:00 +0000 (20:21 +0000)
Note this removes the "non-keyword" or "keyword" phrases from these messages.

Lib/test/test_extcall.py
Misc/NEWS
Python/ceval.c

index 3eea1213313869b24ffe3022d0d76e85aab041e4..bb922c884d6743a3f0b4e9368b461f01dded25d6 100644 (file)
@@ -270,6 +270,15 @@ the function call setup. See <http://bugs.python.org/issue2016>.
     ...     print a,b
     >>> f(**x)
     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
index 895c7538420bd9576cc5da9388e92126e49be003..f93486ef7db3422d8d5d6cb8fb7d50b1746d34ba 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 2.7 beta 1?
 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 #3137: Don't ignore errors at startup, especially a keyboard interrupt
index 7ce3e514e1e9207d1c554317d50c54653210c0df..4b0ff7ee2272a88072047f3ee37c20705be6aff5 100644 (file)
@@ -3055,11 +3055,10 @@ PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
                        if (!(co->co_flags & CO_VARARGS)) {
                                PyErr_Format(PyExc_TypeError,
                                    "%.200s() takes %s %d "
-                                   "%sargument%s (%d given)",
+                                   "argument%s (%d given)",
                                    PyString_AsString(co->co_name),
                                    defcount ? "at most" : "exactly",
                                    co->co_argcount,
-                                   kwcount ? "non-keyword " : "",
                                    co->co_argcount == 1 ? "" : "s",
                                    argcount);
                                goto fail;
@@ -3150,15 +3149,18 @@ PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
                        int m = co->co_argcount - defcount;
                        for (i = argcount; i < m; i++) {
                                if (GETLOCAL(i) == NULL) {
+                                       int j, given = 0;
+                                       for (j = 0; j < co->co_argcount; j++)
+                                               if (GETLOCAL(j))
+                                                       given++;
                                        PyErr_Format(PyExc_TypeError,
                                            "%.200s() takes %s %d "
-                                           "%sargument%s (%d given)",
+                                           "argument%s (%d given)",
                                            PyString_AsString(co->co_name),
                                            ((co->co_flags & CO_VARARGS) ||
                                             defcount) ? "at least"
                                                       : "exactly",
-                                           m, kwcount ? "non-keyword " : "",
-                                           m == 1 ? "" : "s", i);
+                                           m, m == 1 ? "" : "s", given);
                                        goto fail;
                                }
                        }