Skip to content

Commit

Permalink
Add __next__ method to FileLikeProxy
Browse files Browse the repository at this point in the history
  • Loading branch information
ddelange committed Mar 20, 2024
1 parent 6b7904a commit f55c8c6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
25 changes: 25 additions & 0 deletions smart_open/tests/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,36 @@ 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(' ')
_resource('s3').Object(BUCKET_NAME, KEY_NAME).put(
Body='\n'.join(expected).encode('utf-8')
)

# test the __iter__ method
with self.assertApiCalls(GetObject=1):
with smart_open.open(f's3://{BUCKET_NAME}/{KEY_NAME}', 'r') 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(f's3://{BUCKET_NAME}/{KEY_NAME}', 'r') 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
3 changes: 3 additions & 0 deletions smart_open/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,6 @@ def __exit__(self, *args, **kwargs):
super().__exit__(*args, **kwargs)
finally:
self.__inner.__exit__(*args, **kwargs)

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

0 comments on commit f55c8c6

Please sign in to comment.