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

Skip to content

Commit e584457

Browse files
committed
Issue 13274: Make the pure python code for heapq more closely match the C implementation for an undefined corner case.
1 parent 9783b44 commit e584457

1 file changed

Lines changed: 4 additions & 0 deletions

File tree

Lib/heapq.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ def nlargest(n, iterable):
185185
186186
Equivalent to: sorted(iterable, reverse=True)[:n]
187187
"""
188+
if n < 0:
189+
return []
188190
it = iter(iterable)
189191
result = list(islice(it, n))
190192
if not result:
@@ -201,6 +203,8 @@ def nsmallest(n, iterable):
201203
202204
Equivalent to: sorted(iterable)[:n]
203205
"""
206+
if n < 0:
207+
return []
204208
if hasattr(iterable, '__len__') and n * 10 <= len(iterable):
205209
# For smaller values of n, the bisect method is faster than a minheap.
206210
# It is also memory efficient, consuming only n elements of space.

0 commit comments

Comments
 (0)