changeset: 90558:a3c345ba3563 user: Raymond Hettinger date: Sat May 03 21:58:45 2014 -0700 files: Lib/collections/__init__.py Lib/test/test_collections.py Misc/NEWS description: Issue #19414: Have the OrderedDict mark deleted links as unusable. This gives an earlier and more visible failure if a link is deleted during iteration. diff -r 7f6d9990a9b1 -r a3c345ba3563 Lib/collections/__init__.py --- a/Lib/collections/__init__.py Sat May 03 19:25:34 2014 -0700 +++ b/Lib/collections/__init__.py Sat May 03 21:58:45 2014 -0700 @@ -96,6 +96,8 @@ 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)' diff -r 7f6d9990a9b1 -r a3c345ba3563 Lib/test/test_collections.py --- a/Lib/test/test_collections.py Sat May 03 19:25:34 2014 -0700 +++ b/Lib/test/test_collections.py Sat May 03 21:58:45 2014 -0700 @@ -1193,6 +1193,16 @@ [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) diff -r 7f6d9990a9b1 -r a3c345ba3563 Misc/NEWS --- a/Misc/NEWS Sat May 03 19:25:34 2014 -0700 +++ b/Misc/NEWS Sat May 03 21:58:45 2014 -0700 @@ -73,6 +73,9 @@ 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.