Skip to content

Commit

Permalink
Add __next__ method to FileLikeProxy (#811)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddelange authored Mar 20, 2024
1 parent 6b7904a commit 46b422e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
26 changes: 26 additions & 0 deletions smart_open/tests/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,37 @@ def test_binary_iterator(self):
expected = u"выйду ночью в поле с конём".encode('utf-8').split(b' ')
_resource('s3').Object(BUCKET_NAME, KEY_NAME).put(Body=b'\n'.join(expected))

# test the __iter__ method
with self.assertApiCalls(GetObject=1):
with smart_open.s3.open(BUCKET_NAME, KEY_NAME, 'rb') as fin:
actual = [line.rstrip() for line in fin]
self.assertEqual(expected, actual)

# test the __next__ method
with self.assertApiCalls(GetObject=1):
with smart_open.s3.open(BUCKET_NAME, KEY_NAME, 'rb') as fin:
first = next(fin).rstrip()
self.assertEqual(expected[0], first)

def test_text_iterator(self):
expected = u"выйду ночью в поле с конём".split(' ')
uri = f's3://{BUCKET_NAME}/{KEY_NAME}.gz'

with smart_open.open(uri, 'w', encoding='utf-8') as fout:
fout.write('\n'.join(expected))

# test the __iter__ method
with self.assertApiCalls(GetObject=1):
with smart_open.open(uri, 'r', encoding='utf-8') as fin:
actual = [line.rstrip() for line in fin]
self.assertEqual(expected, actual)

# test the __next__ method
with self.assertApiCalls(GetObject=1):
with smart_open.open(uri, 'r', encoding='utf-8') as fin:
first = next(fin).rstrip()
self.assertEqual(expected[0], first)

def test_defer_seek(self):
content = b'englishman\nin\nnew\nyork\n'
_resource('s3').Object(BUCKET_NAME, KEY_NAME).put(Body=content)
Expand Down
5 changes: 4 additions & 1 deletion smart_open/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ def __init__(self, outer, inner):
def __exit__(self, *args, **kwargs):
"""Exit inner after exiting outer."""
try:
super().__exit__(*args, **kwargs)
return super().__exit__(*args, **kwargs)
finally:
self.__inner.__exit__(*args, **kwargs)

def __next__(self):
return self.__wrapped__.__next__()

0 comments on commit 46b422e

Please sign in to comment.