]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #19414: Have the OrderedDict mark deleted links as unusable.
authorRaymond Hettinger <python@rcn.com>
Sun, 4 May 2014 04:58:45 +0000 (21:58 -0700)
committerRaymond Hettinger <python@rcn.com>
Sun, 4 May 2014 04:58:45 +0000 (21:58 -0700)
This gives an earlier and more visible failure if a link is deleted
during iteration.

Lib/collections/__init__.py
Lib/test/test_collections.py
Misc/NEWS

index 3605cc3bc331a9786691522f382f2d6c74f6894e..a55fee1ff4aefb229826f386e3e06aa8d067944b 100644 (file)
@@ -96,6 +96,8 @@ class OrderedDict(dict):
         link_next = link.next
         link_prev.next = link_next
         link_next.prev = link_prev
+        link.prev = None
+        link.next = None
 
     def __iter__(self):
         'od.__iter__() <==> iter(od)'
index d352d2a42417d85a3ed99d03f779bef8c78e8f70..7e149808d44b8a5006471b04cee5d8b5102eb23c 100644 (file)
@@ -1193,6 +1193,16 @@ class TestOrderedDict(unittest.TestCase):
                          [t[1] for t in reversed(pairs)])
         self.assertEqual(list(reversed(od.items())), list(reversed(pairs)))
 
+    def test_detect_deletion_during_iteration(self):
+        od = OrderedDict.fromkeys('abc')
+        it = iter(od)
+        key = next(it)
+        del od[key]
+        with self.assertRaises(Exception):
+            # Note, the exact exception raised is not guaranteed
+            # The only guarantee that the next() will not succeed
+            next(it)
+
     def test_popitem(self):
         pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
         shuffle(pairs)
index c61821223b2375f24008c807950074a0dda02da5..9eaab87bd99797826c1cc735e3a0702ee31202c4 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -73,6 +73,9 @@ Library
   Decimal.quantize() method in the Python version.  It had never been
   present in the C version.
 
+- Issue #19414: Have the OrderedDict mark deleted links as unusable.
+  This gives an early failure if the link is deleted during iteration.
+
 - Issue #21421: Add __slots__ to the MappingViews ABC.
   Patch by Josh Rosenberg.