1# test break within (nested) finally
2
3# basic case with break in finally
4def f():
5    for _ in range(2):
6        print(1)
7        try:
8            pass
9        finally:
10            print(2)
11            break
12            print(3)
13        print(4)
14    print(5)
15f()
16
17# where the finally swallows an exception
18def f():
19    lst = [1, 2, 3]
20    for x in lst:
21        print('a', x)
22        try:
23            raise Exception
24        finally:
25            print(1)
26            break
27        print('b', x)
28f()
29
30# basic nested finally with break in inner finally
31def f():
32    for i in range(2):
33        print('iter', i)
34        try:
35            raise TypeError
36        finally:
37            print(1)
38            try:
39                raise ValueError
40            finally:
41                break
42print(f())
43
44# similar to above but more nesting
45def f():
46    for i in range(2):
47        try:
48            raise ValueError
49        finally:
50            print(1)
51            try:
52                raise TypeError
53            finally:
54                print(2)
55                try:
56                    pass
57                finally:
58                    break
59print(f())
60
61# lots of nesting
62def f():
63    for i in range(2):
64        try:
65            raise ValueError
66        finally:
67            print(1)
68            try:
69                raise TypeError
70            finally:
71                print(2)
72                try:
73                    raise Exception
74                finally:
75                    break
76print(f())
77
78# basic case combined with try-else
79def f(arg):
80    for _ in range(2):
81        print(1)
82        try:
83            if arg == 1:
84                raise ValueError
85            elif arg == 2:
86                raise TypeError
87        except ValueError:
88            print(2)
89        else:
90            print(3)
91        finally:
92            print(4)
93            break
94            print(5)
95        print(6)
96    print(7)
97f(0) # no exception, else should execute
98f(1) # exception caught, else should be skipped
99f(2) # exception not caught, finally swallows exception, else should be skipped
100