diff options
author | Marat Sharafutdinov <decaz89@gmail.com> | 2017-11-07 09:06:05 (GMT) |
---|---|---|
committer | Andrew Svetlov <andrew.svetlov@gmail.com> | 2017-11-07 09:06:05 (GMT) |
commit | 69cfed1cd7be968fa4f6c69eb6dccc94bce5d45c (patch) | |
tree | a00a949294321c72c6e3d6286452c290252d3340 | |
parent | 088929cf62fa22c06f6a44e25915abce9048a545 (diff) | |
download | cpython-69cfed1cd7be968fa4f6c69eb6dccc94bce5d45c.zip cpython-69cfed1cd7be968fa4f6c69eb6dccc94bce5d45c.tar.gz cpython-69cfed1cd7be968fa4f6c69eb6dccc94bce5d45c.tar.bz2 |
Add asyncio.Handle.cancelled() method (#2388)
-rw-r--r-- | Doc/library/asyncio-eventloop.rst | 6 | ||||
-rw-r--r-- | Lib/asyncio/events.py | 3 | ||||
-rw-r--r-- | Lib/test/test_asyncio/test_events.py | 8 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2017-11-04-19-28-08.bpo-31943.bxw5gM.rst | 1 |
4 files changed, 14 insertions, 4 deletions
diff --git a/Doc/library/asyncio-eventloop.rst b/Doc/library/asyncio-eventloop.rst index 9bc7a40..e635cba 100644 --- a/Doc/library/asyncio-eventloop.rst +++ b/Doc/library/asyncio-eventloop.rst @@ -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 ------------------- diff --git a/Lib/asyncio/events.py b/Lib/asyncio/events.py index c2663c5..270a5e4 100644 --- a/Lib/asyncio/events.py +++ b/Lib/asyncio/events.py @@ -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) diff --git a/Lib/test/test_asyncio/test_events.py b/Lib/test/test_asyncio/test_events.py index 0ea9c08..5394ddf 100644 --- a/Lib/test/test_asyncio/test_events.py +++ b/Lib/test/test_asyncio/test_events.py @@ -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) diff --git a/Misc/NEWS.d/next/Library/2017-11-04-19-28-08.bpo-31943.bxw5gM.rst b/Misc/NEWS.d/next/Library/2017-11-04-19-28-08.bpo-31943.bxw5gM.rst new file mode 100644 index 0000000..a26aca3 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2017-11-04-19-28-08.bpo-31943.bxw5gM.rst @@ -0,0 +1 @@ +Add a ``cancelled()`` method to :class:`asyncio.Handle`. Patch by Marat Sharafutdinov. |