diff options
author | Yury Selivanov <yury@magic.io> | 2017-12-18 22:03:23 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-18 22:03:23 (GMT) |
commit | d757aaf9dd767d13205bf9917e520ebf43e7f6e5 (patch) | |
tree | 2e0c92b2daf71cdf63c68a392e49aaa0a730daf7 /Lib/asyncio | |
parent | 2d8f06382e7d5a759ca554110a699a397114824a (diff) | |
download | cpython-d757aaf9dd767d13205bf9917e520ebf43e7f6e5.zip cpython-d757aaf9dd767d13205bf9917e520ebf43e7f6e5.tar.gz cpython-d757aaf9dd767d13205bf9917e520ebf43e7f6e5.tar.bz2 |
bpo-32356: idempotent pause_/resume_reading; new is_reading method. (#4914)
Diffstat (limited to 'Lib/asyncio')
-rw-r--r-- | Lib/asyncio/proactor_events.py | 15 | ||||
-rw-r--r-- | Lib/asyncio/selector_events.py | 15 | ||||
-rw-r--r-- | Lib/asyncio/sslproto.py | 6 | ||||
-rw-r--r-- | Lib/asyncio/transports.py | 4 |
4 files changed, 24 insertions, 16 deletions
diff --git a/Lib/asyncio/proactor_events.py b/Lib/asyncio/proactor_events.py index 291d989..915ad1a 100644 --- a/Lib/asyncio/proactor_events.py +++ b/Lib/asyncio/proactor_events.py @@ -152,21 +152,20 @@ class _ProactorReadPipeTransport(_ProactorBasePipeTransport, self._paused = False self._loop.call_soon(self._loop_reading) + def is_reading(self): + return not self._paused and not self._closing + def pause_reading(self): - if self._closing: - raise RuntimeError('Cannot pause_reading() when closing') - if self._paused: - raise RuntimeError('Already paused') + if self._closing or self._paused: + return self._paused = True if self._loop.get_debug(): logger.debug("%r pauses reading", self) def resume_reading(self): - if not self._paused: - raise RuntimeError('Not paused') - self._paused = False - if self._closing: + if self._closing or not self._paused: return + self._paused = False self._loop.call_soon(self._loop_reading, self._read_fut) if self._loop.get_debug(): logger.debug("%r resumes reading", self) diff --git a/Lib/asyncio/selector_events.py b/Lib/asyncio/selector_events.py index cb33cd3..3f44a99 100644 --- a/Lib/asyncio/selector_events.py +++ b/Lib/asyncio/selector_events.py @@ -702,22 +702,21 @@ class _SelectorSocketTransport(_SelectorTransport): self._loop.call_soon(futures._set_result_unless_cancelled, waiter, None) + def is_reading(self): + return not self._paused and not self._closing + def pause_reading(self): - if self._closing: - raise RuntimeError('Cannot pause_reading() when closing') - if self._paused: - raise RuntimeError('Already paused') + if self._closing or self._paused: + return self._paused = True self._loop._remove_reader(self._sock_fd) if self._loop.get_debug(): logger.debug("%r pauses reading", self) def resume_reading(self): - if not self._paused: - raise RuntimeError('Not paused') - self._paused = False - if self._closing: + if self._closing or not self._paused: return + self._paused = False self._loop._add_reader(self._sock_fd, self._read_ready) if self._loop.get_debug(): logger.debug("%r resumes reading", self) diff --git a/Lib/asyncio/sslproto.py b/Lib/asyncio/sslproto.py index 0c8f01a..8da8570 100644 --- a/Lib/asyncio/sslproto.py +++ b/Lib/asyncio/sslproto.py @@ -317,6 +317,12 @@ class _SSLProtocolTransport(transports._FlowControlMixin, source=self) self.close() + def is_reading(self): + tr = self._ssl_protocol._transport + if tr is None: + raise RuntimeError('SSL transport has not been initialized yet') + return tr.is_reading() + def pause_reading(self): """Pause the receiving end. diff --git a/Lib/asyncio/transports.py b/Lib/asyncio/transports.py index 51f5673..233bbb5 100644 --- a/Lib/asyncio/transports.py +++ b/Lib/asyncio/transports.py @@ -44,6 +44,10 @@ class BaseTransport: class ReadTransport(BaseTransport): """Interface for read-only transports.""" + def is_reading(self): + """Return True if the transport is receiving.""" + raise NotImplementedError + def pause_reading(self): """Pause the receiving end. |