bpo-31033: Add a msg argument to Future.cancel() and Task.cancel() (GH-19979)

This commit is contained in:
Chris Jerdonek 2020-05-15 16:55:50 -07:00 committed by GitHub
parent fe1176e882
commit 1ce5841eca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 355 additions and 70 deletions

View file

@ -51,6 +51,7 @@ class Future:
_exception = None
_loop = None
_source_traceback = None
_cancel_message = None
# This field is used for a dual purpose:
# - Its presence is a marker to declare that a class implements
@ -123,7 +124,7 @@ class Future:
raise RuntimeError("Future object is not initialized.")
return loop
def cancel(self):
def cancel(self, msg=None):
"""Cancel the future and schedule callbacks.
If the future is already done or cancelled, return False. Otherwise,
@ -134,6 +135,7 @@ class Future:
if self._state != _PENDING:
return False
self._state = _CANCELLED
self._cancel_message = msg
self.__schedule_callbacks()
return True
@ -173,7 +175,9 @@ class Future:
the future is done and has an exception set, this exception is raised.
"""
if self._state == _CANCELLED:
raise exceptions.CancelledError
raise exceptions.CancelledError(
'' if self._cancel_message is None else self._cancel_message)
if self._state != _FINISHED:
raise exceptions.InvalidStateError('Result is not ready.')
self.__log_traceback = False
@ -190,7 +194,8 @@ class Future:
InvalidStateError.
"""
if self._state == _CANCELLED:
raise exceptions.CancelledError
raise exceptions.CancelledError(
'' if self._cancel_message is None else self._cancel_message)
if self._state != _FINISHED:
raise exceptions.InvalidStateError('Exception is not set.')
self.__log_traceback = False