-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
[2.7] bpo-33038: gzip.GzipFile assumes non-None name attribute #6095
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
Changes from 7 commits
abb9ca0
112b3ee
7be4b8d
dee6d1b
639a871
865bfa3
7b4507e
ec55e8b
e74b9f8
8a259be
006e588
7fac6c1
f2793e9
b0f3fb1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,9 +95,11 @@ def __init__(self, filename=None, mode=None, | |
if filename is None: | ||
# Issue #13781: os.fdopen() creates a fileobj with a bogus name | ||
# attribute. Avoid saving this in the gzip header's filename field. | ||
if hasattr(fileobj, 'name') and fileobj.name != '<fdopen>': | ||
filename = fileobj.name | ||
else: | ||
filename = getattr(fileobj, 'name', '') | ||
if ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This formatting looks uncommon. I suggest to remove suspicious newlines. Could it fit in one line? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even with if (not isinstance(filename, basestring) or
filename == '<fdopen>'):
filename = '' because it is hard to see from the next line. If you'd rather some other style that's fine, but there doesn't seem to be a precedent in this file? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can write it in the line of length 79. This is in the PEP 8 limit. Even if it be out of the limit, PEP 8 suggests the following options:
if (not isinstance(filename, basestring) or
filename == '<fdopen>'):
filename = ''
if (not isinstance(filename, basestring) or
filename == '<fdopen>'):
# comment
filename = ''
if (not isinstance(filename, basestring)
or filename == '<fdopen>'):
filename = '' There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I'm familiar with the PEP. I got rid of the parens and put everything on one line. |
||
not isinstance(filename, (str, unicode)) or | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
filename == '<fdopen>' | ||
): | ||
filename = '' | ||
if mode is None: | ||
if hasattr(fileobj, 'mode'): mode = fileobj.mode | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
import os | ||
import io | ||
import struct | ||
import tempfile | ||
gzip = test_support.import_module('gzip') | ||
|
||
data1 = """ int length=DEFAULTALLOC, err = Z_OK; | ||
|
@@ -331,6 +332,12 @@ def test_fileobj_from_fdopen(self): | |
with gzip.GzipFile(fileobj=f, mode="w") as g: | ||
self.assertEqual(g.name, "") | ||
|
||
def test_fileobj_from_io_open(self): | ||
fd = os.open(self.filename, os.O_WRONLY | os.O_CREAT) | ||
with io.open(fd, "wb") as f: | ||
with gzip.GzipFile(fileobj=f, mode="w") as g: | ||
self.assertEqual(g.name, "") | ||
|
||
def test_fileobj_mode(self): | ||
gzip.GzipFile(self.filename, "wb").close() | ||
with open(self.filename, "r+b") as f: | ||
|
@@ -359,6 +366,13 @@ def test_read_with_extra(self): | |
with gzip.GzipFile(fileobj=io.BytesIO(gzdata)) as f: | ||
self.assertEqual(f.read(), b'Test') | ||
|
||
def test_fileobj_without_name(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest to backport And add a similar test with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All right then. |
||
# Issue #33038: GzipFile should not assume that file objects that have | ||
# a .name attribute use a non-None value. | ||
with tempfile.SpooledTemporaryFile() as f: | ||
with gzip.GzipFile(fileobj=f, mode='wb') as archive: | ||
archive.write(b'data') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add an assertion for |
||
|
||
def test_main(verbose=None): | ||
test_support.run_unittest(TestGzip) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Using a tempfile.SpooledTemporaryFile object with gzip.GzipFile no longer | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mention also the case of io.open() with a file descriptor. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, I don't think this is a new addition? That wasn't what the bug was aimed at - only for special file objects that (a) have a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This PR fixes a case when fileobj has a non-string name attribute. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now I understand - in that case the |
||
produces an AttributeError exception. Patch by Bo Bayles. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have noticed that your name is not in Misc/ACKS in this branch. Please add it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest to backport the code from 3.x (with adding a special case for
'<fdopen>'
).