Skip to content

bpo-31943: Add asyncio.Handle.cancelled() method #2388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Doc/library/asyncio-eventloop.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add .. versionadded:: 3.6

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3.7 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, added. I hope it will be backported to 3.6?)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. Asyncio is no longer provisional and this is not a bug fix.

.. versionadded:: 3.7


Event loop examples
-------------------
Expand Down
3 changes: 3 additions & 0 deletions Lib/asyncio/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ def cancel(self):
self._callback = None
self._args = None

def cancelled(self):
return self._cancelled

def _run(self):
try:
self._callback(*self._args)
Expand Down
8 changes: 4 additions & 4 deletions Lib/test/test_asyncio/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -2305,10 +2305,10 @@ def callback(*args):
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():
Expand Down Expand Up @@ -2494,11 +2494,11 @@ def callback(*args):
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)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add a ``cancelled()`` method to :class:`asyncio.Handle`. Patch by Marat Sharafutdinov.