[pypy-svn] pypy collections-module: remove().

arigo commits-noreply at bitbucket.org
Tue Feb 15 17:26:09 CET 2011


Author: Armin Rigo <arigo at tunes.org>
Branch: collections-module
Changeset: r41978:7ae2fd1e1675
Date: 2011-02-15 17:25 +0100
http://bitbucket.org/pypy/pypy/changeset/7ae2fd1e1675/

Log:	remove().

diff --git a/pypy/module/_collections/test/test_deque.py b/pypy/module/_collections/test/test_deque.py
--- a/pypy/module/_collections/test/test_deque.py
+++ b/pypy/module/_collections/test/test_deque.py
@@ -200,6 +200,7 @@
         d.pop()
         assert len(d) == 0
         raises(IndexError, d.pop)
+        raises(IndexError, d.popleft)
         assert len(d) == 0
         d.append('c')
         assert len(d) == 1
@@ -207,44 +208,17 @@
         assert len(d) == 2
         d.clear()
         assert len(d) == 0
-
-    def test_underflow(self):
-        d = deque()
-        self.assertRaises(IndexError, d.pop)
-        self.assertRaises(IndexError, d.popleft)
-
-    def test_clear(self):
-        d = deque(xrange(100))
-        self.assertEqual(len(d), 100)
-        d.clear()
-        self.assertEqual(len(d), 0)
-        self.assertEqual(list(d), [])
-        d.clear()               # clear an emtpy deque
-        self.assertEqual(list(d), [])
+        assert list(d) == []
 
     def test_remove(self):
+        from _collections import deque
         d = deque('abcdefghcij')
         d.remove('c')
-        self.assertEqual(d, deque('abdefghcij'))
+        assert d == deque('abdefghcij')
         d.remove('c')
-        self.assertEqual(d, deque('abdefghij'))
-        self.assertRaises(ValueError, d.remove, 'c')
-        self.assertEqual(d, deque('abdefghij'))
-
-        # Handle comparison errors
-        d = deque(['a', 'b', BadCmp(), 'c'])
-        e = deque(d)
-        self.assertRaises(RuntimeError, d.remove, 'c')
-        for x, y in zip(d, e):
-            # verify that original order and values are retained.
-            self.assertTrue(x is y)
-
-        # Handle evil mutator
-        for match in (True, False):
-            d = deque(['ab'])
-            d.extend([MutateCmp(d, match), 'c'])
-            self.assertRaises(IndexError, d.remove, 'c')
-            self.assertEqual(d, deque())
+        assert d == deque('abdefghij')
+        raises(ValueError, d.remove, 'c')
+        assert d == deque('abdefghij')
 
     def test_repr(self):
         d = deque(xrange(200))

diff --git a/pypy/module/_collections/interp_deque.py b/pypy/module/_collections/interp_deque.py
--- a/pypy/module/_collections/interp_deque.py
+++ b/pypy/module/_collections/interp_deque.py
@@ -246,6 +246,27 @@
         self.modified()
         return w_obj
 
+    @unwrap_spec('self', W_Root)
+    def remove(self, w_x):
+        space = self.space
+        block = self.leftblock
+        index = self.leftindex
+        lock = self.getlock()
+        for i in range(self.len):
+            w_item = block.data[index]
+            equal = space.eq_w(w_item, w_x)
+            self.checklock(lock)
+            if equal:
+                self.del_item(i)
+                return
+            # Advance the block/index pair
+            index += 1
+            if index >= BLOCKLEN:
+                block = block.rightlink
+                index = 0
+        raise OperationError(space.w_ValueError,
+                             space.wrap("deque.remove(x): x not in deque"))
+
     @unwrap_spec('self')
     def reverse(self):
         li = self.leftindex
@@ -341,6 +362,13 @@
         assert i >= 0
         return b, i
 
+    def del_item(self, i):
+        # delitem() implemented in terms of rotate for simplicity and
+        # reasonable performance near the end points.
+        self.rotate(-i)
+        self.popleft()
+        self.rotate(i)
+
     @unwrap_spec('self', W_Root)
     def getitem(self, w_index):
         space = self.space
@@ -368,12 +396,7 @@
         space = self.space
         start, stop, step = space.decode_index(w_index, self.len)
         if step == 0:  # index only
-            # delitem() implemented in terms of rotate for simplicity and
-            # reasonable performance near the end points.
-            i = start
-            self.rotate(-i)
-            self.popleft()
-            self.rotate(i)
+            self.del_item(start)
         else:
             raise OperationError(self.w_TypeError,
                                  self.wrap("deque[:] is not supported"))
@@ -427,6 +450,7 @@
     extendleft = interp2app(W_Deque.extendleft),
     pop        = interp2app(W_Deque.pop),
     popleft    = interp2app(W_Deque.popleft),
+    remove     = interp2app(W_Deque.remove),
     reverse    = interp2app(W_Deque.reverse),
     rotate     = interp2app(W_Deque.rotate),
     __weakref__ = make_weakref_descr(W_Deque),


More information about the Pypy-commit mailing list