Skip to content

bpo-32695: Add docs and tests for compresslevel and preset in tarfile#5397

Closed
bbayles wants to merge 11 commits into
python:masterfrom
bbayles:tarfile-fails-compresslevel
Closed

bpo-32695: Add docs and tests for compresslevel and preset in tarfile#5397
bbayles wants to merge 11 commits into
python:masterfrom
bbayles:tarfile-fails-compresslevel

Conversation

@bbayles

@bbayles bbayles commented Jan 28, 2018

Copy link
Copy Markdown
Contributor

I've changed almost all of this - this PR now adds tests for compresslevel (these were lacking before) and documents preset (this was undocumented before).


This PR fixes tarfile.open() when using the w:xz mode and a compresslevel parameter.

TarFile.xzopen() seems to want to do what TarFile.gzopen() and TarFile.bzopen() do (see here), but it uses the keyword preset instead of compresslevel.

This means the preset never gets passed to lzma.LZMAFile(), the compresslevel keyword is thus picked up by **kwargs and passed to TarFile.__init__(), which doesn't recognize it.

>>> import tarfile
>>> good = tarfile.open('/tmp/dummy.tar.gz', 'w:gz', compresslevel=1)
>>> bad = tarfile.open('/tmp/dummy.tar.xz', 'w:xz', compresslevel=1)
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    bad = tarfile.open('/tmp/dummy.tar.xz', 'w:xz', compresslevel=1)
  File "/home/bo/Code/cpython/Lib/tarfile.py", line 1588, in open
    return func(name, filemode, fileobj, **kwargs)
  File "/home/bo/Code/cpython/Lib/tarfile.py", line 1699, in xzopen
    t = cls.taropen(name, mode, fileobj, **kwargs)
  File "/home/bo/Code/cpython/Lib/tarfile.py", line 1618, in taropen
    return cls(name, mode, fileobj, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'compresslevel'

In this PR I change the signature of TarFile.xzopen() to match its neighbors. I also change TarFile.taropen() and document that compresslevel is meaningless when not using one of the w:<some compression> modes. (compresslevel is already ignored when using a read mode; you can do tarfile.open('/tmp/dummy.tar.gz', 'r:gz', compresslevel='kittens') and not get an exception).

I also add tests for compresslevel with the various modes; these were lacking previously.


https://bugs.python.org/issue32695

Comment thread Doc/library/tarfile.rst Outdated
Comment thread Lib/test/test_tarfile.py Outdated
Comment thread Doc/library/tarfile.rst Outdated
Comment thread Doc/library/tarfile.rst Outdated
@bbayles bbayles changed the title bpo-32695: Fix tarfile.open compresslevel for LZMA bpo-32695: Add docs and tests for compresslevel and preset in tarfile Jan 29, 2018
@shunghsiyu

Copy link
Copy Markdown

Hi, is there anything I can help with to get this pull request move forward? :)

I recently encounter the same TypeError: __init__() got an unexpected keyword argument 'compresslevel' error when trying to adjust compression level of 'xz' in tarfile.open(); and there is at least one other person who encounter this error.

It will be really nice if the document mentions that preset should be used to change compression level when xz is used.

@csabella
csabella requested a review from vadmium May 14, 2019 11:48
@bbayles bbayles closed this Jul 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants