Skip to content

gh-133005: Support tarfile.open(mode="w|xz", preset=...) #133007

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Doc/library/tarfile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ Some facts and figures:
``'w|bz2'``, :func:`tarfile.open` accepts the keyword argument
*compresslevel* (default ``9``) to specify the compression level of the file.

For modes ``'w:xz'`` and ``'x: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.

For special purposes, there is a second format for *mode*:
Expand Down Expand Up @@ -167,6 +167,9 @@ Some facts and figures:
.. versionchanged:: 3.12
The *compresslevel* keyword argument also works for streams.

.. versionchanged:: next
The *preset* keyword argument also works for streams.


.. class:: TarFile
:noindex:
Expand Down
13 changes: 10 additions & 3 deletions Lib/tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ class _Stream:
"""

def __init__(self, name, mode, comptype, fileobj, bufsize,
compresslevel):
compresslevel, preset):
"""Construct a _Stream object.
"""
self._extfileobj = True
Expand Down Expand Up @@ -398,7 +398,7 @@ def __init__(self, name, mode, comptype, fileobj, bufsize,
self.cmp = lzma.LZMADecompressor()
self.exception = lzma.LZMAError
else:
self.cmp = lzma.LZMACompressor()
self.cmp = lzma.LZMACompressor(preset=preset)

elif comptype != "tar":
raise CompressionError("unknown compression type %r" % comptype)
Expand Down Expand Up @@ -1885,10 +1885,17 @@ def not_compressed(comptype):

if filemode not in ("r", "w"):
raise ValueError("mode must be 'r' or 'w'")
if "compresslevel" in kwargs and comptype not in ("gz", "bz2"):
raise ValueError(
"compresslevel is only valid for w|gz and w|bz2 modes"
)
if "preset" in kwargs and comptype not in ("xz",):
raise ValueError("preset is only valid for w|xz mode")

compresslevel = kwargs.pop("compresslevel", 9)
preset = kwargs.pop("preset", None)
stream = _Stream(name, filemode, comptype, fileobj, bufsize,
compresslevel)
compresslevel, preset)
try:
t = cls(name, filemode, stream, **kwargs)
except:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Support passing ``preset`` option to :func:`tarfile.open` when using ``'w|xz'``
mode.
Loading