|
1 | 1 | import asyncio |
2 | 2 | import io |
3 | 3 | import unittest |
| 4 | +from unittest import mock |
4 | 5 |
|
5 | 6 |
|
6 | 7 | # To prevent a warning "test altered the execution environment" |
@@ -198,10 +199,32 @@ async def main(): |
198 | 199 | 'T<anon>', |
199 | 200 | ['s capture_test_stack', 'a deep', 'a c1'], |
200 | 201 | [ |
201 | | - ['T<anon>', ['a main', 'a test_stack_gather'], []] |
| 202 | + ['F', [], [ |
| 203 | + ['T<anon>', ['a main', 'a test_stack_gather'], []], |
| 204 | + ]], |
202 | 205 | ] |
203 | 206 | ]) |
204 | 207 |
|
| 208 | + async def test_gather_awaited_by_different_task(self): |
| 209 | + # gh-162061 |
| 210 | + async def consume(fut): |
| 211 | + await fut |
| 212 | + |
| 213 | + release = asyncio.Event() |
| 214 | + child = asyncio.create_task(release.wait()) |
| 215 | + gathered = asyncio.gather(child) |
| 216 | + consumer = asyncio.create_task(consume(gathered), eager_start=True) |
| 217 | + graph = asyncio.capture_call_graph(child, limit=0) |
| 218 | + release.set() |
| 219 | + await consumer |
| 220 | + self.assertFalse(asyncio.capture_call_graph(child, limit=0).awaited_by) |
| 221 | + |
| 222 | + waiter, = graph.awaited_by |
| 223 | + self.assertIs(waiter.future, gathered) |
| 224 | + parent, = waiter.awaited_by |
| 225 | + self.assertIs(parent.future, consumer) |
| 226 | + self.assertFalse(parent.awaited_by) |
| 227 | + |
205 | 228 | async def test_stack_gather_survivor(self): |
206 | 229 | # gh-157213: a child that outlives gather() must not be shown as awaited |
207 | 230 |
|
@@ -562,6 +585,17 @@ def setUp(self): |
562 | 585 | self._Future = asyncio.Future |
563 | 586 | asyncio.Future = futures.Future = futures._PyFuture |
564 | 587 |
|
| 588 | + # The real _GatheringFuture subclasses the C Future, which the |
| 589 | + # pure-Python awaited-by helpers ignore. |
| 590 | + class _PyGatheringFuture(futures._PyFuture): |
| 591 | + def __init__(self, children, *, loop): |
| 592 | + super().__init__(loop=loop) |
| 593 | + self._children = children |
| 594 | + self._cancel_requested = False |
| 595 | + cancel = tasks._GatheringFuture.cancel |
| 596 | + self.enterContext(mock.patch.object(tasks, '_GatheringFuture', |
| 597 | + _PyGatheringFuture)) |
| 598 | + |
565 | 599 | self._Task = asyncio.Task |
566 | 600 | asyncio.Task = tasks.Task = tasks._PyTask |
567 | 601 |
|
|
0 commit comments