Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 29fa9d4

Browse files
committed
3.2 - Fix closes Issue6090 - Raise a ValueError, instead of failing with unrelated
exceptions, when a document with timestamp earlier than 1980 is provided to zipfile. Patch contributed by Petri Lehtinen.
1 parent 1ef0c03 commit 29fa9d4

4 files changed

Lines changed: 23 additions & 1 deletion

File tree

Doc/library/zipfile.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ Instances have the following attributes:
398398
+-------+--------------------------+
399399
| Index | Value |
400400
+=======+==========================+
401-
| ``0`` | Year |
401+
| ``0`` | Year (>= 1980) |
402402
+-------+--------------------------+
403403
| ``1`` | Month (one-based) |
404404
+-------+--------------------------+
@@ -411,6 +411,10 @@ Instances have the following attributes:
411411
| ``5`` | Seconds (zero-based) |
412412
+-------+--------------------------+
413413

414+
.. note::
415+
416+
The ZIP file format does not support timestamps before 1980.
417+
414418

415419
.. attribute:: ZipInfo.compress_type
416420

Lib/test/test_zipfile.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,13 @@ def test_close_on_exception(self):
507507
except zipfile.BadZipFile:
508508
self.assertTrue(zipfp2.fp is None, 'zipfp is not closed')
509509

510+
def test_add_file_before_1980(self):
511+
# Set atime and mtime to 1970-01-01
512+
os.utime(TESTFN, (0, 0))
513+
with zipfile.ZipFile(TESTFN2, "w") as zipfp:
514+
self.assertRaises(ValueError, zipfp.write, TESTFN)
515+
516+
510517
@skipUnless(zlib, "requires zlib")
511518
def test_unicode_filenames(self):
512519
# bug #10801
@@ -1053,6 +1060,10 @@ def test_open_empty_file(self):
10531060
f.close()
10541061
self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN, 'r')
10551062

1063+
def test_create_zipinfo_before_1980(self):
1064+
self.assertRaises(ValueError,
1065+
zipfile.ZipInfo, 'seventies', (1979, 1, 1, 0, 0, 0))
1066+
10561067
def tearDown(self):
10571068
unlink(TESTFN)
10581069
unlink(TESTFN2)

Lib/zipfile.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,10 @@ def __init__(self, filename="NoName", date_time=(1980,1,1,0,0,0)):
300300

301301
self.filename = filename # Normalized file name
302302
self.date_time = date_time # year, month, day, hour, min, sec
303+
304+
if date_time[0] < 1980:
305+
raise ValueError('ZIP does not support timestamps before 1980')
306+
303307
# Standard values:
304308
self.compress_type = ZIP_STORED # Type of compression for the file
305309
self.comment = b"" # Comment for each file

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ Library
5454
- Issue #12448: smtplib now flushes stdout while running ``python -m smtplib``
5555
in order to display the prompt correctly.
5656

57+
- Issue #6090: zipfile raises a ValueError when a document with a timestamp
58+
earlier than 1980 is provided. Patch contributed by Petri Lehtinen.
59+
5760
- Issue #13194: zlib.compressobj().copy() and zlib.decompressobj().copy() are
5861
now available on Windows.
5962

0 commit comments

Comments
 (0)