diff -r c8adc2c13c8b Lib/asyncio/streams.py --- a/Lib/asyncio/streams.py Wed May 13 10:58:35 2015 -0500 +++ b/Lib/asyncio/streams.py Wed May 13 13:01:10 2015 -0400 @@ -6,6 +6,7 @@ ] import socket +import sys if hasattr(socket, 'AF_UNIX'): __all__.extend(['open_unix_connection', 'start_unix_server']) @@ -19,6 +20,7 @@ _DEFAULT_LIMIT = 2**16 +_PY35 = sys.version_info >= (3, 5) class IncompleteReadError(EOFError): @@ -485,3 +487,15 @@ n -= len(block) return b''.join(blocks) + + if _PY35: + @coroutine + def __aiter__(self): + return self + + @coroutine + def __anext__(self): + val = yield from self.readline() + if val == b'': + raise StopAsyncIteration + return val diff -r c8adc2c13c8b Lib/test/test_asyncio/test_pep492.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Lib/test/test_asyncio/test_pep492.py Wed May 13 13:01:10 2015 -0400 @@ -0,0 +1,46 @@ +"""Tests support for new syntax introduced by PEP 492.""" + +import gc +import unittest +from unittest import mock + +import asyncio +from asyncio import test_utils + + +class BaseTest(test_utils.TestCase): + + def setUp(self): + self.loop = asyncio.new_event_loop() + self.set_event_loop(self.loop) + + def tearDown(self): + # just in case if we have transport close callbacks + test_utils.run_briefly(self.loop) + + self.loop.close() + gc.collect() + super().tearDown() + + +class StreamReaderTests(BaseTest): + + def test_readline(self): + DATA = b'line1\nline2\nline3' + + stream = asyncio.StreamReader(loop=self.loop) + stream.feed_data(DATA) + stream.feed_eof() + + async def reader(): + data = [] + async for line in stream: + data.append(line) + return data + + data = self.loop.run_until_complete(reader()) + self.assertEqual(data, [b'line1\n', b'line2\n', b'line3']) + + +if __name__ == '__main__': + unittest.main()