[Python-checkins] python/dist/src/Include fileobject.h,2.29,2.30

gvanrossum@users.sourceforge.net gvanrossum@users.sourceforge.net
Tue, 06 Aug 2002 08:55:30 -0700


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

Modified Files:
	fileobject.h 
Log Message:
SF patch 580331 by Oren Tirosh: make file objects their own iterator.

For a file f, iter(f) now returns f (unless f is closed), and f.next()
is similar to f.readline() when EOF is not reached; however, f.next()
uses a readahead buffer that messes up the file position, so mixing
f.next() and f.readline() (or other methods) doesn't work right.
Calling f.seek() drops the readahead buffer, but other operations
don't.

The real purpose of this change is to reduce the confusion between
objects and their iterators.  By making a file its own iterator, it's
made clearer that using the iterator modifies the file object's state
(in particular the current position).

A nice side effect is that this speeds up "for line in f:" by not
having to use the xreadlines module.  The f.xreadlines() method is
still supported for backwards compatibility, though it is the same as
iter(f) now.

(I made some cosmetic changes to Oren's code, and added a test for
"file closed" to file_iternext() and file_iter().)



Index: fileobject.h
===================================================================
RCS file: /cvsroot/python/python/dist/src/Include/fileobject.h,v
retrieving revision 2.29
retrieving revision 2.30
diff -C2 -d -r2.29 -r2.30
*** fileobject.h	24 May 2002 15:24:38 -0000	2.29
--- fileobject.h	6 Aug 2002 15:55:28 -0000	2.30
***************
*** 14,20 ****
  	PyObject *f_mode;
  	int (*f_close)(FILE *);
! 	int f_softspace; /* Flag used by 'print' command */
! 	int f_binary; /* Flag which indicates whether the file is open
! 			 open in binary (1) or test (0) mode */
  #ifdef WITH_UNIVERSAL_NEWLINES
  	int f_univ_newline;	/* Handle any newline convention */
--- 14,23 ----
  	PyObject *f_mode;
  	int (*f_close)(FILE *);
! 	int f_softspace;	/* Flag used by 'print' command */
! 	int f_binary;		/* Flag which indicates whether the file is 
! 				   open in binary (1) or text (0) mode */
! 	char* f_buf;		/* Allocated readahead buffer */
! 	char* f_bufend;		/* Points after last occupied position */
! 	char* f_bufptr;		/* Current buffer position */
  #ifdef WITH_UNIVERSAL_NEWLINES
  	int f_univ_newline;	/* Handle any newline convention */