Commit 69cfed1c authored by Marat Sharafutdinov's avatar Marat Sharafutdinov Committed by Andrew Svetlov

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

parent 088929cf
...@@ -867,6 +867,12 @@ Handle ...@@ -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
------------------- -------------------
......
...@@ -117,6 +117,9 @@ class Handle: ...@@ -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)
......
...@@ -2305,10 +2305,10 @@ class HandleTests(test_utils.TestCase): ...@@ -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): ...@@ -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)
......
Add a ``cancelled()`` method to :class:`asyncio.Handle`. Patch by Marat Sharafutdinov.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment