Skip to content

Commit

Permalink
bpo-26253: don't add compresslevel as an attribute
Browse files Browse the repository at this point in the history
In tarfile, compresslevel will be simply passed around.

Also added 'versionadded' to stream compression level setting
  • Loading branch information
jarondl committed Jul 31, 2017
1 parent 3ca92e9 commit 1995f6d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
3 changes: 3 additions & 0 deletions Doc/library/tarfile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ Some facts and figures:
.. versionchanged:: 3.6
The *name* parameter accepts a :term:`path-like object`.

.. versionchanged:: 3.7
The *compresslevel* keyword argument also works for streams.


.. class:: TarFile

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

def __init__(self, name, mode, comptype, fileobj, bufsize,
compresslevel=9):
compresslevel):
"""Construct a _Stream object.
"""
self._extfileobj = True
Expand All @@ -358,15 +358,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 @@ -380,7 +379,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 @@ -392,7 +391,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 @@ -419,10 +418,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

0 comments on commit 1995f6d

Please sign in to comment.