[Python-checkins] python/dist/src/Objects listobject.c,2.118,2.119

gvanrossum@users.sourceforge.net gvanrossum@users.sourceforge.net
Tue, 16 Jul 2002 13:07:34 -0700


Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv27419

Modified Files:
	listobject.c 
Log Message:
Make StopIteration a sink state.  This is done by clearing out the
it_seq field when the end of the list is reached.

Also remove the next() method -- one is supplied automatically by
PyType_Ready() because the tp_iternext slot is set.  That's a good
thing, because the implementation given here was buggy (it never
raised StopIteration).


Index: listobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/listobject.c,v
retrieving revision 2.118
retrieving revision 2.119
diff -C2 -d -r2.118 -r2.119
*** listobject.c	16 Jul 2002 15:56:52 -0000	2.118
--- listobject.c	16 Jul 2002 20:07:32 -0000	2.119
***************
*** 1,3 ****
- 
  /* List object implementation */
  
--- 1,2 ----
***************
*** 2017,2021 ****
          PyObject_HEAD
          long it_index;
!         PyListObject *it_seq;
  } listiterobject;
  
--- 2016,2020 ----
          PyObject_HEAD
          long it_index;
! 	PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
  } listiterobject;
  
***************
*** 2045,2049 ****
  {
          _PyObject_GC_UNTRACK(it);
!         Py_DECREF(it->it_seq);
          PyObject_GC_Del(it);
  }
--- 2044,2048 ----
  {
          _PyObject_GC_UNTRACK(it);
! 	Py_XDECREF(it->it_seq);
          PyObject_GC_Del(it);
  }
***************
*** 2052,2055 ****
--- 2051,2056 ----
  listiter_traverse(listiterobject *it, visitproc visit, void *arg)
  {
+ 	if (it->it_seq == NULL)
+ 		return 0;
          return visit((PyObject *)it->it_seq, arg);
  }
***************
*** 2071,2074 ****
--- 2072,2077 ----
  	assert(it != NULL);
          seq = it->it_seq;
+ 	if (seq == NULL)
+ 		return NULL;
          assert(PyList_Check(seq));
  
***************
*** 2079,2091 ****
                  return item;
          }
          return NULL;
  }
  
- static PyMethodDef listiter_methods[] = {
-         {"next",        (PyCFunction)listiter_next,     METH_NOARGS,
-          "it.next() -- get the next value, or raise StopIteration"},
-         {NULL,          NULL}           /* sentinel */
- };
- 
  PyTypeObject PyListIter_Type = {
          PyObject_HEAD_INIT(&PyType_Type)
--- 2082,2091 ----
                  return item;
          }
+ 
+ 	Py_DECREF(seq);
+ 	it->it_seq = NULL;
          return NULL;
  }
  
  PyTypeObject PyListIter_Type = {
          PyObject_HEAD_INIT(&PyType_Type)
***************
*** 2118,2122 ****
          (getiterfunc)listiter_getiter,          /* tp_iter */
          (iternextfunc)listiter_next,            /* tp_iternext */
!         listiter_methods,			/* tp_methods */
          0,                                      /* tp_members */
          0,                                      /* tp_getset */
--- 2118,2122 ----
          (getiterfunc)listiter_getiter,          /* tp_iter */
          (iternextfunc)listiter_next,            /* tp_iternext */
! 	0,					/* tp_methods */
          0,                                      /* tp_members */
          0,                                      /* tp_getset */
***************
*** 2126,2128 ****
          0,                                      /* tp_descr_set */
  };
- 
--- 2126,2127 ----