diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2014-01-23 16:40:03 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2014-01-23 16:40:03 (GMT) |
commit | 183e3477968c5b1b003fa18926fbbca7ab96ac36 (patch) | |
tree | 6dd3f8297c8ada24f5481eadf783262818b80deb /Lib | |
parent | 8ce6e1100e5bb13c2e714e6cdf5d8fac77f39a94 (diff) | |
download | cpython-183e3477968c5b1b003fa18926fbbca7ab96ac36.zip cpython-183e3477968c5b1b003fa18926fbbca7ab96ac36.tar.gz cpython-183e3477968c5b1b003fa18926fbbca7ab96ac36.tar.bz2 |
asyncio (Tulip issue 110): StreamReader.read() and StreamReader.readline() now
raise a RuntimeError, instead of using an assertion, if another coroutine is
already waiting for incoming data
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/asyncio/streams.py | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/Lib/asyncio/streams.py b/Lib/asyncio/streams.py index b1648e0..b53080e 100644 --- a/Lib/asyncio/streams.py +++ b/Lib/asyncio/streams.py @@ -21,7 +21,7 @@ def open_connection(host=None, port=None, *, """A wrapper for create_connection() returning a (reader, writer) pair. The reader returned is a StreamReader instance; the writer is a - StreamWriter. + StreamWriter instance. The arguments are all the usual arguments to create_connection() except protocol_factory; most common are positional host and port, @@ -284,6 +284,16 @@ class StreamReader: else: self._paused = True + def _create_waiter(self, func_name): + # StreamReader uses a future to link the protocol feed_data() method + # to a read coroutine. Running two read coroutines at the same time + # would have an unexpected behaviour. It would not possible to know + # which coroutine would get the next data. + if self._waiter is not None: + raise RuntimeError('%s() called while another coroutine is ' + 'already waiting for incoming data' % func_name) + return futures.Future(loop=self._loop) + @tasks.coroutine def readline(self): if self._exception is not None: @@ -318,8 +328,7 @@ class StreamReader: break if not_enough: - assert self._waiter is None - self._waiter = futures.Future(loop=self._loop) + self._waiter = self._create_waiter('readline') try: yield from self._waiter finally: @@ -341,16 +350,14 @@ class StreamReader: if n < 0: while not self._eof: - assert not self._waiter - self._waiter = futures.Future(loop=self._loop) + self._waiter = self._create_waiter('read') try: yield from self._waiter finally: self._waiter = None else: if not self._byte_count and not self._eof: - assert not self._waiter - self._waiter = futures.Future(loop=self._loop) + self._waiter = self._create_waiter('read') try: yield from self._waiter finally: |