bug in Python's read function (trying again)

Chris Jaeger cjaeger at ensim.com
Fri Oct 26 21:31:25 CEST 2001


Hi all,

	Having looked at the python 2.1.1 implementation
of file_read (which provides the read function for python
file objects), I think I've discovered a bug, unless this
behavior is really intended. file_read calls fread multiple
times in order to try to fill the request of the caller.
Each time fread returns, whatever data is returned is tacked
onto whatever was returned before. If some call to fread after
the first call returns an error, then an exception is raised
and any data already read is lost. This may be fine for regular
files that you can seek on, but for other sources of data (such
as that belonging to the output from commands), this won't
work. This can be reproduced fairly easily by setting O_NONBLOCK
on the file descriptor you are reading.

	So, is this a bug, or is this done by design? If you are
monitoring both stdout and stderr, you need to set O_NONBLOCK on
the files to prevent deadlock. Perhaps on error file_read should
set a bit indicating that it should fail on the next invocation,
as opposed to throwing away the data read during the current
invocation..

Thanks,
Chris




More information about the Python-list mailing list