[Python-checkins] cpython (merge 3.2 -> default): Merge: #13781: Fix GzipFile to work with os.fdopen()'d file objects.

nadeem.vawda python-checkins at python.org
Wed Jan 18 08:32:37 CET 2012


http://hg.python.org/cpython/rev/fe36edf3a341
changeset:   74494:fe36edf3a341
parent:      74492:582274636446
parent:      74493:7d405058e458
user:        Nadeem Vawda <nadeem.vawda at gmail.com>
date:        Wed Jan 18 09:32:25 2012 +0200
summary:
  Merge: #13781: Fix GzipFile to work with os.fdopen()'d file objects.

files:
  Lib/gzip.py           |  6 ++++--
  Lib/test/test_gzip.py |  8 ++++++++
  Misc/NEWS             |  3 +++
  3 files changed, 15 insertions(+), 2 deletions(-)


diff --git a/Lib/gzip.py b/Lib/gzip.py
--- a/Lib/gzip.py
+++ b/Lib/gzip.py
@@ -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 --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py
--- a/Lib/test/test_gzip.py
+++ b/Lib/test/test_gzip.py
@@ -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 --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -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.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list