Author: Armin Rigo <[email protected]>
Branch: py3.5
Changeset: r88472:f930c9e46e70
Date: 2016-11-18 17:04 +0100
http://bitbucket.org/pypy/pypy/changeset/f930c9e46e70/
Log: Fix error messages of some bytes/bytearray operations
diff --git a/pypy/objspace/std/bytearrayobject.py
b/pypy/objspace/std/bytearrayobject.py
--- a/pypy/objspace/std/bytearrayobject.py
+++ b/pypy/objspace/std/bytearrayobject.py
@@ -27,6 +27,8 @@
class W_BytearrayObject(W_Root):
import_from_mixin(StringMethods)
+ _KIND1 = "bytearray"
+ _KIND2 = "bytearray"
def __init__(self, data):
check_list_of_chars(data)
diff --git a/pypy/objspace/std/bytesobject.py b/pypy/objspace/std/bytesobject.py
--- a/pypy/objspace/std/bytesobject.py
+++ b/pypy/objspace/std/bytesobject.py
@@ -400,6 +400,8 @@
class W_BytesObject(W_AbstractBytesObject):
import_from_mixin(StringMethods)
_immutable_fields_ = ['_value']
+ _KIND1 = "byte"
+ _KIND2 = "bytes"
def __init__(self, str):
assert str is not None
@@ -424,7 +426,7 @@
def writebuf_w(self, space):
raise oefmt(space.w_TypeError,
- "Cannot use string as modifiable buffer")
+ "Cannot use bytes as modifiable buffer")
def descr_getbuffer(self, space, w_flags):
#from pypy.objspace.std.bufferobject import W_Buffer
@@ -437,7 +439,7 @@
def ord(self, space):
if len(self._value) != 1:
raise oefmt(space.w_TypeError,
- "ord() expected a character, but string of length %d "
+ "ord() expected a character, but bytes of length %d "
"found", len(self._value))
return space.wrap(ord(self._value[0]))
@@ -756,7 +758,7 @@
if space.isinstance_w(w_source, space.w_unicode):
raise oefmt(space.w_TypeError,
- "cannot convert unicode object to bytes")
+ "cannot convert a (unicode) str object to bytes")
# sequence of bytes
w_iter = space.iter(w_source)
diff --git a/pypy/objspace/std/stringmethods.py
b/pypy/objspace/std/stringmethods.py
--- a/pypy/objspace/std/stringmethods.py
+++ b/pypy/objspace/std/stringmethods.py
@@ -116,6 +116,9 @@
descr_rmul = descr_mul
+ _KIND1 = "string"
+ _KIND2 = "string"
+
def descr_getitem(self, space, w_index):
if isinstance(w_index, W_SliceObject):
selfvalue = self._val(space)
@@ -130,7 +133,7 @@
ret = _descr_getslice_slowpath(selfvalue, start, step, sl)
return self._new_from_list(ret)
- index = space.getindex_w(w_index, space.w_IndexError, "string")
+ index = space.getindex_w(w_index, space.w_IndexError, self._KIND1)
return self._getitem_result(space, index)
def _getitem_result(self, space, index):
@@ -140,7 +143,7 @@
try:
character = selfvalue[index]
except IndexError:
- raise oefmt(space.w_IndexError, "string index out of range")
+ raise oefmt(space.w_IndexError, self._KIND1 + " index out of
range")
from pypy.objspace.std.bytesobject import W_BytesObject
if isinstance(self, W_BytesObject):
return space.wrap(ord(character))
@@ -274,7 +277,7 @@
if res < 0:
raise oefmt(space.w_ValueError,
- "substring not found in string.index")
+ "substring not found in " + self._KIND2 + ".index")
return space.wrap(res)
def descr_rindex(self, space, w_sub, w_start=None, w_end=None):
@@ -288,7 +291,7 @@
if res < 0:
raise oefmt(space.w_ValueError,
- "substring not found in string.rindex")
+ "substring not found in " + self._KIND2 + ".rindex")
return space.wrap(res)
@specialize.arg(2)
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
@@ -552,3 +552,8 @@
def test___alloc__(self):
# pypy: always returns len()+1; cpython: may be bigger
assert bytearray(b'123456').__alloc__() >= 7
+
+ def test_getitem_error_message(self):
+ e = raises(TypeError, bytearray(b'abc').__getitem__, b'd')
+ assert str(e.value).startswith(
+ 'bytearray indices must be integers or slices')
diff --git a/pypy/objspace/std/test/test_bytesobject.py
b/pypy/objspace/std/test/test_bytesobject.py
--- a/pypy/objspace/std/test/test_bytesobject.py
+++ b/pypy/objspace/std/test/test_bytesobject.py
@@ -931,3 +931,8 @@
raises(TypeError, 'b"%b" % "hello world"')
assert b'%b %b' % (b'a', bytearray(b'f f e')) == b'a f f e'
"""
+
+ def test_getitem_error_message(self):
+ e = raises(TypeError, b'abc'.__getitem__, b'd')
+ assert str(e.value).startswith(
+ 'byte indices must be integers or slices')
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit