Skip to content

Commit dd227e2

Browse files
committed
Correctly forward keyword arguments
1 parent e7e3d1d commit dd227e2

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

Doc/library/tarfile.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ Some facts and figures:
137137
a Zstandard dictionary used to improve compression of smaller amounts of
138138
data.
139139

140+
For modes ``'r:zst'`` and ``'r|zst'``, :func:`tarfile.open` accepts the keyword
141+
arguments *options* and *zstd_dict* as well.
142+
140143
For special purposes, there is a second format for *mode*:
141144
``'filemode|[compression]'``. :func:`tarfile.open` will return a :class:`TarFile`
142145
object that processes its data as a stream of blocks. No random seeking will

Lib/tarfile.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ class _Stream:
338338
"""
339339

340340
def __init__(self, name, mode, comptype, fileobj, bufsize,
341-
compresslevel, preset):
341+
compresslevel, preset, level, options, zstd_dict):
342342
"""Construct a _Stream object.
343343
"""
344344
self._extfileobj = True
@@ -405,10 +405,10 @@ def __init__(self, name, mode, comptype, fileobj, bufsize,
405405
raise CompressionError("compression.zstd module is not available") from None
406406
if mode == "r":
407407
self.dbuf = b""
408-
self.cmp = zstd.ZstdDecompressor()
408+
self.cmp = zstd.ZstdDecompressor(zstd_dict, options)
409409
self.exception = zstd.ZstdError
410410
else:
411-
self.cmp = zstd.ZstdCompressor()
411+
self.cmp = zstd.ZstdCompressor(level, options, zstd_dict)
412412
elif comptype != "tar":
413413
raise CompressionError("unknown compression type %r" % comptype)
414414

@@ -1929,10 +1929,20 @@ def not_compressed(comptype):
19291929
if "preset" in kwargs and comptype not in ("xz",):
19301930
raise ValueError("preset is only valid for w|xz mode")
19311931

1932+
if comptype not in ("zst",):
1933+
for arg in ("level", "options", "zstd_dict"):
1934+
if arg in kwargs:
1935+
raise ValueError(
1936+
f"{arg} is only valid for w:zst, x:zst and w|zst modes"
1937+
)
1938+
19321939
compresslevel = kwargs.pop("compresslevel", 6)
19331940
preset = kwargs.pop("preset", None)
1941+
level = kwargs.pop("level", None)
1942+
options = kwargs.pop("options", None)
1943+
zstd_dict = kwargs.pop("zstd_dict", None)
19341944
stream = _Stream(name, filemode, comptype, fileobj, bufsize,
1935-
compresslevel, preset)
1945+
compresslevel, preset, level, options, zstd_dict)
19361946
try:
19371947
t = cls(name, filemode, stream, **kwargs)
19381948
except:

0 commit comments

Comments
 (0)