Dont need context manager for BytesIO

This commit is contained in:
Dana Powers
2016-01-25 15:37:13 -08:00
parent 7e09258409
commit cd956d8d79

View File

@@ -38,33 +38,29 @@ def gzip_encode(payload, compresslevel=None):
if not compresslevel: if not compresslevel:
compresslevel = 9 compresslevel = 9
with BytesIO() as buf: buf = BytesIO()
# Gzip context manager introduced in python 2.6 # Gzip context manager introduced in python 2.7
# so old-fashioned way until we decide to not support 2.6 # so old-fashioned way until we decide to not support 2.6
gzipper = gzip.GzipFile(fileobj=buf, mode="w", compresslevel=compresslevel) gzipper = gzip.GzipFile(fileobj=buf, mode="w", compresslevel=compresslevel)
try: try:
gzipper.write(payload) gzipper.write(payload)
finally: finally:
gzipper.close() gzipper.close()
result = buf.getvalue() return buf.getvalue()
return result
def gzip_decode(payload): def gzip_decode(payload):
with BytesIO(payload) as buf: buf = BytesIO(payload)
# Gzip context manager introduced in python 2.6 # Gzip context manager introduced in python 2.7
# so old-fashioned way until we decide to not support 2.6 # so old-fashioned way until we decide to not support 2.6
gzipper = gzip.GzipFile(fileobj=buf, mode='r') gzipper = gzip.GzipFile(fileobj=buf, mode='r')
try: try:
result = gzipper.read() return gzipper.read()
finally: finally:
gzipper.close() gzipper.close()
return result
def snappy_encode(payload, xerial_compatible=True, xerial_blocksize=32*1024): def snappy_encode(payload, xerial_compatible=True, xerial_blocksize=32*1024):