summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2020-01-21 11:42:49 (GMT)
committerGitHub <noreply@github.com>2020-01-21 11:42:49 (GMT)
commitab0d8e356ecd351d55f89519a6a97a1e69c0dfab (patch)
tree96ebcbddf3f2738c89b56f24d04c40e7b71e0de3 /Lib/test
parent060ad2fc1535adc76f96be8269b4af0f14429161 (diff)
downloadcpython-ab0d8e356ecd351d55f89519a6a97a1e69c0dfab.zip
cpython-ab0d8e356ecd351d55f89519a6a97a1e69c0dfab.tar.gz
cpython-ab0d8e356ecd351d55f89519a6a97a1e69c0dfab.tar.bz2
bpo-39389: gzip: fix compression level metadata (GH-18077)
As described in RFC 1952, section 2.3.1, the XFL (eXtra FLags) byte of a gzip member header should indicate whether the DEFLATE algorithm was tuned for speed or compression ratio. Prior to this patch, archives emitted by the `gzip` module always indicated maximum compression. (cherry picked from commit eab3b3f1c60afecfb4db3c3619109684cb04bd60) Co-authored-by: William Chargin <wchargin@gmail.com>
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_gzip.py20
1 files changed, 20 insertions, 0 deletions
diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py
index 48a36a3..6468286 100644
--- a/Lib/test/test_gzip.py
+++ b/Lib/test/test_gzip.py
@@ -358,6 +358,26 @@ class TestGzip(BaseTest):
isizeBytes = fRead.read(4)
self.assertEqual(isizeBytes, struct.pack('<i', len(data1)))
+ def test_compresslevel_metadata(self):
+ # see RFC 1952: http://www.faqs.org/rfcs/rfc1952.html
+ # specifically, discussion of XFL in section 2.3.1
+ cases = [
+ ('fast', 1, b'\x04'),
+ ('best', 9, b'\x02'),
+ ('tradeoff', 6, b'\x00'),
+ ]
+ xflOffset = 8
+
+ for (name, level, expectedXflByte) in cases:
+ with self.subTest(name):
+ fWrite = gzip.GzipFile(self.filename, 'w', compresslevel=level)
+ with fWrite:
+ fWrite.write(data1)
+ with open(self.filename, 'rb') as fRead:
+ fRead.seek(xflOffset)
+ xflByte = fRead.read(1)
+ self.assertEqual(xflByte, expectedXflByte)
+
def test_with_open(self):
# GzipFile supports the context management protocol
with gzip.GzipFile(self.filename, "wb") as f: