This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: GzipFile doesn't always ignore None as filename
Type: crash Stage: resolved
Components: Library (Lib) Versions: Python 2.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: bbayles, benjamin.peterson, da, ned.deily, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2018-03-10 00:46 by da, last changed 2022-04-11 14:58 by admin. This issue is now closed.

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 merged bbayles, 2018-03-13 01:49
Messages (8)
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!
msg316010 - (view) Author: Diego Argueta (da) * Date: 2018-05-01 19:18
Did this make it into 2.7.15? There aren't any release notes for it on the download page like usual.
msg316011 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2018-05-01 19:33
It looks like PR 6095 for this issue has not been reviewed or merged yet.

The commits for v2.7.15 are here:
msg316091 - (view) Author: bbayles (bbayles) * Date: 2018-05-02 23:49
Is there someone who might be in a position to review the PR? It's pretty short.
msg316112 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-05-03 07:28
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:
msg316323 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-05-09 10:14
New changeset afe5f633e49e0e873d42088ae56819609c803ba0 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)
Date User Action Args
2022-04-11 14:58:58adminsetgithub: 77219
2018-05-09 10:19:46serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2018-05-09 10:14:43serhiy.storchakasetmessages: + msg316323
2018-05-03 07:28:46serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg316112
2018-05-02 23:49:09bbaylessetmessages: + msg316091
2018-05-01 19:33:03ned.deilysetnosy: + ned.deily, benjamin.peterson
messages: + msg316011
2018-05-01 19:18:59dasetmessages: + msg316010
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