Skip to content

Commit

Permalink
Resolve Issue #153: don't wrap GzipFile in contextlib.closing
Browse files Browse the repository at this point in the history
This is no longer necessary since we dropped support for Python 2.6.
The GzipFile from Py2.7 and above is already a context manager, so the
closing is not required.

https://docs.python.org/2/library/gzip.html
  • Loading branch information
mpenkov committed Dec 2, 2017
1 parent 084f43e commit 2c79505
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
3 changes: 1 addition & 2 deletions smart_open/smart_open_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@

if IS_PY2:
import cStringIO as StringIO
import contextlib

if sys.version_info[0] == 2:
import httplib
Expand Down Expand Up @@ -304,7 +303,7 @@ def _detect_codec(filename):


def _wrap_gzip(fileobj, mode):
return contextlib.closing(gzip.GzipFile(fileobj=fileobj, mode=mode))
return gzip.GzipFile(fileobj=fileobj, mode=mode)


def _wrap_none(fileobj, mode):
Expand Down
11 changes: 5 additions & 6 deletions smart_open/tests/test_s3.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
import contextlib
import logging
import gzip
import io
Expand Down Expand Up @@ -131,7 +130,7 @@ def test_read_gzip(self):
expected = u'раcцветали яблони и груши, поплыли туманы над рекой...'.encode('utf-8')
buf = io.BytesIO()
buf.close = lambda: None # keep buffer open so that we can .getvalue()
with contextlib.closing(gzip.GzipFile(fileobj=buf, mode='w')) as zipfile:
with gzip.GzipFile(fileobj=buf, mode='w') as zipfile:
zipfile.write(expected)
create_bucket_and_key(contents=buf.getvalue())

Expand All @@ -145,12 +144,12 @@ def test_read_gzip(self):
# Make sure the buffer we wrote is legitimate gzip.
#
sanity_buf = io.BytesIO(buf.getvalue())
with contextlib.closing(gzip.GzipFile(fileobj=sanity_buf)) as zipfile:
with gzip.GzipFile(fileobj=sanity_buf) as zipfile:
self.assertEqual(zipfile.read(), expected)

_LOGGER.debug('starting actual test')
with smart_open.s3.BufferedInputBase('mybucket', 'mykey') as fin:
with contextlib.closing(gzip.GzipFile(fileobj=fin)) as zipfile:
with gzip.GzipFile(fileobj=fin) as zipfile:
actual = zipfile.read()

self.assertEqual(expected, actual)
Expand Down Expand Up @@ -261,11 +260,11 @@ def test_gzip(self):

expected = u'а не спеть ли мне песню... о любви'.encode('utf-8')
with smart_open.s3.BufferedOutputBase('mybucket', 'writekey') as fout:
with contextlib.closing(gzip.GzipFile(fileobj=fout, mode='w')) as zipfile:
with gzip.GzipFile(fileobj=fout, mode='w') as zipfile:
zipfile.write(expected)

with smart_open.s3.BufferedInputBase('mybucket', 'writekey') as fin:
with contextlib.closing(gzip.GzipFile(fileobj=fin)) as zipfile:
with gzip.GzipFile(fileobj=fin) as zipfile:
actual = zipfile.read()

self.assertEqual(expected, actual)
Expand Down

0 comments on commit 2c79505

Please sign in to comment.