Gzip context manager not supported in py2.6, so use try/finally instead

This commit is contained in:
Dana Powers
2015-03-08 23:41:10 -07:00
parent 610f01e96d
commit 4d59678dd3

View File

@@ -25,16 +25,31 @@ def has_snappy():
def gzip_encode(payload):
with BytesIO() as buf:
with gzip.GzipFile(fileobj=buf, mode="w") as gzipper:
# Gzip context manager introduced in python 2.6
# so old-fashioned way until we decide to not support 2.6
gzipper = gzip.GzipFile(fileobj=buf, mode="w")
try:
gzipper.write(payload)
finally:
gzipper.close()
result = buf.getvalue()
return result
def gzip_decode(payload):
with BytesIO(payload) as buf:
with gzip.GzipFile(fileobj=buf, mode='r') as gzipper:
# Gzip context manager introduced in python 2.6
# so old-fashioned way until we decide to not support 2.6
gzipper = gzip.GzipFile(fileobj=buf, mode='r')
try:
result = gzipper.read()
finally:
gzipper.close()
return result