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.
diff --git a/Objects/listobject.c b/Objects/listobject.c
index 47f4a37..500f823 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -2793,21 +2793,35 @@
static PyObject *
listreviter_next(listreviterobject *it)
{
- PyObject *item = NULL;
+ PyObject *item;
+ long index = it->it_index;
+ PyListObject *seq = it->it_seq;
- assert(PyList_Check(it->it_seq));
- if (it->it_index >= 0) {
- assert(it->it_index < PyList_GET_SIZE(it->it_seq));
- item = PyList_GET_ITEM(it->it_seq, it->it_index);
+ if (index>=0 && index < PyList_GET_SIZE(seq)) {
+ item = PyList_GET_ITEM(seq, index);
it->it_index--;
Py_INCREF(item);
- } else if (it->it_seq != NULL) {
- Py_DECREF(it->it_seq);
- it->it_seq = NULL;
+ return item;
}
- return item;
+ it->it_index = -1;
+ if (seq != NULL) {
+ it->it_seq = NULL;
+ Py_DECREF(seq);
+ }
+ return NULL;
}
+static int
+listreviter_len(listreviterobject *it)
+{
+ return it->it_index + 1;
+}
+
+static PySequenceMethods listreviter_as_sequence = {
+ (inquiry)listreviter_len, /* sq_length */
+ 0, /* sq_concat */
+};
+
PyTypeObject PyListRevIter_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0, /* ob_size */
@@ -2822,7 +2836,7 @@
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
- 0, /* tp_as_sequence */
+ &listreviter_as_sequence, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */