From 44d5214927e428e6837310fb0cb694b42f6d8bb6 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Mon, 4 Mar 2013 20:30:01 +0100 Subject: Issue #17278: Fix a crash in heapq.heappush() and heapq.heappop() when the list is being resized concurrently. --- Lib/test/test_heapq.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'Lib/test/test_heapq.py') diff --git a/Lib/test/test_heapq.py b/Lib/test/test_heapq.py index e0c49c110f..20fb89c000 100644 --- a/Lib/test/test_heapq.py +++ b/Lib/test/test_heapq.py @@ -319,6 +319,16 @@ def L(seqn): return chain(map(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 @@ -370,6 +380,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 -- cgit v1.2.1