summaryrefslogtreecommitdiffstats
path: root/Objects/rangeobject.c
diff options
context:
space:
mode:
authorKristján Valur Jónsson <sweskman@gmail.com>2014-03-05 15:23:07 (GMT)
committerKristján Valur Jónsson <sweskman@gmail.com>2014-03-05 15:23:07 (GMT)
commitc5cc5011ac33f96a8bf28e3ba088980fd5e71d7a (patch)
tree21c775f7b89e60348ed587d1bb983b2cd61e723b /Objects/rangeobject.c
parent25ea45db81540b8c589c65edf2564c04461b1f34 (diff)
parent25dded041fe532fcb041b6e68582bf76b4968132 (diff)
downloadcpython-c5cc5011ac33f96a8bf28e3ba088980fd5e71d7a.zip
cpython-c5cc5011ac33f96a8bf28e3ba088980fd5e71d7a.tar.gz
cpython-c5cc5011ac33f96a8bf28e3ba088980fd5e71d7a.tar.bz2
Make the various iterators' "setstate" sliently and consistently clip the
index. This avoids the possibility of setting an iterator to an invalid state.
Diffstat (limited to 'Objects/rangeobject.c')
-rw-r--r--Objects/rangeobject.c31
1 files changed, 27 insertions, 4 deletions
diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c
index e82ebf44..d6b4279 100644
--- a/Objects/rangeobject.c
+++ b/Objects/rangeobject.c
@@ -807,10 +807,11 @@ rangeiter_setstate(rangeiterobject *r, PyObject *state)
long index = PyLong_AsLong(state);
if (index == -1 && PyErr_Occurred())
return NULL;
- if (index < 0 || index > r->len) {
- PyErr_SetString(PyExc_ValueError, "index out of range");
- return NULL;
- }
+ /* silently clip the index value */
+ if (index < 0)
+ index = 0;
+ else if (index > r->len)
+ index = r->len; /* exhausted iterator */
r->index = index;
Py_RETURN_NONE;
}
@@ -985,6 +986,28 @@ longrangeiter_reduce(longrangeiterobject *r)
static PyObject *
longrangeiter_setstate(longrangeiterobject *r, PyObject *state)
{
+ int cmp;
+
+ /* clip the value */
+ PyObject *zero = PyLong_FromLong(0);
+ if (zero == NULL)
+ return NULL;
+ cmp = PyObject_RichCompareBool(state, zero, Py_LT);
+ if (cmp > 0) {
+ Py_CLEAR(r->index);
+ r->index = zero;
+ Py_RETURN_NONE;
+ }
+ Py_DECREF(zero);
+ if (cmp < 0)
+ return NULL;
+
+ cmp = PyObject_RichCompareBool(r->len, state, Py_LT);
+ if (cmp < 0)
+ return NULL;
+ if (cmp > 0)
+ state = r->len;
+
Py_CLEAR(r->index);
r->index = state;
Py_INCREF(r->index);