Skip to content

Commit ff424fc

Browse files
Reset out-of-range mtime to 0 even if explicitly provided.
1 parent 79686b9 commit ff424fc

2 files changed

Lines changed: 8 additions & 15 deletions

File tree

Lib/gzip.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,6 @@ def __init__(self, filename=None, mode=None,
206206
if mode and 'b' not in mode:
207207
mode += 'b'
208208

209-
if mtime is not None and (mtime < 0 or mtime >= 2**32):
210-
raise ValueError(f'mtime must be in the range 0 through {2**32-1}')
211-
212209
try:
213210
if fileobj is None:
214211
fileobj = self.myfileobj = builtins.open(filename, mode or 'rb')
@@ -301,8 +298,8 @@ def _write_gzip_header(self, compresslevel):
301298
mtime = self._write_mtime
302299
if mtime is None:
303300
mtime = time.time()
304-
if mtime < 0 or mtime >= 2**32:
305-
mtime = 0
301+
if not 0 <= mtime < 2**32:
302+
mtime = 0
306303
write32u(self.fileobj, int(mtime))
307304
if compresslevel == _COMPRESS_LEVEL_BEST:
308305
xfl = b'\002'

Lib/test/test_gzip.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -352,17 +352,13 @@ def test_mtime(self):
352352
self.assertEqual(fRead.mtime, mtime)
353353

354354
def test_mtime_out_of_range(self):
355-
# ValueError should be raised when mtime<0 or mtime>=2**32 and is
356-
# explicitly specified
357-
with self.assertRaises(ValueError):
358-
with gzip.GzipFile(self.filename, 'w', mtime=-1) as fWrite:
359-
pass
360-
with self.assertRaises(ValueError):
361-
with gzip.GzipFile(self.filename, 'w', mtime=2**32) as fWrite:
362-
pass
355+
for mtime in (-1, 2**32):
356+
with gzip.GzipFile(self.filename, 'w', mtime=mtime) as fWrite:
357+
fWrite.write(data1)
358+
with gzip.GzipFile(self.filename) as fRead:
359+
fRead.read()
360+
self.assertEqual(fRead.mtime, 0)
363361

364-
# mtime should be set to 0 when time.time() is out of range and mtime is
365-
# not explicitly given
366362
for mtime in (-1, 2**32):
367363
with mock.patch('time.time', return_value=float(mtime)):
368364
with gzip.GzipFile(self.filename, 'w') as fWrite:

0 commit comments

Comments
 (0)