[pypy-commit] pypy list-strategies: Implemented pop
l.diekmann
noreply at buildbot.pypy.org
Fri Sep 23 13:11:39 CEST 2011
Author: Lukas Diekmann <lukas.diekmann at uni-duesseldorf.de>
Branch: list-strategies
Changeset: r47437:91f2d172c7d6
Date: 2011-02-25 11:47 +0100
http://bitbucket.org/pypy/pypy/changeset/91f2d172c7d6/
Log: Implemented pop
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 deleteslice(self, start, step, length):
self.strategy.deleteslice(self, start, step, length)
+ def pop(self, index):
+ return self.strategy.pop(self, index)
+
def setitem(self, index, w_item):
self.strategy.setitem(self, index, w_item)
@@ -139,6 +142,9 @@
def deleteslice(self, w_list, start, step, slicelength):
raise NotImplementedError
+ def pop(self, w_list, index):
+ raise NotImplementedError
+
def setitem(self, w_list, index, w_item):
raise NotImplementedError
@@ -190,6 +196,9 @@
def deleteslice(self, w_list, start, step, slicelength):
raise IndexError
+ def pop(self, w_list, index):
+ raise IndexError
+
def setitem(self, w_list, index, w_item):
raise IndexError
@@ -382,6 +391,16 @@
w_list.strategy = EmptyListStrategy()
w_list.strategy.init_from_list_w(w_list, items)
+ def pop(self, w_list, index):
+ list_w = self.cast_from_void_star(w_list.storage)
+ item_w = self.wrap(list_w.pop(index))
+
+ if len(list_w) == 0:
+ w_list.strategy = EmptyListStrategy()
+ w_list.strategy.init_from_list_w(w_list, list_w)
+
+ return item_w
+
def inplace_mul(self, w_list, times):
list_w = self.cast_from_void_star(w_list.storage)
list_w *= times
@@ -675,13 +694,12 @@
# note that the default value will come back wrapped!!!
def list_pop__List_ANY(space, w_list, w_idx=-1):
- items = w_list.wrappeditems
- if len(items)== 0:
+ if w_list.length() == 0:
raise OperationError(space.w_IndexError,
space.wrap("pop from empty list"))
idx = space.int_w(w_idx)
try:
- return items.pop(idx)
+ return w_list.pop(idx)
except IndexError:
raise OperationError(space.w_IndexError,
space.wrap("pop 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
@@ -92,7 +92,7 @@
l.insert(0, self.space.wrap(2))
assert isinstance(l.strategy, IntegerListStrategy)
- def test_delete(self):
+ def test_list_empty_after_delete(self):
l = W_ListObject([self.space.wrap(3)])
assert isinstance(l.strategy, IntegerListStrategy)
l.deleteitem(0)
@@ -103,6 +103,11 @@
l.deleteslice(0, 1, 2)
assert isinstance(l.strategy, EmptyListStrategy)
+ l = W_ListObject([self.space.wrap(1)])
+ assert isinstance(l.strategy, IntegerListStrategy)
+ l.pop(-1)
+ assert isinstance(l.strategy, EmptyListStrategy)
+
def test_setslice(self):
l = W_ListObject([])
assert isinstance(l.strategy, EmptyListStrategy)
More information about the pypy-commit
mailing list