summaryrefslogtreecommitdiffstats
path: root/Lib/gzip.py
diff options
context:
space:
mode:
authorNadeem Vawda <nadeem.vawda@gmail.com>2012-06-19 23:48:50 (GMT)
committerNadeem Vawda <nadeem.vawda@gmail.com>2012-06-19 23:48:50 (GMT)
commit10c8791978203be95af2c4c1d7ce33496fac880c (patch)
treed30969af462c1c847aebb202b93e3b5a7cd43250 /Lib/gzip.py
parente67f48ce5e7ad122b17e23b2705bf66cff76d42b (diff)
parent103e8113e4bb4ad3687d641f660481c72904d571 (diff)
downloadcpython-10c8791978203be95af2c4c1d7ce33496fac880c.zip
cpython-10c8791978203be95af2c4c1d7ce33496fac880c.tar.gz
cpython-10c8791978203be95af2c4c1d7ce33496fac880c.tar.bz2
Fix GzipFile's handling of filenames given as bytes objects.
Add relevant tests for GzipFile, and also for BZ2File and LZMAFile.
Diffstat (limited to 'Lib/gzip.py')
-rw-r--r--Lib/gzip.py8
1 files changed, 4 insertions, 4 deletions
diff --git a/Lib/gzip.py b/Lib/gzip.py
index 412bf05..f6f63bb 100644
--- a/Lib/gzip.py
+++ b/Lib/gzip.py
@@ -182,9 +182,8 @@ class GzipFile(io.BufferedIOBase):
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:
mode = getattr(fileobj, 'mode', 'rb')
@@ -258,7 +257,8 @@ class GzipFile(io.BufferedIOBase):
# 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: