Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7b998e9

Browse files
committed
GzipFile.peek improvements, suggested by Nir Aides.
1 parent 977c707 commit 7b998e9

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

Doc/library/gzip.rst

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ The module defines the following items:
7070
including iteration and the :keyword:`with` statement. Only the
7171
:meth:`truncate` method isn't implemented.
7272

73+
:class:`GzipFile` also provides the following method:
74+
75+
.. method:: peek([n])
76+
77+
Read *n* uncompressed bytes without advancing the file position.
78+
At most one single read on the compressed stream is done to satisfy
79+
the call. The number of bytes returned may be more or less than
80+
requested.
81+
82+
.. versionadded:: 3.2
83+
7384
.. versionchanged:: 3.1
7485
Support for the :keyword:`with` statement was added.
7586

@@ -79,9 +90,6 @@ The module defines the following items:
7990
.. versionchanged:: 3.2
8091
Support for unseekable files was added.
8192

82-
.. versionchanged:: 3.2
83-
The :meth:`peek` method was implemented.
84-
8593

8694
.. function:: open(filename, mode='rb', compresslevel=9)
8795

Lib/gzip.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,16 +342,18 @@ def read(self, size=-1):
342342
def peek(self, n):
343343
if self.mode != READ:
344344
import errno
345-
raise IOError(errno.EBADF, "read() on write-only GzipFile object")
345+
raise IOError(errno.EBADF, "peek() on write-only GzipFile object")
346346

347-
# Do not return ridiculously small buffers
347+
# Do not return ridiculously small buffers, for one common idiom
348+
# is to call peek(1) and expect more bytes in return.
348349
if n < 100:
349350
n = 100
350351
if self.extrasize == 0:
351352
if self.fileobj is None:
352353
return b''
353354
try:
354-
self._read(max(self.max_read_chunk, n))
355+
# 1024 is the same buffering heuristic used in read()
356+
self._read(max(n, 1024))
355357
except EOFError:
356358
pass
357359
offset = self.offset - self.extrastart

0 commit comments

Comments
 (0)