]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-33038: Fix gzip.GzipFile for file objects with a non-string name attribute. ...
authorBo Bayles <bbayles@gmail.com>
Wed, 9 May 2018 10:14:40 +0000 (05:14 -0500)
committerSerhiy Storchaka <storchaka@gmail.com>
Wed, 9 May 2018 10:14:40 +0000 (13:14 +0300)
Lib/gzip.py
Lib/test/test_gzip.py
Misc/ACKS
Misc/NEWS.d/next/Library/2018-03-10-20-14-36.bpo-33038.yA6CP5.rst [new file with mode: 0644]

index 07c6db493b0b368aebb974d591a448f3412f863d..76ace394f482ad193643bbe100c36c80c42f9732 100644 (file)
@@ -95,9 +95,8 @@ class GzipFile(io.BufferedIOBase):
         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 not isinstance(filename, basestring) or filename == '<fdopen>':
                 filename = ''
         if mode is None:
             if hasattr(fileobj, 'mode'): mode = fileobj.mode
index 902d93fe043f8e42d2c393407a536a198703f7fe..cdb1af5c3d133ed31beb95e15f951f66ed9c9659 100644 (file)
@@ -6,6 +6,7 @@ from test import test_support
 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 @@ class TestGzip(unittest.TestCase):
             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,14 @@ class TestGzip(unittest.TestCase):
         with gzip.GzipFile(fileobj=io.BytesIO(gzdata)) as f:
             self.assertEqual(f.read(), b'Test')
 
+    def test_fileobj_without_name(self):
+        # 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')
+                self.assertEqual(archive.name, '')
+
 def test_main(verbose=None):
     test_support.run_unittest(TestGzip)
 
index 580b0c5bf76dd59917747b696df38d65908bbbb7..458f31e6a6b75a470a494f9b6567de2ac3cbd032 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -94,6 +94,7 @@ Michael R Bax
 Anthony Baxter
 Mike Bayer
 Samuel L. Bayer
+Bo Bayles
 Donald Beaudry
 David Beazley
 Carlo Beccarini
diff --git a/Misc/NEWS.d/next/Library/2018-03-10-20-14-36.bpo-33038.yA6CP5.rst b/Misc/NEWS.d/next/Library/2018-03-10-20-14-36.bpo-33038.yA6CP5.rst
new file mode 100644 (file)
index 0000000..22d394b
--- /dev/null
@@ -0,0 +1,2 @@
+gzip.GzipFile no longer produces an AttributeError exception when used with
+a file object with a non-string name attribute. Patch by Bo Bayles.