-
Notifications
You must be signed in to change notification settings - Fork 6
/
test_exceptions.py
143 lines (128 loc) · 3.91 KB
/
test_exceptions.py
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from __future__ import print_function
import vmtest
class TestExceptions(vmtest.VmTestCase):
def test_catching_exceptions(self):
self.assert_ok("""\
try:
[][1]
print("Shouldn't be here...")
except IndexError:
print("caught it!")
""")
self.assert_ok("""\
try:
[][1]
print("Shouldn't be here...")
except Exception:
print("caught it!")
""")
self.assert_ok("""\
try:
[][1]
print("Shouldn't be here...")
except:
print("caught it!")
""")
def test_raise_exception(self):
self.assert_ok("raise Exception('oops')", raises=Exception)
def test_raise_exception_class(self):
self.assert_ok("raise ValueError", raises=ValueError)
def test_raise_exception_2args(self):
self.assert_ok("raise ValueError, 'bad'", raises=ValueError)
def test_raise_exception_3args(self):
self.assert_ok("""\
from sys import exc_info
try:
raise Exception
except:
_, _, tb = exc_info()
raise ValueError, "message", tb
""", raises=ValueError)
def test_raise_and_catch_exception(self):
self.assert_ok("""\
try:
raise ValueError("oops")
except ValueError as e:
print("Caught: %s" % e)
print("All done")
""")
def test_raise_and_catch_exception_in_function(self):
self.assert_ok("""\
def fn():
raise ValueError("oops")
try:
fn()
except ValueError as e:
print("Caught: %s" % e)
print("done")
""")
def test_global_name_error(self):
self.assert_ok("fooey", raises=NameError)
self.assert_ok("""\
try:
fooey
print("Yes fooey?")
except NameError:
print("No fooey")
""")
def test_local_name_error(self):
self.assert_ok("""\
def fn():
fooey
fn()
""", raises=NameError)
def test_catch_local_name_error(self):
self.assert_ok("""\
def fn():
try:
fooey
print("Yes fooey?")
except NameError:
print("No fooey")
fn()
""")
def test_reraise(self):
self.assert_ok("""\
def fn():
try:
fooey
print("Yes fooey?")
except NameError:
print("No fooey")
raise
fn()
""", raises=NameError)
def test_reraise_explicit_exception(self):
self.assert_ok("""\
def fn():
try:
raise ValueError("ouch")
except ValueError as e:
print("Caught %s" % e)
raise
fn()
""", raises=ValueError)
def test_finally_while_throwing(self):
self.assert_ok("""\
def fn():
try:
print("About to..")
raise ValueError("ouch")
finally:
print("Finally")
fn()
print("Done")
""", raises=ValueError)
def test_coverage_issue_92(self):
self.assert_ok("""\
l = []
for i in range(3):
try:
l.append(i)
finally:
l.append('f')
l.append('e')
l.append('r')
print(l)
assert l == [0, 'f', 'e', 1, 'f', 'e', 2, 'f', 'e', 'r']
""")