Author: Armin Rigo <[email protected]>
Branch: 
Changeset: r92431:4a1516a142a3
Date: 2017-09-21 09:58 +0200
http://bitbucket.org/pypy/pypy/changeset/4a1516a142a3/

Log:    Improve the error message for ``bytearray[index] = unknown_type''

diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py
--- a/pypy/interpreter/baseobjspace.py
+++ b/pypy/interpreter/baseobjspace.py
@@ -1442,7 +1442,7 @@
             length = 1
         return start, stop, step, length
 
-    def getindex_w(self, w_obj, w_exception, objdescr=None):
+    def getindex_w(self, w_obj, w_exception, objdescr=None, errmsg=None):
         """Return w_obj.__index__() as an RPython int.
         If w_exception is None, silently clamp in case of overflow;
         else raise w_exception.
@@ -1452,8 +1452,10 @@
         except OperationError as err:
             if objdescr is None or not err.match(self, self.w_TypeError):
                 raise
-            raise oefmt(self.w_TypeError, "%s must be an integer, not %T",
-                        objdescr, w_obj)
+            if errmsg is None:
+                errmsg = " must be an integer"
+            raise oefmt(self.w_TypeError, "%s%s, not %T",
+                        objdescr, errmsg, w_obj)
         try:
             # allow_conversion=False it's not really necessary because the
             # return type of __index__ is already checked by space.index(),
@@ -1690,7 +1692,8 @@
             if len(string) != 1:
                 raise oefmt(self.w_ValueError, "string must be of size 1")
             return string[0]
-        value = self.getindex_w(w_obj, None)
+        value = self.getindex_w(w_obj, None, "",
+                                "an integer or string of size 1 is required")
         if not 0 <= value < 256:
             # this includes the OverflowError in case the long is too large
             raise oefmt(self.w_ValueError, "byte must be in range(0, 256)")
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
@@ -658,10 +658,10 @@
         else:
             self.setitem(w_obj, self.newtext(key), w_value)
 
-    def getindex_w(self, w_obj, w_exception, objdescr=None):
+    def getindex_w(self, w_obj, w_exception, objdescr=None, errmsg=None):
         if type(w_obj) is W_IntObject:
             return w_obj.intval
-        return ObjSpace.getindex_w(self, w_obj, w_exception, objdescr)
+        return ObjSpace.getindex_w(self, w_obj, w_exception, objdescr, errmsg)
 
     def unicode_from_object(self, w_obj):
         from pypy.objspace.std.unicodeobject import unicode_from_object
diff --git a/pypy/objspace/std/test/test_bytearrayobject.py 
b/pypy/objspace/std/test/test_bytearrayobject.py
--- a/pypy/objspace/std/test/test_bytearrayobject.py
+++ b/pypy/objspace/std/test/test_bytearrayobject.py
@@ -496,6 +496,15 @@
         b[1] = 'B'
         assert b == 'aBcdefghi'
 
+    def test_setitem_errmsg(self):
+        b = bytearray('abcdefghi')
+        e = raises(TypeError, "b[1] = u'B'")
+        assert str(e.value).startswith(
+            "an integer or string of size 1 is required")
+        e = raises(TypeError, "b[1] = None")
+        assert str(e.value).startswith(
+            "an integer or string of size 1 is required")
+
     def test_setitem_slice(self):
         b = bytearray('abcdefghi')
         b[0:3] = 'ABC'
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to