SF bug #730685: itertools.islice stop argument is not optional
* itertools.islice() stop argument did not perform as documented.
* beefed-up test suite
diff --git a/Doc/lib/libitertools.tex b/Doc/lib/libitertools.tex
index 93116ea..d0e1269 100644
--- a/Doc/lib/libitertools.tex
+++ b/Doc/lib/libitertools.tex
@@ -197,9 +197,9 @@
If \var{start} is non-zero, then elements from the iterable are skipped
until start is reached. Afterward, elements are returned consecutively
unless \var{step} is set higher than one which results in items being
- skipped. If \var{stop} is specified, then iteration stops at the
- specified element position; otherwise, it continues indefinitely or
- until the iterable is exhausted. Unlike regular slicing,
+ skipped. If \var{stop} is not specified or is \code{None}, then iteration
+ continues indefinitely; otherwise, it stops at the specified position.
+ Unlike regular slicing,
\function{islice()} does not support negative values for \var{start},
\var{stop}, or \var{step}. Can be used to extract related fields
from data where the internal structure has been flattened (for
@@ -208,17 +208,20 @@
\begin{verbatim}
def islice(iterable, *args):
- s = slice(*args)
- next = s.start or 0
- stop = s.stop
- step = s.step or 1
+ if args:
+ s = slice(*args)
+ next = s.start or 0
+ stop = s.stop
+ step = s.step or 1
+ else:
+ next, stop, step = 0, None, 1
for cnt, element in enumerate(iterable):
if cnt < next:
continue
- if cnt >= stop:
+ if stop is not None and cnt >= stop:
break
yield element
- next += step
+ next += step
\end{verbatim}
\end{funcdesc}
@@ -360,7 +363,7 @@
>>> def pairwise(seq):
... "s -> (s0,s1), (s1,s2), (s2, s3), ..."
-... return izip(seq, islice(seq,1,len(seq)))
+... return izip(seq, islice(seq,1,None))
>>> def padnone(seq):
... "Returns the sequence elements and then returns None indefinitely"