[pypy-svn] r78122 - in pypy/branch/fast-forward/pypy/objspace/std: . test
afa at codespeak.net
afa at codespeak.net
Wed Oct 20 13:27:35 CEST 2010
Author: afa
Date: Wed Oct 20 13:27:34 2010
New Revision: 78122
Modified:
pypy/branch/fast-forward/pypy/objspace/std/bytearrayobject.py
pypy/branch/fast-forward/pypy/objspace/std/test/test_bytes.py
Log:
Implement bytearray.__iadd__, bytearray item/slice assignment.
Modified: pypy/branch/fast-forward/pypy/objspace/std/bytearrayobject.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/bytearrayobject.py (original)
+++ pypy/branch/fast-forward/pypy/objspace/std/bytearrayobject.py Wed Oct 20 13:27:34 2010
@@ -359,15 +359,20 @@
def list_append__Bytearray_ANY(space, w_bytearray, w_item):
from pypy.objspace.std.bytearraytype import getbytevalue
w_bytearray.data.append(getbytevalue(space, w_item))
- return space.w_None
def list_extend__Bytearray_Bytearray(space, w_bytearray, w_other):
w_bytearray.data += w_other.data
- return space.w_None
def list_extend__Bytearray_ANY(space, w_bytearray, w_other):
w_bytearray.data += [c for c in space.str_w(w_other)]
- return space.w_None
+
+def inplace_add__Bytearray_Bytearray(space, w_bytearray1, w_bytearray2):
+ list_extend__Bytearray_Bytearray(space, w_bytearray1, w_bytearray2)
+ return w_bytearray1
+
+def inplace_add__Bytearray_ANY(space, w_bytearray1, w_iterable2):
+ list_extend__Bytearray_ANY(space, w_bytearray1, w_iterable2)
+ return w_bytearray1
def delslice__Bytearray_ANY_ANY(space, w_bytearray, w_start, w_stop):
length = len(w_bytearray.data)
@@ -376,5 +381,24 @@
return
del w_bytearray.data[start:stop]
+def setitem__Bytearray_ANY_ANY(space, w_bytearray, w_index, w_item):
+ from pypy.objspace.std.bytearraytype import getbytevalue
+ idx = space.getindex_w(w_index, space.w_IndexError, "bytearray index")
+ try:
+ w_bytearray.data[idx] = getbytevalue(space, w_item)
+ except IndexError:
+ raise OperationError(space.w_IndexError,
+ space.wrap("bytearray index out of range"))
+
+def setitem__Bytearray_Slice_ANY(space, w_bytearray, w_slice, w_other):
+ oldsize = len(w_bytearray.data)
+ start, stop, step, slicelength = w_slice.indices4(space, oldsize)
+ if step != 1:
+ raise OperationError(space.w_NotImplementedError,
+ space.wrap("fixme: only step=1 for the moment"))
+ assert start >= 0
+ assert stop >= 0
+ w_bytearray.data[start:stop] = [c for c in space.str_w(w_other)]
+
from pypy.objspace.std import bytearraytype
register_all(vars(), bytearraytype)
Modified: pypy/branch/fast-forward/pypy/objspace/std/test/test_bytes.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/test/test_bytes.py (original)
+++ pypy/branch/fast-forward/pypy/objspace/std/test/test_bytes.py Wed Oct 20 13:27:34 2010
@@ -143,6 +143,12 @@
b.append(ord('e'))
assert b == 'abcde'
+ def test_iadd(self):
+ b = bytearray('abc')
+ b += 'def'
+ assert b == 'abcdef'
+ assert isinstance(b, bytearray)
+
def test_extend(self):
b = bytearray('abc')
b.extend(bytearray('def'))
@@ -155,3 +161,17 @@
assert b == 'abcdei'
del b[:3]
assert b == 'dei'
+
+ def test_setitem(self):
+ b = bytearray('abcdefghi')
+ b[1] = 'B'
+ assert b == 'aBcdefghi'
+
+ def test_setitem_slice(self):
+ b = bytearray('abcdefghi')
+ b[0:3] = 'ABC'
+ assert b == 'ABCdefghi'
+ b[3:3] = '...'
+ assert b == 'ABC...defghi'
+ b[3:6] = '()'
+ assert b == 'ABC()defghi'
More information about the Pypy-commit
mailing list