From: Raymond Hettinger Date: Sun, 30 Oct 2011 21:32:54 +0000 (-0700) Subject: Issue 13274: Make the pure python code for heapq more closely match the C implementa... X-Git-Tag: v3.2.3rc1~452 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=e584457e24606ea1498b66dd94a0ecc9bf4c5dc4;p=thirdparty%2FPython%2Fcpython.git Issue 13274: Make the pure python code for heapq more closely match the C implementation for an undefined corner case. --- diff --git a/Lib/heapq.py b/Lib/heapq.py index f7560358105b..dec15aec9ce3 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.