[Python-checkins] cpython: asyncio.streams.StreamReader: Add 'at_eof()' method

yury.selivanov python-checkins at python.org
Thu Feb 6 06:19:08 CET 2014


http://hg.python.org/cpython/rev/4a3761dedbd2
changeset:   88986:4a3761dedbd2
parent:      88979:260d6e1e9b0f
user:        Yury Selivanov <yselivanov at sprymix.com>
date:        Thu Feb 06 00:14:30 2014 -0500
summary:
  asyncio.streams.StreamReader: Add 'at_eof()' method

files:
  Lib/asyncio/streams.py                |   4 ++++
  Lib/test/test_asyncio/test_streams.py |  15 +++++++++++++++
  2 files changed, 19 insertions(+), 0 deletions(-)


diff --git a/Lib/asyncio/streams.py b/Lib/asyncio/streams.py
--- a/Lib/asyncio/streams.py
+++ b/Lib/asyncio/streams.py
@@ -293,6 +293,10 @@
             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
--- a/Lib/test/test_asyncio/test_streams.py
+++ b/Lib/test/test_asyncio/test_streams.py
@@ -204,6 +204,21 @@
         # 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.

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list