[pypy-svn] pypy bytearray: (mfoord) implement bytearray.remove
mfoord
commits-noreply at bitbucket.org
Tue Jan 18 18:46:55 CET 2011
Author: Michael Foord <michael at voidspace.org.uk>
Branch: bytearray
Changeset: r40893:71efd7c363ed
Date: 2011-01-18 18:36 +0100
http://bitbucket.org/pypy/pypy/changeset/71efd7c363ed/
Log: (mfoord) implement bytearray.remove
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
@@ -203,7 +203,7 @@
return space.wrap(buf.build())
def str__Bytearray(space, w_bytearray):
- return W_StringObject(''.join(w_bytearray.data))
+ return space.wrap(''.join(w_bytearray.data))
def _convert_idx_params(space, w_self, w_start, w_stop):
start = slicetype._Eval_SliceIndex(space, w_start)
@@ -280,6 +280,15 @@
return space.wrap(ord(result))
+def bytearray_remove__Bytearray_ANY(space, w_bytearray, w_char):
+ char = space.int_w(space.index(w_char))
+ try:
+ result = w_bytearray.data.remove(chr(char))
+ except ValueError:
+ raise OperationError(space.w_ValueError, space.wrap(
+ "value not found in bytearray"))
+ return space.w_None
+
# These methods could just delegate to the string implementation,
# but they have to return a bytearray.
def str_replace__Bytearray_ANY_ANY_ANY(space, w_bytearray, w_str1, w_str2, w_max):
diff --git a/pypy/objspace/std/test/test_bytes.py b/pypy/objspace/std/test/test_bytes.py
--- a/pypy/objspace/std/test/test_bytes.py
+++ b/pypy/objspace/std/test/test_bytes.py
@@ -201,6 +201,29 @@
raises(OverflowError, bytearray().pop)
assert bytearray(b'\xff').pop() == 0xff
+ def test_remove(self):
+ class Indexable:
+ def __index__(self):
+ return ord('e')
+
+ b = bytearray(b'hello')
+ b.remove(ord('l'))
+ assert b == 'helo'
+ b.remove(ord('l'))
+ assert b == 'heo'
+ raises(ValueError, b.remove, ord('l'))
+ raises(ValueError, b.remove, 400)
+ raises(TypeError, b.remove, u'e')
+ raises(TypeError, b.remove, 2.3)
+ # remove first and last
+ b.remove(ord('o'))
+ b.remove(ord('h'))
+ assert b == 'e'
+ raises(TypeError, lambda: b.remove(u'e'))
+ b.remove(Indexable())
+ assert b == ''
+
+
def test_delitem(self):
b = bytearray('abc')
del b[1]
diff --git a/pypy/objspace/std/bytearraytype.py b/pypy/objspace/std/bytearraytype.py
--- a/pypy/objspace/std/bytearraytype.py
+++ b/pypy/objspace/std/bytearraytype.py
@@ -19,15 +19,20 @@
list_append, list_extend)
-bytearray_insert = SMM('insert',3,
+bytearray_insert = SMM('insert', 3,
doc="B.insert(index, int) -> None\n\n"
"Insert a single item into the bytearray before "
"the given index.")
-bytearray_pop = SMM('pop',2, defaults=(-1,),
+
+bytearray_pop = SMM('pop', 2, defaults=(-1,),
doc="B.pop([index]) -> int\n\nRemove and return a "
"single item from B. If no index\nargument is given, "
"will pop the last value.")
+bytearray_remove = SMM('remove', 2,
+ doc="B.remove(int) -> None\n\n"
+ "Remove the first occurance of a value in B.")
+
def getbytevalue(space, w_value):
if space.isinstance_w(w_value, space.w_str):
string = space.str_w(w_value)
More information about the Pypy-commit
mailing list