Skip to content

Commit 52acf9e

Browse files
Fix also gzip.compress().
1 parent ff424fc commit 52acf9e

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

Lib/gzip.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,8 @@ def compress(data, compresslevel=_COMPRESS_LEVEL_TRADEOFF, *, mtime=0):
668668
gzip_data = zlib.compress(data, level=compresslevel, wbits=31)
669669
if mtime is None:
670670
mtime = time.time()
671+
if not 0 <= mtime < 2**32:
672+
mtime = 0
671673
# Reuse gzip header created by zlib, replace mtime and OS byte for
672674
# consistency.
673675
header = struct.pack("<4sLBB", gzip_data, int(mtime), gzip_data[8], 255)

Lib/test/test_gzip.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,15 +356,19 @@ def test_mtime_out_of_range(self):
356356
with gzip.GzipFile(self.filename, 'w', mtime=mtime) as fWrite:
357357
fWrite.write(data1)
358358
with gzip.GzipFile(self.filename) as fRead:
359-
fRead.read()
359+
fRead.read(1)
360+
self.assertEqual(fRead.mtime, 0)
361+
datac = gzip.compress(data1, mtime=mtime)
362+
with gzip.GzipFile(fileobj=io.BytesIO(datac)) as fRead:
363+
fRead.read(1)
360364
self.assertEqual(fRead.mtime, 0)
361365

362366
for mtime in (-1, 2**32):
363367
with mock.patch('time.time', return_value=float(mtime)):
364368
with gzip.GzipFile(self.filename, 'w') as fWrite:
365369
fWrite.write(data1)
366370
with gzip.GzipFile(self.filename) as fRead:
367-
fRead.read()
371+
fRead.read(1)
368372
self.assertEqual(fRead.mtime, 0)
369373

370374
def test_metadata(self):

0 commit comments

Comments
 (0)