Mercurial > cpython-lru_cache-weakref
diff Objects/enumobject.c @ 31635:04eb2ebb46e2 legacy-trunk
Tidied up the implementations of reversed (including the custom ones
for xrange and list objects).
* list.__reversed__ now checks the length of the sequence object before
calling PyList_GET_ITEM() because the mutable could have changed length.
* all three implementations are now tranparent with respect to length and
maintain the invariant len(it) == len(list(it)) even when the underlying
sequence mutates.
* __builtin__.reversed() now frees the underlying sequence as soon
as the iterator is exhausted.
* the code paths were rearranged so that the most common paths
do not require a jump.
| author | Raymond Hettinger <python@rcn.com> |
|---|---|
| date | Wed, 10 Mar 2004 10:10:42 +0000 |
| parents | ec6ba03ba962 |
| children | 058821d8aefd |
line wrap: on
line diff
--- a/Objects/enumobject.c Wed Mar 10 08:32:47 2004 +0000 +++ b/Objects/enumobject.c Wed Mar 10 10:10:42 2004 +0000 @@ -217,23 +217,21 @@ reversed_next(reversedobject *ro) { PyObject *item; - - if (ro->index < 0) - return NULL; - - assert(PySequence_Check(ro->seq)); - item = PySequence_GetItem(ro->seq, ro->index); - if (item == NULL) - return NULL; + long index = ro->index; - 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 @@ "\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 */
