feof status (was: Re: [Python-Dev] Rehabilitating fgets)

Guido van Rossum guido@python.org
Sun, 07 Jan 2001 11:52:11 -0500

> This mess reminds me.  For some work I'm doing right now, it would be
> very useful if there were a way to query the end-of-file status of a
> file descriptor without actually doing a read.

I hope you really mean file object (== wrapper around stdio FILE
object).  A file descriptor (small little integer in Unix) doesn't
have a way to find this out.

Even for file objects, it is typically only known that there's an EOF
condition after a lowest-level read operation returned 0 bytes.  So in
effect you must still do a read in order to determine EOF status.

I just ran a small test program, and fread() appears to set the eof
status when it returns a short count.  Normally, Python's read() uses
fread() so this might be useful.  However after a readline(), you
can't know the eof status (unless the last line of the file doesn't
end in a newline).

> I don't see this ability anywhere in the 2.0 API.  Questions:
> 1. Am I missing something obvious?
> 2. If the answer to 1 is that I am not, in fact, being a dumbass, what
>    is the right way to support this?  The obvious alternatives are an 
>    eof member (analogous to the existing `closed' member, or an eof()
>    method.  I favor the latter.
> 3. If we agree on a design, I'm willing to implement this at least for
>    Unix.  Should be a small project.

Before adding an eof() method, can you explain what your program is
trying to do?  Is it reading from a pipe or socket?  Then select() or
poll() might be useful.

--Guido van Rossum (home page: http://www.python.org/~guido/)