Commits

Amaury Forgeot d'Arc committed b7eeb81

Improve error messages in tuple unpacking

  • Participants
  • Parent commits 21f2905
  • Branches py3k

Comments (0)

Files changed (2)

File pypy/interpreter/baseobjspace.py

                     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):

File pypy/objspace/std/objspace.py

     # 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):