]> 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 03:51:57 +0000 (20:51 -0700)
committerRaymond Hettinger <python@rcn.com>
Sun, 15 Sep 2013 03:51:57 +0000 (20:51 -0700)
Lib/heapq.py
Lib/test/test_heapq.py
Misc/ACKS
Misc/NEWS

index 00b429c2d36ea41ac4a234556126a80555a8f038..d615239b94608a89ffd570724953347d7e0cd675 100644 (file)
@@ -358,6 +358,7 @@ def merge(*iterables):
 
     '''
     _heappop, _heapreplace, _StopIteration = heappop, heapreplace, StopIteration
+    _len = len
 
     h = []
     h_append = h.append
@@ -369,17 +370,20 @@ 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
+        yield from next.__self__
 
 # Extend the implementations of nsmallest and nlargest to use a key= argument
 _nsmallest = nsmallest
index b48ca68461438a770816bedf020fd268a6e8a0fc..b5a2fd803a808fa9c7e4a3f0f0e3679321d9fdd6 100644 (file)
@@ -158,6 +158,15 @@ class TestHeap:
         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 9781600b2c06d8a49fd4285e22386e7da0dd75d7..b5ebf0698a2938b880c4d65db52b91513df7a399 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -133,6 +133,7 @@ Paul Boddie
 Matthew Boedicker
 Robin Boerdijk
 David Bolen
+Wouter Bolsterlee
 Gawain Bolton
 Forest Bond
 Gregory Bond
@@ -382,6 +383,7 @@ Nils Fischbeck
 Frederik Fix
 Matt Fleming
 Hernán Martínez Foffani
+Artem Fokin
 Arnaud Fontaine
 Michael Foord
 Amaury Forgeot d'Arc
index 1655238181d6a31487a43e139c299beb0746240c..f74cdb19d1eb077a37a2bbccacf00f009285aa29 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -72,6 +72,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.