[Python-checkins] cpython (2.7): Close #17666: Fix reading gzip files with an extra field.

serhiy.storchaka python-checkins at python.org
Mon Apr 8 21:39:39 CEST 2013


http://hg.python.org/cpython/rev/29f0836c0456
changeset:   83199:29f0836c0456
branch:      2.7
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Mon Apr 08 22:33:55 2013 +0300
summary:
  Close #17666: Fix reading gzip files with an extra field.

files:
  Lib/gzip.py           |  3 ++-
  Lib/test/test_gzip.py |  7 +++++++
  Misc/NEWS             |  2 ++
  3 files changed, 11 insertions(+), 1 deletions(-)


diff --git a/Lib/gzip.py b/Lib/gzip.py
--- a/Lib/gzip.py
+++ b/Lib/gzip.py
@@ -202,7 +202,8 @@
 
         if flag & FEXTRA:
             # Read & discard the extra field, if present
-            self._read_exact(struct.unpack("<H", self._read_exact(2)))
+            extra_len, = struct.unpack("<H", self._read_exact(2))
+            self._read_exact(extra_len)
         if flag & FNAME:
             # Read and discard a null-terminated string containing the filename
             while True:
diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py
--- a/Lib/test/test_gzip.py
+++ b/Lib/test/test_gzip.py
@@ -306,6 +306,13 @@
             with gzip.GzipFile(fileobj=io.BytesIO(truncated[:i])) as f:
                 self.assertRaises(EOFError, f.read, 1)
 
+    def test_read_with_extra(self):
+        # Gzip data with an extra field
+        gzdata = (b'\x1f\x8b\x08\x04\xb2\x17cQ\x02\xff'
+                  b'\x05\x00Extra'
+                  b'\x0bI-.\x01\x002\xd1Mx\x04\x00\x00\x00')
+        with gzip.GzipFile(fileobj=io.BytesIO(gzdata)) as f:
+            self.assertEqual(f.read(), b'Test')
 
 def test_main(verbose=None):
     test_support.run_unittest(TestGzip)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -14,6 +14,8 @@
 Library
 -------
 
+- Issue #17666: Fix reading gzip files with an extra field.
+
 - Issue #13150, #17512: sysconfig no longer parses the Makefile and config.h
   files when imported, instead doing it at build time.  This makes importing
   sysconfig faster and reduces Python startup time by 20%.

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list