]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue 13274: Make the pure python code for heapq more closely match the C implementa...
authorRaymond Hettinger <python@rcn.com>
Sun, 30 Oct 2011 21:29:06 +0000 (14:29 -0700)
committerRaymond Hettinger <python@rcn.com>
Sun, 30 Oct 2011 21:29:06 +0000 (14:29 -0700)
Lib/heapq.py

index fcaca1344222a0955bfd923ba483e69bc5ff3df7..6a4e0f4c31ce6064a21840ff53dcf5d4d8c34bd0 100644 (file)
@@ -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.