mirror of https://github.com/python/cpython
gh-112346: Always set OS byte to 255, simpler gzip.compress function. (GH-120486)
This matches the output behavior in 3.10 and earlier; the optimization in 3.11 allowed the zlib library's "os" value to be filled in instead in the circumstance when mtime was 0. this keeps things consistent.
This commit is contained in:
parent
31d1d72d7e
commit
08d09cf5ba
|
@ -188,9 +188,7 @@ The module defines the following items:
|
||||||
|
|
||||||
Compress the *data*, returning a :class:`bytes` object containing
|
Compress the *data*, returning a :class:`bytes` object containing
|
||||||
the compressed data. *compresslevel* and *mtime* have the same meaning as in
|
the compressed data. *compresslevel* and *mtime* have the same meaning as in
|
||||||
the :class:`GzipFile` constructor above. When *mtime* is set to ``0``, this
|
the :class:`GzipFile` constructor above.
|
||||||
function is equivalent to :func:`zlib.compress` with *wbits* set to ``31``.
|
|
||||||
The zlib function is faster.
|
|
||||||
|
|
||||||
.. versionadded:: 3.2
|
.. versionadded:: 3.2
|
||||||
.. versionchanged:: 3.8
|
.. versionchanged:: 3.8
|
||||||
|
@ -200,6 +198,10 @@ The module defines the following items:
|
||||||
streamed fashion. Calls with *mtime* set to ``0`` are delegated to
|
streamed fashion. Calls with *mtime* set to ``0`` are delegated to
|
||||||
:func:`zlib.compress` for better speed.
|
:func:`zlib.compress` for better speed.
|
||||||
|
|
||||||
|
.. versionchanged:: 3.13
|
||||||
|
The gzip header OS byte is guaranteed to be set to 255 when this function
|
||||||
|
is used as was the case in 3.10 and earlier.
|
||||||
|
|
||||||
.. function:: decompress(data)
|
.. function:: decompress(data)
|
||||||
|
|
||||||
Decompress the *data*, returning a :class:`bytes` object containing the
|
Decompress the *data*, returning a :class:`bytes` object containing the
|
||||||
|
|
38
Lib/gzip.py
38
Lib/gzip.py
|
@ -580,27 +580,6 @@ class _GzipReader(_compression.DecompressReader):
|
||||||
self._new_member = True
|
self._new_member = True
|
||||||
|
|
||||||
|
|
||||||
def _create_simple_gzip_header(compresslevel: int,
|
|
||||||
mtime = None) -> bytes:
|
|
||||||
"""
|
|
||||||
Write a simple gzip header with no extra fields.
|
|
||||||
:param compresslevel: Compresslevel used to determine the xfl bytes.
|
|
||||||
:param mtime: The mtime (must support conversion to a 32-bit integer).
|
|
||||||
:return: A bytes object representing the gzip header.
|
|
||||||
"""
|
|
||||||
if mtime is None:
|
|
||||||
mtime = time.time()
|
|
||||||
if compresslevel == _COMPRESS_LEVEL_BEST:
|
|
||||||
xfl = 2
|
|
||||||
elif compresslevel == _COMPRESS_LEVEL_FAST:
|
|
||||||
xfl = 4
|
|
||||||
else:
|
|
||||||
xfl = 0
|
|
||||||
# Pack ID1 and ID2 magic bytes, method (8=deflate), header flags (no extra
|
|
||||||
# fields added to header), mtime, xfl and os (255 for unknown OS).
|
|
||||||
return struct.pack("<BBBBLBB", 0x1f, 0x8b, 8, 0, int(mtime), xfl, 255)
|
|
||||||
|
|
||||||
|
|
||||||
def compress(data, compresslevel=_COMPRESS_LEVEL_BEST, *, mtime=None):
|
def compress(data, compresslevel=_COMPRESS_LEVEL_BEST, *, mtime=None):
|
||||||
"""Compress data in one shot and return the compressed string.
|
"""Compress data in one shot and return the compressed string.
|
||||||
|
|
||||||
|
@ -608,15 +587,14 @@ def compress(data, compresslevel=_COMPRESS_LEVEL_BEST, *, mtime=None):
|
||||||
mtime can be used to set the modification time. The modification time is
|
mtime can be used to set the modification time. The modification time is
|
||||||
set to the current time by default.
|
set to the current time by default.
|
||||||
"""
|
"""
|
||||||
if mtime == 0:
|
# Wbits=31 automatically includes a gzip header and trailer.
|
||||||
# Use zlib as it creates the header with 0 mtime by default.
|
gzip_data = zlib.compress(data, level=compresslevel, wbits=31)
|
||||||
# This is faster and with less overhead.
|
if mtime is None:
|
||||||
return zlib.compress(data, level=compresslevel, wbits=31)
|
mtime = time.time()
|
||||||
header = _create_simple_gzip_header(compresslevel, mtime)
|
# Reuse gzip header created by zlib, replace mtime and OS byte for
|
||||||
trailer = struct.pack("<LL", zlib.crc32(data), (len(data) & 0xffffffff))
|
# consistency.
|
||||||
# Wbits=-15 creates a raw deflate block.
|
header = struct.pack("<4sLBB", gzip_data, int(mtime), gzip_data[8], 255)
|
||||||
return (header + zlib.compress(data, level=compresslevel, wbits=-15) +
|
return header + gzip_data[10:]
|
||||||
trailer)
|
|
||||||
|
|
||||||
|
|
||||||
def decompress(data):
|
def decompress(data):
|
||||||
|
|
|
@ -714,7 +714,6 @@ class TestGzip(BaseTest):
|
||||||
self.assertEqual(f.mtime, mtime)
|
self.assertEqual(f.mtime, mtime)
|
||||||
|
|
||||||
def test_compress_correct_level(self):
|
def test_compress_correct_level(self):
|
||||||
# gzip.compress calls with mtime == 0 take a different code path.
|
|
||||||
for mtime in (0, 42):
|
for mtime in (0, 42):
|
||||||
with self.subTest(mtime=mtime):
|
with self.subTest(mtime=mtime):
|
||||||
nocompress = gzip.compress(data1, compresslevel=0, mtime=mtime)
|
nocompress = gzip.compress(data1, compresslevel=0, mtime=mtime)
|
||||||
|
@ -722,6 +721,17 @@ class TestGzip(BaseTest):
|
||||||
self.assertIn(data1, nocompress)
|
self.assertIn(data1, nocompress)
|
||||||
self.assertNotIn(data1, yescompress)
|
self.assertNotIn(data1, yescompress)
|
||||||
|
|
||||||
|
def test_issue112346(self):
|
||||||
|
# The OS byte should be 255, this should not change between Python versions.
|
||||||
|
for mtime in (0, 42):
|
||||||
|
with self.subTest(mtime=mtime):
|
||||||
|
compress = gzip.compress(data1, compresslevel=1, mtime=mtime)
|
||||||
|
self.assertEqual(
|
||||||
|
struct.unpack("<IxB", compress[4:10]),
|
||||||
|
(mtime, 255),
|
||||||
|
"Gzip header does not properly set either mtime or OS byte."
|
||||||
|
)
|
||||||
|
|
||||||
def test_decompress(self):
|
def test_decompress(self):
|
||||||
for data in (data1, data2):
|
for data in (data1, data2):
|
||||||
buf = io.BytesIO()
|
buf = io.BytesIO()
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
The OS byte in gzip headers is now always set to 255 when using
|
||||||
|
:func:`gzip.compress`.
|
Loading…
Reference in New Issue