Message 413481 - Python tracker (original) (raw)

I propose the following, backwards compatible solution:

Add a new keyword argument to Task.cancel(): "scope: object = None". The behavior would be as follows: the scope is saved, and included in the raised CancelledError. If Task.cancel() is called again, but with scope=None (the default), it clears out the saved scope, if any. Any other scope will be ignored.

This simple change would allow for proper implementation of any context manager that needs to swallow or transform a CancelledError raised in the task.