Add asyncio.Handle.cancelled() method (#2388)

This commit is contained in:
Marat Sharafutdinov 2017-11-07 12:06:05 +03:00 committed by Andrew Svetlov
parent 088929cf62
commit 69cfed1cd7
4 changed files with 14 additions and 4 deletions

View file

@ -867,6 +867,12 @@ Handle
Cancel the call. If the callback is already canceled or executed, Cancel the call. If the callback is already canceled or executed,
this method has no effect. this method has no effect.
.. method:: cancelled()
Return ``True`` if the call was cancelled.
.. versionadded:: 3.7
Event loop examples Event loop examples
------------------- -------------------

View file

@ -117,6 +117,9 @@ class Handle:
self._callback = None self._callback = None
self._args = None self._args = None
def cancelled(self):
return self._cancelled
def _run(self): def _run(self):
try: try:
self._callback(*self._args) self._callback(*self._args)

View file

@ -2305,10 +2305,10 @@ class HandleTests(test_utils.TestCase):
h = asyncio.Handle(callback, args, self.loop) h = asyncio.Handle(callback, args, self.loop)
self.assertIs(h._callback, callback) self.assertIs(h._callback, callback)
self.assertIs(h._args, args) self.assertIs(h._args, args)
self.assertFalse(h._cancelled) self.assertFalse(h.cancelled())
h.cancel() h.cancel()
self.assertTrue(h._cancelled) self.assertTrue(h.cancelled())
def test_callback_with_exception(self): def test_callback_with_exception(self):
def callback(): def callback():
@ -2494,11 +2494,11 @@ class TimerTests(unittest.TestCase):
h = asyncio.TimerHandle(when, callback, args, mock.Mock()) h = asyncio.TimerHandle(when, callback, args, mock.Mock())
self.assertIs(h._callback, callback) self.assertIs(h._callback, callback)
self.assertIs(h._args, args) self.assertIs(h._args, args)
self.assertFalse(h._cancelled) self.assertFalse(h.cancelled())
# cancel # cancel
h.cancel() h.cancel()
self.assertTrue(h._cancelled) self.assertTrue(h.cancelled())
self.assertIsNone(h._callback) self.assertIsNone(h._callback)
self.assertIsNone(h._args) self.assertIsNone(h._args)

View file

@ -0,0 +1 @@
Add a ``cancelled()`` method to :class:`asyncio.Handle`. Patch by Marat Sharafutdinov.