[pypy-svn] pypy default: Merge bytearray branch

mfoord commits-noreply at bitbucket.org
Wed Jan 19 00:29:22 CET 2011


Author: Michael Foord <michael at voidspace.org.uk>
Branch: 
Changeset: r40910:2058f2c9656e
Date: 2011-01-19 00:27 +0100
http://bitbucket.org/pypy/pypy/changeset/2058f2c9656e/

Log:	Merge bytearray branch

diff --git a/pypy/objspace/std/listobject.py b/pypy/objspace/std/listobject.py
--- a/pypy/objspace/std/listobject.py
+++ b/pypy/objspace/std/listobject.py
@@ -344,14 +344,18 @@
 def list_insert__List_ANY_ANY(space, w_list, w_where, w_any):
     where = space.int_w(w_where)
     length = len(w_list.wrappeditems)
+    index = get_positive_index(where, length)
+    w_list.wrappeditems.insert(index, w_any)
+    return space.w_None
+
+def get_positive_index(where, length):
     if where < 0:
         where += length
         if where < 0:
             where = 0
     elif where > length:
         where = length
-    w_list.wrappeditems.insert(where, w_any)
-    return space.w_None
+    return where
 
 def list_append__List_ANY(space, w_list, w_any):
     w_list.wrappeditems.append(w_any)

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
@@ -7,7 +7,10 @@
 from pypy.rlib.rstring import StringBuilder
 from pypy.rlib.debug import check_annotation
 from pypy.objspace.std.intobject import W_IntObject
-from pypy.objspace.std.listobject import _delitem_slice_helper, _setitem_slice_helper
+from pypy.objspace.std.listobject import (
+    _delitem_slice_helper, _setitem_slice_helper,
+    get_positive_index
+)
 from pypy.objspace.std.listtype import get_list_index
 from pypy.objspace.std.stringobject import W_StringObject
 from pypy.objspace.std.unicodeobject import W_UnicodeObject
@@ -15,7 +18,7 @@
 from pypy.objspace.std import slicetype
 from pypy.interpreter import gateway
 from pypy.interpreter.buffer import RWBuffer
-from pypy.objspace.std.bytearraytype import makebytearraydata_w
+from pypy.objspace.std.bytearraytype import makebytearraydata_w, getbytevalue
 from pypy.tool.sourcetools import func_with_new_name
 
 
@@ -65,7 +68,7 @@
     return W_BytearrayObject(newdata)
 
 def contains__Bytearray_Int(space, w_bytearray, w_char):
-    char = w_char.intval
+    char = space.int_w(w_char)
     if not 0 <= char < 256:
         raise OperationError(space.w_ValueError,
                              space.wrap("byte must be in range(0, 256)"))
@@ -203,7 +206,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)
@@ -261,6 +264,39 @@
         newdata.extend([c for c in space.str_w(list_w[i])])
     return W_BytearrayObject(newdata)
 
+def bytearray_insert__Bytearray_Int_ANY(space, w_bytearray, w_idx, w_other):
+    where = space.int_w(w_idx)
+    length = len(w_bytearray.data)
+    index = get_positive_index(where, length)
+    val = getbytevalue(space, w_other)
+    w_bytearray.data.insert(index, val)
+    return space.w_None
+
+def bytearray_pop__Bytearray_Int(space, w_bytearray, w_idx):
+    index = space.int_w(w_idx)
+    try:
+        result = w_bytearray.data.pop(index)
+    except IndexError:
+        if not w_bytearray.data:
+            raise OperationError(space.w_OverflowError, space.wrap(
+                "cannot pop an empty bytearray"))
+        raise OperationError(space.w_IndexError, space.wrap(
+            "pop index out of range"))
+    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"))
+
+def bytearray_reverse__Bytearray(space, w_bytearray):
+    w_bytearray.data.reverse()
+    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
@@ -172,6 +172,62 @@
         b.append(ord('e'))
         assert b == 'abcde'
 
+    def test_insert(self):
+        b = bytearray('abc')
+        b.insert(0, 'd')
+        assert b == bytearray('dabc')
+
+        b.insert(-1, ord('e'))
+        assert b == bytearray('dabec')
+
+        b.insert(6, 'f')
+        assert b == bytearray('dabecf')
+
+        b.insert(1, 'g')
+        assert b == bytearray('dgabecf')
+
+        b.insert(-12, 'h')
+        assert b == bytearray('hdgabecf')
+
+        raises(ValueError, b.insert, 1, 'go')
+        raises(TypeError, b.insert, 'g', 'o')
+
+    def test_pop(self):
+        b = bytearray('world')
+        assert b.pop() == ord('d')
+        assert b.pop(0) == ord('w')
+        assert b.pop(-2) == ord('r')
+        raises(IndexError, b.pop, 10)
+        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, b.remove, u'e')
+        b.remove(Indexable())
+        assert b == ''
+
+    def test_reverse(self):
+        b = bytearray('hello')
+        b.reverse()
+        assert b == bytearray('olleh')
+
     def test_delitem(self):
         b = bytearray('abc')
         del b[1]


More information about the Pypy-commit mailing list