]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Merged revisions 85455 via svnmerge from
authorGeorg Brandl <georg@python.org>
Fri, 26 Nov 2010 07:22:28 +0000 (07:22 +0000)
committerGeorg Brandl <georg@python.org>
Fri, 26 Nov 2010 07:22:28 +0000 (07:22 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r85455 | georg.brandl | 2010-10-14 08:59:45 +0200 (Do, 14 Okt 2010) | 1 line

  #1710703: write zipfile structures also in the case of closing a new, but empty, archive.
........

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

index 33f58b25ec59e301ff6bfe9c2942fadb5dd96aeb..bd6c3a6af1d976ac6f300c8e4c3016f5fa3ea740 100644 (file)
@@ -122,6 +122,10 @@ ZipFile Objects
    and :program:`unzip` commands on Unix (the InfoZIP utilities) don't support
    these extensions.
 
+   If the file is created with mode ``'a'`` or ``'w'`` and then
+   :meth:`close`\ d without adding any files to the archive, the appropriate
+   ZIP structures for an empty archive will be written to the file.
+
    ZipFile is also a context manager and therefore supports the
    :keyword:`with` statement.  In the example, *myzip* is closed after the
    :keyword:`with` statement's suite is finished---even if an exception occurs::
index aee499ca73550a4476b9a68bf9899917d15aa739..ddd713f974c01395b181cad4e58f575a9681756c 100644 (file)
@@ -949,6 +949,31 @@ class OtherTests(unittest.TestCase):
     def test_read_return_size_deflated(self):
         self.check_read_return_size(zipfile.ZIP_DEFLATED)
 
+    def test_empty_zipfile(self):
+        # Check that creating a file in 'w' or 'a' mode and closing without
+        # adding any files to the archives creates a valid empty ZIP file
+        zipf = zipfile.ZipFile(TESTFN, mode="w")
+        zipf.close()
+        try:
+            zipf = zipfile.ZipFile(TESTFN, mode="r")
+        except zipfile.BadZipFile:
+            self.fail("Unable to create empty ZIP file in 'w' mode")
+
+        zipf = zipfile.ZipFile(TESTFN, mode="a")
+        zipf.close()
+        try:
+            zipf = zipfile.ZipFile(TESTFN, mode="r")
+        except:
+            self.fail("Unable to create empty ZIP file in 'a' mode")
+
+    def test_open_empty_file(self):
+        # Issue 1710703: Check that opening a file with less than 22 bytes
+        # raises a BadZipfile exception (rather than the previously unhelpful
+        # IOError)
+        f = open(TESTFN, 'w')
+        f.close()
+        self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN, 'r')
+
     def tearDown(self):
         unlink(TESTFN)
         unlink(TESTFN2)
index 496cf83fda6f6cd4c0762e17d71341fc1481b6bf..7e3caf0aa840d38064080fe425573090f03e35aa 100644 (file)
@@ -158,7 +158,13 @@ def _EndRecData64(fpin, offset, endrec):
     """
     Read the ZIP64 end-of-archive records and use that to update endrec
     """
-    fpin.seek(offset - sizeEndCentDir64Locator, 2)
+    try:
+        fpin.seek(offset - sizeEndCentDir64Locator, 2)
+    except IOError:
+        # If the seek fails, the file is not large enough to contain a ZIP64
+        # end-of-archive record, so just return the end record we were given.
+        return endrec
+
     data = fpin.read(sizeEndCentDir64Locator)
     sig, diskno, reloff, disks = struct.unpack(structEndArchive64Locator, data)
     if sig != stringEndArchive64Locator:
@@ -705,14 +711,22 @@ class ZipFile:
         if key == 'r':
             self._GetContents()
         elif key == 'w':
-            pass
+            # set the modified flag so central directory gets written
+            # even if no files are added to the archive
+            self._didModify = True
         elif key == 'a':
-            try:                        # See if file is a zip file
+            try:
+                # See if file is a zip file
                 self._RealGetContents()
                 # seek to start of directory and overwrite
                 self.fp.seek(self.start_dir, 0)
-            except BadZipfile:          # file is not a zip file, just append
+            except BadZipfile:
+                # file is not a zip file, just append
                 self.fp.seek(0, 2)
+
+                # set the modified flag so central directory gets written
+                # even if no files are added to the archive
+                self._didModify = True
         else:
             if not self._filePassed:
                 self.fp.close()
@@ -739,7 +753,10 @@ class ZipFile:
     def _RealGetContents(self):
         """Read in the table of contents for the ZIP file."""
         fp = self.fp
-        endrec = _EndRecData(fp)
+        try:
+            endrec = _EndRecData(fp)
+        except IOError:
+            raise BadZipfile("File is not a zip file")
         if not endrec:
             raise BadZipfile, "File is not a zip file"
         if self.debug > 1:
index 1f16cf52ff887f585f87b5f76d9b0d2505d30f91..4a9ea8e82930f1e758f3945562281f201b54352c 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -15,6 +15,10 @@ Library
 
 - Issue #10459: Update CJK character names to Unicode 5.2.
 
+- Issue #1710703: Write structures for an empty ZIP archive when a ZipFile is
+  created in modes 'a' or 'w' and then closed without adding any files. Raise
+  BadZipfile (rather than IOError) when opening small non-ZIP files.
+
 - Issue #4493: urllib2 adds '/' in front of path components which does not
   start with '/. Common behavior exhibited by browsers and other clients.