Skip to content

Commit

Permalink
Python 2.6 GzipFile is not a context manager.
Browse files Browse the repository at this point in the history
  • Loading branch information
Giorgio Salluzzo committed Feb 18, 2017
1 parent 1b4114a commit e03f47c
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions mocket/mocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,10 @@ def true_sendall(self, data, *args, **kwargs):
if line_no + 1 in gzipped_lines:
gzip_buffer = io.BytesIO()
gzip_file = gzip.GzipFile(mode='wb', fileobj=gzip_buffer)
gzip_file.write(line)
gzip_file.close()
try:
gzip_file.write(line)
finally:
gzip_file.close()
line = gzip_buffer.getvalue()
r_lines.append(line)
encoded_response = b'\r\n'.join(r_lines)
Expand Down Expand Up @@ -277,8 +279,11 @@ def true_sendall(self, data, *args, **kwargs):
try:
line = decode_utf8(line)
except UnicodeDecodeError:
with gzip.GzipFile(mode='rb', fileobj=io.BytesIO(line)) as f:
f = gzip.GzipFile(mode='rb', fileobj=io.BytesIO(line))
try:
line = f.read(len(line))
finally:
f.close()
line = decode_utf8(line)
gzipped_lines.append(line_no + 1)

Expand Down

0 comments on commit e03f47c

Please sign in to comment.