import pytest class noop: def __await__(self): yield async def agen(raised): try: yield 1 finally: try: await noop() except Exception as exc: raised.append(exc) def test_bug(): raised = [] g = agen(raised) # Run g.__anext__ coro = g.__anext__() with pytest.raises(StopIteration) as record: coro.send(None) assert record.value.value == 1 # Run g.__aclose__ up to noop() coro = g.aclose() assert coro.send(None) is None # Thow a runtime error into agen exc = RuntimeError() with pytest.raises(StopIteration) as record: coro.throw(exc) assert record.value.value is None assert raised[0] is exc