Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cc91ac0

Browse files
committed
Factor out the protect-from-exceptions helpers and make capture_events()
use it. This simplifies the individual tests a little. Added some new tests related to exception handling.
1 parent 8dee809 commit cc91ac0

1 file changed

Lines changed: 65 additions & 35 deletions

File tree

Lib/test/test_profilehooks.py

Lines changed: 65 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -46,51 +46,49 @@ def test_simple(self):
4646
def f(p):
4747
pass
4848
f_ident = ident(f)
49-
self.check_events(f, [(0, 'call', f_ident),
50-
(0, 'return', f_ident),
49+
self.check_events(f, [(1, 'call', f_ident),
50+
(1, 'return', f_ident),
5151
])
5252

5353
def test_exception(self):
54+
def f(p):
55+
1/0
56+
f_ident = ident(f)
57+
self.check_events(f, [(1, 'call', f_ident),
58+
(1, 'exception', f_ident),
59+
(0, 'exception', protect_ident),
60+
])
61+
62+
def test_caught_exception(self):
5463
def f(p):
5564
try: 1/0
5665
except: pass
5766
f_ident = ident(f)
58-
self.check_events(f, [(0, 'call', f_ident),
59-
(0, 'exception', f_ident),
60-
(0, 'return', f_ident),
67+
self.check_events(f, [(1, 'call', f_ident),
68+
(1, 'exception', f_ident),
69+
(1, 'return', f_ident),
6170
])
6271

6372
def test_caught_nested_exception(self):
6473
def f(p):
6574
try: 1/0
6675
except: pass
67-
def g(p):
68-
f(p)
6976
f_ident = ident(f)
70-
g_ident = ident(g)
71-
self.check_events(g, [(0, 'call', g_ident),
72-
(1, 'call', f_ident),
77+
self.check_events(f, [(1, 'call', f_ident),
7378
(1, 'exception', f_ident),
7479
(1, 'return', f_ident),
75-
(0, 'return', g_ident),
7680
])
7781

7882
def test_nested_exception(self):
7983
def f(p):
8084
1/0
81-
def g(p):
82-
try: f(p)
83-
except: pass
8485
f_ident = ident(f)
85-
g_ident = ident(g)
86-
self.check_events(g, [(0, 'call', g_ident),
87-
(1, 'call', f_ident),
86+
self.check_events(f, [(1, 'call', f_ident),
8887
(1, 'exception', f_ident),
8988
# This isn't what I expected:
90-
(0, 'exception', g_ident),
89+
(0, 'exception', protect_ident),
9190
# I expected this again:
9291
# (1, 'exception', f_ident),
93-
(0, 'return', g_ident),
9492
])
9593

9694
def test_exception_in_except_clause(self):
@@ -104,14 +102,14 @@ def g(p):
104102
except: pass
105103
f_ident = ident(f)
106104
g_ident = ident(g)
107-
self.check_events(g, [(0, 'call', g_ident),
108-
(1, 'call', f_ident),
109-
(1, 'exception', f_ident),
110-
(0, 'exception', g_ident),
105+
self.check_events(g, [(1, 'call', g_ident),
111106
(2, 'call', f_ident),
112107
(2, 'exception', f_ident),
113-
(0, 'exception', g_ident),
114-
(0, 'return', g_ident),
108+
(1, 'exception', g_ident),
109+
(3, 'call', f_ident),
110+
(3, 'exception', f_ident),
111+
(1, 'exception', g_ident),
112+
(1, 'return', g_ident),
115113
])
116114

117115
def test_exception_propogation(self):
@@ -120,22 +118,47 @@ def f(p):
120118
def g(p):
121119
try: f(p)
122120
finally: p.add_event("falling through")
123-
def h(p):
124-
try: g(p)
125-
except: pass
126121
f_ident = ident(f)
127122
g_ident = ident(g)
128-
h_ident = ident(h)
129-
self.check_events(h, [(0, 'call', h_ident),
130-
(1, 'call', g_ident),
123+
self.check_events(g, [(1, 'call', g_ident),
131124
(2, 'call', f_ident),
132125
(2, 'exception', f_ident),
133126
(1, 'exception', g_ident),
134127
(1, 'falling through', g_ident),
135-
(0, 'exception', h_ident),
136-
(0, 'return', h_ident),
128+
(0, 'exception', protect_ident),
137129
])
138130

131+
def test_raise_twice(self):
132+
def f(p):
133+
try: 1/0
134+
except: 1/0
135+
f_ident = ident(f)
136+
self.check_events(f, [(1, 'call', f_ident),
137+
(1, 'exception', f_ident),
138+
(1, 'exception', f_ident),
139+
(0, 'exception', protect_ident)
140+
])
141+
142+
def test_raise_reraise(self):
143+
def f(p):
144+
try: 1/0
145+
except: raise
146+
f_ident = ident(f)
147+
self.check_events(f, [(1, 'call', f_ident),
148+
(1, 'exception', f_ident),
149+
(0, 'exception', protect_ident)
150+
])
151+
152+
def test_raise(self):
153+
def f(p):
154+
raise Exception()
155+
f_ident = ident(f)
156+
self.check_events(f, [(1, 'call', f_ident),
157+
(1, 'exception', f_ident),
158+
(0, 'exception', protect_ident)
159+
])
160+
161+
139162
def ident(function):
140163
if hasattr(function, "f_code"):
141164
code = function.f_code
@@ -144,12 +167,19 @@ def ident(function):
144167
return code.co_firstlineno, code.co_name
145168

146169

170+
def protect(f, p):
171+
try: f(p)
172+
except: pass
173+
174+
protect_ident = ident(protect)
175+
176+
147177
def capture_events(callable):
148178
p = HookWatcher()
149179
sys.setprofile(p.callback)
150-
callable(p)
180+
protect(callable, p)
151181
sys.setprofile(None)
152-
return p.get_events()
182+
return p.get_events()[1:-1]
153183

154184

155185
def show_events(callable):

0 commit comments

Comments
 (0)