]> granicus.if.org Git - python/commitdiff
Fix closes Issue6090 - Raise a ValueError, instead of failing with unrelated
authorSenthil Kumaran <senthil@uthcode.com>
Wed, 19 Oct 2011 17:38:35 +0000 (01:38 +0800)
committerSenthil Kumaran <senthil@uthcode.com>
Wed, 19 Oct 2011 17:38:35 +0000 (01:38 +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 30a03614ba53873ed7090e9d8bbd9f94f7d2a061..14e40c8b787480095b34d2f561e1442f4886d1dc 100644 (file)
@@ -388,7 +388,7 @@ Instances have the following attributes:
    +-------+--------------------------+
    | Index | Value                    |
    +=======+==========================+
-   | ``0`` | Year                     |
+   | ``0`` | Year (>= 1980)           |
    +-------+--------------------------+
    | ``1`` | Month (one-based)        |
    +-------+--------------------------+
@@ -401,6 +401,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 6cba26c2a1380b379a691809216fd8297f6698d2..7ebb66376d80fdb43ce76e81d4881e9243b1d557 100644 (file)
@@ -477,6 +477,12 @@ 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)
+
     def tearDown(self):
         unlink(TESTFN)
         unlink(TESTFN2)
@@ -990,6 +996,10 @@ class OtherTests(unittest.TestCase):
             pass
         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 f876f428ae49252f33336136bd0eb7b79f62ccdb..d9181f2393fc4a0ee39c75f97f7413cc4730f3dd 100644 (file)
@@ -290,6 +290,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 = ""               # Comment for each file
index 03bbf054e49ed368287bd21ddb8335f101733dfb..1a0f6fe67b6510f1299735c278f369edd4497234 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -63,6 +63,9 @@ Core and Builtins
 Library
 -------
 
+- 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.