[Python-checkins] CVS: python/dist/src/Include abstract.h,2.30,2.31 iterobject.h,1.2,1.3 object.h,2.78,2.79

Guido van Rossum gvanrossum@users.sourceforge.net
Mon, 23 Apr 2001 07:08:51 -0700


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

Modified Files:
	abstract.h iterobject.h object.h 
Log Message:
Mondo changes to the iterator stuff, without changing how Python code
sees it (test_iter.py is unchanged).

- Added a tp_iternext slot, which calls the iterator's next() method;
  this is much faster for built-in iterators over built-in types
  such as lists and dicts, speeding up pybench's ForLoop with about
  25% compared to Python 2.1.  (Now there's a good argument for
  iterators. ;-)

- Renamed the built-in sequence iterator SeqIter, affecting the C API
  functions for it.  (This frees up the PyIter prefix for generic
  iterator operations.)

- Added PyIter_Check(obj), which checks that obj's type has a
  tp_iternext slot and that the proper feature flag is set.

- Added PyIter_Next(obj) which calls the tp_iternext slot.  It has a
  somewhat complex return condition due to the need for speed: when it
  returns NULL, it may not have set an exception condition, meaning
  the iterator is exhausted; when the exception StopIteration is set
  (or a derived exception class), it means the same thing; any other
  exception means some other error occurred.



Index: abstract.h
===================================================================
RCS file: /cvsroot/python/python/dist/src/Include/abstract.h,v
retrieving revision 2.30
retrieving revision 2.31
diff -C2 -r2.30 -r2.31
*** abstract.h	2001/04/20 19:13:01	2.30
--- abstract.h	2001/04/23 14:08:49	2.31
***************
*** 471,478 ****
--- 471,491 ----
         */
  
+ /* Iterators */
+ 
       DL_IMPORT(PyObject *) PyObject_GetIter(PyObject *);
       /* Takes an object and returns an iterator for it.
          This is typically a new iterator but if the argument
  	is an iterator, this returns itself. */
+ 
+ #define PyIter_Check(obj) \
+     (PyType_HasFeature((obj)->ob_type, Py_TPFLAGS_HAVE_ITER) && \
+      (obj)->ob_type->tp_iternext != NULL)
+ 
+      DL_IMPORT(PyObject *) PyIter_Next(PyObject *);
+      /* Takes an iterator object and calls its tp_iternext slot,
+ 	returning the next value.  If the iterator is exhausted,
+ 	this can return NULL without setting an exception, *or*
+ 	NULL with a StopIteration exception.
+ 	NULL with any other exception  means an error occurred. */
  
  /*  Number Protocol:*/

Index: iterobject.h
===================================================================
RCS file: /cvsroot/python/python/dist/src/Include/iterobject.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -r1.2 -r1.3
*** iterobject.h	2001/04/20 21:06:45	1.2
--- iterobject.h	2001/04/23 14:08:49	1.3
***************
*** 1,9 ****
  /* Iterators (the basic kind, over a sequence) */
  
! extern DL_IMPORT(PyTypeObject) PyIter_Type;
  
! #define PyIter_Check(op) ((op)->ob_type == &PyIter_Type)
  
! extern DL_IMPORT(PyObject *) PyIter_New(PyObject *);
  
  extern DL_IMPORT(PyTypeObject) PyCallIter_Type;
--- 1,9 ----
  /* Iterators (the basic kind, over a sequence) */
  
! extern DL_IMPORT(PyTypeObject) PySeqIter_Type;
  
! #define PySeqIter_Check(op) ((op)->ob_type == &PySeqIter_Type)
  
! extern DL_IMPORT(PyObject *) PySeqIter_New(PyObject *);
  
  extern DL_IMPORT(PyTypeObject) PyCallIter_Type;

Index: object.h
===================================================================
RCS file: /cvsroot/python/python/dist/src/Include/object.h,v
retrieving revision 2.78
retrieving revision 2.79
diff -C2 -r2.78 -r2.79
*** object.h	2001/04/20 19:13:01	2.78
--- object.h	2001/04/23 14:08:49	2.79
***************
*** 202,205 ****
--- 202,206 ----
  typedef PyObject *(*richcmpfunc) (PyObject *, PyObject *, int);
  typedef PyObject *(*getiterfunc) (PyObject *);
+ typedef PyObject *(*iternextfunc) (PyObject *);
  
  typedef struct _typeobject {
***************
*** 253,256 ****
--- 254,258 ----
  	/* Iterators */
  	getiterfunc tp_iter;
+ 	iternextfunc tp_iternext;
  
  #ifdef COUNT_ALLOCS