+20
-3
lines changedFilter options
+20
-3
lines changed Original file line number Diff line number Diff line change
@@ -95,9 +95,8 @@ def __init__(self, filename=None, mode=None,
95
95
if filename is None:
96
96
# Issue #13781: os.fdopen() creates a fileobj with a bogus name
97
97
# attribute. Avoid saving this in the gzip header's filename field.
98
-
if hasattr(fileobj, 'name') and fileobj.name != '<fdopen>':
99
-
filename = fileobj.name
100
-
else:
98
+
filename = getattr(fileobj, 'name', '')
99
+
if not isinstance(filename, basestring) or filename == '<fdopen>':
101
100
filename = ''
102
101
if mode is None:
103
102
if hasattr(fileobj, 'mode'): mode = fileobj.mode
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
6
6
import os
7
7
import io
8
8
import struct
9
+
import tempfile
9
10
gzip = test_support.import_module('gzip')
10
11
11
12
data1 = """ int length=DEFAULTALLOC, err = Z_OK;
@@ -331,6 +332,12 @@ def test_fileobj_from_fdopen(self):
331
332
with gzip.GzipFile(fileobj=f, mode="w") as g:
332
333
self.assertEqual(g.name, "")
333
334
335
+
def test_fileobj_from_io_open(self):
336
+
fd = os.open(self.filename, os.O_WRONLY | os.O_CREAT)
337
+
with io.open(fd, "wb") as f:
338
+
with gzip.GzipFile(fileobj=f, mode="w") as g:
339
+
self.assertEqual(g.name, "")
340
+
334
341
def test_fileobj_mode(self):
335
342
gzip.GzipFile(self.filename, "wb").close()
336
343
with open(self.filename, "r+b") as f:
@@ -359,6 +366,14 @@ def test_read_with_extra(self):
359
366
with gzip.GzipFile(fileobj=io.BytesIO(gzdata)) as f:
360
367
self.assertEqual(f.read(), b'Test')
361
368
369
+
def test_fileobj_without_name(self):
370
+
# Issue #33038: GzipFile should not assume that file objects that have
371
+
# a .name attribute use a non-None value.
372
+
with tempfile.SpooledTemporaryFile() as f:
373
+
with gzip.GzipFile(fileobj=f, mode='wb') as archive:
374
+
archive.write(b'data')
375
+
self.assertEqual(archive.name, '')
376
+
362
377
def test_main(verbose=None):
363
378
test_support.run_unittest(TestGzip)
364
379
Original file line number Diff line number Diff line change
@@ -94,6 +94,7 @@ Michael R Bax
94
94
Anthony Baxter
95
95
Mike Bayer
96
96
Samuel L. Bayer
97
+
Bo Bayles
97
98
Donald Beaudry
98
99
David Beazley
99
100
Carlo Beccarini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1
+
gzip.GzipFile no longer produces an AttributeError exception when used with
2
+
a file object with a non-string name attribute. Patch by Bo Bayles.
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4