-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
bpo-26253: Add compressionlevel to tarfile stream #2962
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
Changes from 1 commit
89fa205
31b9835
c217360
f48acbd
2b1b417
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
`tarfile` already accepts a compressionlevel argument for creating files. This patch adds the same for stream-based tarfile usage. The default is 9, the value that was previously hard-coded.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -336,7 +336,8 @@ class _Stream: | |
_Stream is intended to be used only internally. | ||
""" | ||
|
||
def __init__(self, name, mode, comptype, fileobj, bufsize): | ||
def __init__(self, name, mode, comptype, fileobj, bufsize, | ||
compresslevel=9): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since |
||
"""Construct a _Stream object. | ||
""" | ||
self._extfileobj = True | ||
|
@@ -350,14 +351,15 @@ 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.buf = b"" | ||
self.pos = 0 | ||
self.closed = False | ||
self.fileobj = fileobj | ||
self.bufsize = bufsize | ||
self.compresslevel = compresslevel | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it needed to save There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No it is not needed. My first approach was to pass it as an argument to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Other attributes are used in multiple |
||
self.buf = b"" | ||
self.pos = 0 | ||
self.closed = False | ||
|
||
try: | ||
if comptype == "gz": | ||
|
@@ -383,7 +385,7 @@ def __init__(self, name, mode, comptype, fileobj, bufsize): | |
self.cmp = bz2.BZ2Decompressor() | ||
self.exception = OSError | ||
else: | ||
self.cmp = bz2.BZ2Compressor() | ||
self.cmp = bz2.BZ2Compressor(self.compresslevel) | ||
|
||
elif comptype == "xz": | ||
try: | ||
|
@@ -413,10 +415,11 @@ def __del__(self): | |
def _init_write_gz(self): | ||
"""Initialize for writing with gzip compression. | ||
""" | ||
self.cmp = self.zlib.compressobj(9, self.zlib.DEFLATED, | ||
-self.zlib.MAX_WBITS, | ||
self.zlib.DEF_MEM_LEVEL, | ||
0) | ||
self.cmp = self.zlib.compressobj(self.compresslevel, | ||
self.zlib.DEFLATED, | ||
-self.zlib.MAX_WBITS, | ||
self.zlib.DEF_MEM_LEVEL, | ||
0) | ||
timestamp = struct.pack("<L", int(time.time())) | ||
self.__write(b"\037\213\010\010" + timestamp + b"\002\377") | ||
if self.name.endswith(".gz"): | ||
|
@@ -1649,7 +1652,9 @@ def not_compressed(comptype): | |
if filemode not in ("r", "w"): | ||
raise ValueError("mode must be 'r' or 'w'") | ||
|
||
stream = _Stream(name, filemode, comptype, fileobj, bufsize) | ||
compresslevel = kwargs.pop("compresslevel", 9) | ||
stream = _Stream(name, filemode, comptype, fileobj, bufsize, | ||
compresslevel) | ||
try: | ||
t = cls(name, filemode, stream, **kwargs) | ||
except: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Adjustable compression level for tarfile streams. | ||
serhiy-storchaka marked this conversation as resolved.
Show resolved
Hide resolved
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add the
versionchanged
directive.Document the change in the What's New document.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
versionchanged
- Done