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

GzipFile doesn't always ignore None as filename #77219

Closed
da mannequin opened this issue Mar 10, 2018 · 8 comments
Closed

GzipFile doesn't always ignore None as filename #77219

da mannequin opened this issue Mar 10, 2018 · 8 comments
Labels
stdlib Python modules in the Lib dir type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@da
Copy link
Mannequin

da mannequin commented Mar 10, 2018

BPO 33038
Nosy @benjaminp, @ned-deily, @serhiy-storchaka, @bbayles
PRs
  • [2.7] bpo-33038: gzip.GzipFile assumes non-None name attribute #6095
  • Files
  • gzip_filename_fix.patch: Diff for suggested fix
  • 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 2018-05-09.10:19:46.430>
    created_at = <Date 2018-03-10.00:46:10.803>
    labels = ['library', 'type-crash']
    title = "GzipFile doesn't always ignore None as filename"
    updated_at = <Date 2018-05-09.10:19:46.430>
    user = 'https://bugs.python.org/da'

    bugs.python.org fields:

    activity = <Date 2018-05-09.10:19:46.430>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-05-09.10:19:46.430>
    closer = 'serhiy.storchaka'
    components = ['Library (Lib)']
    creation = <Date 2018-03-10.00:46:10.803>
    creator = 'da'
    dependencies = []
    files = ['47473']
    hgrepos = []
    issue_num = 33038
    keywords = ['patch']
    message_count = 8.0
    messages = ['313512', '313578', '313632', '316010', '316011', '316091', '316112', '316323']
    nosy_count = 5.0
    nosy_names = ['benjamin.peterson', 'ned.deily', 'serhiy.storchaka', 'bbayles', 'da']
    pr_nums = ['6095']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'crash'
    url = 'https://bugs.python.org/issue33038'
    versions = ['Python 2.7']

    @da
    Copy link
    Mannequin Author

    da mannequin commented Mar 10, 2018

    The Python documentation states that if the GzipFile can't determine a filename from fileobj it'll use an empty string and won't be included in the header. Unfortunately, this doesn't work for SpooledTemporaryFile which has a name attribute but doesn't set it initially. The result is a crash.

    To reproduce

    import gzip
    import tempfile
    
    with tempfile.SpooledTemporaryFile() as fd:
        with gzip.GzipFile(mode='wb', fileobj=fd) as gz:
            gz.write(b'asdf')
    

    Result:

    Traceback (most recent call last):
      File "<stdin>", line 2, in <module>
      File "/Users/diegoargueta/.pyenv/versions/2.7.14/lib/python2.7/gzip.py", line 136, in __init__
        self._write_gzip_header()
      File "/Users/diegoargueta/.pyenv/versions/2.7.14/lib/python2.7/gzip.py", line 170, in _write_gzip_header
        fname = os.path.basename(self.name)
      File "/Users/diegoargueta/.pyenv/versions/gds27/lib/python2.7/posixpath.py", line 114, in basename
        i = p.rfind('/') + 1
    AttributeError: 'NoneType' object has no attribute 'rfind'
    

    This doesn't happen on Python 3.6, where the null filename is handled properly. I've attached a patch file that fixed the issue for me.

    @da da mannequin added stdlib Python modules in the Lib dir type-crash A hard crash of the interpreter, possibly with a core dump labels Mar 10, 2018
    @bbayles
    Copy link
    Mannequin

    bbayles mannequin commented Mar 11, 2018

    da, would you mind if I add a test and a news entry to your patch and submit it as a Github pull request?

    @da
    Copy link
    Mannequin Author

    da mannequin commented Mar 12, 2018

    Yeah that's fine. Thanks!

    @da
    Copy link
    Mannequin Author

    da mannequin commented May 1, 2018

    Did this make it into 2.7.15? There aren't any release notes for it on the download page like usual.

    @ned-deily
    Copy link
    Member

    It looks like PR 6095 for this issue has not been reviewed or merged yet.

    The commits for v2.7.15 are here:
    https://github.com/python/cpython/commits/v2.7.15

    @bbayles
    Copy link
    Mannequin

    bbayles mannequin commented May 2, 2018

    Is there someone who might be in a position to review the PR? It's pretty short.

    @serhiy-storchaka
    Copy link
    Member

    The workaround in the current 2.7 is specifying an explicit filename argument:

    with tempfile.SpooledTemporaryFile() as fd:
        with gzip.GzipFile(filename='', mode='wb', fileobj=fd) as gz:
            gz.write(b'asdf')

    @serhiy-storchaka
    Copy link
    Member

    New changeset afe5f63 by Serhiy Storchaka (Bo Bayles) in branch '2.7':
    bpo-33038: Fix gzip.GzipFile for file objects with a non-string name attribute. (GH-6095)
    afe5f63

    @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
    stdlib Python modules in the Lib dir type-crash A hard crash of the interpreter, possibly with a core dump
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants