Skip to content

Commit ede221e

Browse files
bpo-32695: Docs and tests for compresslevel and preset kwargs in tarfile (GH-21470) (GH-27674)
Co-Authored-By: Bo Bayles <[email protected]> (cherry picked from commit eb2d4a6) Co-authored-by: Zackery Spytz <[email protected]>
1 parent 6a6bcf1 commit ede221e

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
@@ -1705,15 +1705,30 @@ def test_create_taropen_pathlike_name(self):
17051705

17061706

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

17101715

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

17141724

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

17181733

17191734
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)