]> granicus.if.org Git - python/commitdiff
Issue #2846: Add support for gzip.GzipFile reading zero-padded files.
authorAntoine Pitrou <solipsis@pitrou.net>
Wed, 13 Jan 2010 14:32:10 +0000 (14:32 +0000)
committerAntoine Pitrou <solipsis@pitrou.net>
Wed, 13 Jan 2010 14:32:10 +0000 (14:32 +0000)
Patch by Brian Curtin.

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

index c76bae869e6d0633d8cee5d7d387917f2ae4eda2..b789a3cb148dee9c381c41ad0ce948717a052f3d 100644 (file)
@@ -72,6 +72,9 @@ The module defines the following items:
    .. versionchanged:: 2.7
       Support for the :keyword:`with` statement was added.
 
+   .. versionchanged:: 2.7
+      Support for zero-padded files was added.
+
 
 .. function:: open(filename[, mode[, compresslevel]])
 
index 26f435456b6e0897da9d8c5827ffe14d27f061ba..13f2ca24b77ec10f3084ceb0183bb511f0fd60b0 100644 (file)
@@ -330,6 +330,15 @@ class GzipFile(io.BufferedIOBase):
         elif isize != (self.size & 0xffffffffL):
             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 = "\x00"
+        while c == "\x00":
+            c = self.fileobj.read(1)
+        if c:
+            self.fileobj.seek(-1, 1)
+
     @property
     def closed(self):
         return self.fileobj is None
index 60094dceb1a1f0229c0d16cd8abbbd4e71c9f982..b6901343eb893728d82c4c8b5eefb9e614b8852f 100644 (file)
@@ -252,6 +252,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("\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):
     test_support.run_unittest(TestGzip)
 
index 82f3d92ec23219046e28fc2c566713604937eede..312a2a912987c8397b7473f1e874fd33fd987e86 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -32,6 +32,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #2846: Add support for gzip.GzipFile reading zero-padded files.
+  Patch by Brian Curtin.
+
 - Issue #5827: Make sure that normpath preserves unicode.  Initial patch
   by Matt Giuca.