[Python-checkins] cpython (2.7): Issue #13781: Fix GzipFile to work with os.fdopen()'d file objects.
nadeem.vawda
python-checkins at python.org
Wed Jan 18 23:41:56 CET 2012
http://hg.python.org/cpython/rev/a08e9e84f33f
changeset: 74514:a08e9e84f33f
branch: 2.7
parent: 74501:f824744557ba
user: Nadeem Vawda <nadeem.vawda at gmail.com>
date: Thu Jan 19 00:40:46 2012 +0200
summary:
Issue #13781: Fix GzipFile to work with os.fdopen()'d file objects.
files:
Lib/gzip.py | 8 ++++++--
Lib/test/test_gzip.py | 8 ++++++++
Misc/NEWS | 3 +++
Modules/posixmodule.c | 2 ++
4 files changed, 19 insertions(+), 2 deletions(-)
diff --git a/Lib/gzip.py b/Lib/gzip.py
--- a/Lib/gzip.py
+++ b/Lib/gzip.py
@@ -88,8 +88,12 @@
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'
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
@@ -274,6 +274,14 @@
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)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -89,6 +89,9 @@
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.
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -6755,6 +6755,8 @@
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);
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list