Skip to content

Commit 1995f6d

Browse files
committed
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
1 parent 3ca92e9 commit 1995f6d

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

Doc/library/tarfile.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ Some facts and figures:
149149
.. versionchanged:: 3.6
150150
The *name* parameter accepts a :term:`path-like object`.
151151

152+
.. versionchanged:: 3.7
153+
The *compresslevel* keyword argument also works for streams.
154+
152155

153156
.. class:: TarFile
154157

Lib/tarfile.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ class _Stream:
344344
"""
345345

346346
def __init__(self, name, mode, comptype, fileobj, bufsize,
347-
compresslevel=9):
347+
compresslevel):
348348
"""Construct a _Stream object.
349349
"""
350350
self._extfileobj = True
@@ -358,15 +358,14 @@ def __init__(self, name, mode, comptype, fileobj, bufsize,
358358
fileobj = _StreamProxy(fileobj)
359359
comptype = fileobj.getcomptype()
360360

361-
self.name = name or ""
362-
self.mode = mode
361+
self.name = name or ""
362+
self.mode = mode
363363
self.comptype = comptype
364-
self.fileobj = fileobj
365-
self.bufsize = bufsize
366-
self.compresslevel = compresslevel
367-
self.buf = b""
368-
self.pos = 0
369-
self.closed = False
364+
self.fileobj = fileobj
365+
self.bufsize = bufsize
366+
self.buf = b""
367+
self.pos = 0
368+
self.closed = False
370369

371370
try:
372371
if comptype == "gz":
@@ -380,7 +379,7 @@ def __init__(self, name, mode, comptype, fileobj, bufsize,
380379
self._init_read_gz()
381380
self.exception = zlib.error
382381
else:
383-
self._init_write_gz()
382+
self._init_write_gz(compresslevel)
384383

385384
elif comptype == "bz2":
386385
try:
@@ -392,7 +391,7 @@ def __init__(self, name, mode, comptype, fileobj, bufsize,
392391
self.cmp = bz2.BZ2Decompressor()
393392
self.exception = OSError
394393
else:
395-
self.cmp = bz2.BZ2Compressor(self.compresslevel)
394+
self.cmp = bz2.BZ2Compressor(compresslevel)
396395

397396
elif comptype == "xz":
398397
try:
@@ -419,10 +418,10 @@ def __del__(self):
419418
if hasattr(self, "closed") and not self.closed:
420419
self.close()
421420

422-
def _init_write_gz(self):
421+
def _init_write_gz(self, compresslevel):
423422
"""Initialize for writing with gzip compression.
424423
"""
425-
self.cmp = self.zlib.compressobj(self.compresslevel,
424+
self.cmp = self.zlib.compressobj(compresslevel,
426425
self.zlib.DEFLATED,
427426
-self.zlib.MAX_WBITS,
428427
self.zlib.DEF_MEM_LEVEL,

0 commit comments

Comments
 (0)