]> granicus.if.org Git - python/commitdiff
Issue #13781: Fix GzipFile to work with os.fdopen()'d file objects.
authorNadeem Vawda <nadeem.vawda@gmail.com>
Wed, 18 Jan 2012 22:40:46 +0000 (00:40 +0200)
committerNadeem Vawda <nadeem.vawda@gmail.com>
Wed, 18 Jan 2012 22:40:46 +0000 (00:40 +0200)
Lib/gzip.py
Lib/test/test_gzip.py
Misc/NEWS
Modules/posixmodule.c

index 2bcb4dbfb0b386e85c1f0705ffa8c0c4a2328d83..8fdac8397d4d7e9e385dfd445abc28513619dab8 100644 (file)
@@ -88,8 +88,12 @@ class GzipFile(io.BufferedIOBase):
         if fileobj is None:
             fileobj = self.myfileobj = __builtin__.open(filename, mode or 'rb')
         if filename is None:
-            if hasattr(fileobj, 'name'): filename = fileobj.name
-            else: filename = ''
+            # 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 = ''
         if mode is None:
             if hasattr(fileobj, 'mode'): mode = fileobj.mode
             else: mode = 'rb'
index 573879e52285455637a7ecd9e7a1be2a0c7de254..a28cd34b0ca9a06b467c7f807466b13e6db87e94 100644 (file)
@@ -274,6 +274,14 @@ class TestGzip(unittest.TestCase):
             d = f.read()
             self.assertEqual(d, data1 * 50, "Incorrect data in file")
 
+    def test_fileobj_from_fdopen(self):
+        # Issue #13781: Creating a GzipFile using a fileobj from os.fdopen()
+        # should not embed the fake filename "<fdopen>" in the output file.
+        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:
+                self.assertEqual(g.name, "")
+
 def test_main(verbose=None):
     test_support.run_unittest(TestGzip)
 
index 9d32b125592ca47d07fb02e8bb65701e320591d6..0233823bfb45ea8a400cf0dec3344d008c987455 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -89,6 +89,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #13781: Prevent gzip.GzipFile from using the dummy filename provided by
+  file objects opened with os.fdopen().
+
 - Issue #13589: Fix some serialization primitives in the aifc module.
   Patch by Oleg Plakhotnyuk.
 
index f249e1e8cb5745ea39dc83d460e37842111ac421..d3b14c17fc198b64118a5fc5319a9ac0d94741d8 100644 (file)
@@ -6755,6 +6755,8 @@ posix_fdopen(PyObject *self, PyObject *args)
     PyMem_FREE(mode);
     if (fp == NULL)
         return posix_error();
+    /* The dummy filename used here must be kept in sync with the value
+       tested against in gzip.GzipFile.__init__() - see issue #13781. */
     f = PyFile_FromFile(fp, "<fdopen>", orgmode, fclose);
     if (f != NULL)
         PyFile_SetBufSize(f, bufsize);