try_finally_loops.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # Test various loop types, some may be implemented/optimized differently
  2. while True:
  3. try:
  4. break
  5. finally:
  6. print('finally 1')
  7. for i in [1, 5, 10]:
  8. try:
  9. continue
  10. finally:
  11. print('finally 2')
  12. for i in range(3):
  13. try:
  14. continue
  15. finally:
  16. print('finally 3')
  17. # Multi-level
  18. for i in range(4):
  19. print(i)
  20. try:
  21. while True:
  22. try:
  23. try:
  24. break
  25. finally:
  26. print('finally 1')
  27. finally:
  28. print('finally 2')
  29. print('here')
  30. finally:
  31. print('finnaly 3')
  32. # break from within try-finally, within for-loop
  33. for i in [1]:
  34. try:
  35. print(i)
  36. break
  37. finally:
  38. print('finally 4')
  39. # Test unwind-jump where there is nothing in the body of the try or finally.
  40. # This checks that the bytecode emitter allocates enough stack for the unwind.
  41. for i in [1]:
  42. try:
  43. break
  44. finally:
  45. pass
  46. # The following test checks that the globals dict is valid after a call to a
  47. # function that has an unwind jump.
  48. # There was a bug where an unwind jump would trash the globals dict upon return
  49. # from a function, because it used the Python-stack incorrectly.
  50. def f():
  51. for i in [1]:
  52. try:
  53. break
  54. finally:
  55. pass
  56. def g():
  57. global global_var
  58. f()
  59. print(global_var)
  60. global_var = 'global'
  61. g()