diff --git a/smart_open/tests/test_s3.py b/smart_open/tests/test_s3.py index b17ef1f8..ff44ad4f 100644 --- a/smart_open/tests/test_s3.py +++ b/smart_open/tests/test_s3.py @@ -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) diff --git a/smart_open/utils.py b/smart_open/utils.py index 505e252b..2be57d19 100644 --- a/smart_open/utils.py +++ b/smart_open/utils.py @@ -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__()