async_with.py 526 B

1234567891011121314151617181920212223242526272829
  1. # test simple async with execution
  2. class AContext:
  3. async def __aenter__(self):
  4. print('enter')
  5. return 1
  6. async def __aexit__(self, exc_type, exc, tb):
  7. print('exit', exc_type, exc)
  8. async def f():
  9. async with AContext():
  10. print('body')
  11. o = f()
  12. try:
  13. o.send(None)
  14. except StopIteration:
  15. print('finished')
  16. async def g():
  17. async with AContext() as ac:
  18. print(ac)
  19. raise ValueError('error')
  20. o = g()
  21. try:
  22. o.send(None)
  23. except ValueError:
  24. print('ValueError')