import asyncio
async def boom_orphan():
raise ValueError('boom')
async def slow_orphan():
return 'slow ok'
g_failed = asyncio.gather(boom_orphan())
try:
await g_failed assert False, 'g_failed should have raised'
except ValueError as e:
assert str(e) == 'boom', f'first await: {e}'
try:
await asyncio.gather(slow_orphan(), g_failed) assert False, 'reuse of failed gather should raise'
except ValueError as e:
assert str(e) == 'boom', f'second await: {e}'
result_after_orphan = await asyncio.gather(slow_orphan()) assert result_after_orphan == ['slow ok'], f'post-orphan gather: {result_after_orphan}'
async def boom_double_fail():
raise ValueError('double-fail err')
async def double_fail_main():
await asyncio.gather(asyncio.gather(boom_double_fail()))
try:
await double_fail_main() assert False, 'double_fail_main should have raised'
except ValueError as e:
assert str(e) == 'double-fail err', f'double-fail error: {e}'
async def boom_triple():
raise ValueError('triple')
async def triple_main():
await asyncio.gather(asyncio.gather(asyncio.gather(boom_triple())))
try:
await triple_main() assert False, 'triple_main should have raised'
except ValueError as e:
assert str(e) == 'triple', f'triple-nested error: {e}'
async def boom_a():
raise NotImplementedError('a')
async def boom_b():
raise NotImplementedError('b')
async def ext_c():
raise NotImplementedError('c')
async def sibling_main():
inner = asyncio.gather(boom_a(), boom_b())
outer = asyncio.gather(inner, ext_c())
try:
await outer
assert False, 'sibling_main should have raised'
except NotImplementedError as e:
assert str(e) in ('a', 'b', 'c'), f'sibling error: {e}'
await sibling_main()
async def boom_replay():
raise ValueError('replay')
async def slow_replay():
return 1
g_replay = asyncio.gather(boom_replay())
try:
await g_replay except ValueError:
pass
outer_replay = asyncio.gather(slow_replay(), g_replay)
try:
await outer_replay assert False, 'first outer_replay should raise'
except ValueError as e:
assert str(e) == 'replay', f'first outer_replay: {e}'
try:
await outer_replay assert False, 'second outer_replay should raise'
except ValueError as e:
assert str(e) == 'replay', f'second outer_replay: {e}'
async def make_payload():
return 'payload'
c_shared = make_payload()
g_share_1 = asyncio.gather(c_shared)
g_share_2 = asyncio.gather(c_shared)
try:
await asyncio.gather(g_share_1, g_share_2) assert False, 'shared-coroutine outer gather should raise'
except RuntimeError as e:
assert str(e) == 'cannot reuse already awaited coroutine', f'cross-gather: {e}'
final = await asyncio.gather(make_payload(), make_payload()) assert final == ['payload', 'payload'], f'final gather: {final}'