Skip to content

Commit d5c8ad2

Browse files
bpo-32695: Docs and tests for compresslevel and preset kwargs in tarfile (GH-21470)
Co-Authored-By: Bo Bayles <bbayles@gmail.com> (cherry picked from commit eb2d4a6) Co-authored-by: Zackery Spytz <zspytz@gmail.com>
1 parent 762ef85 commit d5c8ad2

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

Doc/library/tarfile.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ Some facts and figures:
102102
``'x:bz2'``, :func:`tarfile.open` accepts the keyword argument
103103
*compresslevel* (default ``9``) to specify the compression level of the file.
104104

105+
For modes ``'w:xz'`` and ``'x:xz'``, :func:`tarfile.open` accepts the
106+
keyword argument *preset* to specify the compression level of the file.
107+
105108
For special purposes, there is a second format for *mode*:
106109
``'filemode|[compression]'``. :func:`tarfile.open` will return a :class:`TarFile`
107110
object that processes its data as a stream of blocks. No random seeking will

Lib/test/test_tarfile.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1706,15 +1706,30 @@ def test_create_taropen_pathlike_name(self):
17061706

17071707

17081708
class GzipCreateTest(GzipTest, CreateTest):
1709-
pass
1709+
1710+
def test_create_with_compresslevel(self):
1711+
with tarfile.open(tmpname, self.mode, compresslevel=1) as tobj:
1712+
tobj.add(self.file_path)
1713+
with tarfile.open(tmpname, 'r:gz', compresslevel=1) as tobj:
1714+
pass
17101715

17111716

17121717
class Bz2CreateTest(Bz2Test, CreateTest):
1713-
pass
1718+
1719+
def test_create_with_compresslevel(self):
1720+
with tarfile.open(tmpname, self.mode, compresslevel=1) as tobj:
1721+
tobj.add(self.file_path)
1722+
with tarfile.open(tmpname, 'r:bz2', compresslevel=1) as tobj:
1723+
pass
17141724

17151725

17161726
class LzmaCreateTest(LzmaTest, CreateTest):
1717-
pass
1727+
1728+
# Unlike gz and bz2, xz uses the preset keyword instead of compresslevel.
1729+
# It does not allow for preset to be specified when reading.
1730+
def test_create_with_preset(self):
1731+
with tarfile.open(tmpname, self.mode, preset=1) as tobj:
1732+
tobj.add(self.file_path)
17181733

17191734

17201735
class CreateWithXModeTest(CreateTest):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The *compresslevel* and *preset* keyword arguments of :func:`tarfile.open`
2+
are now both documented and tested.

0 commit comments

Comments
 (0)