[pypy-commit] pypy py3k: Improve error messages in tuple unpacking

amauryfa noreply at buildbot.pypy.org
Tue Nov 20 23:16:42 CET 2012


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: py3k
Changeset: r59019:b7eeb81b0412
Date: 2012-11-20 23:14 +0100
http://bitbucket.org/pypy/pypy/changeset/b7eeb81b0412/

Log:	Improve error messages in tuple unpacking

diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py
--- a/pypy/interpreter/baseobjspace.py
+++ b/pypy/interpreter/baseobjspace.py
@@ -913,18 +913,15 @@
                     raise
                 break  # done
             if idx == expected_length:
-                raise OperationError(self.w_ValueError,
-                                    self.wrap("too many values to unpack"))
+                raise operationerrfmt(self.w_ValueError,
+                                      "too many values to unpack (expected %d)",
+                                      expected_length)
             items[idx] = w_item
             idx += 1
         if idx < expected_length:
-            if idx == 1:
-                plural = ""
-            else:
-                plural = "s"
             raise operationerrfmt(self.w_ValueError,
                                   "need more than %d value%s to unpack",
-                                  idx, plural)
+                                  idx, idx != 1 and "s" or "")
         return items
 
     def unpackiterable_unroll(self, w_iterable, expected_length):
diff --git a/pypy/objspace/std/objspace.py b/pypy/objspace/std/objspace.py
--- a/pypy/objspace/std/objspace.py
+++ b/pypy/objspace/std/objspace.py
@@ -420,8 +420,14 @@
     # one is not
 
     def _wrap_expected_length(self, expected, got):
-        return OperationError(self.w_ValueError,
-                self.wrap("expected length %d, got %d" % (expected, got)))
+        if got > expected:
+            raise operationerrfmt(self.w_ValueError,
+                                  "too many values to unpack (expected %d)",
+                                  expected)
+        else:
+            raise operationerrfmt(self.w_ValueError,
+                                  "need more than %d value%s to unpack",
+                                  got, got != 1 and "s" or "")
 
     def unpackiterable(self, w_obj, expected_length=-1):
         if isinstance(w_obj, W_AbstractTupleObject):


More information about the pypy-commit mailing list