@@ -188,8 +188,10 @@ def __init__(self, filename=None, mode=None,
188188
189189 The optional mtime argument is the timestamp requested by gzip. The time
190190 is in Unix format, i.e., seconds since 00:00:00 UTC, January 1, 1970.
191- If mtime is omitted or None, the current time is used. Use mtime = 0
192- to generate a compressed stream that does not depend on creation time.
191+ Set mtime to 0 to generate a compressed stream that does not depend on
192+ creation time. If mtime is omitted or None, the current time is used.
193+ If the resulting mtime is outside the range 0 to 2**32-1, then the
194+ value 0 is used instead.
193195
194196 """
195197
@@ -295,6 +297,8 @@ def _write_gzip_header(self, compresslevel):
295297 mtime = self ._write_mtime
296298 if mtime is None :
297299 mtime = time .time ()
300+ if not 0 <= mtime < 2 ** 32 :
301+ mtime = 0
298302 write32u (self .fileobj , int (mtime ))
299303 if compresslevel == _COMPRESS_LEVEL_BEST :
300304 xfl = b'\002 '
@@ -663,6 +667,8 @@ def compress(data, compresslevel=_COMPRESS_LEVEL_TRADEOFF, *, mtime=0):
663667 gzip_data = zlib .compress (data , level = compresslevel , wbits = 31 )
664668 if mtime is None :
665669 mtime = time .time ()
670+ if not 0 <= mtime < 2 ** 32 :
671+ mtime = 0
666672 # Reuse gzip header created by zlib, replace mtime and OS byte for
667673 # consistency.
668674 header = struct .pack ("<4sLBB" , gzip_data , int (mtime ), gzip_data [8 ], 255 )
0 commit comments