[pypy-svn] pypy fast-forward: CPython changed a bit its error messages.

amauryfa commits-noreply at bitbucket.org
Fri Jan 7 11:41:04 CET 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: fast-forward
Changeset: r40442:75a9c1a51728
Date: 2011-01-07 11:38 +0100
http://bitbucket.org/pypy/pypy/changeset/75a9c1a51728/

Log:	CPython changed a bit its error messages. Also, count the arguments
	before trying to match keywords

diff --git a/pypy/interpreter/argument.py b/pypy/interpreter/argument.py
--- a/pypy/interpreter/argument.py
+++ b/pypy/interpreter/argument.py
@@ -272,6 +272,12 @@
         args_w = self.arguments_w
         num_args = len(args_w)
 
+        keywords = self.keywords
+        keywords_w = self.keywords_w
+        num_kwds = 0
+        if keywords is not None:
+            num_kwds = len(keywords)
+
         avail = num_args + upfront
 
         if input_argcount < co_argcount:
@@ -287,11 +293,24 @@
                 scope_w[i + input_argcount] = args_w[i]
             input_argcount += take
 
-        keywords = self.keywords
-        keywords_w = self.keywords_w
-        num_kwds = 0
-        if keywords is not None:
-            num_kwds = len(keywords)
+        # collect extra positional arguments into the *vararg
+        if has_vararg:
+            args_left = co_argcount - upfront
+            if args_left < 0:  # check required by rpython
+                assert extravarargs is not None
+                starargs_w = extravarargs
+                if num_args:
+                    starargs_w = starargs_w + args_w
+            elif num_args > args_left:
+                starargs_w = args_w[args_left:]
+            else:
+                starargs_w = []
+            scope_w[co_argcount] = self.space.newtuple(starargs_w)
+        elif avail > co_argcount:
+            raise ArgErrCount(avail, num_kwds,
+                              co_argcount, has_vararg, has_kwarg,
+                              defaults_w, 0)
+
         # the code assumes that keywords can potentially be large, but that
         # argnames is typically not too large
         num_remainingkwds = num_kwds
@@ -332,24 +351,6 @@
                     # keyword arguments, which will be checked for below.
                     missing += 1
 
-        # collect extra positional arguments into the *vararg
-        if has_vararg:
-            args_left = co_argcount - upfront
-            if args_left < 0:  # check required by rpython
-                assert extravarargs is not None
-                starargs_w = extravarargs
-                if num_args:
-                    starargs_w = starargs_w + args_w
-            elif num_args > args_left:
-                starargs_w = args_w[args_left:]
-            else:
-                starargs_w = []
-            scope_w[co_argcount] = self.space.newtuple(starargs_w)
-        elif avail > co_argcount:
-            raise ArgErrCount(avail, num_kwds,
-                              co_argcount, has_vararg, has_kwarg,
-                              defaults_w, 0)
-
         # collect extra keyword arguments into the **kwarg
         if has_kwarg:
             w_kwds = self.space.newdict()
@@ -360,6 +361,10 @@
                         self.space.setitem(w_kwds, self.space.wrap(key), keywords_w[i])
             scope_w[co_argcount + has_vararg] = w_kwds
         elif num_remainingkwds:
+            if co_argcount == 0:
+                raise ArgErrCount(avail, num_kwds,
+                              co_argcount, has_vararg, has_kwarg,
+                              defaults_w, missing)
             raise ArgErrUnknownKwds(num_remainingkwds, keywords, used_keywords)
 
         if missing:
@@ -593,14 +598,7 @@
     def getmsg(self, fnname):
         args = None
         #args_w, kwds_w = args.unpack()
-        if self.has_kwarg or (self.num_kwds and self.num_defaults):
-            if self.missing_args:
-                required_args = self.expected_nargs - self.num_defaults
-                nargs = required_args - self.missing_args
-            else:
-                nargs = self.num_args
-        else:
-            nargs = self.num_args + self.num_kwds
+        nargs = self.num_args + self.num_kwds
         n = self.expected_nargs
         if n == 0:
             msg = "%s() takes no argument (%d given)" % (

diff --git a/pypy/interpreter/test/test_argument.py b/pypy/interpreter/test/test_argument.py
--- a/pypy/interpreter/test/test_argument.py
+++ b/pypy/interpreter/test/test_argument.py
@@ -495,10 +495,10 @@
         assert s == "foo() takes at most 2 arguments (3 given)"
         err = ArgErrCount(0, 1, 2, True, False, ['a'], 1)
         s = err.getmsg('foo')
-        assert s == "foo() takes at least 1 argument (0 given)"
+        assert s == "foo() takes at least 1 argument (1 given)"
         err = ArgErrCount(2, 1, 1, False, True, [], 0)
         s = err.getmsg('foo')
-        assert s == "foo() takes exactly 1 argument (2 given)"
+        assert s == "foo() takes exactly 1 argument (3 given)"
 
 
     def test_unknown_keywords(self):
@@ -514,6 +514,19 @@
         s = err.getmsg('foo')
         assert s == "foo() got multiple values for keyword argument 'bla'"
 
+class AppTestArgument:
+    def test_error_message(self):
+        exc = raises(TypeError, (lambda a, b=2: 0), b=3)
+        assert exc.value.message == "<lambda>() takes at least 1 argument (1 given)"
+        exc = raises(TypeError, (lambda: 0), b=3)
+        assert exc.value.message == "<lambda>() takes no argument (1 given)"
+        exc = raises(TypeError, (lambda a, b: 0), 1, 2, 3, a=1)
+        assert exc.value.message == "<lambda>() takes exactly 2 arguments (4 given)"
+        exc = raises(TypeError, (lambda a, b=1: 0), 1, 2, 3, a=1)
+        assert exc.value.message == "<lambda>() takes at most 2 arguments (4 given)"
+        exc = raises(TypeError, (lambda a, b=1, **kw: 0), 1, 2, 3)
+        assert exc.value.message == "<lambda>() takes at most 2 arguments (3 given)"
+
 def make_arguments_for_translation(space, args_w, keywords_w={},
                                    w_stararg=None, w_starstararg=None):
     return ArgumentsForTranslation(space, args_w, keywords_w.keys(),


More information about the Pypy-commit mailing list