]> granicus.if.org Git - python/commitdiff
Issue #19018: The heapq.merge() function no longer suppresses IndexError
authorRaymond Hettinger <python@rcn.com>
Sun, 15 Sep 2013 05:17:39 +0000 (22:17 -0700)
committerRaymond Hettinger <python@rcn.com>
Sun, 15 Sep 2013 05:17:39 +0000 (22:17 -0700)
Lib/heapq.py
Lib/test/test_heapq.py
Misc/ACKS
Misc/NEWS

index ca79db15295f49a62eda157937168a5c833dbcf5..19037ab1290efa3f415b88fa496802356eb69104 100644 (file)
@@ -366,6 +366,7 @@ def merge(*iterables):
 
     '''
     _heappop, _heapreplace, _StopIteration = heappop, heapreplace, StopIteration
+    _len = len
 
     h = []
     h_append = h.append
@@ -377,17 +378,21 @@ def merge(*iterables):
             pass
     heapify(h)
 
-    while 1:
+    while _len(h) > 1:
         try:
-            while 1:
-                v, itnum, next = s = h[0]   # raises IndexError when h is empty
+            while True:
+                v, itnum, next = s = h[0]
                 yield v
                 s[0] = next()               # raises StopIteration when exhausted
                 _heapreplace(h, s)          # restore heap condition
         except _StopIteration:
             _heappop(h)                     # remove empty iterator
-        except IndexError:
-            return
+    if h:
+        # fast case when only a single iterator remains
+        v, itnum, next = h[0]
+        yield v
+        for v in next.__self__:
+            yield v
 
 # Extend the implementations of nsmallest and nlargest to use a key= argument
 _nsmallest = nsmallest
index 73b88f09b9bd4a3dda732258a942adc15abb8db1..c4de593bb820a847a5f55703741c15df03324c0e 100644 (file)
@@ -158,6 +158,15 @@ class TestHeap(TestCase):
         self.assertEqual(sorted(chain(*inputs)), list(self.module.merge(*inputs)))
         self.assertEqual(list(self.module.merge()), [])
 
+    def test_merge_does_not_suppress_index_error(self):
+        # Issue 19018: Heapq.merge suppresses IndexError from user generator
+        def iterable():
+            s = list(range(10))
+            for i in range(20):
+                yield s[i]       # IndexError when i > 10
+        with self.assertRaises(IndexError):
+            list(self.module.merge(iterable(), iterable()))
+
     def test_merge_stability(self):
         class Int(int):
             pass
index 6f2b3a76fd9792fcfce3bbf648f91e4508e0a043..6048ed8dd71c5be598b07ec3b4ce6712d88617f6 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -110,6 +110,7 @@ Paul Boddie
 Matthew Boedicker
 Robin Boerdijk
 David Bolen
+Wouter Bolsterlee
 Gawain Bolton
 Gregory Bond
 Jurjen Bos
@@ -313,6 +314,7 @@ Nils Fischbeck
 Frederik Fix
 Matt Fleming
 Hernán Martínez Foffani
+Artem Fokin
 Arnaud Fontaine
 Michael Foord
 Amaury Forgeot d'Arc
index e025df630d6f3582c66fe0d93a05641f2f23ec09..c4698f4d7ffb7d12632d9074f76d6e538a8b7dcc 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -35,6 +35,9 @@ Library
 - Issue #17324: Fix http.server's request handling case on trailing '/'. Patch
   contributed by Vajrasky Kok.
 
+- Issue #19018: The heapq.merge() function no longer suppresses IndexError
+  in the underlying iterables.
+
 - Issue #18784: The uuid module no more attempts to load libc via ctypes.CDLL,
   if all necessary functions are already found in libuuid.
   Patch by Evgeny Sologubov.