]> granicus.if.org Git - python/commitdiff
Band-aid solution to SF bug #470634: readlines() on linux requires 2 ^D's.
authorGuido van Rossum <guido@python.org>
Fri, 12 Oct 2001 20:01:53 +0000 (20:01 +0000)
committerGuido van Rossum <guido@python.org>
Fri, 12 Oct 2001 20:01:53 +0000 (20:01 +0000)
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.

Objects/fileobject.c

index 8f903d1b467bf31f163f9a6224964b4e5ac1bae3..18f9ce2cc282ff97aa63abc42a15ffe1995474cb 100644 (file)
@@ -1045,6 +1045,7 @@ file_readlines(PyFileObject *f, PyObject *args)
        size_t totalread = 0;
        char *p, *q, *end;
        int err;
+       int shortread = 0;
 
        if (f->f_fp == NULL)
                return err_closed();
@@ -1053,10 +1054,16 @@ file_readlines(PyFileObject *f, PyObject *args)
        if ((list = PyList_New(0)) == NULL)
                return NULL;
        for (;;) {
-               Py_BEGIN_ALLOW_THREADS
-               errno = 0;
-               nread = fread(buffer+nfilled, 1, buffersize-nfilled, f->f_fp);
-               Py_END_ALLOW_THREADS
+               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;
                        if (!ferror(f->f_fp))