async_with_break.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # test async with, escaped by a break
  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 f1():
  9. while 1:
  10. async with AContext():
  11. print('body')
  12. break
  13. print('no 1')
  14. print('no 2')
  15. o = f1()
  16. try:
  17. print(o.send(None))
  18. except StopIteration:
  19. print('finished')
  20. async def f2():
  21. while 1:
  22. try:
  23. async with AContext():
  24. print('body')
  25. break
  26. print('no 1')
  27. finally:
  28. print('finally')
  29. print('no 2')
  30. o = f2()
  31. try:
  32. print(o.send(None))
  33. except StopIteration:
  34. print('finished')
  35. async def f3():
  36. while 1:
  37. try:
  38. try:
  39. async with AContext():
  40. print('body')
  41. break
  42. print('no 1')
  43. finally:
  44. print('finally inner')
  45. finally:
  46. print('finally outer')
  47. print('no 2')
  48. o = f3()
  49. try:
  50. print(o.send(None))
  51. except StopIteration:
  52. print('finished')