Skip to content
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.

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

tarfile.open() raises TypeError when using compresslevel parameter with LZMA #76876

Closed
bbayles mannequin opened this issue Jan 28, 2018 · 5 comments
Closed

tarfile.open() raises TypeError when using compresslevel parameter with LZMA #76876

bbayles mannequin opened this issue Jan 28, 2018 · 5 comments
Labels
3.9 only security fixes 3.10 only security fixes 3.11 bug and security fixes stdlib Python modules in the Lib dir

Comments

@bbayles
Copy link
Mannequin

bbayles mannequin commented Jan 28, 2018

BPO 32695
Nosy @ambv, @bbayles, @ZackerySpytz, @miss-islington
PRs
  • bpo-32695: Add docs and tests for compresslevel and preset in tarfile #5397
  • bpo-32695: Docs and tests for compresslevel and preset kwargs in tarfile #21470
  • [3.9] bpo-32695: Docs and tests for compresslevel and preset kwargs in tarfile (GH-21470) #27674
  • [3.10] bpo-32695: Docs and tests for compresslevel and preset kwargs in tarfile (GH-21470) #27675
  • 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 = <Date 2021-08-09.10:39:27.763>
    created_at = <Date 2018-01-28.15:22:05.981>
    labels = ['library', '3.9', '3.10', '3.11']
    title = 'tarfile.open() raises TypeError when using compresslevel parameter with LZMA'
    updated_at = <Date 2021-08-09.10:39:27.758>
    user = 'https://github.com/bbayles'

    bugs.python.org fields:

    activity = <Date 2021-08-09.10:39:27.758>
    actor = 'lukasz.langa'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-08-09.10:39:27.763>
    closer = 'lukasz.langa'
    components = ['Library (Lib)']
    creation = <Date 2018-01-28.15:22:05.981>
    creator = 'bbayles'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 32695
    keywords = ['patch']
    message_count = 5.0
    messages = ['310954', '311582', '399254', '399256', '399257']
    nosy_count = 4.0
    nosy_names = ['lukasz.langa', 'bbayles', 'ZackerySpytz', 'miss-islington']
    pr_nums = ['5397', '21470', '27674', '27675']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue32695'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    @bbayles
    Copy link
    Mannequin Author

    bbayles mannequin commented Jan 28, 2018

    tarfile.open() allows for specifying both a compression type and a compression level. However, the compresslevel parameter doesn't work when using the xz modes.

        import tarfile
        good_archive = tarfile.open('/tmp/dummy.tar.gz', 'w:gz', compresslevel=9)
        bad_archive = tarfile.open('/tmp/dummy.tar.gz', 'w:xz', compresslevel=9)

    This gives an error in the tarfile.TarFile constructor:

    TypeError: __init__() got an unexpected keyword argument 'compresslevel'
    

    This appears to be due to tarfile.xzopen() specifying a preset keyword instead of a compresslevel keyword. This matches the LZMAFile compressor, but it means that it doesn't get passed in and is then carried along in the kwargs - see [1].

    Changing to lzma.LZMAFile(fileobj or name, mode, preset=compresslevel) seems to fix the issue. Assuming that's the right fix, I'll submit a PR.

    [1]

    cpython/Lib/tarfile.py

    Lines 1684 to 1699 in 374c6e1

    def xzopen(cls, name, mode="r", fileobj=None, preset=None, **kwargs):
    """Open lzma compressed tar archive name for reading or writing.
    Appending is not allowed.
    """
    if mode not in ("r", "w", "x"):
    raise ValueError("mode must be 'r', 'w' or 'x'")
    try:
    import lzma
    except ImportError:
    raise CompressionError("lzma module is not available")
    fileobj = lzma.LZMAFile(fileobj or name, mode, preset=preset)
    try:
    t = cls.taropen(name, mode, fileobj, **kwargs)

    @bbayles bbayles mannequin added 3.7 (EOL) end of life stdlib Python modules in the Lib dir labels Jan 28, 2018
    @bbayles
    Copy link
    Mannequin Author

    bbayles mannequin commented Feb 4, 2018

    In working on a patch I convinced myself that a better change might just be to document that you can use the *preset* keyword for LZMA compression instead of *compresslevel*.

    @ambv
    Copy link
    Contributor

    ambv commented Aug 9, 2021

    New changeset eb2d4a6 by Zackery Spytz in branch 'main':
    bpo-32695: Docs and tests for compresslevel and preset kwargs in tarfile (GH-21470)
    eb2d4a6

    @ambv
    Copy link
    Contributor

    ambv commented Aug 9, 2021

    New changeset ede221e by Miss Islington (bot) in branch '3.9':
    bpo-32695: Docs and tests for compresslevel and preset kwargs in tarfile (GH-21470) (GH-27674)
    ede221e

    @miss-islington
    Copy link
    Contributor

    New changeset d5c8ad2 by Miss Islington (bot) in branch '3.10':
    bpo-32695: Docs and tests for compresslevel and preset kwargs in tarfile (GH-21470)
    d5c8ad2

    @ambv ambv added 3.9 only security fixes 3.10 only security fixes 3.11 bug and security fixes and removed 3.7 (EOL) end of life labels Aug 9, 2021
    @ambv ambv closed this as completed Aug 9, 2021
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes 3.10 only security fixes 3.11 bug and security fixes stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants