GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-26253: Add compressionlevel to tarfile stream by jarondl · Pull Request #2962 · python/cpython

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.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
serhiy-storchaka merged 5 commits into python:main from jarondl:tarfile-compress-26253
Jun 25, 2022

Uh oh!

There was an error while loading. Please reload this page.

Merged

bpo-26253: Add compressionlevel to tarfile stream #2962

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
5 commits
Select commit Hold shift + click to select a range
89fa205
bpo-26253: Add compressionlevel to tarfile stream
jarondl Jul 31, 2017
31b9835
bpo-26253: don't add compresslevel as an attribute
jarondl Jul 31, 2017
c217360
bpo-26253: tarfile: add compresslevel unit tests
jarondl Aug 1, 2017
f48acbd
Apply suggestions from code review
serhiy-storchaka Jun 17, 2022
2b1b417
Update Misc/NEWS.d/next/Library/2017-07-31-13-35-28.bpo-26253.8v_sCs.rst
serhiy-storchaka Jun 17, 2022
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

7 changes: 5 additions & 2 deletions 7 Doc/library/tarfile.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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'``, ``'r:gz'``, ``'w:bz2'``, ``'r:bz2'``, ``'x:gz'``,
``'x:bz2'``, :func:`tarfile.open` accepts the keyword argument
For modes ``'w:gz'``, ``'x:gz'``, ``'w|gz'``, ``'w:bz2'``, ``'x:bz2'``,
Comment thread

@serhiy-storchaka serhiy-storchaka Jul 31, 2017

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the versionchanged directive.

Document the change in the What's New document.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@jarondl jarondl Aug 1, 2017 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

versionchanged - Done

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
``'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
Expand Down Expand Up @@ -152,6 +152,9 @@ Some facts and figures:
.. versionchanged:: 3.6
The *name* parameter accepts a :term:`path-like object`.

.. versionchanged:: 3.12
The *compresslevel* keyword argument also works for streams.


.. class:: TarFile
:noindex:
Expand Down
22 changes: 13 additions & 9 deletions 22 Lib/tarfile.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,8 @@ class _Stream:
_Stream is intended to be used only internally.
"""

def __init__(self, name, mode, comptype, fileobj, bufsize):
def __init__(self, name, mode, comptype, fileobj, bufsize,
compresslevel):
"""Construct a _Stream object.
"""
self._extfileobj = True
Expand Down Expand Up @@ -371,7 +372,7 @@ def __init__(self, name, mode, comptype, fileobj, bufsize):
self._init_read_gz()
self.exception = zlib.error
else:
self._init_write_gz()
self._init_write_gz(compresslevel)

elif comptype == "bz2":
try:
Expand All @@ -383,7 +384,7 @@ def __init__(self, name, mode, comptype, fileobj, bufsize):
self.cmp = bz2.BZ2Decompressor()
self.exception = OSError
else:
self.cmp = bz2.BZ2Compressor()
self.cmp = bz2.BZ2Compressor(compresslevel)

elif comptype == "xz":
try:
Expand All @@ -410,13 +411,14 @@ def __del__(self):
if hasattr(self, "closed") and not self.closed:
self.close()

def _init_write_gz(self):
def _init_write_gz(self, compresslevel):
"""Initialize for writing with gzip compression.
"""
self.cmp = self.zlib.compressobj(9, self.zlib.DEFLATED,
-self.zlib.MAX_WBITS,
self.zlib.DEF_MEM_LEVEL,
0)
self.cmp = self.zlib.compressobj(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"):
Expand Down Expand Up @@ -1649,7 +1651,9 @@ def not_compressed(comptype):
if filemode not in ("r", "w"):
raise ValueError("mode must be 'r' or 'w'")

stream = _Stream(name, filemode, comptype, fileobj, bufsize)
compresslevel = kwargs.pop("compresslevel", 9)
stream = _Stream(name, filemode, comptype, fileobj, bufsize,
compresslevel)
try:
t = cls(name, filemode, stream, **kwargs)
except:
Expand Down
68 changes: 68 additions & 0 deletions 68 Lib/test/test_tarfile.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1532,6 +1532,74 @@ class Bz2StreamWriteTest(Bz2Test, StreamWriteTest):
class LzmaStreamWriteTest(LzmaTest, StreamWriteTest):
decompressor = lzma.LZMADecompressor if lzma else None

class _CompressedWriteTest(TarTest):
# This is not actually a standalone test.
# It does not inherit WriteTest because it only makes sense with gz,bz2
source = (b"And we move to Bristol where they have a special, " +
b"Very Silly candidate")

def _compressed_tar(self, compresslevel):
fobj = io.BytesIO()
with tarfile.open(tmpname, self.mode, fobj,
compresslevel=compresslevel) as tarfl:
tarfl.addfile(tarfile.TarInfo("foo"), io.BytesIO(self.source))
return fobj

def _test_bz2_header(self, compresslevel):
fobj = self._compressed_tar(compresslevel)
self.assertEqual(fobj.getvalue()[0:10],
b"BZh%d1AY&SY" % compresslevel)

def _test_gz_header(self, compresslevel):
fobj = self._compressed_tar(compresslevel)
self.assertEqual(fobj.getvalue()[:3], b"\x1f\x8b\x08")

class Bz2CompressWriteTest(Bz2Test, _CompressedWriteTest, unittest.TestCase):
prefix = "w:"
def test_compression_levels(self):
self._test_bz2_header(1)
self._test_bz2_header(5)
self._test_bz2_header(9)

class Bz2CompressStreamWriteTest(Bz2Test, _CompressedWriteTest,
unittest.TestCase):
prefix = "w|"
def test_compression_levels(self):
self._test_bz2_header(1)
self._test_bz2_header(5)
self._test_bz2_header(9)

class GzCompressWriteTest(GzipTest, _CompressedWriteTest, unittest.TestCase):
prefix = "w:"
def test_compression_levels(self):
self._test_gz_header(1)
self._test_gz_header(5)
self._test_gz_header(9)

class GzCompressStreamWriteTest(GzipTest, _CompressedWriteTest,
unittest.TestCase):
prefix = "w|"
def test_compression_levels(self):
self._test_gz_header(1)
self._test_gz_header(5)
self._test_gz_header(9)

class CompressLevelRaises(unittest.TestCase):
def test_compresslevel_wrong_modes(self):
compresslevel = 5
fobj = io.BytesIO()
with self.assertRaises(TypeError):
tarfile.open(tmpname, "w:", fobj, compresslevel=compresslevel)

def test_wrong_compresslevels(self):
# BZ2 checks that the compresslevel is in [1,9]. gz does not
fobj = io.BytesIO()
with self.assertRaises(ValueError):
tarfile.open(tmpname, "w:bz2", fobj, compresslevel=0)
with self.assertRaises(ValueError):
tarfile.open(tmpname, "w:bz2", fobj, compresslevel=10)
with self.assertRaises(ValueError):
tarfile.open(tmpname, "w|bz2", fobj, compresslevel=10)

class GNUWriteTest(unittest.TestCase):
# This testcase checks for correct creation of GNU Longname
Expand Down
2 changes: 2 additions & 0 deletions 2 Misc/NEWS.d/next/Library/2017-07-31-13-35-28.bpo-26253.8v_sCs.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Allow adjustable compression level for tarfile streams in
:func:`tarfile.open`.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.