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

Nikolaus Rath report at bugs.python.org
Wed Jul 24 23:58:51 CEST 2013


Nikolaus Rath added the comment:

On 07/24/2013 02:51 PM, Antoine Pitrou wrote:
> 
> 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).

I agree that this is how it *should* work. But that's not what's
happening in practice:

> $ python3
Python 3.2.3 (default, Feb 20 2013, 14:44:27)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import io
>>> raw = io.BytesIO(bytes(200))
>>> buffered = io.BufferedReader(raw, 10)
>>> while True:
...     buf = buffered.read1(20)
...     print('Read %d bytes' % len(buf))
...     if not buf:
...        break
...
Read 10 bytes
Read 10 bytes
Read 10 bytes
Read 10 bytes
Read 10 bytes
Read 10 bytes
Read 10 bytes
Read 10 bytes
[...]

----------

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


More information about the Python-bugs-list mailing list