[pypy-svn] r58815 - in pypy/branch/2.5-merge/pypy/lib: . app_test

iko at codespeak.net iko at codespeak.net
Wed Oct 8 14:34:28 CEST 2008


Author: iko
Date: Wed Oct  8 14:34:27 2008
New Revision: 58815

Added:
   pypy/branch/2.5-merge/pypy/lib/app_test/test_collections.py   (contents, props changed)
Modified:
   pypy/branch/2.5-merge/pypy/lib/collections.py
Log:
(iko, cfbolz)
fix deque remove failures



Added: pypy/branch/2.5-merge/pypy/lib/app_test/test_collections.py
==============================================================================
--- (empty file)
+++ pypy/branch/2.5-merge/pypy/lib/app_test/test_collections.py	Wed Oct  8 14:34:27 2008
@@ -0,0 +1,16 @@
+import py
+import collections
+
+def test_deque_remove_empty():
+    d = collections.deque([])
+    py.test.raises(ValueError, d.remove, 1)
+
+def test_deque_remove_mutating():
+    class MutatingCmp(object):
+        def __eq__(self, other):
+            d.clear()
+            return True
+
+    d = collections.deque([MutatingCmp()])
+    py.test.raises(IndexError, d.remove, 1)
+    

Modified: pypy/branch/2.5-merge/pypy/lib/collections.py
==============================================================================
--- pypy/branch/2.5-merge/pypy/lib/collections.py	(original)
+++ pypy/branch/2.5-merge/pypy/lib/collections.py	Wed Oct  8 14:34:27 2008
@@ -112,7 +112,17 @@
         return x
 
     def remove(self, value):
-        del self[operator.indexOf(self, value)]
+        # Need to be defensive for mutating comparisons
+        i = 0
+        while i < len(self):
+            if self[i] == value:
+                if i < len(self):
+                    del self[i]
+                    return
+                else:
+                    raise IndexError("deque mutated during remove()")
+            i += 1
+        raise ValueError("deque.remove(x): x not in deque")
 
     def rotate(self, n=1):
         length = len(self)



More information about the Pypy-commit mailing list