Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
89fa20531b9835c217360f48acbd2b1b417File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
`tarfile` already accepts a compressionlevel argument for creatingfiles. This patch adds the same for stream-based tarfile usage.The default is 9, the value that was previously hard-coded.
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -98,8 +98,8 @@ Some facts and figures: | ||
| If *fileobj* is specified, it is used as an alternative to a :term:`file object` | ||
| opened in binary mode for *name*. It is supposed to be at position 0. | ||
| For modes ``'w:gz'``, ``'x:gz'``, ``'w|gz'``, ``'w:bz2'``, ``'x:bz2'``, | ||
Member 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. Add the Document the change in the What's New document. ContributorAuthor 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.
| ||
| ``'w|bz2'``, :func:`tarfile.open` accepts the keyword argument | ||
| *compresslevel* (default ``9``) to specify the compression level of the file. | ||
| For modes ``'w:xz'`` and ``'x:xz'``, :func:`tarfile.open` accepts the | ||
| 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, | ||
| compresslevel=9): | ||
| ||
| """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.comptype = comptype | ||
| self.fileobj = fileobj | ||
| self.bufsize = bufsize | ||
| self.compresslevel = compresslevel | ||
| ||
| 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.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(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'") | ||
| 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. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||