diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2013-03-04 19:30:01 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2013-03-04 19:30:01 (GMT) |
commit | 49e4dfeec8a54eb130200d881d4e72d808a36769 (patch) | |
tree | dfe78ed829ca68ff8e41927a929bbb80d0a2c44b /Lib | |
parent | a84ecc649b9680e306f05bfd38c48181c59b416c (diff) | |
download | cpython-49e4dfeec8a54eb130200d881d4e72d808a36769.zip cpython-49e4dfeec8a54eb130200d881d4e72d808a36769.tar.gz cpython-49e4dfeec8a54eb130200d881d4e72d808a36769.tar.bz2 |
Issue #17278: Fix a crash in heapq.heappush() and heapq.heappop() when the list is being resized concurrently.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_heapq.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/Lib/test/test_heapq.py b/Lib/test/test_heapq.py index 5932a40..73b88f0 100644 --- a/Lib/test/test_heapq.py +++ b/Lib/test/test_heapq.py @@ -315,6 +315,16 @@ def L(seqn): 'Test multiple tiers of iterators' return chain(imap(lambda x:x, R(Ig(G(seqn))))) +class SideEffectLT: + def __init__(self, value, heap): + self.value = value + self.heap = heap + + def __lt__(self, other): + self.heap[:] = [] + return self.value < other.value + + class TestErrorHandling(TestCase): module = None @@ -361,6 +371,22 @@ class TestErrorHandling(TestCase): self.assertRaises(TypeError, f, 2, N(s)) self.assertRaises(ZeroDivisionError, f, 2, E(s)) + # Issue #17278: the heap may change size while it's being walked. + + def test_heappush_mutating_heap(self): + heap = [] + heap.extend(SideEffectLT(i, heap) for i in range(200)) + # Python version raises IndexError, C version RuntimeError + with self.assertRaises((IndexError, RuntimeError)): + self.module.heappush(heap, SideEffectLT(5, heap)) + + def test_heappop_mutating_heap(self): + heap = [] + heap.extend(SideEffectLT(i, heap) for i in range(200)) + # Python version raises IndexError, C version RuntimeError + with self.assertRaises((IndexError, RuntimeError)): + self.module.heappop(heap) + class TestErrorHandlingPython(TestErrorHandling): module = py_heapq |