[New-bugs-announce] [issue5811] io.BufferedReader.peek(): Documentation differs from Implementation

Torsten Rottmann report at bugs.python.org
Wed Apr 22 08:15:53 CEST 2009


New submission from Torsten Rottmann <trott at meditec-gmbh.com>:

The documented behavior of io.BufferedReader.peek([n]) states:

peek([n])
Return 1 (or n if specified) bytes from a buffer without advancing the 
position.

Thereas the parameter n is the _max_ length of returned bytes.

Implementation is:

    def _peek_unlocked(self, n=0):
        want = min(n, self.buffer_size)
        have = len(self._read_buf) - self._read_pos
        if have < want:
            to_read = self.buffer_size - have
            current = self.raw.read(to_read)
            if current:
                self._read_buf = self._read_buf[self._read_pos:] + 
current
                self._read_pos = 0
        return self._read_buf[self._read_pos:]

Where you may see that the parameter n is the _min_ length
of returned bytes. So peek(1) will return _not_ just 1 Byte, but the
remaining bytes in the buffer.

----------
components: Library (Lib)
messages: 86274
nosy: trott
severity: normal
status: open
title: io.BufferedReader.peek(): Documentation differs from Implementation
type: behavior
versions: Python 3.0

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


More information about the New-bugs-announce mailing list