]> granicus.if.org Git - python/commitdiff
Issue #17278: Fix a crash in heapq.heappush() and heapq.heappop() when the list is...
authorAntoine Pitrou <solipsis@pitrou.net>
Mon, 4 Mar 2013 19:33:36 +0000 (20:33 +0100)
committerAntoine Pitrou <solipsis@pitrou.net>
Mon, 4 Mar 2013 19:33:36 +0000 (20:33 +0100)
1  2 
Lib/test/test_heapq.py
Misc/NEWS
Modules/_heapqmodule.c

index 54395c07405765df5439442e5e76011270ebc030,20fb89c000848c1e4acd4c5c3d0d26654ccb36d8..b48ca68461438a770816bedf020fd268a6e8a0fc
@@@ -319,7 -319,18 +319,17 @@@ def L(seqn)
      return chain(map(lambda x:x, R(Ig(G(seqn)))))
  
  
 -class TestErrorHandling(TestCase):
 -    module = None
+ 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:
  
      def test_non_sequence(self):
          for f in (self.module.heapify, self.module.heappop):
                  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):
 +class TestErrorHandlingPython(TestErrorHandling, TestCase):
      module = py_heapq
  
  @skipUnless(c_heapq, 'requires _heapq')
diff --cc Misc/NEWS
index 958cc1a7d45ea65366917ca8b7a1f09258e5057b,e9c78b3b31a8fe52b3c07f513e1fe8482f01cc23..5bc17841cda2253c9d3c58b4e1dac23f71ed8565
+++ b/Misc/NEWS
@@@ -193,9 -233,9 +193,12 @@@ Core and Builtin
  Library
  -------
  
+ - Issue #17278: Fix a crash in heapq.heappush() and heapq.heappop() when
+   the list is being resized concurrently.
 +- Issue #16962: Use getdents64 instead of the obsolete getdents syscall
 +  in the subprocess module on Linux.
 +
  - Issue #17018: Make Process.join() retry if os.waitpid() fails with EINTR.
  
  - Issue #14720: sqlite3: Convert datetime microseconds correctly.
Simple merge