From 001f228f369d5c3e9fc28ae7a9815e1317bbe72f Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 8 Nov 2003 11:58:44 +0000 Subject: [PATCH] Improve the reverse list iterator to free memory as soon as the iterator is exhausted. --- Objects/listobject.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Objects/listobject.c b/Objects/listobject.c index 7198e3481af..3782c3baacf 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -2771,7 +2771,7 @@ PyTypeObject PyListIter_Type = { typedef struct { PyObject_HEAD long it_index; - PyListObject *it_seq; + PyListObject *it_seq; /* Set to NULL when iterator is exhausted */ } listreviterobject; PyTypeObject PyListRevIter_Type; @@ -2819,6 +2819,9 @@ listreviter_next(listreviterobject *it) item = PyList_GET_ITEM(it->it_seq, it->it_index); it->it_index--; Py_INCREF(item); + } else if (it->it_seq != NULL) { + Py_DECREF(it->it_seq); + it->it_seq = NULL; } return item; }