Author: Ronan Lamy <ronan.l...@gmail.com> Branch: PyBuffer-backport Changeset: r91196:97f60ebf3ec0 Date: 2017-05-06 20:42 +0100 http://bitbucket.org/pypy/pypy/changeset/97f60ebf3ec0/
Log: Add space.byte_w() diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py --- a/pypy/interpreter/baseobjspace.py +++ b/pypy/interpreter/baseobjspace.py @@ -1654,6 +1654,23 @@ def fsencode_or_none_w(self, w_obj): return None if self.is_none(w_obj) else self.fsencode_w(w_obj) + def byte_w(self, w_obj): + """ + Convert an index-like object to an interp-level char + + Used for app-level code like "bytearray(b'abc')[0] = 42". + """ + if self.isinstance_w(w_obj, self.w_bytes): + string = self.bytes_w(w_obj) + 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) + 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)") + return chr(value) + def int_w(self, w_obj, allow_conversion=True): """ Unwrap an app-level int object into an interpret-level int. 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 @@ -415,7 +415,7 @@ else: idx = space.getindex_w(w_index, space.w_IndexError, "bytearray index") - newvalue = getbytevalue(space, w_other) + newvalue = space.byte_w(w_other) self._data[self._fixindex(space, idx)] = newvalue def descr_delitem(self, space, w_idx): @@ -442,7 +442,7 @@ _shrink_after_delete_from_start, self) def descr_append(self, space, w_item): - self._data.append(getbytevalue(space, w_item)) + self._data.append(space.byte_w(w_item)) def descr_extend(self, space, w_other): if isinstance(w_other, W_BytearrayObject): @@ -452,10 +452,9 @@ def descr_insert(self, space, w_idx, w_other): where = space.int_w(w_idx) - val = getbytevalue(space, w_other) data = self.getdata() - length = len(data) - index = get_positive_index(where, length) + index = get_positive_index(where, len(data)) + val = space.byte_w(w_other) data.insert(index, val) @unwrap_spec(w_idx=WrappedDefault(-1)) @@ -552,20 +551,6 @@ # ____________________________________________________________ -def getbytevalue(space, w_value): - if space.isinstance_w(w_value, space.w_bytes): - string = space.bytes_w(w_value) - if len(string) != 1: - raise oefmt(space.w_ValueError, "string must be of size 1") - return string[0] - - value = space.getindex_w(w_value, None) - if not 0 <= value < 256: - # this includes the OverflowError in case the long is too large - raise oefmt(space.w_ValueError, "byte must be in range(0, 256)") - return chr(value) - - def new_bytearray(space, w_bytearraytype, data): w_obj = space.allocate_instance(W_BytearrayObject, w_bytearraytype) W_BytearrayObject.__init__(w_obj, data) @@ -594,8 +579,7 @@ if not e.match(space, space.w_StopIteration): raise break - value = getbytevalue(space, w_item) - data.append(value) + data.append(space.byte_w(w_item)) extended += 1 if extended < length_hint: resizelist_hint(data, extended) diff --git a/pypy/objspace/std/test/test_memoryobject.py b/pypy/objspace/std/test/test_memoryobject.py --- a/pypy/objspace/std/test/test_memoryobject.py +++ b/pypy/objspace/std/test/test_memoryobject.py @@ -27,7 +27,8 @@ v[0:3] = v[2:5] assert data == bytearray(eval("b'23f3fg'")) exc = raises(ValueError, "v[2] = 'spam'") - assert str(exc.value) == "cannot modify size of memoryview object" + assert str(exc.value) in ("cannot modify size of memoryview object", + "string must be of size 1") exc = raises(NotImplementedError, "v[0:2:2] = 'spam'") assert str(exc.value) == "" _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit