]> granicus.if.org Git - python/commitdiff
Merged revisions 77472-77473 via svnmerge from
authorAntoine Pitrou <solipsis@pitrou.net>
Wed, 13 Jan 2010 14:37:26 +0000 (14:37 +0000)
committerAntoine Pitrou <solipsis@pitrou.net>
Wed, 13 Jan 2010 14:37:26 +0000 (14:37 +0000)
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r77472 | antoine.pitrou | 2010-01-13 15:32:10 +0100 (mer., 13 janv. 2010) | 5 lines

  Issue #2846: Add support for gzip.GzipFile reading zero-padded files.
  Patch by Brian Curtin.
........
  r77473 | antoine.pitrou | 2010-01-13 15:32:51 +0100 (mer., 13 janv. 2010) | 3 lines

  Add ACKS entry for r77472.
........

Doc/library/gzip.rst
Lib/gzip.py
Lib/test/test_gzip.py
Misc/ACKS
Misc/NEWS

index 1f644286c9c9cf2d8b094bb21c5c7c68b2fd310b..a8305936157f6b94292f2698fd9bae699449a004 100644 (file)
@@ -72,6 +72,9 @@ The module defines the following items:
    .. versionchanged:: 3.1
       Support for the :keyword:`with` statement was added.
 
+   .. versionchanged:: 3.2
+      Support for zero-padded files was added.
+
 
 .. function:: open(filename, mode='rb', compresslevel=9)
 
index 66fc88daa0d442c1253e1479d067171c6bcd0140..ef6befcb7d3f2eee1309941018796733eacf978e 100644 (file)
@@ -348,6 +348,15 @@ class GzipFile(io.BufferedIOBase):
         elif isize != (self.size & 0xffffffff):
             raise IOError("Incorrect length of data produced")
 
+        # Gzip files can be padded with zeroes and still have archives.
+        # Consume all zero bytes and set the file position to the first
+        # non-zero byte. See http://www.gzip.org/#faq8
+        c = b"\x00"
+        while c == b"\x00":
+            c = self.fileobj.read(1)
+        if c:
+            self.fileobj.seek(-1, 1)
+
     @property
     def closed(self):
         return self.fileobj is None
index 320adfda8a0c2f7c8461b15f8b0f4076500889a2..429ada0dcd406f82604d04187982396f05fd54c4 100644 (file)
@@ -253,6 +253,18 @@ class TestGzip(unittest.TestCase):
         else:
             self.fail("1/0 didn't raise an exception")
 
+    def test_zero_padded_file(self):
+        with gzip.GzipFile(self.filename, "wb") as f:
+            f.write(data1 * 50)
+
+        # Pad the file with zeroes
+        with open(self.filename, "ab") as f:
+            f.write(b"\x00" * 50)
+
+        with gzip.GzipFile(self.filename, "rb") as f:
+            d = f.read()
+            self.assertEqual(d, data1 * 50, "Incorrect data in file")
+
 def test_main(verbose=None):
     support.run_unittest(TestGzip)
 
index 72e2096c22cd9999fdc484d9391d42d6e848aeee..f0c667a57c885643c963374fea3ba74dd9f9b84a 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -164,6 +164,7 @@ Simon Cross
 Drew Csillag
 John Cugini
 Tom Culliton
+Brian Curtin
 Lisandro Dalcin
 Andrew Dalke
 Lars Damerow
index 3e4073d43d0bda3b2dac9c466b836fa2c1c165a2..a577e65a239c089a2f5e6b3d1dec2a526305ffdc 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -213,6 +213,9 @@ C-API
 Library
 -------
 
+- Issue #2846: Add support for gzip.GzipFile reading zero-padded files.
+  Patch by Brian Curtin.
+
 - Issue #7681: Use floor division in appropiate places in the wave module.
 
 - Issue #5372: Drop the reuse of .o files in Distutils' ccompiler (since