summaryrefslogtreecommitdiffstatshomepage
path: root/tests/basics/try_finally_break.py
blob: ae7226637d72ba96c7eb897e3c8933cb9b6c86c8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# test break within (nested) finally

# basic case with break in finally
def f():
    for _ in range(2):
        print(1)
        try:
            pass
        finally:
            print(2)
            break
            print(3)
        print(4)
    print(5)
f()

# where the finally swallows an exception
def f():
    lst = [1, 2, 3]
    for x in lst:
        print('a', x)
        try:
            raise Exception
        finally:
            print(1)
            break
        print('b', x)
f()

# basic nested finally with break in inner finally
def f():
    for i in range(2):
        print('iter', i)
        try:
            raise TypeError
        finally:
            print(1)
            try:
                raise ValueError
            finally:
                break
print(f())

# similar to above but more nesting
def f():
    for i in range(2):
        try:
            raise ValueError
        finally:
            print(1)
            try:
                raise TypeError
            finally:
                print(2)
                try:
                    pass
                finally:
                    break
print(f())

# lots of nesting
def f():
    for i in range(2):
        try:
            raise ValueError
        finally:
            print(1)
            try:
                raise TypeError
            finally:
                print(2)
                try:
                    raise Exception
                finally:
                    break
print(f())

# basic case combined with try-else
def f(arg):
    for _ in range(2):
        print(1)
        try:
            if arg == 1:
                raise ValueError
            elif arg == 2:
                raise TypeError
        except ValueError:
            print(2)
        else:
            print(3)
        finally:
            print(4)
            break
            print(5)
        print(6)
    print(7)
f(0) # no exception, else should execute
f(1) # exception caught, else should be skipped
f(2) # exception not caught, finally swallows exception, else should be skipped