File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments