diff options
author | William Chargin <wchargin@gmail.com> | 2020-01-21 11:25:24 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2020-01-21 11:25:24 (GMT) |
commit | eab3b3f1c60afecfb4db3c3619109684cb04bd60 (patch) | |
tree | 311e513af209b2fa14f0d46720801aa4e1f02462 /Lib/gzip.py | |
parent | 85ead4fc62829cb7ef2eb0af1a2933282f58c629 (diff) | |
download | cpython-eab3b3f1c60afecfb4db3c3619109684cb04bd60.zip cpython-eab3b3f1c60afecfb4db3c3619109684cb04bd60.tar.gz cpython-eab3b3f1c60afecfb4db3c3619109684cb04bd60.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.
Diffstat (limited to 'Lib/gzip.py')
-rw-r--r-- | Lib/gzip.py | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/Lib/gzip.py b/Lib/gzip.py index e60d8ad..e422773 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -209,7 +209,7 @@ class GzipFile(_compression.BaseStream): self.fileobj = fileobj if self.mode == WRITE: - self._write_gzip_header() + self._write_gzip_header(compresslevel) @property def filename(self): @@ -236,7 +236,7 @@ class GzipFile(_compression.BaseStream): self.bufsize = 0 self.offset = 0 # Current file offset for seek(), tell(), etc - def _write_gzip_header(self): + def _write_gzip_header(self, compresslevel): self.fileobj.write(b'\037\213') # magic header self.fileobj.write(b'\010') # compression method try: @@ -257,7 +257,13 @@ class GzipFile(_compression.BaseStream): if mtime is None: mtime = time.time() write32u(self.fileobj, int(mtime)) - self.fileobj.write(b'\002') + if compresslevel == _COMPRESS_LEVEL_BEST: + xfl = b'\002' + elif compresslevel == _COMPRESS_LEVEL_FAST: + xfl = b'\004' + else: + xfl = b'\000' + self.fileobj.write(xfl) self.fileobj.write(b'\377') if fname: self.fileobj.write(fname + b'\000') |