summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2011-10-30 21:33:31 (GMT)
committerRaymond Hettinger <python@rcn.com>2011-10-30 21:33:31 (GMT)
commit385c803658e7603f0b416de0d06a9fc967e58051 (patch)
treef94537af8b6f0bee0e4276033afb8391304fbb43
parent75861df9ab2b5492406ce866f5a24989a00a24b9 (diff)
parente584457e24606ea1498b66dd94a0ecc9bf4c5dc4 (diff)
downloadcpython-385c803658e7603f0b416de0d06a9fc967e58051.zip
cpython-385c803658e7603f0b416de0d06a9fc967e58051.tar.gz
cpython-385c803658e7603f0b416de0d06a9fc967e58051.tar.bz2
Merge
-rw-r--r--Lib/heapq.py4
1 files changed, 4 insertions, 0 deletions
diff --git a/Lib/heapq.py b/Lib/heapq.py
index f756035..dec15ae 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.