Author: Armin Rigo <ar...@tunes.org>
Branch: py3.5
Changeset: r88497:4ab8b2a8f971
Date: 2016-11-20 17:34 +0100
http://bitbucket.org/pypy/pypy/changeset/4ab8b2a8f971/

Log:    deque.__contains__()

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
@@ -284,8 +284,7 @@
         self.modified()
         return w_obj
 
-    def remove(self, w_x):
-        "Remove first occurrence of value."
+    def _find(self, w_x):
         space = self.space
         block = self.leftblock
         index = self.leftindex
@@ -295,14 +294,25 @@
             equal = space.eq_w(w_item, w_x)
             self.checklock(lock)
             if equal:
-                self.del_item(i)
-                return
+                return i
             # Advance the block/index pair
             index += 1
             if index >= BLOCKLEN:
                 block = block.rightlink
                 index = 0
-        raise oefmt(space.w_ValueError, "deque.remove(x): x not in deque")
+        return -1
+
+    def remove(self, w_x):
+        "Remove first occurrence of value."
+        i = self._find(w_x)
+        if i < 0:
+            raise oefmt(self.space.w_ValueError,
+                        "deque.remove(x): x not in deque")
+        self.del_item(i)
+
+    def contains(self, w_x):
+        i = self._find(w_x)
+        return self.space.newbool(i >= 0)
 
     def reverse(self):
         "Reverse *IN PLACE*."
@@ -582,6 +592,7 @@
     __imul__ = interp2app(W_Deque.imul),
     __rmul__ = interp2app(W_Deque.rmul),
     maxlen = GetSetProperty(W_Deque.get_maxlen),
+    __contains__ = interp2app(W_Deque.contains),
 )
 
 # ------------------------------------------------------------
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
@@ -364,6 +364,8 @@
             d.insert(i, 'a')
             assert 'a' in d
             assert 'b' not in d
+            assert d.__contains__('a')
+            assert not d.__contains__('b')
             assert d.index('a') == i
         d = deque(range(10))
         d.insert(-1, 500)
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to