summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZackery Spytz <zspytz@gmail.com>2021-08-09 03:05:31 -0700
committerGitHub <noreply@github.com>2021-08-09 12:05:31 +0200
commiteb2d4a66ff07aa6e51cfaaa31afed31addf76936 (patch)
treee2b6320a36e69b0e6a1de9c2434290312df4d4cd
parentac75f6bdd4748b3378dd321f862d13aa1898f77a (diff)
downloadcpython-git-eb2d4a66ff07aa6e51cfaaa31afed31addf76936.tar.gz
bpo-32695: Docs and tests for compresslevel and preset kwargs in tarfile (GH-21470)
Co-Authored-By: Bo Bayles <bbayles@gmail.com>
-rw-r--r--Doc/library/tarfile.rst3
-rw-r--r--Lib/test/test_tarfile.py21
-rw-r--r--Misc/NEWS.d/next/Library/2020-07-13-23-46-59.bpo-32695.tTqqXe.rst2
3 files changed, 23 insertions, 3 deletions
diff --git a/Doc/library/tarfile.rst b/Doc/library/tarfile.rst
index 13088a10d7..6afb8397b7 100644
--- a/Doc/library/tarfile.rst
+++ b/Doc/library/tarfile.rst
@@ -102,6 +102,9 @@ Some facts and figures:
``'x: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
+ keyword argument *preset* to specify the compression level of the file.
+
For special purposes, there is a second format for *mode*:
``'filemode|[compression]'``. :func:`tarfile.open` will return a :class:`TarFile`
object that processes its data as a stream of blocks. No random seeking will
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
index 817e6f1799..cfdda24a26 100644
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -1706,15 +1706,30 @@ class CreateTest(WriteTestBase, unittest.TestCase):
class GzipCreateTest(GzipTest, CreateTest):
- pass
+
+ def test_create_with_compresslevel(self):
+ with tarfile.open(tmpname, self.mode, compresslevel=1) as tobj:
+ tobj.add(self.file_path)
+ with tarfile.open(tmpname, 'r:gz', compresslevel=1) as tobj:
+ pass
class Bz2CreateTest(Bz2Test, CreateTest):
- pass
+
+ def test_create_with_compresslevel(self):
+ with tarfile.open(tmpname, self.mode, compresslevel=1) as tobj:
+ tobj.add(self.file_path)
+ with tarfile.open(tmpname, 'r:bz2', compresslevel=1) as tobj:
+ pass
class LzmaCreateTest(LzmaTest, CreateTest):
- pass
+
+ # Unlike gz and bz2, xz uses the preset keyword instead of compresslevel.
+ # It does not allow for preset to be specified when reading.
+ def test_create_with_preset(self):
+ with tarfile.open(tmpname, self.mode, preset=1) as tobj:
+ tobj.add(self.file_path)
class CreateWithXModeTest(CreateTest):
diff --git a/Misc/NEWS.d/next/Library/2020-07-13-23-46-59.bpo-32695.tTqqXe.rst b/Misc/NEWS.d/next/Library/2020-07-13-23-46-59.bpo-32695.tTqqXe.rst
new file mode 100644
index 0000000000..c71316ed65
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-07-13-23-46-59.bpo-32695.tTqqXe.rst
@@ -0,0 +1,2 @@
+The *compresslevel* and *preset* keyword arguments of :func:`tarfile.open`
+are now both documented and tested.