bpo-31033: Add a msg argument to Future.cancel() and Task.cancel() (G… · python/cpython@1ce5841 (original) (raw)
`@@ -230,7 +230,7 @@ def print_stack(self, *, limit=None, file=None):
`
230
230
` """
`
231
231
`return base_tasks._task_print_stack(self, limit, file)
`
232
232
``
233
``
`-
def cancel(self):
`
``
233
`+
def cancel(self, msg=None):
`
234
234
`"""Request that this task cancel itself.
`
235
235
``
236
236
` This arranges for a CancelledError to be thrown into the
`
`@@ -254,13 +254,14 @@ def cancel(self):
`
254
254
`if self.done():
`
255
255
`return False
`
256
256
`if self._fut_waiter is not None:
`
257
``
`-
if self._fut_waiter.cancel():
`
``
257
`+
if self._fut_waiter.cancel(msg=msg):
`
258
258
`# Leave self._fut_waiter; it may be a Task that
`
259
259
`# catches and ignores the cancellation so we may have
`
260
260
`# to cancel it again later.
`
261
261
`return True
`
262
262
`# It must be the case that self.__step is already scheduled.
`
263
263
`self._must_cancel = True
`
``
264
`+
self._cancel_message = msg
`
264
265
`return True
`
265
266
``
266
267
`def __step(self, exc=None):
`
`@@ -269,7 +270,8 @@ def __step(self, exc=None):
`
269
270
`f'_step(): already done: {self!r}, {exc!r}')
`
270
271
`if self._must_cancel:
`
271
272
`if not isinstance(exc, exceptions.CancelledError):
`
272
``
`-
exc = exceptions.CancelledError()
`
``
273
`+
exc = exceptions.CancelledError(''
`
``
274
`+
if self._cancel_message is None else self._cancel_message)
`
273
275
`self._must_cancel = False
`
274
276
`coro = self._coro
`
275
277
`self._fut_waiter = None
`
`@@ -287,11 +289,15 @@ def __step(self, exc=None):
`
287
289
`if self._must_cancel:
`
288
290
`# Task is cancelled right before coro stops.
`
289
291
`self._must_cancel = False
`
290
``
`-
super().cancel()
`
``
292
`+
super().cancel(msg=self._cancel_message)
`
291
293
`else:
`
292
294
`super().set_result(exc.value)
`
293
``
`-
except exceptions.CancelledError:
`
294
``
`-
super().cancel() # I.e., Future.cancel(self).
`
``
295
`+
except exceptions.CancelledError as exc:
`
``
296
`+
if exc.args:
`
``
297
`+
cancel_msg = exc.args[0]
`
``
298
`+
else:
`
``
299
`+
cancel_msg = None
`
``
300
`+
super().cancel(msg=cancel_msg) # I.e., Future.cancel(self).
`
295
301
`except (KeyboardInterrupt, SystemExit) as exc:
`
296
302
`super().set_exception(exc)
`
297
303
`raise
`
`@@ -319,7 +325,8 @@ def __step(self, exc=None):
`
319
325
`self.__wakeup, context=self._context)
`
320
326
`self._fut_waiter = result
`
321
327
`if self._must_cancel:
`
322
``
`-
if self._fut_waiter.cancel():
`
``
328
`+
if self._fut_waiter.cancel(
`
``
329
`+
msg=self._cancel_message):
`
323
330
`self._must_cancel = False
`
324
331
`else:
`
325
332
`new_exc = RuntimeError(
`
`@@ -716,12 +723,12 @@ def init(self, children, *, loop=None):
`
716
723
`self._children = children
`
717
724
`self._cancel_requested = False
`
718
725
``
719
``
`-
def cancel(self):
`
``
726
`+
def cancel(self, msg=None):
`
720
727
`if self.done():
`
721
728
`return False
`
722
729
`ret = False
`
723
730
`for child in self._children:
`
724
``
`-
if child.cancel():
`
``
731
`+
if child.cancel(msg=msg):
`
725
732
`ret = True
`
726
733
`if ret:
`
727
734
`# If any child tasks were actually cancelled, we should
`
`@@ -780,7 +787,8 @@ def _done_callback(fut):
`
780
787
`# Check if 'fut' is cancelled first, as
`
781
788
`# 'fut.exception()' will raise a CancelledError
`
782
789
`# instead of returning it.
`
783
``
`-
exc = exceptions.CancelledError()
`
``
790
`+
exc = exceptions.CancelledError(''
`
``
791
`+
if fut._cancel_message is None else fut._cancel_message)
`
784
792
`outer.set_exception(exc)
`
785
793
`return
`
786
794
`else:
`
`@@ -799,7 +807,9 @@ def _done_callback(fut):
`
799
807
`# Check if 'fut' is cancelled first, as
`
800
808
`# 'fut.exception()' will raise a CancelledError
`
801
809
`# instead of returning it.
`
802
``
`-
res = exceptions.CancelledError()
`
``
810
`+
res = exceptions.CancelledError(
`
``
811
`+
'' if fut._cancel_message is None else
`
``
812
`+
fut._cancel_message)
`
803
813
`else:
`
804
814
`res = fut.exception()
`
805
815
`if res is None:
`
`@@ -810,7 +820,9 @@ def _done_callback(fut):
`
810
820
`# If gather is being cancelled we must propagate the
`
811
821
`# cancellation regardless of return_exceptions argument.
`
812
822
`# See issue 32684.
`
813
``
`-
outer.set_exception(exceptions.CancelledError())
`
``
823
`+
exc = exceptions.CancelledError(''
`
``
824
`+
if fut._cancel_message is None else fut._cancel_message)
`
``
825
`+
outer.set_exception(exc)
`
814
826
`else:
`
815
827
`outer.set_result(results)
`
816
828
``