summaryrefslogtreecommitdiff
path: root/Lib/heapq.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2011-10-30 14:32:54 -0700
committerRaymond Hettinger <python@rcn.com>2011-10-30 14:32:54 -0700
commite584457e24606ea1498b66dd94a0ecc9bf4c5dc4 (patch)
treeb0cecb42803ad49d624f872d6e70cf3d0e9e8aaf /Lib/heapq.py
parent9783b44bcf0f539b56165d823d2b6eede080c1d8 (diff)
downloadcpython-git-e584457e24606ea1498b66dd94a0ecc9bf4c5dc4.tar.gz
Issue 13274: Make the pure python code for heapq more closely match the C implementation for an undefined corner case.
Diffstat (limited to 'Lib/heapq.py')
-rw-r--r--Lib/heapq.py4
1 files changed, 4 insertions, 0 deletions
diff --git a/Lib/heapq.py b/Lib/heapq.py
index f756035810..dec15aec9c 100644
--- a/Lib/heapq.py
+++ b/Lib/heapq.py
@@ -185,6 +185,8 @@ def nlargest(n, iterable):
Equivalent to: sorted(iterable, reverse=True)[:n]
"""
+ if n < 0:
+ return []
it = iter(iterable)
result = list(islice(it, n))
if not result:
@@ -201,6 +203,8 @@ def nsmallest(n, iterable):
Equivalent to: sorted(iterable)[:n]
"""
+ if n < 0:
+ return []
if hasattr(iterable, '__len__') and n * 10 <= len(iterable):
# For smaller values of n, the bisect method is faster than a minheap.
# It is also memory efficient, consuming only n elements of space.