[Python-checkins] r82220 - in python/branches/py3k: Lib/inspect.py Lib/test/test_extcall.py Lib/test/test_keywordonlyarg.py Misc/NEWS Python/ceval.c

benjamin.peterson python-checkins at python.org
Fri Jun 25 21:30:21 CEST 2010


Author: benjamin.peterson
Date: Fri Jun 25 21:30:21 2010
New Revision: 82220

Log:
only take into account positional arguments count in related error messages

Modified:
   python/branches/py3k/Lib/inspect.py
   python/branches/py3k/Lib/test/test_extcall.py
   python/branches/py3k/Lib/test/test_keywordonlyarg.py
   python/branches/py3k/Misc/NEWS
   python/branches/py3k/Python/ceval.c

Modified: python/branches/py3k/Lib/inspect.py
==============================================================================
--- python/branches/py3k/Lib/inspect.py	(original)
+++ python/branches/py3k/Lib/inspect.py	Fri Jun 25 21:30:21 2010
@@ -959,7 +959,7 @@
         else:
             arg2value[varargs] = ()
     elif 0 < num_args < num_pos:
-        raise TypeError('%s() takes %s %d %s (%d given)' % (
+        raise TypeError('%s() takes %s %d positional %s (%d given)' % (
             f_name, 'at most' if defaults else 'exactly', num_args,
             'arguments' if num_args > 1 else 'argument', num_total))
     elif num_args == 0 and num_total:

Modified: python/branches/py3k/Lib/test/test_extcall.py
==============================================================================
--- python/branches/py3k/Lib/test/test_extcall.py	(original)
+++ python/branches/py3k/Lib/test/test_extcall.py	Fri Jun 25 21:30:21 2010
@@ -279,13 +279,13 @@
     >>> f(6, a=4, *(1, 2, 3))
     Traceback (most recent call last):
       ...
-    TypeError: f() takes exactly 1 argument (5 given)
+    TypeError: f() takes exactly 1 positional argument (5 given)
     >>> def f(a, *, kw):
     ...    pass
     >>> f(6, 4, kw=4)
     Traceback (most recent call last):
       ...
-    TypeError: f() takes exactly 2 arguments (3 given)
+    TypeError: f() takes exactly 1 positional argument (3 given)
 """
 
 import sys

Modified: python/branches/py3k/Lib/test/test_keywordonlyarg.py
==============================================================================
--- python/branches/py3k/Lib/test/test_keywordonlyarg.py	(original)
+++ python/branches/py3k/Lib/test/test_keywordonlyarg.py	Fri Jun 25 21:30:21 2010
@@ -73,6 +73,14 @@
         fundef3 += "lastarg):\n  pass\n"
         compile(fundef3, "<test>", "single")
 
+    def testTooManyPositionalErrorMessage(self):
+        def f(a, b=None, *, c=None):
+            pass
+        with self.assertRaises(TypeError) as exc:
+            f(1, 2, 3)
+        expected = "f() takes at most 2 positional arguments (3 given)"
+        self.assertEqual(str(exc.exception), expected)
+
     def testSyntaxErrorForFunctionCall(self):
         self.assertRaisesSyntaxError("f(p, k=1, p2)")
         self.assertRaisesSyntaxError("f(p, k1=50, *(1,2), k1=100)")

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Fri Jun 25 21:30:21 2010
@@ -12,6 +12,8 @@
 Core and Builtins
 -----------------
 
+- Be more specific in error messages about positional arguments.
+
 - Issue #8949: "z" format of PyArg_Parse*() functions doesn't accept bytes
   objects, as described in the documentation.
 

Modified: python/branches/py3k/Python/ceval.c
==============================================================================
--- python/branches/py3k/Python/ceval.c	(original)
+++ python/branches/py3k/Python/ceval.c	Fri Jun 25 21:30:21 2010
@@ -3100,11 +3100,11 @@
             if (!(co->co_flags & CO_VARARGS)) {
                 PyErr_Format(PyExc_TypeError,
                     "%U() takes %s %d "
-                    "argument%s (%d given)",
+                    "positional argument%s (%d given)",
                     co->co_name,
                     defcount ? "at most" : "exactly",
-                    total_args,
-                    total_args == 1 ? "" : "s",
+                    co->co_argcount,
+                    co->co_argcount == 1 ? "" : "s",
                     argcount + kwcount);
                 goto fail;
             }


More information about the Python-checkins mailing list