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