Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
bpo-26253: don't add compresslevel as an attribute
In tarfile, compresslevel will be simply passed around.

Also added 'versionadded' to stream compression level setting
  • Loading branch information
jarondl committed May 8, 2022
commit 31b9835c2018d691c773902b2eba7856ed53f50c
3 changes: 3 additions & 0 deletions Doc/library/tarfile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ Some facts and figures:
.. versionchanged:: 3.6
The *name* parameter accepts a :term:`path-like object`.

.. versionchanged:: 3.7
Comment thread
serhiy-storchaka marked this conversation as resolved.
Outdated
The *compresslevel* keyword argument also works for streams.


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

def __init__(self, name, mode, comptype, fileobj, bufsize,
compresslevel=9):
compresslevel):
"""Construct a _Stream object.
"""
self._extfileobj = True
Expand All @@ -351,15 +351,14 @@ def __init__(self, name, mode, comptype, fileobj, bufsize,
fileobj = _StreamProxy(fileobj)
comptype = fileobj.getcomptype()

self.name = name or ""
self.mode = mode
self.name = name or ""
self.mode = mode
self.comptype = comptype
self.fileobj = fileobj
self.bufsize = bufsize
self.compresslevel = compresslevel
self.buf = b""
self.pos = 0
self.closed = False
self.fileobj = fileobj
self.bufsize = bufsize
self.buf = b""
self.pos = 0
self.closed = False

try:
if comptype == "gz":
Expand All @@ -373,7 +372,7 @@ def __init__(self, name, mode, comptype, fileobj, bufsize,
self._init_read_gz()
self.exception = zlib.error
else:
self._init_write_gz()
self._init_write_gz(compresslevel)

elif comptype == "bz2":
try:
Expand All @@ -385,7 +384,7 @@ def __init__(self, name, mode, comptype, fileobj, bufsize,
self.cmp = bz2.BZ2Decompressor()
self.exception = OSError
else:
self.cmp = bz2.BZ2Compressor(self.compresslevel)
self.cmp = bz2.BZ2Compressor(compresslevel)

elif comptype == "xz":
try:
Expand All @@ -412,10 +411,10 @@ def __del__(self):
if hasattr(self, "closed") and not self.closed:
self.close()

def _init_write_gz(self):
def _init_write_gz(self, compresslevel):
"""Initialize for writing with gzip compression.
"""
self.cmp = self.zlib.compressobj(self.compresslevel,
self.cmp = self.zlib.compressobj(compresslevel,
self.zlib.DEFLATED,
-self.zlib.MAX_WBITS,
self.zlib.DEF_MEM_LEVEL,
Expand Down