diff --git a/smart_open/tests/test_s3.py b/smart_open/tests/test_s3.py index b17ef1f8..088e1d5d 100644 --- a/smart_open/tests/test_s3.py +++ b/smart_open/tests/test_s3.py @@ -379,11 +379,40 @@ 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', 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( + f's3://{BUCKET_NAME}/{KEY_NAME}', '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..5d9fdedf 100644 --- a/smart_open/utils.py +++ b/smart_open/utils.py @@ -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__()