Author: Lukas Diekmann <[email protected]>
Branch: list-strategies
Changeset: r47421:b55d9ead88af
Date: 2011-02-16 12:44 +0100
http://bitbucket.org/pypy/pypy/changeset/b55d9ead88af/

Log:    Implemented setitem

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
@@ -93,6 +93,9 @@
     def deleteitem(self, index):
         self.strategy.deleteitem(self, index)
 
+    def setitem(self, index, w_item):
+        self.strategy.setitem(self, index, w_item)
+
 registerimplementation(W_ListObject)
 
 
@@ -121,6 +124,9 @@
     def deleteitem(self, w_list, index):
         raise NotImplementedError
 
+    def setitem(self, w_list, index, w_item):
+        raise NotImplementedError
+
 class EmptyListStrategy(ListStrategy):
     def init_from_list_w(self, w_list, list_w):
         assert len(list_w) == 0
@@ -157,6 +163,9 @@
     def deleteitem(self, w_list, index):
         raise IndexError
 
+    def setitem(self, w_list, index, w_item):
+        raise IndexError
+
 class ObjectListStrategy(ListStrategy):
     def init_from_list_w(self, w_list, list_w):
         w_list.storage = cast_to_void_star(list_w, "object")
@@ -191,6 +200,10 @@
         list_w = cast_from_void_star(w_list.storage, "object")
         del list_w[index]
 
+    def setitem(self, w_list, index, w_item):
+        list_w = cast_from_void_star(w_list.storage, "object")
+        list_w[index] = w_item
+
 class IntegerListStrategy(ListStrategy):
 
     def init_from_list_w(self, w_list, list_w):
@@ -234,6 +247,10 @@
         list_w = cast_from_void_star(w_list.storage, "integer")
         del list_w[index]
 
+    def setitem(self, w_list, index, w_item):
+        list_w = cast_from_void_star(w_list.storage, "integer")
+        list_w[index] = w_item
+
 class StringListStrategy(ListStrategy):
 
     def init_from_list_w(self, w_list, list_w):
@@ -277,6 +294,10 @@
         list_w = cast_from_void_star(w_list.storage, "string")
         del list_w[index]
 
+    def setitem(self, w_list, index, w_item):
+        list_w = cast_from_void_star(w_list.storage, "string")
+        list_w[index] = w_item
+
 # _______________________________________________________
 
 init_signature = Signature(['sequence'], None, None)
@@ -490,7 +511,8 @@
 def setitem__List_ANY_ANY(space, w_list, w_index, w_any):
     idx = get_list_index(space, w_index)
     try:
-        w_list.wrappeditems[idx] = w_any
+        #w_list.wrappeditems[idx] = w_any
+        w_list.setitem(idx, w_any)
     except IndexError:
         raise OperationError(space.w_IndexError,
                              space.wrap("list index out of range"))
diff --git a/pypy/objspace/std/test/test_liststrategies.py 
b/pypy/objspace/std/test/test_liststrategies.py
--- a/pypy/objspace/std/test/test_liststrategies.py
+++ b/pypy/objspace/std/test/test_liststrategies.py
@@ -41,5 +41,17 @@
         l.append(self.space.wrap(3))
         assert isinstance(l.strategy, ObjectListStrategy)
 
+    def test_setitem(self):
+        # This should work if test_listobject.py passes
+        l = 
W_ListObject([self.space.wrap('a'),self.space.wrap('b'),self.space.wrap('c')])
+        assert self.space.eq_w(l.getitem(0), self.space.wrap('a'))
+        l.setitem(0, self.space.wrap('d'))
+        assert self.space.eq_w(l.getitem(0), self.space.wrap('d'))
 
+        # Test strategy change
+        l = 
W_ListObject([self.space.wrap(1),self.space.wrap(2),self.space.wrap(3)])
+        assert isinstance(l.strategy, IntegerListStrategy)
+        l.setitem(0, self.space.wrap('d'))
+        assert isinstance(l.strategy, ObjectListStrategy)
 
+
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to