Fix GzipFile's handling of filenames given as bytes objects.

This commit is contained in:
Nadeem Vawda 2012-06-20 01:35:22 +02:00
parent f29ec4b0c8
commit 103e8113e4
3 changed files with 20 additions and 4 deletions

View file

@ -159,9 +159,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:
if hasattr(fileobj, 'mode'): mode = fileobj.mode
@ -236,7 +235,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: