[Python-checkins] cpython (3.2): Fix GzipFile's handling of filenames given as bytes objects.
nadeem.vawda
python-checkins at python.org
Wed Jun 20 01:51:45 CEST 2012
http://hg.python.org/cpython/rev/e044fa016c85
changeset: 77520:e044fa016c85
branch: 3.2
parent: 77507:e1cd1f430ff1
user: Nadeem Vawda <nadeem.vawda at gmail.com>
date: Wed Jun 20 01:35:22 2012 +0200
summary:
Fix GzipFile's handling of filenames given as bytes objects.
files:
Lib/gzip.py | 8 ++++----
Lib/test/test_gzip.py | 14 ++++++++++++++
Misc/NEWS | 2 ++
3 files changed, 20 insertions(+), 4 deletions(-)
diff --git a/Lib/gzip.py b/Lib/gzip.py
--- a/Lib/gzip.py
+++ b/Lib/gzip.py
@@ -159,9 +159,8 @@
if fileobj is None:
fileobj = self.myfileobj = builtins.open(filename, mode or 'rb')
if filename is None:
- if hasattr(fileobj, 'name') and isinstance(fileobj.name, str):
- filename = fileobj.name
- else:
+ filename = getattr(fileobj, 'name', '')
+ if not isinstance(filename, (str, bytes)):
filename = ''
if mode is None:
if hasattr(fileobj, 'mode'): mode = fileobj.mode
@@ -236,7 +235,8 @@
# RFC 1952 requires the FNAME field to be Latin-1. Do not
# include filenames that cannot be represented that way.
fname = os.path.basename(self.name)
- fname = fname.encode('latin-1')
+ if not isinstance(fname, bytes):
+ fname = fname.encode('latin-1')
if fname.endswith(b'.gz'):
fname = fname[:-3]
except UnicodeEncodeError:
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
@@ -331,6 +331,20 @@
with gzip.GzipFile(fileobj=f, mode="w") as g:
pass
+ def test_bytes_filename(self):
+ str_filename = self.filename
+ try:
+ bytes_filename = str_filename.encode("ascii")
+ except UnicodeEncodeError:
+ self.skipTest("Temporary file name needs to be ASCII")
+ with gzip.GzipFile(bytes_filename, "wb") as f:
+ f.write(data1 * 50)
+ with gzip.GzipFile(bytes_filename, "rb") as f:
+ self.assertEqual(f.read(), data1 * 50)
+ # Sanity check that we are actually operating on the right file.
+ with gzip.GzipFile(str_filename, "rb") as f:
+ self.assertEqual(f.read(), data1 * 50)
+
# Testing compress/decompress shortcut functions
def test_compress(self):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -70,6 +70,8 @@
Library
-------
+- Fix GzipFile's handling of filenames given as bytes objects.
+
- Issue #15101: Make pool finalizer avoid joining current thread.
- Issue #15036: Mailbox no longer throws an error if a flush is done
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list