[Python-checkins] CVS: python/dist/src/Objects fileobject.c,2.133,2.134

Guido van Rossum gvanrossum@users.sourceforge.net
Fri, 12 Oct 2001 13:01:55 -0700


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

Modified Files:
	fileobject.c 
Log Message:
Band-aid solution to SF bug #470634: readlines() on linux requires 2 ^D's.

The problem is that if fread() returns a short count, we attempt
another fread() the next time through the loop, and apparently glibc
clears or ignores the eof condition so the second fread() requires
another ^D to make it see the eof condition.

According to the man page (and the C std, I hope) fread() can only
return a short count on error or eof.  I'm using that in the band-aid
solution to avoid calling fread() a second time after a short read.

Note that xreadlines() still has this problem: it calls
readlines(sizehint) until it gets a zero-length return.  Since
xreadlines() is mostly used for reading real files, I won't worry
about this until we get a bug report.


Index: fileobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/fileobject.c,v
retrieving revision 2.133
retrieving revision 2.134
diff -C2 -d -r2.133 -r2.134
*** fileobject.c	2001/10/10 22:03:27	2.133
--- fileobject.c	2001/10/12 20:01:53	2.134
***************
*** 1046,1049 ****
--- 1046,1050 ----
  	char *p, *q, *end;
  	int err;
+ 	int shortread = 0;
  
  	if (f->f_fp == NULL)
***************
*** 1054,1061 ****
  		return NULL;
  	for (;;) {
! 		Py_BEGIN_ALLOW_THREADS
! 		errno = 0;
! 		nread = fread(buffer+nfilled, 1, buffersize-nfilled, f->f_fp);
! 		Py_END_ALLOW_THREADS
  		if (nread == 0) {
  			sizehint = 0;
--- 1055,1068 ----
  		return NULL;
  	for (;;) {
! 		if (shortread)
! 			nread = 0;
! 		else {
! 			Py_BEGIN_ALLOW_THREADS
! 			errno = 0;
! 			nread = fread(buffer+nfilled, 1,
! 				      buffersize-nfilled, f->f_fp);
! 			Py_END_ALLOW_THREADS
! 			shortread = (nread < buffersize-nfilled);
! 		}
  		if (nread == 0) {
  			sizehint = 0;