From: Yury Selivanov Date: Thu, 6 Feb 2014 05:14:30 +0000 (-0500) Subject: asyncio.streams.StreamReader: Add 'at_eof()' method X-Git-Tag: v3.4.0rc1~77 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f0020f5d7762a73e77bc7be79251e2ebb3a4a727;p=thirdparty%2FPython%2Fcpython.git asyncio.streams.StreamReader: Add 'at_eof()' method --- diff --git a/Lib/asyncio/streams.py b/Lib/asyncio/streams.py index 3da1d10facb3..8fc21474e90c 100644 --- a/Lib/asyncio/streams.py +++ b/Lib/asyncio/streams.py @@ -293,6 +293,10 @@ class StreamReader: if not waiter.cancelled(): waiter.set_result(True) + def at_eof(self): + """Return True if the buffer is empty and 'feed_eof' was called.""" + return self._eof and not self._buffer + def feed_data(self, data): assert not self._eof, 'feed_data after feed_eof' diff --git a/Lib/test/test_asyncio/test_streams.py b/Lib/test/test_asyncio/test_streams.py index 83474a87eee9..ee3fb450291a 100644 --- a/Lib/test/test_asyncio/test_streams.py +++ b/Lib/test/test_asyncio/test_streams.py @@ -204,6 +204,21 @@ class StreamReaderTests(unittest.TestCase): # expected to be empty now. self.assertEqual(b'', stream._buffer) + def test_at_eof(self): + stream = asyncio.StreamReader(loop=self.loop) + self.assertFalse(stream.at_eof()) + + stream.feed_data(b'some data\n') + self.assertFalse(stream.at_eof()) + + self.loop.run_until_complete(stream.readline()) + self.assertFalse(stream.at_eof()) + + stream.feed_data(b'some data\n') + stream.feed_eof() + self.loop.run_until_complete(stream.readline()) + self.assertTrue(stream.at_eof()) + def test_readline_limit(self): # Read one line. StreamReaders are fed with data after # their 'readline' methods are called.