1 | n/a | import gc |
---|
2 | n/a | import pprint |
---|
3 | n/a | import sys |
---|
4 | n/a | import unittest |
---|
5 | n/a | |
---|
6 | n/a | |
---|
7 | n/a | class TestGetProfile(unittest.TestCase): |
---|
8 | n/a | def setUp(self): |
---|
9 | n/a | sys.setprofile(None) |
---|
10 | n/a | |
---|
11 | n/a | def tearDown(self): |
---|
12 | n/a | sys.setprofile(None) |
---|
13 | n/a | |
---|
14 | n/a | def test_empty(self): |
---|
15 | n/a | self.assertIsNone(sys.getprofile()) |
---|
16 | n/a | |
---|
17 | n/a | def test_setget(self): |
---|
18 | n/a | def fn(*args): |
---|
19 | n/a | pass |
---|
20 | n/a | |
---|
21 | n/a | sys.setprofile(fn) |
---|
22 | n/a | self.assertIs(sys.getprofile(), fn) |
---|
23 | n/a | |
---|
24 | n/a | class HookWatcher: |
---|
25 | n/a | def __init__(self): |
---|
26 | n/a | self.frames = [] |
---|
27 | n/a | self.events = [] |
---|
28 | n/a | |
---|
29 | n/a | def callback(self, frame, event, arg): |
---|
30 | n/a | if (event == "call" |
---|
31 | n/a | or event == "return" |
---|
32 | n/a | or event == "exception"): |
---|
33 | n/a | self.add_event(event, frame) |
---|
34 | n/a | |
---|
35 | n/a | def add_event(self, event, frame=None): |
---|
36 | n/a | """Add an event to the log.""" |
---|
37 | n/a | if frame is None: |
---|
38 | n/a | frame = sys._getframe(1) |
---|
39 | n/a | |
---|
40 | n/a | try: |
---|
41 | n/a | frameno = self.frames.index(frame) |
---|
42 | n/a | except ValueError: |
---|
43 | n/a | frameno = len(self.frames) |
---|
44 | n/a | self.frames.append(frame) |
---|
45 | n/a | |
---|
46 | n/a | self.events.append((frameno, event, ident(frame))) |
---|
47 | n/a | |
---|
48 | n/a | def get_events(self): |
---|
49 | n/a | """Remove calls to add_event().""" |
---|
50 | n/a | disallowed = [ident(self.add_event.__func__), ident(ident)] |
---|
51 | n/a | self.frames = None |
---|
52 | n/a | |
---|
53 | n/a | return [item for item in self.events if item[2] not in disallowed] |
---|
54 | n/a | |
---|
55 | n/a | |
---|
56 | n/a | class ProfileSimulator(HookWatcher): |
---|
57 | n/a | def __init__(self, testcase): |
---|
58 | n/a | self.testcase = testcase |
---|
59 | n/a | self.stack = [] |
---|
60 | n/a | HookWatcher.__init__(self) |
---|
61 | n/a | |
---|
62 | n/a | def callback(self, frame, event, arg): |
---|
63 | n/a | # Callback registered with sys.setprofile()/sys.settrace() |
---|
64 | n/a | self.dispatch[event](self, frame) |
---|
65 | n/a | |
---|
66 | n/a | def trace_call(self, frame): |
---|
67 | n/a | self.add_event('call', frame) |
---|
68 | n/a | self.stack.append(frame) |
---|
69 | n/a | |
---|
70 | n/a | def trace_return(self, frame): |
---|
71 | n/a | self.add_event('return', frame) |
---|
72 | n/a | self.stack.pop() |
---|
73 | n/a | |
---|
74 | n/a | def trace_exception(self, frame): |
---|
75 | n/a | self.testcase.fail( |
---|
76 | n/a | "the profiler should never receive exception events") |
---|
77 | n/a | |
---|
78 | n/a | def trace_pass(self, frame): |
---|
79 | n/a | pass |
---|
80 | n/a | |
---|
81 | n/a | dispatch = { |
---|
82 | n/a | 'call': trace_call, |
---|
83 | n/a | 'exception': trace_exception, |
---|
84 | n/a | 'return': trace_return, |
---|
85 | n/a | 'c_call': trace_pass, |
---|
86 | n/a | 'c_return': trace_pass, |
---|
87 | n/a | 'c_exception': trace_pass, |
---|
88 | n/a | } |
---|
89 | n/a | |
---|
90 | n/a | |
---|
91 | n/a | class TestCaseBase(unittest.TestCase): |
---|
92 | n/a | def check_events(self, callable, expected): |
---|
93 | n/a | events = capture_events(callable, self.new_watcher()) |
---|
94 | n/a | if events != expected: |
---|
95 | n/a | self.fail("Expected events:\n%s\nReceived events:\n%s" |
---|
96 | n/a | % (pprint.pformat(expected), pprint.pformat(events))) |
---|
97 | n/a | |
---|
98 | n/a | |
---|
99 | n/a | class ProfileHookTestCase(TestCaseBase): |
---|
100 | n/a | def new_watcher(self): |
---|
101 | n/a | return HookWatcher() |
---|
102 | n/a | |
---|
103 | n/a | def test_simple(self): |
---|
104 | n/a | def f(p): |
---|
105 | n/a | pass |
---|
106 | n/a | f_ident = ident(f) |
---|
107 | n/a | self.check_events(f, [(1, 'call', f_ident), |
---|
108 | n/a | (1, 'return', f_ident), |
---|
109 | n/a | ]) |
---|
110 | n/a | |
---|
111 | n/a | def test_exception(self): |
---|
112 | n/a | def f(p): |
---|
113 | n/a | 1/0 |
---|
114 | n/a | f_ident = ident(f) |
---|
115 | n/a | self.check_events(f, [(1, 'call', f_ident), |
---|
116 | n/a | (1, 'return', f_ident), |
---|
117 | n/a | ]) |
---|
118 | n/a | |
---|
119 | n/a | def test_caught_exception(self): |
---|
120 | n/a | def f(p): |
---|
121 | n/a | try: 1/0 |
---|
122 | n/a | except: pass |
---|
123 | n/a | f_ident = ident(f) |
---|
124 | n/a | self.check_events(f, [(1, 'call', f_ident), |
---|
125 | n/a | (1, 'return', f_ident), |
---|
126 | n/a | ]) |
---|
127 | n/a | |
---|
128 | n/a | def test_caught_nested_exception(self): |
---|
129 | n/a | def f(p): |
---|
130 | n/a | try: 1/0 |
---|
131 | n/a | except: pass |
---|
132 | n/a | f_ident = ident(f) |
---|
133 | n/a | self.check_events(f, [(1, 'call', f_ident), |
---|
134 | n/a | (1, 'return', f_ident), |
---|
135 | n/a | ]) |
---|
136 | n/a | |
---|
137 | n/a | def test_nested_exception(self): |
---|
138 | n/a | def f(p): |
---|
139 | n/a | 1/0 |
---|
140 | n/a | f_ident = ident(f) |
---|
141 | n/a | self.check_events(f, [(1, 'call', f_ident), |
---|
142 | n/a | # This isn't what I expected: |
---|
143 | n/a | # (0, 'exception', protect_ident), |
---|
144 | n/a | # I expected this again: |
---|
145 | n/a | (1, 'return', f_ident), |
---|
146 | n/a | ]) |
---|
147 | n/a | |
---|
148 | n/a | def test_exception_in_except_clause(self): |
---|
149 | n/a | def f(p): |
---|
150 | n/a | 1/0 |
---|
151 | n/a | def g(p): |
---|
152 | n/a | try: |
---|
153 | n/a | f(p) |
---|
154 | n/a | except: |
---|
155 | n/a | try: f(p) |
---|
156 | n/a | except: pass |
---|
157 | n/a | f_ident = ident(f) |
---|
158 | n/a | g_ident = ident(g) |
---|
159 | n/a | self.check_events(g, [(1, 'call', g_ident), |
---|
160 | n/a | (2, 'call', f_ident), |
---|
161 | n/a | (2, 'return', f_ident), |
---|
162 | n/a | (3, 'call', f_ident), |
---|
163 | n/a | (3, 'return', f_ident), |
---|
164 | n/a | (1, 'return', g_ident), |
---|
165 | n/a | ]) |
---|
166 | n/a | |
---|
167 | n/a | def test_exception_propagation(self): |
---|
168 | n/a | def f(p): |
---|
169 | n/a | 1/0 |
---|
170 | n/a | def g(p): |
---|
171 | n/a | try: f(p) |
---|
172 | n/a | finally: p.add_event("falling through") |
---|
173 | n/a | f_ident = ident(f) |
---|
174 | n/a | g_ident = ident(g) |
---|
175 | n/a | self.check_events(g, [(1, 'call', g_ident), |
---|
176 | n/a | (2, 'call', f_ident), |
---|
177 | n/a | (2, 'return', f_ident), |
---|
178 | n/a | (1, 'falling through', g_ident), |
---|
179 | n/a | (1, 'return', g_ident), |
---|
180 | n/a | ]) |
---|
181 | n/a | |
---|
182 | n/a | def test_raise_twice(self): |
---|
183 | n/a | def f(p): |
---|
184 | n/a | try: 1/0 |
---|
185 | n/a | except: 1/0 |
---|
186 | n/a | f_ident = ident(f) |
---|
187 | n/a | self.check_events(f, [(1, 'call', f_ident), |
---|
188 | n/a | (1, 'return', f_ident), |
---|
189 | n/a | ]) |
---|
190 | n/a | |
---|
191 | n/a | def test_raise_reraise(self): |
---|
192 | n/a | def f(p): |
---|
193 | n/a | try: 1/0 |
---|
194 | n/a | except: raise |
---|
195 | n/a | f_ident = ident(f) |
---|
196 | n/a | self.check_events(f, [(1, 'call', f_ident), |
---|
197 | n/a | (1, 'return', f_ident), |
---|
198 | n/a | ]) |
---|
199 | n/a | |
---|
200 | n/a | def test_raise(self): |
---|
201 | n/a | def f(p): |
---|
202 | n/a | raise Exception() |
---|
203 | n/a | f_ident = ident(f) |
---|
204 | n/a | self.check_events(f, [(1, 'call', f_ident), |
---|
205 | n/a | (1, 'return', f_ident), |
---|
206 | n/a | ]) |
---|
207 | n/a | |
---|
208 | n/a | def test_distant_exception(self): |
---|
209 | n/a | def f(): |
---|
210 | n/a | 1/0 |
---|
211 | n/a | def g(): |
---|
212 | n/a | f() |
---|
213 | n/a | def h(): |
---|
214 | n/a | g() |
---|
215 | n/a | def i(): |
---|
216 | n/a | h() |
---|
217 | n/a | def j(p): |
---|
218 | n/a | i() |
---|
219 | n/a | f_ident = ident(f) |
---|
220 | n/a | g_ident = ident(g) |
---|
221 | n/a | h_ident = ident(h) |
---|
222 | n/a | i_ident = ident(i) |
---|
223 | n/a | j_ident = ident(j) |
---|
224 | n/a | self.check_events(j, [(1, 'call', j_ident), |
---|
225 | n/a | (2, 'call', i_ident), |
---|
226 | n/a | (3, 'call', h_ident), |
---|
227 | n/a | (4, 'call', g_ident), |
---|
228 | n/a | (5, 'call', f_ident), |
---|
229 | n/a | (5, 'return', f_ident), |
---|
230 | n/a | (4, 'return', g_ident), |
---|
231 | n/a | (3, 'return', h_ident), |
---|
232 | n/a | (2, 'return', i_ident), |
---|
233 | n/a | (1, 'return', j_ident), |
---|
234 | n/a | ]) |
---|
235 | n/a | |
---|
236 | n/a | def test_generator(self): |
---|
237 | n/a | def f(): |
---|
238 | n/a | for i in range(2): |
---|
239 | n/a | yield i |
---|
240 | n/a | def g(p): |
---|
241 | n/a | for i in f(): |
---|
242 | n/a | pass |
---|
243 | n/a | f_ident = ident(f) |
---|
244 | n/a | g_ident = ident(g) |
---|
245 | n/a | self.check_events(g, [(1, 'call', g_ident), |
---|
246 | n/a | # call the iterator twice to generate values |
---|
247 | n/a | (2, 'call', f_ident), |
---|
248 | n/a | (2, 'return', f_ident), |
---|
249 | n/a | (2, 'call', f_ident), |
---|
250 | n/a | (2, 'return', f_ident), |
---|
251 | n/a | # once more; returns end-of-iteration with |
---|
252 | n/a | # actually raising an exception |
---|
253 | n/a | (2, 'call', f_ident), |
---|
254 | n/a | (2, 'return', f_ident), |
---|
255 | n/a | (1, 'return', g_ident), |
---|
256 | n/a | ]) |
---|
257 | n/a | |
---|
258 | n/a | def test_stop_iteration(self): |
---|
259 | n/a | def f(): |
---|
260 | n/a | for i in range(2): |
---|
261 | n/a | yield i |
---|
262 | n/a | def g(p): |
---|
263 | n/a | for i in f(): |
---|
264 | n/a | pass |
---|
265 | n/a | f_ident = ident(f) |
---|
266 | n/a | g_ident = ident(g) |
---|
267 | n/a | self.check_events(g, [(1, 'call', g_ident), |
---|
268 | n/a | # call the iterator twice to generate values |
---|
269 | n/a | (2, 'call', f_ident), |
---|
270 | n/a | (2, 'return', f_ident), |
---|
271 | n/a | (2, 'call', f_ident), |
---|
272 | n/a | (2, 'return', f_ident), |
---|
273 | n/a | # once more to hit the raise: |
---|
274 | n/a | (2, 'call', f_ident), |
---|
275 | n/a | (2, 'return', f_ident), |
---|
276 | n/a | (1, 'return', g_ident), |
---|
277 | n/a | ]) |
---|
278 | n/a | |
---|
279 | n/a | |
---|
280 | n/a | class ProfileSimulatorTestCase(TestCaseBase): |
---|
281 | n/a | def new_watcher(self): |
---|
282 | n/a | return ProfileSimulator(self) |
---|
283 | n/a | |
---|
284 | n/a | def test_simple(self): |
---|
285 | n/a | def f(p): |
---|
286 | n/a | pass |
---|
287 | n/a | f_ident = ident(f) |
---|
288 | n/a | self.check_events(f, [(1, 'call', f_ident), |
---|
289 | n/a | (1, 'return', f_ident), |
---|
290 | n/a | ]) |
---|
291 | n/a | |
---|
292 | n/a | def test_basic_exception(self): |
---|
293 | n/a | def f(p): |
---|
294 | n/a | 1/0 |
---|
295 | n/a | f_ident = ident(f) |
---|
296 | n/a | self.check_events(f, [(1, 'call', f_ident), |
---|
297 | n/a | (1, 'return', f_ident), |
---|
298 | n/a | ]) |
---|
299 | n/a | |
---|
300 | n/a | def test_caught_exception(self): |
---|
301 | n/a | def f(p): |
---|
302 | n/a | try: 1/0 |
---|
303 | n/a | except: pass |
---|
304 | n/a | f_ident = ident(f) |
---|
305 | n/a | self.check_events(f, [(1, 'call', f_ident), |
---|
306 | n/a | (1, 'return', f_ident), |
---|
307 | n/a | ]) |
---|
308 | n/a | |
---|
309 | n/a | def test_distant_exception(self): |
---|
310 | n/a | def f(): |
---|
311 | n/a | 1/0 |
---|
312 | n/a | def g(): |
---|
313 | n/a | f() |
---|
314 | n/a | def h(): |
---|
315 | n/a | g() |
---|
316 | n/a | def i(): |
---|
317 | n/a | h() |
---|
318 | n/a | def j(p): |
---|
319 | n/a | i() |
---|
320 | n/a | f_ident = ident(f) |
---|
321 | n/a | g_ident = ident(g) |
---|
322 | n/a | h_ident = ident(h) |
---|
323 | n/a | i_ident = ident(i) |
---|
324 | n/a | j_ident = ident(j) |
---|
325 | n/a | self.check_events(j, [(1, 'call', j_ident), |
---|
326 | n/a | (2, 'call', i_ident), |
---|
327 | n/a | (3, 'call', h_ident), |
---|
328 | n/a | (4, 'call', g_ident), |
---|
329 | n/a | (5, 'call', f_ident), |
---|
330 | n/a | (5, 'return', f_ident), |
---|
331 | n/a | (4, 'return', g_ident), |
---|
332 | n/a | (3, 'return', h_ident), |
---|
333 | n/a | (2, 'return', i_ident), |
---|
334 | n/a | (1, 'return', j_ident), |
---|
335 | n/a | ]) |
---|
336 | n/a | |
---|
337 | n/a | |
---|
338 | n/a | def ident(function): |
---|
339 | n/a | if hasattr(function, "f_code"): |
---|
340 | n/a | code = function.f_code |
---|
341 | n/a | else: |
---|
342 | n/a | code = function.__code__ |
---|
343 | n/a | return code.co_firstlineno, code.co_name |
---|
344 | n/a | |
---|
345 | n/a | |
---|
346 | n/a | def protect(f, p): |
---|
347 | n/a | try: f(p) |
---|
348 | n/a | except: pass |
---|
349 | n/a | |
---|
350 | n/a | protect_ident = ident(protect) |
---|
351 | n/a | |
---|
352 | n/a | |
---|
353 | n/a | def capture_events(callable, p=None): |
---|
354 | n/a | if p is None: |
---|
355 | n/a | p = HookWatcher() |
---|
356 | n/a | # Disable the garbage collector. This prevents __del__s from showing up in |
---|
357 | n/a | # traces. |
---|
358 | n/a | old_gc = gc.isenabled() |
---|
359 | n/a | gc.disable() |
---|
360 | n/a | try: |
---|
361 | n/a | sys.setprofile(p.callback) |
---|
362 | n/a | protect(callable, p) |
---|
363 | n/a | sys.setprofile(None) |
---|
364 | n/a | finally: |
---|
365 | n/a | if old_gc: |
---|
366 | n/a | gc.enable() |
---|
367 | n/a | return p.get_events()[1:-1] |
---|
368 | n/a | |
---|
369 | n/a | |
---|
370 | n/a | def show_events(callable): |
---|
371 | n/a | import pprint |
---|
372 | n/a | pprint.pprint(capture_events(callable)) |
---|
373 | n/a | |
---|
374 | n/a | |
---|
375 | n/a | if __name__ == "__main__": |
---|
376 | n/a | unittest.main() |
---|