[pypy-svn] pypy default: (mfoord) implement bytarray delitem for indexes and slices

mfoord commits-noreply at bitbucket.org
Mon Jan 17 13:29:16 CET 2011


Author: Michael Foord <michael at voidspace.org.uk>
Branch: 
Changeset: r40757:0d44ffddf4d0
Date: 2011-01-17 13:25 +0100
http://bitbucket.org/pypy/pypy/changeset/0d44ffddf4d0/

Log:	(mfoord) implement bytarray delitem for indexes and slices

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
@@ -13,7 +13,7 @@
 
 class W_ListObject(W_Object):
     from pypy.objspace.std.listtype import list_typedef as typedef
-    
+
     def __init__(w_self, wrappeditems):
         w_self.wrappeditems = wrappeditems
 
@@ -98,7 +98,7 @@
 def delslice__List_ANY_ANY(space, w_list, w_start, w_stop):
     length = len(w_list.wrappeditems)
     start, stop = normalize_simple_slice(space, length, w_start, w_stop)
-    _delitem_slice_helper(space, w_list, start, 1, stop-start)
+    _delitem_slice_helper(space, w_list.wrappeditems, start, 1, stop-start)
 
 def contains__List_ANY(space, w_list, w_obj):
     # needs to be safe against eq_w() mutating the w_list behind our back
@@ -214,22 +214,21 @@
 def delitem__List_Slice(space, w_list, w_slice):
     start, stop, step, slicelength = w_slice.indices4(space,
                                                       len(w_list.wrappeditems))
-    _delitem_slice_helper(space, w_list, start, step, slicelength)
+    _delitem_slice_helper(space, w_list.wrappeditems, start, step, slicelength)
 
-def _delitem_slice_helper(space, w_list, start, step, slicelength):
+def _delitem_slice_helper(space, items, start, step, slicelength):
     if slicelength==0:
         return
 
     if step < 0:
         start = start + step * (slicelength-1)
         step = -step
-        
+
     if step == 1:
         assert start >= 0
         assert slicelength >= 0
-        del w_list.wrappeditems[start:start+slicelength]
+        del items[start:start+slicelength]
     else:
-        items = w_list.wrappeditems
         n = len(items)
         i = start
 
@@ -262,7 +261,7 @@
     start, stop, step, slicelength = w_slice.indices4(space, oldsize)
     _setitem_slice_helper(space, w_list, start, step, slicelength, w_iterable)
 
-def _setitem_slice_helper(space, w_list, start, step, slicelength, w_iterable):
+def _setitem_slice_helper(space, items, start, step, slicelength, w_iterable):
     sequence2 = space.listview(w_iterable)
     assert slicelength >= 0
     items = w_list.wrappeditems
@@ -322,7 +321,7 @@
                 del currently_in_repr[list_id]
             except:
                 pass
-""", filename=__file__) 
+""", filename=__file__)
 
 listrepr = app.interphook("listrepr")
 
@@ -466,15 +465,15 @@
     has_reverse = space.is_true(w_reverse)
 
     # create and setup a TimSort instance
-    if has_cmp: 
-        if has_key: 
+    if has_cmp:
+        if has_key:
             sorterclass = CustomKeyCompareSort
-        else: 
+        else:
             sorterclass = CustomCompareSort
-    else: 
-        if has_key: 
+    else:
+        if has_key:
             sorterclass = CustomKeySort
-        else: 
+        else:
             sorterclass = SimpleSort
     items = w_list.wrappeditems
     sorter = sorterclass(items, len(items))

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
@@ -5,13 +5,18 @@
 from pypy.objspace.std.multimethod import FailedToImplement
 from pypy.rlib.rarithmetic import intmask
 from pypy.rlib.rstring import StringBuilder
-from pypy.objspace.std.intobject import W_IntObject
+from pypy.objspace.std.intobject import W_IntObject
+from pypy.objspace.std.listobject import _delitem_slice_helper
+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
 from pypy.objspace.std.sliceobject import W_SliceObject, normalize_simple_slice
 from pypy.objspace.std import slicetype
 from pypy.interpreter import gateway
 from pypy.interpreter.buffer import RWBuffer
+
+from pypy.tool.sourcetools import func_with_new_name
+
 
 class W_BytearrayObject(W_Object):
     from pypy.objspace.std.bytearraytype import bytearray_typedef as typedef
@@ -410,6 +415,24 @@
                              space.wrap("fixme: only step=1 for the moment"))
     _setitem_helper(w_bytearray, start, stop, slicelength,
                     space.str_w(w_other))
+
+def delitem__Bytearray_ANY(space, w_bytearray, w_idx):
+    idx = get_list_index(space, w_idx)
+    try:
+        del w_bytearray.data[idx]
+    except IndexError:
+        raise OperationError(space.w_IndexError,
+                             space.wrap("bytearray deletion index out of range"))
+    return space.w_None
+
+def delitem__Bytearray_Slice(space, w_bytearray, w_slice):
+    start, stop, step, slicelength = w_slice.indices4(space,
+                                                      len(w_bytearray.data))
+    delitem_slice_helper(space, w_bytearray.data, start, step, slicelength)
+
+# create new helper function with different list type specialisation
+delitem_slice_helper = func_with_new_name(_delitem_slice_helper,
+                                          'delitem_slice_helper')
 
 def _setitem_helper(w_bytearray, start, stop, slicelength, data):
     assert start >= 0

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
@@ -166,6 +166,19 @@
         b.append(ord('e'))
         assert b == 'abcde'
 
+    def test_delitem(self):
+        b = bytearray('abc')
+        del b[1]
+        assert b == bytearray('ac')
+        del b[1:1]
+        assert b == bytearray('ac')
+        del b[:]
+        assert b == bytearray()
+
+        b = bytearray('fooble')
+        del b[::2]
+        assert b == bytearray('obe')
+
     def test_iadd(self):
         b = bytearray('abc')
         b += 'def'


More information about the Pypy-commit mailing list