[pypy-commit] pypy list-strategies: Implemented insert
l.diekmann
noreply at buildbot.pypy.org
Fri Sep 23 13:11:24 CEST 2011
Author: Lukas Diekmann <lukas.diekmann at uni-duesseldorf.de>
Branch: list-strategies
Changeset: r47424:5fca9acec5b7
Date: 2011-02-16 13:23 +0100
http://bitbucket.org/pypy/pypy/changeset/5fca9acec5b7/
Log: Implemented insert
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
@@ -96,6 +96,9 @@
def setitem(self, index, w_item):
self.strategy.setitem(self, index, w_item)
+ def insert(self, index, w_item):
+ self.strategy.insert(self, index, w_item)
+
registerimplementation(W_ListObject)
@@ -127,6 +130,9 @@
def setitem(self, w_list, index, w_item):
raise NotImplementedError
+ def insert(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
@@ -166,6 +172,9 @@
def setitem(self, w_list, index, w_item):
raise IndexError
+ def insert(self, w_list, index, w_item):
+ self.append(w_list, w_item)
+
class ObjectListStrategy(ListStrategy):
def init_from_list_w(self, w_list, list_w):
w_list.storage = cast_to_void_star(list_w, "object")
@@ -204,6 +213,11 @@
list_w = cast_from_void_star(w_list.storage, "object")
list_w[index] = w_item
+ def insert(self, w_list, index, w_item):
+ list_w = cast_from_void_star(w_list.storage, "object")
+ list_w.insert(index, w_item)
+
+
class IntegerListStrategy(ListStrategy):
def init_from_list_w(self, w_list, list_w):
@@ -255,6 +269,15 @@
w_list.strategy = ObjectListStrategy()
w_list.strategy.init_from_list_w(w_list, list_w)
+ def insert(self, w_list, index, w_item):
+ list_w = cast_from_void_star(w_list.storage, "integer")
+ list_w.insert(index, w_item)
+
+ if not is_W_IntObject(w_item):
+ w_list.strategy = ObjectListStrategy()
+ w_list.strategy.init_from_list_w(w_list, list_w)
+
+
class StringListStrategy(ListStrategy):
def init_from_list_w(self, w_list, list_w):
@@ -306,6 +329,14 @@
w_list.strategy = ObjectListStrategy()
w_list.strategy.init_from_list_w(w_list, list_w)
+ def insert(self, w_list, index, w_item):
+ list_w = cast_from_void_star(w_list.storage, "string")
+ list_w.insert(index, w_item)
+
+ if not is_W_StringObject(w_item):
+ w_list.strategy = ObjectListStrategy()
+ w_list.strategy.init_from_list_w(w_list, list_w)
+
# _______________________________________________________
init_signature = Signature(['sequence'], None, None)
@@ -605,14 +636,14 @@
def list_insert__List_ANY_ANY(space, w_list, w_where, w_any):
where = space.int_w(w_where)
- length = len(w_list.wrappeditems)
+ length = w_list.length()
if where < 0:
where += length
if where < 0:
where = 0
elif where > length:
where = length
- w_list.wrappeditems.insert(where, w_any)
+ w_list.insert(where, w_any)
return space.w_None
def list_append__List_ANY(space, w_list, w_any):
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
@@ -62,3 +62,35 @@
l.setitem(0, self.space.wrap(2))
assert isinstance(l.strategy, ObjectListStrategy)
+ def test_insert(self):
+ # no change
+ l = W_ListObject([self.space.wrap(1),self.space.wrap(2),self.space.wrap(3)])
+ assert isinstance(l.strategy, IntegerListStrategy)
+ l.insert(3, self.space.wrap(4))
+ assert isinstance(l.strategy, IntegerListStrategy)
+
+ # StringStrategy
+ l = W_ListObject([self.space.wrap('a'),self.space.wrap('b'),self.space.wrap('c')])
+ assert isinstance(l.strategy, StringListStrategy)
+ l.insert(3, self.space.wrap(2))
+ assert isinstance(l.strategy, ObjectListStrategy)
+
+ # IntegerStrategy
+ l = W_ListObject([self.space.wrap(1),self.space.wrap(2),self.space.wrap(3)])
+ assert isinstance(l.strategy, IntegerListStrategy)
+ l.insert(3, self.space.wrap('d'))
+ assert isinstance(l.strategy, ObjectListStrategy)
+
+ # EmptyStrategy
+ l = W_ListObject([])
+ assert isinstance(l.strategy, EmptyListStrategy)
+ l.insert(0, self.space.wrap('a'))
+ assert isinstance(l.strategy, StringListStrategy)
+
+ l = W_ListObject([])
+ assert isinstance(l.strategy, EmptyListStrategy)
+ l.insert(0, self.space.wrap(2))
+ assert isinstance(l.strategy, IntegerListStrategy)
+
+
+
More information about the pypy-commit
mailing list