Skip to content

Instantly share code, notes, and snippets.

@matham
Created August 11, 2020 06:10
Show Gist options
  • Save matham/340767ba7f61f5f9045a0cf0f0680578 to your computer and use it in GitHub Desktop.
Save matham/340767ba7f61f5f9045a0cf0f0680578 to your computer and use it in GitHub Desktop.
import trio
import contextlib
from async_generator import aclosing
async def gen():
for i in range(10):
yield i
@contextlib.asynccontextmanager
async def get_gen():
async with aclosing(gen()) as aiter:
yield aiter
async def iterate_gen():
async with get_gen() as aiter:
async for item in aiter:
yield item
async def eat_iterate():
async with aclosing(iterate_gen()) as aiter:
async for item in aiter:
print(item)
if item == 5:
raise ValueError
trio.run(eat_iterate)
Traceback (most recent call last):
File "C:/Users/Matthew Einhorn/AppData/Roaming/JetBrains/PyCharm2020.2/scratches/scratch_13.py", line 14, in get_gen
yield aiter
File "C:/Users/Matthew Einhorn/AppData/Roaming/JetBrains/PyCharm2020.2/scratches/scratch_13.py", line 20, in iterate_gen
yield item
GeneratorExit
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/Matthew Einhorn/AppData/Roaming/JetBrains/PyCharm2020.2/scratches/scratch_13.py", line 31, in <module>
trio.run(eat_iterate)
File "E:\Python\Python37\lib\site-packages\trio\_core\_run.py", line 1896, in run
raise runner.main_task_outcome.error
File "C:/Users/Matthew Einhorn/AppData/Roaming/JetBrains/PyCharm2020.2/scratches/scratch_13.py", line 28, in eat_iterate
raise ValueError
File "E:\Python\Python37\lib\site-packages\async_generator\_util.py", line 14, in __aexit__
await self._aiter.aclose()
File "C:/Users/Matthew Einhorn/AppData/Roaming/JetBrains/PyCharm2020.2/scratches/scratch_13.py", line 20, in iterate_gen
yield item
File "C:\Users\Matthew Einhorn\AppData\Local\Programs\Python\Python37\Lib\contextlib.py", line 189, in __aexit__
raise RuntimeError("generator didn't stop after throw()")
RuntimeError: generator didn't stop after throw()
0
1
2
3
4
5
Process finished with exit code 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment