Skip to content

Commit 845212a

Browse files
committed
(Merge 3.4) Issue #21163, asyncio: Ignore "destroy pending task" warnings for
private tasks in gather().
2 parents 72f8e25 + f03b3c7 commit 845212a

1 file changed

Lines changed: 23 additions & 11 deletions

File tree

Lib/asyncio/tasks.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -558,21 +558,33 @@ def gather(*coros_or_futures, loop=None, return_exceptions=False):
558558
prevent the cancellation of one child to cause other children to
559559
be cancelled.)
560560
"""
561-
arg_to_fut = {arg: async(arg, loop=loop) for arg in set(coros_or_futures)}
562-
children = [arg_to_fut[arg] for arg in coros_or_futures]
563-
n = len(children)
564-
if n == 0:
561+
if not coros_or_futures:
565562
outer = futures.Future(loop=loop)
566563
outer.set_result([])
567564
return outer
568-
if loop is None:
569-
loop = children[0]._loop
570-
for fut in children:
571-
if fut._loop is not loop:
572-
raise ValueError("futures are tied to different event loops")
565+
566+
arg_to_fut = {}
567+
for arg in set(coros_or_futures):
568+
if not isinstance(arg, futures.Future):
569+
fut = async(arg, loop=loop)
570+
if loop is None:
571+
loop = fut._loop
572+
# The caller cannot control this future, the "destroy pending task"
573+
# warning should not be emitted.
574+
fut._log_destroy_pending = False
575+
else:
576+
fut = arg
577+
if loop is None:
578+
loop = fut._loop
579+
elif fut._loop is not loop:
580+
raise ValueError("futures are tied to different event loops")
581+
arg_to_fut[arg] = fut
582+
583+
children = [arg_to_fut[arg] for arg in coros_or_futures]
584+
nchildren = len(children)
573585
outer = _GatheringFuture(children, loop=loop)
574586
nfinished = 0
575-
results = [None] * n
587+
results = [None] * nchildren
576588

577589
def _done_callback(i, fut):
578590
nonlocal nfinished
@@ -595,7 +607,7 @@ def _done_callback(i, fut):
595607
res = fut._result
596608
results[i] = res
597609
nfinished += 1
598-
if nfinished == n:
610+
if nfinished == nchildren:
599611
outer.set_result(results)
600612

601613
for i, fut in enumerate(children):

0 commit comments

Comments
 (0)