mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
GzipFile.peek improvements, suggested by Nir Aides.
This commit is contained in:
parent
977c707b42
commit
7b998e9f52
2 changed files with 16 additions and 6 deletions
|
@ -70,6 +70,17 @@ The module defines the following items:
|
||||||
including iteration and the :keyword:`with` statement. Only the
|
including iteration and the :keyword:`with` statement. Only the
|
||||||
:meth:`truncate` method isn't implemented.
|
:meth:`truncate` method isn't implemented.
|
||||||
|
|
||||||
|
:class:`GzipFile` also provides the following method:
|
||||||
|
|
||||||
|
.. method:: peek([n])
|
||||||
|
|
||||||
|
Read *n* uncompressed bytes without advancing the file position.
|
||||||
|
At most one single read on the compressed stream is done to satisfy
|
||||||
|
the call. The number of bytes returned may be more or less than
|
||||||
|
requested.
|
||||||
|
|
||||||
|
.. versionadded:: 3.2
|
||||||
|
|
||||||
.. versionchanged:: 3.1
|
.. versionchanged:: 3.1
|
||||||
Support for the :keyword:`with` statement was added.
|
Support for the :keyword:`with` statement was added.
|
||||||
|
|
||||||
|
@ -79,9 +90,6 @@ The module defines the following items:
|
||||||
.. versionchanged:: 3.2
|
.. versionchanged:: 3.2
|
||||||
Support for unseekable files was added.
|
Support for unseekable files was added.
|
||||||
|
|
||||||
.. versionchanged:: 3.2
|
|
||||||
The :meth:`peek` method was implemented.
|
|
||||||
|
|
||||||
|
|
||||||
.. function:: open(filename, mode='rb', compresslevel=9)
|
.. function:: open(filename, mode='rb', compresslevel=9)
|
||||||
|
|
||||||
|
|
|
@ -342,16 +342,18 @@ class GzipFile(io.BufferedIOBase):
|
||||||
def peek(self, n):
|
def peek(self, n):
|
||||||
if self.mode != READ:
|
if self.mode != READ:
|
||||||
import errno
|
import errno
|
||||||
raise IOError(errno.EBADF, "read() on write-only GzipFile object")
|
raise IOError(errno.EBADF, "peek() on write-only GzipFile object")
|
||||||
|
|
||||||
# Do not return ridiculously small buffers
|
# Do not return ridiculously small buffers, for one common idiom
|
||||||
|
# is to call peek(1) and expect more bytes in return.
|
||||||
if n < 100:
|
if n < 100:
|
||||||
n = 100
|
n = 100
|
||||||
if self.extrasize == 0:
|
if self.extrasize == 0:
|
||||||
if self.fileobj is None:
|
if self.fileobj is None:
|
||||||
return b''
|
return b''
|
||||||
try:
|
try:
|
||||||
self._read(max(self.max_read_chunk, n))
|
# 1024 is the same buffering heuristic used in read()
|
||||||
|
self._read(max(n, 1024))
|
||||||
except EOFError:
|
except EOFError:
|
||||||
pass
|
pass
|
||||||
offset = self.offset - self.extrastart
|
offset = self.offset - self.extrastart
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue