From e1b7a1a0050fe1bd6b0ab2ceba024eeffc6d085f Mon Sep 17 00:00:00 2001 From: Yury Selivanov Date: Thu, 16 Nov 2017 21:59:53 -0500 Subject: [PATCH] Make Transport.resume_reading() and pause_reading() idempotent. Add is_reading(). Fixes issue #93. Implements https://github.com/python/cpython/pull/528 asyncio change. --- tests/test_tcp.py | 11 +++++++++++ uvloop/handles/stream.pyx | 17 ++++++----------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/tests/test_tcp.py b/tests/test_tcp.py index 08d4eb66..07c74027 100644 --- a/tests/test_tcp.py +++ b/tests/test_tcp.py @@ -549,10 +549,15 @@ async def test_client(addr): t.set_protocol(p) self.assertFalse(t._paused) + self.assertTrue(t.is_reading()) t.pause_reading() + t.pause_reading() # Check that it's OK to call it 2nd time. self.assertTrue(t._paused) + self.assertFalse(t.is_reading()) t.resume_reading() + t.resume_reading() # Check that it's OK to call it 2nd time. self.assertFalse(t._paused) + self.assertTrue(t.is_reading()) sock = t.get_extra_info('socket') self.assertIs(sock, t.get_extra_info('socket')) @@ -580,6 +585,12 @@ async def test_client(addr): t.abort() self.assertTrue(t._closing) + self.assertFalse(t.is_reading()) + # Check that pause_reading and resume_reading don't raise + # errors if called after the transport is closed. + t.pause_reading() + t.resume_reading() + await fut # Test that peername and sockname are available after diff --git a/uvloop/handles/stream.pyx b/uvloop/handles/stream.pyx index 28762d48..6619b30f 100644 --- a/uvloop/handles/stream.pyx +++ b/uvloop/handles/stream.pyx @@ -664,21 +664,16 @@ cdef class UVStream(UVBaseTransport): def can_write_eof(self): return True - def pause_reading(self): - self._ensure_alive() + def is_reading(self): + return self._is_reading() - if self._closing: - raise RuntimeError('Cannot pause_reading() when closing') - if not self._is_reading(): - raise RuntimeError('Already paused') + def pause_reading(self): + if self._closing or not self._is_reading(): + return self._stop_reading() def resume_reading(self): - self._ensure_alive() - - if self._is_reading(): - raise RuntimeError('Not paused') - if self._closing: + if self._is_reading() or self._closing: return self._start_reading()