]> granicus.if.org Git - python/commitdiff
3.2 - Fix closes Issue6090 - Raise a ValueError, instead of failing with unrelated
authorSenthil Kumaran <senthil@uthcode.com>
Wed, 19 Oct 2011 17:46:00 +0000 (01:46 +0800)
committerSenthil Kumaran <senthil@uthcode.com>
Wed, 19 Oct 2011 17:46:00 +0000 (01:46 +0800)
exceptions, when a document with timestamp earlier than 1980 is provided to
zipfile. Patch contributed by  Petri Lehtinen.

Doc/library/zipfile.rst
Lib/test/test_zipfile.py
Lib/zipfile.py
Misc/NEWS

index 019a894721e8e4fb87ef7ce39843604ebc69756a..6f84bcce5705caa0da2266ba90e919e295ff5cc0 100644 (file)
@@ -398,7 +398,7 @@ Instances have the following attributes:
    +-------+--------------------------+
    | Index | Value                    |
    +=======+==========================+
-   | ``0`` | Year                     |
+   | ``0`` | Year (>= 1980)           |
    +-------+--------------------------+
    | ``1`` | Month (one-based)        |
    +-------+--------------------------+
@@ -411,6 +411,10 @@ Instances have the following attributes:
    | ``5`` | Seconds (zero-based)     |
    +-------+--------------------------+
 
+   .. note::
+
+      The ZIP file format does not support timestamps before 1980.
+
 
 .. attribute:: ZipInfo.compress_type
 
index 782020c6c84c1f000f30556b19c094cdf1087e3e..bb0d79ad4eeb53edf8a273414a818741ebb82ee8 100644 (file)
@@ -507,6 +507,13 @@ class TestsWithSourceFile(unittest.TestCase):
         except zipfile.BadZipFile:
             self.assertTrue(zipfp2.fp is None, 'zipfp is not closed')
 
+    def test_add_file_before_1980(self):
+        # Set atime and mtime to 1970-01-01
+        os.utime(TESTFN, (0, 0))
+        with zipfile.ZipFile(TESTFN2, "w") as zipfp:
+            self.assertRaises(ValueError, zipfp.write, TESTFN)
+
+
     @skipUnless(zlib, "requires zlib")
     def test_unicode_filenames(self):
         # bug #10801
@@ -1053,6 +1060,10 @@ class OtherTests(unittest.TestCase):
         f.close()
         self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN, 'r')
 
+    def test_create_zipinfo_before_1980(self):
+        self.assertRaises(ValueError,
+                          zipfile.ZipInfo, 'seventies', (1979, 1, 1, 0, 0, 0))
+
     def tearDown(self):
         unlink(TESTFN)
         unlink(TESTFN2)
index 5cc7816e0c383c53f1c6bf00ede26cb4da6e0124..6ca269fe175099ff1d218e1d071d269f916dcc31 100644 (file)
@@ -300,6 +300,10 @@ class ZipInfo (object):
 
         self.filename = filename        # Normalized file name
         self.date_time = date_time      # year, month, day, hour, min, sec
+
+        if date_time[0] < 1980:
+            raise ValueError('ZIP does not support timestamps before 1980')
+
         # Standard values:
         self.compress_type = ZIP_STORED # Type of compression for the file
         self.comment = b""              # Comment for each file
index c9804fc265fe8000dddd61d9d1791c9b089909c9..9b4d833fbd517e2c0753561f673f83c8eeb80725 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -54,6 +54,9 @@ Library
 - Issue #12448: smtplib now flushes stdout while running ``python -m smtplib``
   in order to display the prompt correctly.
 
+- Issue #6090: zipfile raises a ValueError when a document with a timestamp
+  earlier than 1980 is provided. Patch contributed by Petri Lehtinen.
+
 - Issue #13194: zlib.compressobj().copy() and zlib.decompressobj().copy() are
   now available on Windows.