gh-91349: Adjust default compression level to 6 (down from 9) in gzip and tarfile (GH-131470)
Some checks are pending
Tests / (push) Blocked by required conditions
Tests / Windows MSI (push) Blocked by required conditions
Tests / Change detection (push) Waiting to run
Tests / Docs (push) Blocked by required conditions
Tests / Check if Autoconf files are up to date (push) Blocked by required conditions
Tests / Check if generated files are up to date (push) Blocked by required conditions
Tests / Ubuntu SSL tests with OpenSSL (push) Blocked by required conditions
Tests / Ubuntu SSL tests with AWS-LC (push) Blocked by required conditions
Tests / WASI (push) Blocked by required conditions
Tests / Hypothesis tests on Ubuntu (push) Blocked by required conditions
Tests / Address sanitizer (push) Blocked by required conditions
Tests / Undefined behavior sanitizer (push) Blocked by required conditions
Tests / Cross build Linux (push) Blocked by required conditions
Tests / CIFuzz (push) Blocked by required conditions
Tests / All required checks pass (push) Blocked by required conditions
Lint / lint (push) Waiting to run
mypy / Run mypy on Lib/_pyrepl (push) Waiting to run
mypy / Run mypy on Lib/test/libregrtest (push) Waiting to run
mypy / Run mypy on Lib/tomllib (push) Waiting to run
mypy / Run mypy on Tools/build (push) Waiting to run
mypy / Run mypy on Tools/cases_generator (push) Waiting to run
mypy / Run mypy on Tools/clinic (push) Waiting to run
mypy / Run mypy on Tools/jit (push) Waiting to run
mypy / Run mypy on Tools/peg_generator (push) Waiting to run

gh-91349: Adjust default compression level to 6 (down from 9) in gzip and tarfile

It is the default level used by most compression tools and a
better tradeoff between speed and performance.

Co-authored-by: rmorotti <romain.morotti@man.com>
Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
This commit is contained in:
morotti 2025-07-20 17:43:40 +01:00 committed by GitHub
parent 958657bbc3
commit e24c66d55a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 31 additions and 10 deletions

View file

@ -26,7 +26,7 @@ Note that additional file formats which can be decompressed by the
The module defines the following items: The module defines the following items:
.. function:: open(filename, mode='rb', compresslevel=9, encoding=None, errors=None, newline=None) .. function:: open(filename, mode='rb', compresslevel=6, encoding=None, errors=None, newline=None)
Open a gzip-compressed file in binary or text mode, returning a :term:`file Open a gzip-compressed file in binary or text mode, returning a :term:`file
object`. object`.
@ -59,6 +59,11 @@ The module defines the following items:
.. versionchanged:: 3.6 .. versionchanged:: 3.6
Accepts a :term:`path-like object`. Accepts a :term:`path-like object`.
.. versionchanged:: next
The default compression level was reduced to 6 (down from 9).
It is the default level used by most compression tools and a better
tradeoff between speed and performance.
.. exception:: BadGzipFile .. exception:: BadGzipFile
An exception raised for invalid gzip files. It inherits from :exc:`OSError`. An exception raised for invalid gzip files. It inherits from :exc:`OSError`.
@ -67,7 +72,7 @@ The module defines the following items:
.. versionadded:: 3.8 .. versionadded:: 3.8
.. class:: GzipFile(filename=None, mode=None, compresslevel=9, fileobj=None, mtime=None) .. class:: GzipFile(filename=None, mode=None, compresslevel=6, fileobj=None, mtime=None)
Constructor for the :class:`GzipFile` class, which simulates most of the Constructor for the :class:`GzipFile` class, which simulates most of the
methods of a :term:`file object`, with the exception of the :meth:`~io.IOBase.truncate` methods of a :term:`file object`, with the exception of the :meth:`~io.IOBase.truncate`
@ -181,8 +186,13 @@ The module defines the following items:
Remove the ``filename`` attribute, use the :attr:`~GzipFile.name` Remove the ``filename`` attribute, use the :attr:`~GzipFile.name`
attribute instead. attribute instead.
.. versionchanged:: next
The default compression level was reduced to 6 (down from 9).
It is the default level used by most compression tools and a better
tradeoff between speed and performance.
.. function:: compress(data, compresslevel=9, *, mtime=0)
.. function:: compress(data, compresslevel=6, *, mtime=0)
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
@ -206,6 +216,10 @@ The module defines the following items:
The *mtime* parameter now defaults to 0 for reproducible output. The *mtime* parameter now defaults to 0 for reproducible output.
For the previous behaviour of using the current time, For the previous behaviour of using the current time,
pass ``None`` to *mtime*. pass ``None`` to *mtime*.
.. versionchanged:: next
The default compression level was reduced to 6 (down from 9).
It is the default level used by most compression tools and a better
tradeoff between speed and performance.
.. function:: decompress(data) .. function:: decompress(data)

View file

@ -123,7 +123,7 @@ Some facts and figures:
For modes ``'w:gz'``, ``'x:gz'``, ``'w|gz'``, ``'w:bz2'``, ``'x:bz2'``, For modes ``'w:gz'``, ``'x:gz'``, ``'w|gz'``, ``'w:bz2'``, ``'x:bz2'``,
``'w|bz2'``, :func:`tarfile.open` accepts the keyword argument ``'w|bz2'``, :func:`tarfile.open` accepts the keyword argument
*compresslevel* (default ``9``) to specify the compression level of the file. *compresslevel* (default ``6``) to specify the compression level of the file.
For modes ``'w:xz'``, ``'x:xz'`` and ``'w|xz'``, :func:`tarfile.open` accepts the For modes ``'w:xz'``, ``'x:xz'`` and ``'w|xz'``, :func:`tarfile.open` accepts the
keyword argument *preset* to specify the compression level of the file. keyword argument *preset* to specify the compression level of the file.
@ -198,6 +198,10 @@ Some facts and figures:
.. versionchanged:: 3.14 .. versionchanged:: 3.14
The *preset* keyword argument also works for streams. The *preset* keyword argument also works for streams.
.. versionchanged:: next
The default compression level was reduced to 6 (down from 9).
It is the default level used by most compression tools and a better
tradeoff between speed and performance.
.. class:: TarFile .. class:: TarFile
:noindex: :noindex:

View file

@ -30,7 +30,7 @@ READ_BUFFER_SIZE = 128 * 1024
_WRITE_BUFFER_SIZE = 4 * io.DEFAULT_BUFFER_SIZE _WRITE_BUFFER_SIZE = 4 * io.DEFAULT_BUFFER_SIZE
def open(filename, mode="rb", compresslevel=_COMPRESS_LEVEL_BEST, def open(filename, mode="rb", compresslevel=_COMPRESS_LEVEL_TRADEOFF,
encoding=None, errors=None, newline=None): encoding=None, errors=None, newline=None):
"""Open a gzip-compressed file in binary or text mode. """Open a gzip-compressed file in binary or text mode.
@ -158,7 +158,7 @@ class GzipFile(_streams.BaseStream):
myfileobj = None myfileobj = None
def __init__(self, filename=None, mode=None, def __init__(self, filename=None, mode=None,
compresslevel=_COMPRESS_LEVEL_BEST, fileobj=None, mtime=None): compresslevel=_COMPRESS_LEVEL_TRADEOFF, fileobj=None, mtime=None):
"""Constructor for the GzipFile class. """Constructor for the GzipFile class.
At least one of fileobj and filename must be given a At least one of fileobj and filename must be given a
@ -621,7 +621,7 @@ class _GzipReader(_streams.DecompressReader):
self._new_member = True self._new_member = True
def compress(data, compresslevel=_COMPRESS_LEVEL_BEST, *, mtime=0): def compress(data, compresslevel=_COMPRESS_LEVEL_TRADEOFF, *, mtime=0):
"""Compress data in one shot and return the compressed string. """Compress data in one shot and return the compressed string.
compresslevel sets the compression level in range of 0-9. compresslevel sets the compression level in range of 0-9.

View file

@ -1927,7 +1927,7 @@ class TarFile(object):
if "preset" in kwargs and comptype not in ("xz",): if "preset" in kwargs and comptype not in ("xz",):
raise ValueError("preset is only valid for w|xz mode") raise ValueError("preset is only valid for w|xz mode")
compresslevel = kwargs.pop("compresslevel", 9) compresslevel = kwargs.pop("compresslevel", 6)
preset = kwargs.pop("preset", None) preset = kwargs.pop("preset", None)
stream = _Stream(name, filemode, comptype, fileobj, bufsize, stream = _Stream(name, filemode, comptype, fileobj, bufsize,
compresslevel, preset) compresslevel, preset)
@ -1953,7 +1953,7 @@ class TarFile(object):
return cls(name, mode, fileobj, **kwargs) return cls(name, mode, fileobj, **kwargs)
@classmethod @classmethod
def gzopen(cls, name, mode="r", fileobj=None, compresslevel=9, **kwargs): def gzopen(cls, name, mode="r", fileobj=None, compresslevel=6, **kwargs):
"""Open gzip compressed tar archive name for reading or writing. """Open gzip compressed tar archive name for reading or writing.
Appending is not allowed. Appending is not allowed.
""" """

View file

@ -353,7 +353,7 @@ class TestGzip(BaseTest):
def test_metadata(self): def test_metadata(self):
mtime = 123456789 mtime = 123456789
with gzip.GzipFile(self.filename, 'w', mtime = mtime) as fWrite: with gzip.GzipFile(self.filename, 'w', mtime = mtime, compresslevel = 9) as fWrite:
fWrite.write(data1) fWrite.write(data1)
with open(self.filename, 'rb') as fRead: with open(self.filename, 'rb') as fRead:

View file

@ -0,0 +1,3 @@
Adjust default ``compressionlevel=`` to 6 (down from 9) in :mod:`gzip` and :mod:`tarfile`.
It is the default level used by most compression tools and a better
tradeoff between speed and performance.