(original) (raw)

changeset: 74494:fe36edf3a341 parent: 74492:582274636446 parent: 74493:7d405058e458 user: Nadeem Vawda nadeem.vawda@gmail.com date: Wed Jan 18 09:32:25 2012 +0200 files: Lib/gzip.py Lib/test/test_gzip.py Misc/NEWS description: Merge: #13781: Fix GzipFile to work with os.fdopen()'d file objects. diff -r 582274636446 -r fe36edf3a341 Lib/gzip.py --- a/Lib/gzip.py Wed Jan 18 05:05:41 2012 +0100 +++ b/Lib/gzip.py Wed Jan 18 09:32:25 2012 +0200 @@ -144,8 +144,10 @@ 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 -r 582274636446 -r fe36edf3a341 Lib/test/test_gzip.py --- a/Lib/test/test_gzip.py Wed Jan 18 05:05:41 2012 +0100 +++ b/Lib/test/test_gzip.py Wed Jan 18 09:32:25 2012 +0200 @@ -346,6 +346,14 @@ with io.TextIOWrapper(f, encoding="ascii") as t: self.assertEqual(t.readlines(), lines) + 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 -r 582274636446 -r fe36edf3a341 Misc/NEWS --- a/Misc/NEWS Wed Jan 18 05:05:41 2012 +0100 +++ b/Misc/NEWS Wed Jan 18 09:32:25 2012 +0200 @@ -447,6 +447,9 @@ 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. /nadeem.vawda@gmail.com