mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
Add asyncio.Handle.cancelled() method (#2388)
This commit is contained in:
parent
088929cf62
commit
69cfed1cd7
4 changed files with 14 additions and 4 deletions
|
@ -867,6 +867,12 @@ Handle
|
|||
Cancel the call. If the callback is already canceled or executed,
|
||||
this method has no effect.
|
||||
|
||||
.. method:: cancelled()
|
||||
|
||||
Return ``True`` if the call was cancelled.
|
||||
|
||||
.. versionadded:: 3.7
|
||||
|
||||
|
||||
Event loop examples
|
||||
-------------------
|
||||
|
|
|
@ -117,6 +117,9 @@ class Handle:
|
|||
self._callback = None
|
||||
self._args = None
|
||||
|
||||
def cancelled(self):
|
||||
return self._cancelled
|
||||
|
||||
def _run(self):
|
||||
try:
|
||||
self._callback(*self._args)
|
||||
|
|
|
@ -2305,10 +2305,10 @@ class HandleTests(test_utils.TestCase):
|
|||
h = asyncio.Handle(callback, args, self.loop)
|
||||
self.assertIs(h._callback, callback)
|
||||
self.assertIs(h._args, args)
|
||||
self.assertFalse(h._cancelled)
|
||||
self.assertFalse(h.cancelled())
|
||||
|
||||
h.cancel()
|
||||
self.assertTrue(h._cancelled)
|
||||
self.assertTrue(h.cancelled())
|
||||
|
||||
def test_callback_with_exception(self):
|
||||
def callback():
|
||||
|
@ -2494,11 +2494,11 @@ class TimerTests(unittest.TestCase):
|
|||
h = asyncio.TimerHandle(when, callback, args, mock.Mock())
|
||||
self.assertIs(h._callback, callback)
|
||||
self.assertIs(h._args, args)
|
||||
self.assertFalse(h._cancelled)
|
||||
self.assertFalse(h.cancelled())
|
||||
|
||||
# cancel
|
||||
h.cancel()
|
||||
self.assertTrue(h._cancelled)
|
||||
self.assertTrue(h.cancelled())
|
||||
self.assertIsNone(h._callback)
|
||||
self.assertIsNone(h._args)
|
||||
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
Add a ``cancelled()`` method to :class:`asyncio.Handle`. Patch by Marat Sharafutdinov.
|
Loading…
Add table
Add a link
Reference in a new issue