Title: GzipFile doesn't always ignore None as filename
Type: crash Stage: patch review
Components: Library (Lib) Versions: Python 2.7
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: bbayles, da
Priority: normal Keywords: patch

Created on 2018-03-10 00:46 by da, last changed 2018-03-13 01:49 by bbayles.

File name Uploaded Description Edit
gzip_filename_fix.patch da, 2018-03-10 00:46 Diff for suggested fix
Pull Requests
URL Status Linked Edit
PR 6095 open bbayles, 2018-03-13 01:49
Messages (3)
msg313512 - (view) Author: Diego Argueta (da) * Date: 2018-03-10 00:46
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:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/Users/diegoargueta/.pyenv/versions/2.7.14/lib/python2.7/", line 136, in __init__
  File "/Users/diegoargueta/.pyenv/versions/2.7.14/lib/python2.7/", line 170, in _write_gzip_header
    fname = os.path.basename(
  File "/Users/diegoargueta/.pyenv/versions/gds27/lib/python2.7/", 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.
msg313578 - (view) Author: bbayles (bbayles) * Date: 2018-03-11 01:51
da, would you mind if I add a test and a news entry to your patch and submit it as a Github pull request?
msg313632 - (view) Author: Diego Argueta (da) * Date: 2018-03-12 04:08
Yeah that's fine. Thanks!
Date User Action Args
2018-03-13 01:49:31bbaylessetstage: patch review
pull_requests: + pull_request5857
2018-03-12 04:08:21dasetmessages: + msg313632
2018-03-11 01:51:41bbaylessetnosy: + bbayles
messages: + msg313578
2018-03-10 00:46:10dacreate