Sitelet https://github.com/python/cpython/issues/70441
Skip to content

tarfile in stream mode always set zlib compression level to 9 #70441

Description

@PatrikDufresne
BPO 26253
Nosy @gustaebel, @vadmium, @wimglenn, @zhangyangyu, @jarondl
PRs
  • bpo-26253: Add compressionlevel to tarfile stream #2962
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2016-02-01.00:55:23.398>
    labels = ['3.7', 'type-feature', 'library']
    title = 'tarfile in stream mode always set zlib compression level to 9'
    updated_at = <Date 2017-08-01.17:03:47.325>
    user = 'https://bugs.python.org/PatrikDufresne'

    bugs.python.org fields:

    activity = <Date 2017-08-01.17:03:47.325>
    actor = 'jarondl'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2016-02-01.00:55:23.398>
    creator = 'Patrik Dufresne'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 26253
    keywords = []
    message_count = 6.0
    messages = ['259304', '259308', '259987', '292628', '297220', '299622']
    nosy_count = 6.0
    nosy_names = ['lars.gustaebel', 'martin.panter', 'wim.glenn', 'xiang.zhang', 'Patrik Dufresne', 'jarondl']
    pr_nums = ['2962']
    priority = 'normal'
    resolution = None
    stage = None
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue26253'
    versions = ['Python 3.7']

    Activity

    1. PatrikDufresne commented on Feb 1, 2016

      PatrikDufresnemannequin
      MannequinAuthor

      When using tarfile.open(mode='w|gz'), the compression level is hard-coded to 9. Seed _Stream._init_write_gz():
      self.zlib.compressobj(9,

      1. In regards to zlib, I would start by replacing the value of 9 by zlib.Z_DEFAULT_COMPRESSION. This is the default value and zipfile is using it. Why using something different.

      2. Then, I would also love to control the compression level when calling tarfile.open()

    2. added
      type-bugAn unexpected behavior, bug, or error
      stdlibStandard Library Python modules in the Lib/ directory
      on Feb 1, 2016
    3. vadmium commented on Feb 1, 2016

      @vadmium
      Member

      It looks like the default has been hard-coded to 9 ever since tarfile was added to Python. The gzip module is also hard-coded to 9 since it was added. If tarfile is changed, maybe gzip should too.

      Why would you want to use zlib’s default (apparently 6)? Memory usage or speed perhaps? If we do change the default, maybe it is best to only do it in 3.6. I don’t see it as a bug fix, and there is a chance it could break someone’s code.

      To be able to control the compression level, perhaps you can already do it by wrapping the tar stream with GzipFile (untested):

      gz_writer = GzipFile(fileobj=raw_writer, mode="wb", compresslevel=...)
      tar_writer = tarfile.open(fileobj=gz_writer, mode="w|")
      tar_writer.addfile(...)
      tar_writer.close()
      gz_writer.close()

      If the default is changed, it certainly makes sense to add an easy compression level parameter, to be able to restore the old behaviour.

    4. added
      type-featureA feature request or enhancement
      and removed
      type-bugAn unexpected behavior, bug, or error
      on Feb 1, 2016
    5. vadmium commented on Feb 10, 2016

      @vadmium
      Member

      Actually it’s not really obvious from the signatures, but in the middle of the tarfile.open() documentation it says “. . . tarfile.open() accepts the keyword argument _compresslevel_”, so it should already be possible.

    6. zhangyangyu commented on Apr 30, 2017

      @zhangyangyu
      Member

      *compresslevel* takes effect for modes 'w:gz', 'r:gz', 'w:bz2', 'r:bz2', 'x:gz', 'x:bz2'. For stream modes, 'r|gz', 'w|gz', 'r|bz2', 'w|bz2', the *compresslevel* doesn't make sense. It seems not hard to make it possible but I'm not sure it's worth it or there is any reason it's hard-coded.

    7. wimglenn commented on Jun 28, 2017

      wimglennmannequin
      Mannequin

      This issue also got me. compresslevel kwarg works fine for tarfile.open(..., mode='w:gz') but raises exception for tarfile.open(..., mode='w|gz')

      I want to use stream compression, and compresslevel=1 is more than enough for my use case, the default of 9 is way too slow.

    8. jarondl commented on Aug 1, 2017

      jarondlmannequin
      Mannequin

      I have submitted a PR on GitHub #2962

    9. transferred this issue fromon Apr 10, 2022
    10. added
      3.12only security fixes
      and removed on Jun 9, 2022
    11. tiran commented on Jun 25, 2022

      @tiran
      Member

      The PR broke tests on systems without the optional bz2 module.

    12. reopened this on Jun 25, 2022
    13. tiran commented on Jun 25, 2022

      @tiran
      Member
      ======================================================================
      ERROR: test_wrong_compresslevels (test.test_tarfile.CompressLevelRaises.test_wrong_compresslevels)
      ----------------------------------------------------------------------
      Traceback (most recent call last):
        File "/Lib/test/test_tarfile.py", line 1620, in test_wrong_compresslevels
          tarfile.open(tmpname, "w:bz2", fobj, compresslevel=0)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/Lib/tarfile.py", line 1654, in open
          return func(name, filemode, fileobj, **kwargs)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/Lib/tarfile.py", line 1732, in bz2open
          raise CompressionError("bz2 module is not available") from None
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      tarfile.CompressionError: bz2 module is not available
      ----------------------------------------------------------------------
      
    14. added 2 commits that reference this issue on Jun 25, 2022
    15. hauntsaninja commented on Oct 12, 2022

      @hauntsaninja
      Contributor

      Thanks, looks like this has been completed and the test has been fixed

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.12only security fixesstdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

      Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions