From b17e21df09b4f0bf4751bc5b82c38bf20cd2f23e Mon Sep 17 00:00:00 2001 From: rmorotti Date: Wed, 19 Mar 2025 12:46:00 +0000 Subject: [PATCH 1/8] 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. --- Doc/library/gzip.rst | 6 +++--- Lib/gzip.py | 6 +++--- Lib/tarfile.py | 2 +- Lib/test/test_gzip.py | 2 +- .../Library/2025-03-19-12-41-42.gh-issue-91349.8eTOCP.rst | 3 +++ 5 files changed, 11 insertions(+), 8 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2025-03-19-12-41-42.gh-issue-91349.8eTOCP.rst diff --git a/Doc/library/gzip.rst b/Doc/library/gzip.rst index c9d96085ef739d..94ae852d323a41 100644 --- a/Doc/library/gzip.rst +++ b/Doc/library/gzip.rst @@ -26,7 +26,7 @@ Note that additional file formats which can be decompressed by the 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 object`. @@ -67,7 +67,7 @@ The module defines the following items: .. 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 methods of a :term:`file object`, with the exception of the :meth:`~io.IOBase.truncate` @@ -182,7 +182,7 @@ The module defines the following items: attribute instead. -.. function:: compress(data, compresslevel=9, *, mtime=0) +.. function:: compress(data, compresslevel=6, *, mtime=0) Compress the *data*, returning a :class:`bytes` object containing the compressed data. *compresslevel* and *mtime* have the same meaning as in diff --git a/Lib/gzip.py b/Lib/gzip.py index c00f51858de0f0..89c4738ec69325 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -30,7 +30,7 @@ _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): """Open a gzip-compressed file in binary or text mode. @@ -158,7 +158,7 @@ class GzipFile(_streams.BaseStream): myfileobj = 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. At least one of fileobj and filename must be given a @@ -621,7 +621,7 @@ def _rewind(self): 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. compresslevel sets the compression level in range of 0-9. diff --git a/Lib/tarfile.py b/Lib/tarfile.py index 068aa13ed70356..58466cc4db9f89 100644 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -1953,7 +1953,7 @@ def taropen(cls, name, mode="r", fileobj=None, **kwargs): return cls(name, mode, fileobj, **kwargs) @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. Appending is not allowed. """ diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py index a12ff5662a73db..9a2e1dd248fe94 100644 --- a/Lib/test/test_gzip.py +++ b/Lib/test/test_gzip.py @@ -353,7 +353,7 @@ def test_mtime(self): def test_metadata(self): 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) with open(self.filename, 'rb') as fRead: diff --git a/Misc/NEWS.d/next/Library/2025-03-19-12-41-42.gh-issue-91349.8eTOCP.rst b/Misc/NEWS.d/next/Library/2025-03-19-12-41-42.gh-issue-91349.8eTOCP.rst new file mode 100644 index 00000000000000..8739a4b6498630 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-03-19-12-41-42.gh-issue-91349.8eTOCP.rst @@ -0,0 +1,3 @@ +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. From 7ba376e74beabb05a15d8236dd8af15ee23da815 Mon Sep 17 00:00:00 2001 From: rmorotti Date: Wed, 25 Jun 2025 15:32:29 +0100 Subject: [PATCH 2/8] add versionchanged note --- Doc/library/gzip.rst | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Doc/library/gzip.rst b/Doc/library/gzip.rst index 94ae852d323a41..33eb1402a439de 100644 --- a/Doc/library/gzip.rst +++ b/Doc/library/gzip.rst @@ -59,6 +59,11 @@ The module defines the following items: .. versionchanged:: 3.6 Accepts a :term:`path-like object`. + .. versionchanged:: 3.14 + 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 An exception raised for invalid gzip files. It inherits from :exc:`OSError`. @@ -181,6 +186,11 @@ The module defines the following items: Remove the ``filename`` attribute, use the :attr:`~GzipFile.name` attribute instead. + .. versionchanged:: 3.14 + 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=6, *, mtime=0) @@ -206,6 +216,10 @@ The module defines the following items: The *mtime* parameter now defaults to 0 for reproducible output. For the previous behaviour of using the current time, pass ``None`` to *mtime*. + .. versionchanged:: 3.14 + 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) From 461178f50aed28633602afadb7fc2bd21b84feb9 Mon Sep 17 00:00:00 2001 From: rmorotti Date: Wed, 25 Jun 2025 15:37:40 +0100 Subject: [PATCH 3/8] add versionchanged note --- Doc/library/tarfile.rst | 6 +++++- Lib/tarfile.py | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Doc/library/tarfile.rst b/Doc/library/tarfile.rst index 7cec108a5bd41d..61dac6cd73d57c 100644 --- a/Doc/library/tarfile.rst +++ b/Doc/library/tarfile.rst @@ -110,7 +110,7 @@ Some facts and figures: For modes ``'w:gz'``, ``'x:gz'``, ``'w|gz'``, ``'w:bz2'``, ``'x:bz2'``, ``'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 keyword argument *preset* to specify the compression level of the file. @@ -170,6 +170,10 @@ Some facts and figures: .. versionchanged:: 3.14 The *preset* keyword argument also works for streams. + .. versionchanged:: 3.14 + 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 :noindex: diff --git a/Lib/tarfile.py b/Lib/tarfile.py index 58466cc4db9f89..80d8644af86f74 100644 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -1927,7 +1927,7 @@ def not_compressed(comptype): if "preset" in kwargs and comptype not in ("xz",): 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) stream = _Stream(name, filemode, comptype, fileobj, bufsize, compresslevel, preset) From 93ea66a5abe1dd0dd2babc42968b70ad01d9d9df Mon Sep 17 00:00:00 2001 From: morotti Date: Fri, 18 Jul 2025 18:52:02 +0100 Subject: [PATCH 4/8] Update Doc/library/gzip.rst set versionchanged to next Co-authored-by: Pieter Eendebak --- Doc/library/gzip.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/gzip.rst b/Doc/library/gzip.rst index 33eb1402a439de..3825c9e5f8bb56 100644 --- a/Doc/library/gzip.rst +++ b/Doc/library/gzip.rst @@ -59,7 +59,7 @@ The module defines the following items: .. versionchanged:: 3.6 Accepts a :term:`path-like object`. - .. versionchanged:: 3.14 + .. 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. From b2dd72721fa606a25579a3cfec477e0b5172920a Mon Sep 17 00:00:00 2001 From: morotti Date: Fri, 18 Jul 2025 18:52:42 +0100 Subject: [PATCH 5/8] Update Doc/library/gzip.rst set versionchanged to next --- Doc/library/gzip.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/gzip.rst b/Doc/library/gzip.rst index 3825c9e5f8bb56..2d3275d681fe49 100644 --- a/Doc/library/gzip.rst +++ b/Doc/library/gzip.rst @@ -216,7 +216,7 @@ The module defines the following items: The *mtime* parameter now defaults to 0 for reproducible output. For the previous behaviour of using the current time, pass ``None`` to *mtime*. - .. versionchanged:: 3.14 + .. 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. From f9241618eeee3a593fa6b5bfd4ca190b389e9b6e Mon Sep 17 00:00:00 2001 From: morotti Date: Fri, 18 Jul 2025 18:55:26 +0100 Subject: [PATCH 6/8] set versionchanged to next --- Doc/library/gzip.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/gzip.rst b/Doc/library/gzip.rst index 2d3275d681fe49..c59014a6f5bdce 100644 --- a/Doc/library/gzip.rst +++ b/Doc/library/gzip.rst @@ -186,7 +186,7 @@ The module defines the following items: Remove the ``filename`` attribute, use the :attr:`~GzipFile.name` attribute instead. - .. versionchanged:: 3.14 + .. 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. From 808f7cda875aa63642fe4d6e686241a25beab7b9 Mon Sep 17 00:00:00 2001 From: morotti Date: Fri, 18 Jul 2025 18:55:50 +0100 Subject: [PATCH 7/8] Update Doc/library/tarfile.rst set versionchanged to next --- Doc/library/tarfile.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/tarfile.rst b/Doc/library/tarfile.rst index 61dac6cd73d57c..d496c0d7793756 100644 --- a/Doc/library/tarfile.rst +++ b/Doc/library/tarfile.rst @@ -170,7 +170,7 @@ Some facts and figures: .. versionchanged:: 3.14 The *preset* keyword argument also works for streams. - .. versionchanged:: 3.14 + .. 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. From 793ca569407f98b25c77097c054f77d76d08edd9 Mon Sep 17 00:00:00 2001 From: morotti Date: Sun, 20 Jul 2025 13:15:17 +0100 Subject: [PATCH 8/8] Update Misc/NEWS.d/next/Library/2025-03-19-12-41-42.gh-issue-91349.8eTOCP.rst highlight function in rst file --- .../next/Library/2025-03-19-12-41-42.gh-issue-91349.8eTOCP.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-03-19-12-41-42.gh-issue-91349.8eTOCP.rst b/Misc/NEWS.d/next/Library/2025-03-19-12-41-42.gh-issue-91349.8eTOCP.rst index 8739a4b6498630..0e866fa4ef6169 100644 --- a/Misc/NEWS.d/next/Library/2025-03-19-12-41-42.gh-issue-91349.8eTOCP.rst +++ b/Misc/NEWS.d/next/Library/2025-03-19-12-41-42.gh-issue-91349.8eTOCP.rst @@ -1,3 +1,3 @@ -Adjust default compression level to 6 (down from 9) in gzip and tarfile. +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.