[Python-checkins] CVS: python/dist/src/Include Python.h,2.31,2.32 abstract.h,2.29,2.30 object.h,2.77,2.78 opcode.h,2.34,2.35 pyerrors.h,2.44,2.45

Guido van Rossum gvanrossum@users.sourceforge.net
Fri, 20 Apr 2001 12:13:04 -0700


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

Modified Files:
	Python.h abstract.h object.h opcode.h pyerrors.h 
Log Message:
Iterators phase 1.  This comprises:

new slot tp_iter in type object, plus new flag Py_TPFLAGS_HAVE_ITER
new C API PyObject_GetIter(), calls tp_iter
new builtin iter(), with two forms: iter(obj), and iter(function, sentinel)
new internal object types iterobject and calliterobject
new exception StopIteration
new opcodes for "for" loops, GET_ITER and FOR_ITER (also supported by dis.py)
new magic number for .pyc files
new special method for instances: __iter__() returns an iterator
iteration over dictionaries: "for x in dict" iterates over the keys
iteration over files: "for x in file" iterates over lines

TODO:

documentation
test suite
decide whether to use a different way to spell iter(function, sentinal)
decide whether "for key in dict" is a good idea
use iterators in map/filter/reduce, min/max, and elsewhere (in/not in?)
speed tuning (make next() a slot tp_next???)



Index: Python.h
===================================================================
RCS file: /cvsroot/python/python/dist/src/Include/Python.h,v
retrieving revision 2.31
retrieving revision 2.32
diff -C2 -r2.31 -r2.32
*** Python.h	2001/01/25 20:04:14	2.31
--- Python.h	2001/04/20 19:13:01	2.32
***************
*** 83,86 ****
--- 83,87 ----
  #include "sliceobject.h"
  #include "cellobject.h"
+ #include "iterobject.h"
  
  #include "codecs.h"

Index: abstract.h
===================================================================
RCS file: /cvsroot/python/python/dist/src/Include/abstract.h,v
retrieving revision 2.29
retrieving revision 2.30
diff -C2 -r2.29 -r2.30
*** abstract.h	2001/03/21 18:40:58	2.29
--- abstract.h	2001/04/20 19:13:01	2.30
***************
*** 471,474 ****
--- 471,479 ----
         */
  
+      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. */
+ 
  /*  Number Protocol:*/
  

Index: object.h
===================================================================
RCS file: /cvsroot/python/python/dist/src/Include/object.h,v
retrieving revision 2.77
retrieving revision 2.78
diff -C2 -r2.77 -r2.78
*** object.h	2001/02/26 18:56:37	2.77
--- object.h	2001/04/20 19:13:01	2.78
***************
*** 201,204 ****
--- 201,205 ----
  typedef long (*hashfunc)(PyObject *);
  typedef PyObject *(*richcmpfunc) (PyObject *, PyObject *, int);
+ typedef PyObject *(*getiterfunc) (PyObject *);
  
  typedef struct _typeobject {
***************
*** 250,255 ****
  	long tp_weaklistoffset;
  
  #ifdef COUNT_ALLOCS
! 	/* these must be last */
  	int tp_alloc;
  	int tp_free;
--- 251,259 ----
  	long tp_weaklistoffset;
  
+ 	/* Iterators */
+ 	getiterfunc tp_iter;
+ 
  #ifdef COUNT_ALLOCS
! 	/* these must be last and never explicitly initialized */
  	int tp_alloc;
  	int tp_free;
***************
*** 343,354 ****
  #define Py_TPFLAGS_CHECKTYPES (1L<<4)
  
  #define Py_TPFLAGS_HAVE_RICHCOMPARE (1L<<5)
  
  /* Objects which are weakly referencable if their tp_weaklistoffset is >0 */
- /* XXX Should this have the same value as Py_TPFLAGS_HAVE_RICHCOMPARE?
-  * These both indicate a feature that appeared in the same alpha release.
-  */
  #define Py_TPFLAGS_HAVE_WEAKREFS (1L<<6)
  
  #define Py_TPFLAGS_DEFAULT  ( \
                               Py_TPFLAGS_HAVE_GETCHARBUFFER | \
--- 347,359 ----
  #define Py_TPFLAGS_CHECKTYPES (1L<<4)
  
+ /* tp_richcompare is defined */
  #define Py_TPFLAGS_HAVE_RICHCOMPARE (1L<<5)
  
  /* Objects which are weakly referencable if their tp_weaklistoffset is >0 */
  #define Py_TPFLAGS_HAVE_WEAKREFS (1L<<6)
  
+ /* tp_iter is defined */
+ #define Py_TPFLAGS_HAVE_ITER (1L<<7)
+ 
  #define Py_TPFLAGS_DEFAULT  ( \
                               Py_TPFLAGS_HAVE_GETCHARBUFFER | \
***************
*** 357,360 ****
--- 362,366 ----
                               Py_TPFLAGS_HAVE_RICHCOMPARE | \
                               Py_TPFLAGS_HAVE_WEAKREFS | \
+                              Py_TPFLAGS_HAVE_ITER | \
                              0)
  

Index: opcode.h
===================================================================
RCS file: /cvsroot/python/python/dist/src/Include/opcode.h,v
retrieving revision 2.34
retrieving revision 2.35
diff -C2 -r2.34 -r2.35
*** opcode.h	2001/02/01 22:48:12	2.34
--- opcode.h	2001/04/20 19:13:01	2.35
***************
*** 54,57 ****
--- 54,58 ----
  #define BINARY_OR	66
  #define INPLACE_POWER	67
+ #define GET_ITER	68
  
  #define PRINT_EXPR	70
***************
*** 81,84 ****
--- 82,86 ----
  #define DELETE_NAME	91	/* "" */
  #define UNPACK_SEQUENCE	92	/* Number of sequence items */
+ #define FOR_ITER	93
  
  #define STORE_ATTR	95	/* Index in name list */

Index: pyerrors.h
===================================================================
RCS file: /cvsroot/python/python/dist/src/Include/pyerrors.h,v
retrieving revision 2.44
retrieving revision 2.45
diff -C2 -r2.44 -r2.45
*** pyerrors.h	2001/02/28 21:44:20	2.44
--- pyerrors.h	2001/04/20 19:13:01	2.45
***************
*** 25,28 ****
--- 25,29 ----
  
  extern DL_IMPORT(PyObject *) PyExc_Exception;
+ extern DL_IMPORT(PyObject *) PyExc_StopIteration;
  extern DL_IMPORT(PyObject *) PyExc_StandardError;
  extern DL_IMPORT(PyObject *) PyExc_ArithmeticError;