diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2010-01-13 14:32:10 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2010-01-13 14:32:10 (GMT) |
commit | 5a9112c0cc33614c284c18a6f622a32c97b7ae3d (patch) | |
tree | 99f33d8df4116a351e721dd0abfc10f07b9e6330 | |
parent | 10042922d9dbb25c6e8b63698c34b6f3943a8cf1 (diff) | |
download | cpython-5a9112c0cc33614c284c18a6f622a32c97b7ae3d.zip cpython-5a9112c0cc33614c284c18a6f622a32c97b7ae3d.tar.gz cpython-5a9112c0cc33614c284c18a6f622a32c97b7ae3d.tar.bz2 |
Issue #2846: Add support for gzip.GzipFile reading zero-padded files.
Patch by Brian Curtin.
-rw-r--r-- | Doc/library/gzip.rst | 3 | ||||
-rw-r--r-- | Lib/gzip.py | 9 | ||||
-rw-r--r-- | Lib/test/test_gzip.py | 12 | ||||
-rw-r--r-- | Misc/NEWS | 3 |
4 files changed, 27 insertions, 0 deletions
diff --git a/Doc/library/gzip.rst b/Doc/library/gzip.rst index c76bae8..b789a3c 100644 --- a/Doc/library/gzip.rst +++ b/Doc/library/gzip.rst @@ -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]]) diff --git a/Lib/gzip.py b/Lib/gzip.py index 26f4354..13f2ca2 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -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 diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py index 60094dc..b690134 100644 --- a/Lib/test/test_gzip.py +++ b/Lib/test/test_gzip.py @@ -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) @@ -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. |