summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2003-05-02 19:04:37 (GMT)
committerRaymond Hettinger <python@rcn.com>2003-05-02 19:04:37 (GMT)
commit14ef54cd833c7ccfa0b3d03e2b45074d54a8ac87 (patch)
treee21099b898f9374f4b0082e68cd1ab53a8c1530a /Modules
parente2df5ffa539bc1d903684e8c5863645a8886bfe2 (diff)
downloadcpython-14ef54cd833c7ccfa0b3d03e2b45074d54a8ac87.zip
cpython-14ef54cd833c7ccfa0b3d03e2b45074d54a8ac87.tar.gz
cpython-14ef54cd833c7ccfa0b3d03e2b45074d54a8ac87.tar.bz2
SF bug #730685: itertools.islice stop argument is not optional
* itertools.islice() stop argument did not perform as documented. * beefed-up test suite
Diffstat (limited to 'Modules')
-rw-r--r--Modules/itertoolsmodule.c46
1 files changed, 33 insertions, 13 deletions
diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c
index 35fa1d0..f05ebd6 100644
--- a/Modules/itertoolsmodule.c
+++ b/Modules/itertoolsmodule.c
@@ -471,27 +471,47 @@ static PyObject *
islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyObject *seq;
- long a1=0, a2=0, a3=0, start=0, stop=0, step=1;
- PyObject *it;
+ long start=0, stop=-1, step=1;
+ PyObject *it, *a1=NULL, *a2=NULL;
int numargs;
isliceobject *lz;
numargs = PyTuple_Size(args);
- if (!PyArg_ParseTuple(args, "Ol|ll:islice", &seq, &a1, &a2, &a3))
+ if (!PyArg_ParseTuple(args, "O|OOl:islice", &seq, &a1, &a2, &step))
return NULL;
if (numargs == 2) {
- stop = a1;
- } else if (numargs == 3) {
- start = a1;
- stop = a2;
- } else {
- start = a1;
- stop = a2;
- step = a3;
+ if (a1 != Py_None) {
+ stop = PyInt_AsLong(a1);
+ if (stop == -1) {
+ if (PyErr_Occurred())
+ PyErr_Clear();
+ PyErr_SetString(PyExc_ValueError,
+ "Stop argument must be an integer or None.");
+ return NULL;
+ }
+ }
+ } else if (numargs == 3 || numargs == 4) {
+ start = PyInt_AsLong(a1);
+ if (start == -1 && PyErr_Occurred()) {
+ PyErr_Clear();
+ PyErr_SetString(PyExc_ValueError,
+ "Start argument must be an integer.");
+ return NULL;
+ }
+ if (a2 != Py_None) {
+ stop = PyInt_AsLong(a2);
+ if (stop == -1) {
+ if (PyErr_Occurred())
+ PyErr_Clear();
+ PyErr_SetString(PyExc_ValueError,
+ "Stop argument must be an integer or None.");
+ return NULL;
+ }
+ }
}
- if (start<0 || stop<0) {
+ if (start<0 || stop<-1) {
PyErr_SetString(PyExc_ValueError,
"Indices for islice() must be positive.");
return NULL;
@@ -554,7 +574,7 @@ islice_next(isliceobject *lz)
Py_DECREF(item);
lz->cnt++;
}
- if (lz->cnt >= lz->stop)
+ if (lz->stop != -1 && lz->cnt >= lz->stop)
return NULL;
assert(PyIter_Check(it));
item = (*it->ob_type->tp_iternext)(it);