diff options
Diffstat (limited to 'Objects/enumobject.c')
-rw-r--r-- | Objects/enumobject.c | 36 |
1 files changed, 20 insertions, 16 deletions
diff --git a/Objects/enumobject.c b/Objects/enumobject.c index 1d13123..28719a9 100644 --- a/Objects/enumobject.c +++ b/Objects/enumobject.c @@ -217,23 +217,21 @@ static PyObject * reversed_next(reversedobject *ro) { PyObject *item; + long index = ro->index; - if (ro->index < 0) - return NULL; - - assert(PySequence_Check(ro->seq)); - item = PySequence_GetItem(ro->seq, ro->index); - if (item == NULL) - return NULL; - - ro->index--; - return item; -} - -static int -reversed_len(reversedobject *ro) -{ - return PyObject_Size(ro->seq); + if (index >= 0) { + item = PySequence_GetItem(ro->seq, index); + if (item != NULL) { + ro->index--; + return item; + } + } + ro->index = -1; + if (ro->seq != NULL) { + Py_DECREF(ro->seq); + ro->seq = NULL; + } + return NULL; } PyDoc_STRVAR(reversed_doc, @@ -241,6 +239,12 @@ PyDoc_STRVAR(reversed_doc, "\n" "Return a reverse iterator"); +static int +reversed_len(reversedobject *ro) +{ + return ro->index + 1; +} + static PySequenceMethods reversed_as_sequence = { (inquiry)reversed_len, /* sq_length */ 0, /* sq_concat */ |