summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2011-10-30 21:29:06 (GMT)
committerRaymond Hettinger <python@rcn.com>2011-10-30 21:29:06 (GMT)
commite11a47e2070c9c271da5b26a77c8ab50437e58b4 (patch)
treec188a9c1663efb3b4ac75b86ca224c34732a8b0c
parent6361ea2b07f126a4f4ee44196e0b1368324d6165 (diff)
downloadcpython-e11a47e2070c9c271da5b26a77c8ab50437e58b4.zip
cpython-e11a47e2070c9c271da5b26a77c8ab50437e58b4.tar.gz
cpython-e11a47e2070c9c271da5b26a77c8ab50437e58b4.tar.bz2
Issue 13274: Make the pure python code for heapq more closely match the C implementation for an undefined corner case.
-rw-r--r--Lib/heapq.py4
1 files changed, 4 insertions, 0 deletions
diff --git a/Lib/heapq.py b/Lib/heapq.py
index fcaca13..6a4e0f4 100644
--- a/Lib/heapq.py
+++ b/Lib/heapq.py
@@ -193,6 +193,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:
@@ -209,6 +211,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.