summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2003-05-02 19:44:20 (GMT)
committerRaymond Hettinger <python@rcn.com>2003-05-02 19:44:20 (GMT)
commit341deb74e7ec78eaf99838669ce04871e9f15783 (patch)
treec3c8257703ef5e718c8f850945dc2e7070f25071
parent14ef54cd833c7ccfa0b3d03e2b45074d54a8ac87 (diff)
downloadcpython-341deb74e7ec78eaf99838669ce04871e9f15783.zip
cpython-341deb74e7ec78eaf99838669ce04871e9f15783.tar.gz
cpython-341deb74e7ec78eaf99838669ce04871e9f15783.tar.bz2
The previous made the stop argument optional.
It is better to be explicit and just allow stop to be None.
-rw-r--r--Doc/lib/libitertools.tex17
-rw-r--r--Lib/test/test_itertools.py2
-rw-r--r--Modules/itertoolsmodule.c4
3 files changed, 10 insertions, 13 deletions
diff --git a/Doc/lib/libitertools.tex b/Doc/lib/libitertools.tex
index d0e1269..f291fe3 100644
--- a/Doc/lib/libitertools.tex
+++ b/Doc/lib/libitertools.tex
@@ -197,9 +197,9 @@ by functions or loops that truncate the stream.
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 not specified or is \code{None}, then iteration
- continues indefinitely; otherwise, it stops at the specified position.
- Unlike regular slicing,
+ skipped. If \var{stop} is \code{None}, then iteration continues until
+ the iterator is exhausted, if at all; 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,13 +208,10 @@ by functions or loops that truncate the stream.
\begin{verbatim}
def islice(iterable, *args):
- 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
+ s = slice(*args)
+ next = s.start or 0
+ stop = s.stop
+ step = s.step or 1
for cnt, element in enumerate(iterable):
if cnt < next:
continue
diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py
index d0b1ce8..ef2ab26 100644
--- a/Lib/test/test_itertools.py
+++ b/Lib/test/test_itertools.py
@@ -78,12 +78,12 @@ class TestBasicOps(unittest.TestCase):
self.assertEqual(list(islice(xrange(100), *args)), range(*tgtargs))
# Test stop=None
- self.assertEqual(list(islice(xrange(10))), range(10))
self.assertEqual(list(islice(xrange(10), None)), range(10))
self.assertEqual(list(islice(xrange(10), 2, None)), range(2, 10))
self.assertEqual(list(islice(xrange(10), 1, None, 2)), range(1, 10, 2))
# Test invalid arguments
+ self.assertRaises(TypeError, islice, xrange(10))
self.assertRaises(TypeError, islice, xrange(10), 1, 2, 3, 4)
self.assertRaises(ValueError, islice, xrange(10), -5, 10, 1)
self.assertRaises(ValueError, islice, xrange(10), 1, -5, -1)
diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c
index f05ebd6..2d496b5 100644
--- a/Modules/itertoolsmodule.c
+++ b/Modules/itertoolsmodule.c
@@ -477,7 +477,7 @@ islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
isliceobject *lz;
numargs = PyTuple_Size(args);
- if (!PyArg_ParseTuple(args, "O|OOl:islice", &seq, &a1, &a2, &step))
+ if (!PyArg_ParseTuple(args, "OO|Ol:islice", &seq, &a1, &a2, &step))
return NULL;
if (numargs == 2) {
@@ -491,7 +491,7 @@ islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return NULL;
}
}
- } else if (numargs == 3 || numargs == 4) {
+ } else {
start = PyInt_AsLong(a1);
if (start == -1 && PyErr_Occurred()) {
PyErr_Clear();