summaryrefslogtreecommitdiff
path: root/kafka/codec.py
diff options
context:
space:
mode:
authorDana Powers <dana.powers@rd.io>2015-03-08 23:41:10 -0700
committerDana Powers <dana.powers@rd.io>2015-03-09 00:03:16 -0700
commit4d59678dd38393fcdc2ef627ca717d5e87d0e744 (patch)
tree0d4168ee4743dbb0eecadcc621e27d8a218923f5 /kafka/codec.py
parent610f01e96d3bfd9f632371de5bd6cf911a8e71ef (diff)
downloadkafka-python-4d59678dd38393fcdc2ef627ca717d5e87d0e744.tar.gz
Gzip context manager not supported in py2.6, so use try/finally instead
Diffstat (limited to 'kafka/codec.py')
-rw-r--r--kafka/codec.py19
1 files changed, 17 insertions, 2 deletions
diff --git a/kafka/codec.py b/kafka/codec.py
index 7883158..56689ce 100644
--- a/kafka/codec.py
+++ b/kafka/codec.py
@@ -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