[pypy-commit] pypy py3.7: implement error message improvement of CPython

cfbolz pypy.commits at gmail.com
Wed Jan 8 06:50:13 EST 2020


Author: Carl Friedrich Bolz-Tereick <cfbolz at gmx.de>
Branch: py3.7
Changeset: r98485:a4a502d7215c
Date: 2020-01-08 12:31 +0100
http://bitbucket.org/pypy/pypy/changeset/a4a502d7215c/

Log:	implement error message improvement of CPython

diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py
--- a/pypy/interpreter/baseobjspace.py
+++ b/pypy/interpreter/baseobjspace.py
@@ -918,11 +918,23 @@
                         w_obj.getclass(self))
         return w_obj
 
+    def _iter_unpackiterable(self, w_iterable):
+        try:
+            return self.iter(w_iterable)
+        except OperationError as e:
+            if e.got_any_traceback():
+                raise
+            if not e.match(self, self.w_TypeError):
+                raise
+            raise oefmt(self.w_TypeError,
+                    "cannot unpack non-iterable %T object",
+                    w_iterable)
+
     def unpackiterable(self, w_iterable, expected_length=-1):
         """Unpack an iterable into a real (interpreter-level) list.
 
         Raise an OperationError(w_ValueError) if the length is wrong."""
-        w_iterator = self.iter(w_iterable)
+        w_iterator = self._iter_unpackiterable(w_iterable)
         if expected_length == -1:
             if self.is_generator(w_iterator):
                 # special hack for speed
@@ -999,7 +1011,7 @@
         # Like unpackiterable(), but for the cases where we have
         # an expected_length and want to unroll when JITted.
         # Returns a fixed-size list.
-        w_iterator = self.iter(w_iterable)
+        w_iterator = self._iter_unpackiterable(w_iterable)
         assert expected_length != -1
         return self._unpackiterable_known_length_jitlook(w_iterator,
                                                          expected_length)
diff --git a/pypy/interpreter/test/test_interpreter.py b/pypy/interpreter/test/test_interpreter.py
--- a/pypy/interpreter/test/test_interpreter.py
+++ b/pypy/interpreter/test/test_interpreter.py
@@ -516,3 +516,13 @@
         else:
             assert False, "Expected ValueError"
             """
+
+    def test_errormsg_unpacking(self):
+        with raises(TypeError) as excinfo:
+            a, b, c = 1
+        assert str(excinfo.value) == "cannot unpack non-iterable int object"
+
+        with raises(TypeError) as excinfo:
+            for a, b in range(10):
+                pass
+        assert str(excinfo.value) == "cannot unpack non-iterable int object"


More information about the pypy-commit mailing list