Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 46f5ca3

Browse files
committed
Issue #19018: The heapq.merge() function no longer suppresses IndexError
1 parent 0a32d92 commit 46f5ca3

4 files changed

Lines changed: 23 additions & 5 deletions

File tree

Lib/heapq.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ def merge(*iterables):
358358
359359
'''
360360
_heappop, _heapreplace, _StopIteration = heappop, heapreplace, StopIteration
361+
_len = len
361362

362363
h = []
363364
h_append = h.append
@@ -369,17 +370,20 @@ def merge(*iterables):
369370
pass
370371
heapify(h)
371372

372-
while 1:
373+
while _len(h) > 1:
373374
try:
374-
while 1:
375-
v, itnum, next = s = h[0] # raises IndexError when h is empty
375+
while True:
376+
v, itnum, next = s = h[0]
376377
yield v
377378
s[0] = next() # raises StopIteration when exhausted
378379
_heapreplace(h, s) # restore heap condition
379380
except _StopIteration:
380381
_heappop(h) # remove empty iterator
381-
except IndexError:
382-
return
382+
if h:
383+
# fast case when only a single iterator remains
384+
v, itnum, next = h[0]
385+
yield v
386+
yield from next.__self__
383387

384388
# Extend the implementations of nsmallest and nlargest to use a key= argument
385389
_nsmallest = nsmallest

Lib/test/test_heapq.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,15 @@ def test_merge(self):
158158
self.assertEqual(sorted(chain(*inputs)), list(self.module.merge(*inputs)))
159159
self.assertEqual(list(self.module.merge()), [])
160160

161+
def test_merge_does_not_suppress_index_error(self):
162+
# Issue 19018: Heapq.merge suppresses IndexError from user generator
163+
def iterable():
164+
s = list(range(10))
165+
for i in range(20):
166+
yield s[i] # IndexError when i > 10
167+
with self.assertRaises(IndexError):
168+
list(self.module.merge(iterable(), iterable()))
169+
161170
def test_merge_stability(self):
162171
class Int(int):
163172
pass

Misc/ACKS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ Paul Boddie
133133
Matthew Boedicker
134134
Robin Boerdijk
135135
David Bolen
136+
Wouter Bolsterlee
136137
Gawain Bolton
137138
Forest Bond
138139
Gregory Bond
@@ -382,6 +383,7 @@ Nils Fischbeck
382383
Frederik Fix
383384
Matt Fleming
384385
Hernán Martínez Foffani
386+
Artem Fokin
385387
Arnaud Fontaine
386388
Michael Foord
387389
Amaury Forgeot d'Arc

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ Library
7272
- Issue #17324: Fix http.server's request handling case on trailing '/'. Patch
7373
contributed by Vajrasky Kok.
7474

75+
- Issue #19018: The heapq.merge() function no longer suppresses IndexError
76+
in the underlying iterables.
77+
7578
- Issue #18784: The uuid module no more attempts to load libc via ctypes.CDLL,
7679
if all necessary functions are already found in libuuid.
7780
Patch by Evgeny Sologubov.

0 commit comments

Comments
 (0)