From: Skip Montanaro Date: Thu, 23 May 2002 01:43:05 +0000 (+0000) Subject: force gzip module to open files using 'b'inary mode. X-Git-Tag: v2.3c1~5610 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=12424bc0eff82e0ebf5134cca1139b7d4b6faed0;p=python force gzip module to open files using 'b'inary mode. closes patch #536278. --- diff --git a/Lib/gzip.py b/Lib/gzip.py index b2bbeda3e8..9e198c75d5 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -35,6 +35,10 @@ class GzipFile: def __init__(self, filename=None, mode=None, compresslevel=9, fileobj=None): + # guarantee the file is opened in binary mode on platforms + # that care about that sort of thing + if mode and 'b' not in mode: + mode += 'b' if fileobj is None: fileobj = self.myfileobj = __builtin__.open(filename, mode or 'rb') if filename is None: diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py index a5660a9ec2..d42dee6667 100644 --- a/Lib/test/test_gzip.py +++ b/Lib/test/test_gzip.py @@ -18,7 +18,7 @@ data2 = """/* zlibmodule.c -- gzip-compatible data compression */ f = gzip.GzipFile(filename, 'wb') ; f.write(data1 * 50) ; f.close() -f = gzip.GzipFile(filename, 'rb') ; d = f.read() ; f.close() +f = gzip.GzipFile(filename, 'r') ; d = f.read() ; f.close() verify(d == data1*50) # Append to the previous file @@ -75,4 +75,8 @@ for pos in range(0, 256, 16): f.write('GZ\n') f.close() +f = gzip.GzipFile(filename, 'r') +verify(f.myfileobj.mode == 'rb') +f.close() + os.unlink(filename)