[issue18524] BufferedReader.read1() documentation/implementation difference

Antoine Pitrou report at bugs.python.org
Wed Jul 24 23:51:31 CEST 2013


Antoine Pitrou added the comment:

Le mercredi 24 juillet 2013 à 21:45 +0000, Nikolaus Rath a écrit :
> The documentation is correct that read1(n) never returns more than n bytes.
> 
> My complaint is that the actual bound is stricter than this, band
> read1(n) will never return more than min(n, bufsize) bytes.

That's not really true. If the buffer is empty, read1(n) will try to
read at most n bytes from the raw stream. So:
- if the buffer is not empty, the whole buffer is returned and 0 raw I/O
call is issued
- if the buffer is empty, 1 raw I/O call is issued and at most 1 byte is
returned

Therefore, if you call read1(n) in a loop, all calls but the first will
really try to read *n* bytes from the raw stream (because the first call
will have emptied the buffer).

(one implicit goal is to minimize the number of memory copies when
returning data to the application; a well-known consumer of read1()
calls is TextIOWrapper when it fills its internal buffer)

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue18524>
_______________________________________


More information about the Python-bugs-list mailing list