bpo-47167: Allow overriding a future compliance check in asyncio.Task (GH-32197)

This commit is contained in:
Andrew Svetlov 2022-04-01 04:25:15 +03:00 committed by GitHub
parent ab89ccff3c
commit d4bb38f82b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 133 additions and 15 deletions

View file

@ -252,6 +252,10 @@ class Task(futures._PyFuture): # Inherit Python Task implementation
self._num_cancels_requested -= 1
return self._num_cancels_requested
def _check_future(self, future):
"""Return False if task and future loops are not compatible."""
return futures._get_loop(future) is self._loop
def __step(self, exc=None):
if self.done():
raise exceptions.InvalidStateError(
@ -292,7 +296,7 @@ class Task(futures._PyFuture): # Inherit Python Task implementation
blocking = getattr(result, '_asyncio_future_blocking', None)
if blocking is not None:
# Yielded Future must come from Future.__iter__().
if futures._get_loop(result) is not self._loop:
if not self._check_future(result):
new_exc = RuntimeError(
f'Task {self!r} got Future '
f'{result!r} attached to a different loop')