]> granicus.if.org Git - python/commitdiff
Add asyncio.Handle.cancelled() method (#2388)
authorMarat Sharafutdinov <decaz89@gmail.com>
Tue, 7 Nov 2017 09:06:05 +0000 (12:06 +0300)
committerAndrew Svetlov <andrew.svetlov@gmail.com>
Tue, 7 Nov 2017 09:06:05 +0000 (12:06 +0300)
Doc/library/asyncio-eventloop.rst
Lib/asyncio/events.py
Lib/test/test_asyncio/test_events.py
Misc/NEWS.d/next/Library/2017-11-04-19-28-08.bpo-31943.bxw5gM.rst [new file with mode: 0644]

index 9bc7a40fc71bea9597e632e48dc7bbbca59aaa59..e635cba659a36b9445e4f562f91fe22f0ac70afa 100644 (file)
@@ -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
 -------------------
index c2663c5062dedd3d9ca1c431bd9f38194dd71869..270a5e4c33d7d9e5209a4bbf171d7d986e9d4177 100644 (file)
@@ -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)
index 0ea9c086747c502f815e81b8368d66ee467cbed5..5394ddfce24b566c2822ee3db302560b1e5f87db 100644 (file)
@@ -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 (file)
index 0000000..a26aca3
--- /dev/null
@@ -0,0 +1 @@
+Add a ``cancelled()`` method to :class:`asyncio.Handle`.  Patch by Marat Sharafutdinov.