From: Nadeem Vawda Date: Wed, 18 Jan 2012 07:25:58 +0000 (+0200) Subject: Issue #13781: Fix GzipFile to work with os.fdopen()'d file objects. X-Git-Tag: v3.2.3rc1~161 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=892b0b928df40da72b0d88c5e1a2c879eff543c4;p=thirdparty%2FPython%2Fcpython.git Issue #13781: Fix GzipFile to work with os.fdopen()'d file objects. --- diff --git a/Lib/gzip.py b/Lib/gzip.py index ba2149ebf970..4462187116df 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -156,8 +156,10 @@ class GzipFile(io.BufferedIOBase): if fileobj is None: fileobj = self.myfileobj = builtins.open(filename, mode or 'rb') if filename is None: - if hasattr(fileobj, 'name'): filename = fileobj.name - else: filename = '' + if hasattr(fileobj, 'name') and isinstance(fileobj.name, str): + filename = fileobj.name + else: + filename = '' if mode is None: if hasattr(fileobj, 'mode'): mode = fileobj.mode else: mode = 'rb' diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py index 2b0ac36c23c1..5ae7467e669b 100644 --- a/Lib/test/test_gzip.py +++ b/Lib/test/test_gzip.py @@ -323,6 +323,14 @@ class TestGzip(unittest.TestCase): self.assertEqual(f.read(100), b'') self.assertEqual(nread, len(uncompressed)) + def test_fileobj_from_fdopen(self): + # Issue #13781: Opening a GzipFile for writing fails when using a + # fileobj created with os.fdopen(). + fd = os.open(self.filename, os.O_WRONLY | os.O_CREAT) + with os.fdopen(fd, "wb") as f: + with gzip.GzipFile(fileobj=f, mode="w") as g: + pass + # Testing compress/decompress shortcut functions def test_compress(self): diff --git a/Misc/NEWS b/Misc/NEWS index bceed4665335..b3c5b88bed35 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -100,6 +100,9 @@ Core and Builtins Library ------- +- Issue #13781: Fix GzipFile bug that caused an exception to be raised when + opening for writing using a fileobj returned by os.fdopen(). + - Issue #13803: Under Solaris, distutils doesn't include bitness in the directory name.