1 | n/a | # Testing the line trace facility. |
---|
2 | n/a | |
---|
3 | n/a | from test import support |
---|
4 | n/a | import unittest |
---|
5 | n/a | import sys |
---|
6 | n/a | import difflib |
---|
7 | n/a | import gc |
---|
8 | n/a | |
---|
9 | n/a | # A very basic example. If this fails, we're in deep trouble. |
---|
10 | n/a | def basic(): |
---|
11 | n/a | return 1 |
---|
12 | n/a | |
---|
13 | n/a | basic.events = [(0, 'call'), |
---|
14 | n/a | (1, 'line'), |
---|
15 | n/a | (1, 'return')] |
---|
16 | n/a | |
---|
17 | n/a | # Many of the tests below are tricky because they involve pass statements. |
---|
18 | n/a | # If there is implicit control flow around a pass statement (in an except |
---|
19 | n/a | # clause or else caluse) under what conditions do you set a line number |
---|
20 | n/a | # following that clause? |
---|
21 | n/a | |
---|
22 | n/a | |
---|
23 | n/a | # The entire "while 0:" statement is optimized away. No code |
---|
24 | n/a | # exists for it, so the line numbers skip directly from "del x" |
---|
25 | n/a | # to "x = 1". |
---|
26 | n/a | def arigo_example(): |
---|
27 | n/a | x = 1 |
---|
28 | n/a | del x |
---|
29 | n/a | while 0: |
---|
30 | n/a | pass |
---|
31 | n/a | x = 1 |
---|
32 | n/a | |
---|
33 | n/a | arigo_example.events = [(0, 'call'), |
---|
34 | n/a | (1, 'line'), |
---|
35 | n/a | (2, 'line'), |
---|
36 | n/a | (5, 'line'), |
---|
37 | n/a | (5, 'return')] |
---|
38 | n/a | |
---|
39 | n/a | # check that lines consisting of just one instruction get traced: |
---|
40 | n/a | def one_instr_line(): |
---|
41 | n/a | x = 1 |
---|
42 | n/a | del x |
---|
43 | n/a | x = 1 |
---|
44 | n/a | |
---|
45 | n/a | one_instr_line.events = [(0, 'call'), |
---|
46 | n/a | (1, 'line'), |
---|
47 | n/a | (2, 'line'), |
---|
48 | n/a | (3, 'line'), |
---|
49 | n/a | (3, 'return')] |
---|
50 | n/a | |
---|
51 | n/a | def no_pop_tops(): # 0 |
---|
52 | n/a | x = 1 # 1 |
---|
53 | n/a | for a in range(2): # 2 |
---|
54 | n/a | if a: # 3 |
---|
55 | n/a | x = 1 # 4 |
---|
56 | n/a | else: # 5 |
---|
57 | n/a | x = 1 # 6 |
---|
58 | n/a | |
---|
59 | n/a | no_pop_tops.events = [(0, 'call'), |
---|
60 | n/a | (1, 'line'), |
---|
61 | n/a | (2, 'line'), |
---|
62 | n/a | (3, 'line'), |
---|
63 | n/a | (6, 'line'), |
---|
64 | n/a | (2, 'line'), |
---|
65 | n/a | (3, 'line'), |
---|
66 | n/a | (4, 'line'), |
---|
67 | n/a | (2, 'line'), |
---|
68 | n/a | (2, 'return')] |
---|
69 | n/a | |
---|
70 | n/a | def no_pop_blocks(): |
---|
71 | n/a | y = 1 |
---|
72 | n/a | while not y: |
---|
73 | n/a | bla |
---|
74 | n/a | x = 1 |
---|
75 | n/a | |
---|
76 | n/a | no_pop_blocks.events = [(0, 'call'), |
---|
77 | n/a | (1, 'line'), |
---|
78 | n/a | (2, 'line'), |
---|
79 | n/a | (4, 'line'), |
---|
80 | n/a | (4, 'return')] |
---|
81 | n/a | |
---|
82 | n/a | def called(): # line -3 |
---|
83 | n/a | x = 1 |
---|
84 | n/a | |
---|
85 | n/a | def call(): # line 0 |
---|
86 | n/a | called() |
---|
87 | n/a | |
---|
88 | n/a | call.events = [(0, 'call'), |
---|
89 | n/a | (1, 'line'), |
---|
90 | n/a | (-3, 'call'), |
---|
91 | n/a | (-2, 'line'), |
---|
92 | n/a | (-2, 'return'), |
---|
93 | n/a | (1, 'return')] |
---|
94 | n/a | |
---|
95 | n/a | def raises(): |
---|
96 | n/a | raise Exception |
---|
97 | n/a | |
---|
98 | n/a | def test_raise(): |
---|
99 | n/a | try: |
---|
100 | n/a | raises() |
---|
101 | n/a | except Exception as exc: |
---|
102 | n/a | x = 1 |
---|
103 | n/a | |
---|
104 | n/a | test_raise.events = [(0, 'call'), |
---|
105 | n/a | (1, 'line'), |
---|
106 | n/a | (2, 'line'), |
---|
107 | n/a | (-3, 'call'), |
---|
108 | n/a | (-2, 'line'), |
---|
109 | n/a | (-2, 'exception'), |
---|
110 | n/a | (-2, 'return'), |
---|
111 | n/a | (2, 'exception'), |
---|
112 | n/a | (3, 'line'), |
---|
113 | n/a | (4, 'line'), |
---|
114 | n/a | (4, 'return')] |
---|
115 | n/a | |
---|
116 | n/a | def _settrace_and_return(tracefunc): |
---|
117 | n/a | sys.settrace(tracefunc) |
---|
118 | n/a | sys._getframe().f_back.f_trace = tracefunc |
---|
119 | n/a | def settrace_and_return(tracefunc): |
---|
120 | n/a | _settrace_and_return(tracefunc) |
---|
121 | n/a | |
---|
122 | n/a | settrace_and_return.events = [(1, 'return')] |
---|
123 | n/a | |
---|
124 | n/a | def _settrace_and_raise(tracefunc): |
---|
125 | n/a | sys.settrace(tracefunc) |
---|
126 | n/a | sys._getframe().f_back.f_trace = tracefunc |
---|
127 | n/a | raise RuntimeError |
---|
128 | n/a | def settrace_and_raise(tracefunc): |
---|
129 | n/a | try: |
---|
130 | n/a | _settrace_and_raise(tracefunc) |
---|
131 | n/a | except RuntimeError as exc: |
---|
132 | n/a | pass |
---|
133 | n/a | |
---|
134 | n/a | settrace_and_raise.events = [(2, 'exception'), |
---|
135 | n/a | (3, 'line'), |
---|
136 | n/a | (4, 'line'), |
---|
137 | n/a | (4, 'return')] |
---|
138 | n/a | |
---|
139 | n/a | # implicit return example |
---|
140 | n/a | # This test is interesting because of the else: pass |
---|
141 | n/a | # part of the code. The code generate for the true |
---|
142 | n/a | # part of the if contains a jump past the else branch. |
---|
143 | n/a | # The compiler then generates an implicit "return None" |
---|
144 | n/a | # Internally, the compiler visits the pass statement |
---|
145 | n/a | # and stores its line number for use on the next instruction. |
---|
146 | n/a | # The next instruction is the implicit return None. |
---|
147 | n/a | def ireturn_example(): |
---|
148 | n/a | a = 5 |
---|
149 | n/a | b = 5 |
---|
150 | n/a | if a == b: |
---|
151 | n/a | b = a+1 |
---|
152 | n/a | else: |
---|
153 | n/a | pass |
---|
154 | n/a | |
---|
155 | n/a | ireturn_example.events = [(0, 'call'), |
---|
156 | n/a | (1, 'line'), |
---|
157 | n/a | (2, 'line'), |
---|
158 | n/a | (3, 'line'), |
---|
159 | n/a | (4, 'line'), |
---|
160 | n/a | (6, 'line'), |
---|
161 | n/a | (6, 'return')] |
---|
162 | n/a | |
---|
163 | n/a | # Tight loop with while(1) example (SF #765624) |
---|
164 | n/a | def tightloop_example(): |
---|
165 | n/a | items = range(0, 3) |
---|
166 | n/a | try: |
---|
167 | n/a | i = 0 |
---|
168 | n/a | while 1: |
---|
169 | n/a | b = items[i]; i+=1 |
---|
170 | n/a | except IndexError: |
---|
171 | n/a | pass |
---|
172 | n/a | |
---|
173 | n/a | tightloop_example.events = [(0, 'call'), |
---|
174 | n/a | (1, 'line'), |
---|
175 | n/a | (2, 'line'), |
---|
176 | n/a | (3, 'line'), |
---|
177 | n/a | (4, 'line'), |
---|
178 | n/a | (5, 'line'), |
---|
179 | n/a | (5, 'line'), |
---|
180 | n/a | (5, 'line'), |
---|
181 | n/a | (5, 'line'), |
---|
182 | n/a | (5, 'exception'), |
---|
183 | n/a | (6, 'line'), |
---|
184 | n/a | (7, 'line'), |
---|
185 | n/a | (7, 'return')] |
---|
186 | n/a | |
---|
187 | n/a | def tighterloop_example(): |
---|
188 | n/a | items = range(1, 4) |
---|
189 | n/a | try: |
---|
190 | n/a | i = 0 |
---|
191 | n/a | while 1: i = items[i] |
---|
192 | n/a | except IndexError: |
---|
193 | n/a | pass |
---|
194 | n/a | |
---|
195 | n/a | tighterloop_example.events = [(0, 'call'), |
---|
196 | n/a | (1, 'line'), |
---|
197 | n/a | (2, 'line'), |
---|
198 | n/a | (3, 'line'), |
---|
199 | n/a | (4, 'line'), |
---|
200 | n/a | (4, 'line'), |
---|
201 | n/a | (4, 'line'), |
---|
202 | n/a | (4, 'line'), |
---|
203 | n/a | (4, 'exception'), |
---|
204 | n/a | (5, 'line'), |
---|
205 | n/a | (6, 'line'), |
---|
206 | n/a | (6, 'return')] |
---|
207 | n/a | |
---|
208 | n/a | def generator_function(): |
---|
209 | n/a | try: |
---|
210 | n/a | yield True |
---|
211 | n/a | "continued" |
---|
212 | n/a | finally: |
---|
213 | n/a | "finally" |
---|
214 | n/a | def generator_example(): |
---|
215 | n/a | # any() will leave the generator before its end |
---|
216 | n/a | x = any(generator_function()) |
---|
217 | n/a | |
---|
218 | n/a | # the following lines were not traced |
---|
219 | n/a | for x in range(10): |
---|
220 | n/a | y = x |
---|
221 | n/a | |
---|
222 | n/a | generator_example.events = ([(0, 'call'), |
---|
223 | n/a | (2, 'line'), |
---|
224 | n/a | (-6, 'call'), |
---|
225 | n/a | (-5, 'line'), |
---|
226 | n/a | (-4, 'line'), |
---|
227 | n/a | (-4, 'return'), |
---|
228 | n/a | (-4, 'call'), |
---|
229 | n/a | (-4, 'exception'), |
---|
230 | n/a | (-1, 'line'), |
---|
231 | n/a | (-1, 'return')] + |
---|
232 | n/a | [(5, 'line'), (6, 'line')] * 10 + |
---|
233 | n/a | [(5, 'line'), (5, 'return')]) |
---|
234 | n/a | |
---|
235 | n/a | |
---|
236 | n/a | class Tracer: |
---|
237 | n/a | def __init__(self): |
---|
238 | n/a | self.events = [] |
---|
239 | n/a | def trace(self, frame, event, arg): |
---|
240 | n/a | self.events.append((frame.f_lineno, event)) |
---|
241 | n/a | return self.trace |
---|
242 | n/a | def traceWithGenexp(self, frame, event, arg): |
---|
243 | n/a | (o for o in [1]) |
---|
244 | n/a | self.events.append((frame.f_lineno, event)) |
---|
245 | n/a | return self.trace |
---|
246 | n/a | |
---|
247 | n/a | class TraceTestCase(unittest.TestCase): |
---|
248 | n/a | |
---|
249 | n/a | # Disable gc collection when tracing, otherwise the |
---|
250 | n/a | # deallocators may be traced as well. |
---|
251 | n/a | def setUp(self): |
---|
252 | n/a | self.using_gc = gc.isenabled() |
---|
253 | n/a | gc.disable() |
---|
254 | n/a | self.addCleanup(sys.settrace, sys.gettrace()) |
---|
255 | n/a | |
---|
256 | n/a | def tearDown(self): |
---|
257 | n/a | if self.using_gc: |
---|
258 | n/a | gc.enable() |
---|
259 | n/a | |
---|
260 | n/a | def compare_events(self, line_offset, events, expected_events): |
---|
261 | n/a | events = [(l - line_offset, e) for (l, e) in events] |
---|
262 | n/a | if events != expected_events: |
---|
263 | n/a | self.fail( |
---|
264 | n/a | "events did not match expectation:\n" + |
---|
265 | n/a | "\n".join(difflib.ndiff([str(x) for x in expected_events], |
---|
266 | n/a | [str(x) for x in events]))) |
---|
267 | n/a | |
---|
268 | n/a | def run_and_compare(self, func, events): |
---|
269 | n/a | tracer = Tracer() |
---|
270 | n/a | sys.settrace(tracer.trace) |
---|
271 | n/a | func() |
---|
272 | n/a | sys.settrace(None) |
---|
273 | n/a | self.compare_events(func.__code__.co_firstlineno, |
---|
274 | n/a | tracer.events, events) |
---|
275 | n/a | |
---|
276 | n/a | def run_test(self, func): |
---|
277 | n/a | self.run_and_compare(func, func.events) |
---|
278 | n/a | |
---|
279 | n/a | def run_test2(self, func): |
---|
280 | n/a | tracer = Tracer() |
---|
281 | n/a | func(tracer.trace) |
---|
282 | n/a | sys.settrace(None) |
---|
283 | n/a | self.compare_events(func.__code__.co_firstlineno, |
---|
284 | n/a | tracer.events, func.events) |
---|
285 | n/a | |
---|
286 | n/a | def test_set_and_retrieve_none(self): |
---|
287 | n/a | sys.settrace(None) |
---|
288 | n/a | assert sys.gettrace() is None |
---|
289 | n/a | |
---|
290 | n/a | def test_set_and_retrieve_func(self): |
---|
291 | n/a | def fn(*args): |
---|
292 | n/a | pass |
---|
293 | n/a | |
---|
294 | n/a | sys.settrace(fn) |
---|
295 | n/a | try: |
---|
296 | n/a | assert sys.gettrace() is fn |
---|
297 | n/a | finally: |
---|
298 | n/a | sys.settrace(None) |
---|
299 | n/a | |
---|
300 | n/a | def test_01_basic(self): |
---|
301 | n/a | self.run_test(basic) |
---|
302 | n/a | def test_02_arigo(self): |
---|
303 | n/a | self.run_test(arigo_example) |
---|
304 | n/a | def test_03_one_instr(self): |
---|
305 | n/a | self.run_test(one_instr_line) |
---|
306 | n/a | def test_04_no_pop_blocks(self): |
---|
307 | n/a | self.run_test(no_pop_blocks) |
---|
308 | n/a | def test_05_no_pop_tops(self): |
---|
309 | n/a | self.run_test(no_pop_tops) |
---|
310 | n/a | def test_06_call(self): |
---|
311 | n/a | self.run_test(call) |
---|
312 | n/a | def test_07_raise(self): |
---|
313 | n/a | self.run_test(test_raise) |
---|
314 | n/a | |
---|
315 | n/a | def test_08_settrace_and_return(self): |
---|
316 | n/a | self.run_test2(settrace_and_return) |
---|
317 | n/a | def test_09_settrace_and_raise(self): |
---|
318 | n/a | self.run_test2(settrace_and_raise) |
---|
319 | n/a | def test_10_ireturn(self): |
---|
320 | n/a | self.run_test(ireturn_example) |
---|
321 | n/a | def test_11_tightloop(self): |
---|
322 | n/a | self.run_test(tightloop_example) |
---|
323 | n/a | def test_12_tighterloop(self): |
---|
324 | n/a | self.run_test(tighterloop_example) |
---|
325 | n/a | |
---|
326 | n/a | def test_13_genexp(self): |
---|
327 | n/a | self.run_test(generator_example) |
---|
328 | n/a | # issue1265: if the trace function contains a generator, |
---|
329 | n/a | # and if the traced function contains another generator |
---|
330 | n/a | # that is not completely exhausted, the trace stopped. |
---|
331 | n/a | # Worse: the 'finally' clause was not invoked. |
---|
332 | n/a | tracer = Tracer() |
---|
333 | n/a | sys.settrace(tracer.traceWithGenexp) |
---|
334 | n/a | generator_example() |
---|
335 | n/a | sys.settrace(None) |
---|
336 | n/a | self.compare_events(generator_example.__code__.co_firstlineno, |
---|
337 | n/a | tracer.events, generator_example.events) |
---|
338 | n/a | |
---|
339 | n/a | def test_14_onliner_if(self): |
---|
340 | n/a | def onliners(): |
---|
341 | n/a | if True: x=False |
---|
342 | n/a | else: x=True |
---|
343 | n/a | return 0 |
---|
344 | n/a | self.run_and_compare( |
---|
345 | n/a | onliners, |
---|
346 | n/a | [(0, 'call'), |
---|
347 | n/a | (1, 'line'), |
---|
348 | n/a | (3, 'line'), |
---|
349 | n/a | (3, 'return')]) |
---|
350 | n/a | |
---|
351 | n/a | def test_15_loops(self): |
---|
352 | n/a | # issue1750076: "while" expression is skipped by debugger |
---|
353 | n/a | def for_example(): |
---|
354 | n/a | for x in range(2): |
---|
355 | n/a | pass |
---|
356 | n/a | self.run_and_compare( |
---|
357 | n/a | for_example, |
---|
358 | n/a | [(0, 'call'), |
---|
359 | n/a | (1, 'line'), |
---|
360 | n/a | (2, 'line'), |
---|
361 | n/a | (1, 'line'), |
---|
362 | n/a | (2, 'line'), |
---|
363 | n/a | (1, 'line'), |
---|
364 | n/a | (1, 'return')]) |
---|
365 | n/a | |
---|
366 | n/a | def while_example(): |
---|
367 | n/a | # While expression should be traced on every loop |
---|
368 | n/a | x = 2 |
---|
369 | n/a | while x > 0: |
---|
370 | n/a | x -= 1 |
---|
371 | n/a | self.run_and_compare( |
---|
372 | n/a | while_example, |
---|
373 | n/a | [(0, 'call'), |
---|
374 | n/a | (2, 'line'), |
---|
375 | n/a | (3, 'line'), |
---|
376 | n/a | (4, 'line'), |
---|
377 | n/a | (3, 'line'), |
---|
378 | n/a | (4, 'line'), |
---|
379 | n/a | (3, 'line'), |
---|
380 | n/a | (3, 'return')]) |
---|
381 | n/a | |
---|
382 | n/a | def test_16_blank_lines(self): |
---|
383 | n/a | namespace = {} |
---|
384 | n/a | exec("def f():\n" + "\n" * 256 + " pass", namespace) |
---|
385 | n/a | self.run_and_compare( |
---|
386 | n/a | namespace["f"], |
---|
387 | n/a | [(0, 'call'), |
---|
388 | n/a | (257, 'line'), |
---|
389 | n/a | (257, 'return')]) |
---|
390 | n/a | |
---|
391 | n/a | def test_17_none_f_trace(self): |
---|
392 | n/a | # Issue 20041: fix TypeError when f_trace is set to None. |
---|
393 | n/a | def func(): |
---|
394 | n/a | sys._getframe().f_trace = None |
---|
395 | n/a | lineno = 2 |
---|
396 | n/a | self.run_and_compare(func, |
---|
397 | n/a | [(0, 'call'), |
---|
398 | n/a | (1, 'line')]) |
---|
399 | n/a | |
---|
400 | n/a | |
---|
401 | n/a | class RaisingTraceFuncTestCase(unittest.TestCase): |
---|
402 | n/a | def setUp(self): |
---|
403 | n/a | self.addCleanup(sys.settrace, sys.gettrace()) |
---|
404 | n/a | |
---|
405 | n/a | def trace(self, frame, event, arg): |
---|
406 | n/a | """A trace function that raises an exception in response to a |
---|
407 | n/a | specific trace event.""" |
---|
408 | n/a | if event == self.raiseOnEvent: |
---|
409 | n/a | raise ValueError # just something that isn't RuntimeError |
---|
410 | n/a | else: |
---|
411 | n/a | return self.trace |
---|
412 | n/a | |
---|
413 | n/a | def f(self): |
---|
414 | n/a | """The function to trace; raises an exception if that's the case |
---|
415 | n/a | we're testing, so that the 'exception' trace event fires.""" |
---|
416 | n/a | if self.raiseOnEvent == 'exception': |
---|
417 | n/a | x = 0 |
---|
418 | n/a | y = 1/x |
---|
419 | n/a | else: |
---|
420 | n/a | return 1 |
---|
421 | n/a | |
---|
422 | n/a | def run_test_for_event(self, event): |
---|
423 | n/a | """Tests that an exception raised in response to the given event is |
---|
424 | n/a | handled OK.""" |
---|
425 | n/a | self.raiseOnEvent = event |
---|
426 | n/a | try: |
---|
427 | n/a | for i in range(sys.getrecursionlimit() + 1): |
---|
428 | n/a | sys.settrace(self.trace) |
---|
429 | n/a | try: |
---|
430 | n/a | self.f() |
---|
431 | n/a | except ValueError: |
---|
432 | n/a | pass |
---|
433 | n/a | else: |
---|
434 | n/a | self.fail("exception not raised!") |
---|
435 | n/a | except RuntimeError: |
---|
436 | n/a | self.fail("recursion counter not reset") |
---|
437 | n/a | |
---|
438 | n/a | # Test the handling of exceptions raised by each kind of trace event. |
---|
439 | n/a | def test_call(self): |
---|
440 | n/a | self.run_test_for_event('call') |
---|
441 | n/a | def test_line(self): |
---|
442 | n/a | self.run_test_for_event('line') |
---|
443 | n/a | def test_return(self): |
---|
444 | n/a | self.run_test_for_event('return') |
---|
445 | n/a | def test_exception(self): |
---|
446 | n/a | self.run_test_for_event('exception') |
---|
447 | n/a | |
---|
448 | n/a | def test_trash_stack(self): |
---|
449 | n/a | def f(): |
---|
450 | n/a | for i in range(5): |
---|
451 | n/a | print(i) # line tracing will raise an exception at this line |
---|
452 | n/a | |
---|
453 | n/a | def g(frame, why, extra): |
---|
454 | n/a | if (why == 'line' and |
---|
455 | n/a | frame.f_lineno == f.__code__.co_firstlineno + 2): |
---|
456 | n/a | raise RuntimeError("i am crashing") |
---|
457 | n/a | return g |
---|
458 | n/a | |
---|
459 | n/a | sys.settrace(g) |
---|
460 | n/a | try: |
---|
461 | n/a | f() |
---|
462 | n/a | except RuntimeError: |
---|
463 | n/a | # the test is really that this doesn't segfault: |
---|
464 | n/a | import gc |
---|
465 | n/a | gc.collect() |
---|
466 | n/a | else: |
---|
467 | n/a | self.fail("exception not propagated") |
---|
468 | n/a | |
---|
469 | n/a | |
---|
470 | n/a | def test_exception_arguments(self): |
---|
471 | n/a | def f(): |
---|
472 | n/a | x = 0 |
---|
473 | n/a | # this should raise an error |
---|
474 | n/a | x.no_such_attr |
---|
475 | n/a | def g(frame, event, arg): |
---|
476 | n/a | if (event == 'exception'): |
---|
477 | n/a | type, exception, trace = arg |
---|
478 | n/a | self.assertIsInstance(exception, Exception) |
---|
479 | n/a | return g |
---|
480 | n/a | |
---|
481 | n/a | existing = sys.gettrace() |
---|
482 | n/a | try: |
---|
483 | n/a | sys.settrace(g) |
---|
484 | n/a | try: |
---|
485 | n/a | f() |
---|
486 | n/a | except AttributeError: |
---|
487 | n/a | # this is expected |
---|
488 | n/a | pass |
---|
489 | n/a | finally: |
---|
490 | n/a | sys.settrace(existing) |
---|
491 | n/a | |
---|
492 | n/a | |
---|
493 | n/a | # 'Jump' tests: assigning to frame.f_lineno within a trace function |
---|
494 | n/a | # moves the execution position - it's how debuggers implement a Jump |
---|
495 | n/a | # command (aka. "Set next statement"). |
---|
496 | n/a | |
---|
497 | n/a | class JumpTracer: |
---|
498 | n/a | """Defines a trace function that jumps from one place to another, |
---|
499 | n/a | with the source and destination lines of the jump being defined by |
---|
500 | n/a | the 'jump' property of the function under test.""" |
---|
501 | n/a | |
---|
502 | n/a | def __init__(self, function): |
---|
503 | n/a | self.function = function |
---|
504 | n/a | self.jumpFrom = function.jump[0] |
---|
505 | n/a | self.jumpTo = function.jump[1] |
---|
506 | n/a | self.done = False |
---|
507 | n/a | |
---|
508 | n/a | def trace(self, frame, event, arg): |
---|
509 | n/a | if not self.done and frame.f_code == self.function.__code__: |
---|
510 | n/a | firstLine = frame.f_code.co_firstlineno |
---|
511 | n/a | if event == 'line' and frame.f_lineno == firstLine + self.jumpFrom: |
---|
512 | n/a | # Cope with non-integer self.jumpTo (because of |
---|
513 | n/a | # no_jump_to_non_integers below). |
---|
514 | n/a | try: |
---|
515 | n/a | frame.f_lineno = firstLine + self.jumpTo |
---|
516 | n/a | except TypeError: |
---|
517 | n/a | frame.f_lineno = self.jumpTo |
---|
518 | n/a | self.done = True |
---|
519 | n/a | return self.trace |
---|
520 | n/a | |
---|
521 | n/a | # The first set of 'jump' tests are for things that are allowed: |
---|
522 | n/a | |
---|
523 | n/a | def jump_simple_forwards(output): |
---|
524 | n/a | output.append(1) |
---|
525 | n/a | output.append(2) |
---|
526 | n/a | output.append(3) |
---|
527 | n/a | |
---|
528 | n/a | jump_simple_forwards.jump = (1, 3) |
---|
529 | n/a | jump_simple_forwards.output = [3] |
---|
530 | n/a | |
---|
531 | n/a | def jump_simple_backwards(output): |
---|
532 | n/a | output.append(1) |
---|
533 | n/a | output.append(2) |
---|
534 | n/a | |
---|
535 | n/a | jump_simple_backwards.jump = (2, 1) |
---|
536 | n/a | jump_simple_backwards.output = [1, 1, 2] |
---|
537 | n/a | |
---|
538 | n/a | def jump_out_of_block_forwards(output): |
---|
539 | n/a | for i in 1, 2: |
---|
540 | n/a | output.append(2) |
---|
541 | n/a | for j in [3]: # Also tests jumping over a block |
---|
542 | n/a | output.append(4) |
---|
543 | n/a | output.append(5) |
---|
544 | n/a | |
---|
545 | n/a | jump_out_of_block_forwards.jump = (3, 5) |
---|
546 | n/a | jump_out_of_block_forwards.output = [2, 5] |
---|
547 | n/a | |
---|
548 | n/a | def jump_out_of_block_backwards(output): |
---|
549 | n/a | output.append(1) |
---|
550 | n/a | for i in [1]: |
---|
551 | n/a | output.append(3) |
---|
552 | n/a | for j in [2]: # Also tests jumping over a block |
---|
553 | n/a | output.append(5) |
---|
554 | n/a | output.append(6) |
---|
555 | n/a | output.append(7) |
---|
556 | n/a | |
---|
557 | n/a | jump_out_of_block_backwards.jump = (6, 1) |
---|
558 | n/a | jump_out_of_block_backwards.output = [1, 3, 5, 1, 3, 5, 6, 7] |
---|
559 | n/a | |
---|
560 | n/a | def jump_to_codeless_line(output): |
---|
561 | n/a | output.append(1) |
---|
562 | n/a | # Jumping to this line should skip to the next one. |
---|
563 | n/a | output.append(3) |
---|
564 | n/a | |
---|
565 | n/a | jump_to_codeless_line.jump = (1, 2) |
---|
566 | n/a | jump_to_codeless_line.output = [3] |
---|
567 | n/a | |
---|
568 | n/a | def jump_to_same_line(output): |
---|
569 | n/a | output.append(1) |
---|
570 | n/a | output.append(2) |
---|
571 | n/a | output.append(3) |
---|
572 | n/a | |
---|
573 | n/a | jump_to_same_line.jump = (2, 2) |
---|
574 | n/a | jump_to_same_line.output = [1, 2, 3] |
---|
575 | n/a | |
---|
576 | n/a | # Tests jumping within a finally block, and over one. |
---|
577 | n/a | def jump_in_nested_finally(output): |
---|
578 | n/a | try: |
---|
579 | n/a | output.append(2) |
---|
580 | n/a | finally: |
---|
581 | n/a | output.append(4) |
---|
582 | n/a | try: |
---|
583 | n/a | output.append(6) |
---|
584 | n/a | finally: |
---|
585 | n/a | output.append(8) |
---|
586 | n/a | output.append(9) |
---|
587 | n/a | |
---|
588 | n/a | jump_in_nested_finally.jump = (4, 9) |
---|
589 | n/a | jump_in_nested_finally.output = [2, 9] |
---|
590 | n/a | |
---|
591 | n/a | def jump_infinite_while_loop(output): |
---|
592 | n/a | output.append(1) |
---|
593 | n/a | while 1: |
---|
594 | n/a | output.append(2) |
---|
595 | n/a | output.append(3) |
---|
596 | n/a | |
---|
597 | n/a | jump_infinite_while_loop.jump = (3, 4) |
---|
598 | n/a | jump_infinite_while_loop.output = [1, 3] |
---|
599 | n/a | |
---|
600 | n/a | # The second set of 'jump' tests are for things that are not allowed: |
---|
601 | n/a | |
---|
602 | n/a | def no_jump_too_far_forwards(output): |
---|
603 | n/a | try: |
---|
604 | n/a | output.append(2) |
---|
605 | n/a | output.append(3) |
---|
606 | n/a | except ValueError as e: |
---|
607 | n/a | output.append('after' in str(e)) |
---|
608 | n/a | |
---|
609 | n/a | no_jump_too_far_forwards.jump = (3, 6) |
---|
610 | n/a | no_jump_too_far_forwards.output = [2, True] |
---|
611 | n/a | |
---|
612 | n/a | def no_jump_too_far_backwards(output): |
---|
613 | n/a | try: |
---|
614 | n/a | output.append(2) |
---|
615 | n/a | output.append(3) |
---|
616 | n/a | except ValueError as e: |
---|
617 | n/a | output.append('before' in str(e)) |
---|
618 | n/a | |
---|
619 | n/a | no_jump_too_far_backwards.jump = (3, -1) |
---|
620 | n/a | no_jump_too_far_backwards.output = [2, True] |
---|
621 | n/a | |
---|
622 | n/a | # Test each kind of 'except' line. |
---|
623 | n/a | def no_jump_to_except_1(output): |
---|
624 | n/a | try: |
---|
625 | n/a | output.append(2) |
---|
626 | n/a | except: |
---|
627 | n/a | e = sys.exc_info()[1] |
---|
628 | n/a | output.append('except' in str(e)) |
---|
629 | n/a | |
---|
630 | n/a | no_jump_to_except_1.jump = (2, 3) |
---|
631 | n/a | no_jump_to_except_1.output = [True] |
---|
632 | n/a | |
---|
633 | n/a | def no_jump_to_except_2(output): |
---|
634 | n/a | try: |
---|
635 | n/a | output.append(2) |
---|
636 | n/a | except ValueError: |
---|
637 | n/a | e = sys.exc_info()[1] |
---|
638 | n/a | output.append('except' in str(e)) |
---|
639 | n/a | |
---|
640 | n/a | no_jump_to_except_2.jump = (2, 3) |
---|
641 | n/a | no_jump_to_except_2.output = [True] |
---|
642 | n/a | |
---|
643 | n/a | def no_jump_to_except_3(output): |
---|
644 | n/a | try: |
---|
645 | n/a | output.append(2) |
---|
646 | n/a | except ValueError as e: |
---|
647 | n/a | output.append('except' in str(e)) |
---|
648 | n/a | |
---|
649 | n/a | no_jump_to_except_3.jump = (2, 3) |
---|
650 | n/a | no_jump_to_except_3.output = [True] |
---|
651 | n/a | |
---|
652 | n/a | def no_jump_to_except_4(output): |
---|
653 | n/a | try: |
---|
654 | n/a | output.append(2) |
---|
655 | n/a | except (ValueError, RuntimeError) as e: |
---|
656 | n/a | output.append('except' in str(e)) |
---|
657 | n/a | |
---|
658 | n/a | no_jump_to_except_4.jump = (2, 3) |
---|
659 | n/a | no_jump_to_except_4.output = [True] |
---|
660 | n/a | |
---|
661 | n/a | def no_jump_forwards_into_block(output): |
---|
662 | n/a | try: |
---|
663 | n/a | output.append(2) |
---|
664 | n/a | for i in 1, 2: |
---|
665 | n/a | output.append(4) |
---|
666 | n/a | except ValueError as e: |
---|
667 | n/a | output.append('into' in str(e)) |
---|
668 | n/a | |
---|
669 | n/a | no_jump_forwards_into_block.jump = (2, 4) |
---|
670 | n/a | no_jump_forwards_into_block.output = [True] |
---|
671 | n/a | |
---|
672 | n/a | def no_jump_backwards_into_block(output): |
---|
673 | n/a | try: |
---|
674 | n/a | for i in 1, 2: |
---|
675 | n/a | output.append(3) |
---|
676 | n/a | output.append(4) |
---|
677 | n/a | except ValueError as e: |
---|
678 | n/a | output.append('into' in str(e)) |
---|
679 | n/a | |
---|
680 | n/a | no_jump_backwards_into_block.jump = (4, 3) |
---|
681 | n/a | no_jump_backwards_into_block.output = [3, 3, True] |
---|
682 | n/a | |
---|
683 | n/a | def no_jump_into_finally_block(output): |
---|
684 | n/a | try: |
---|
685 | n/a | try: |
---|
686 | n/a | output.append(3) |
---|
687 | n/a | x = 1 |
---|
688 | n/a | finally: |
---|
689 | n/a | output.append(6) |
---|
690 | n/a | except ValueError as e: |
---|
691 | n/a | output.append('finally' in str(e)) |
---|
692 | n/a | |
---|
693 | n/a | no_jump_into_finally_block.jump = (4, 6) |
---|
694 | n/a | no_jump_into_finally_block.output = [3, 6, True] # The 'finally' still runs |
---|
695 | n/a | |
---|
696 | n/a | def no_jump_out_of_finally_block(output): |
---|
697 | n/a | try: |
---|
698 | n/a | try: |
---|
699 | n/a | output.append(3) |
---|
700 | n/a | finally: |
---|
701 | n/a | output.append(5) |
---|
702 | n/a | output.append(6) |
---|
703 | n/a | except ValueError as e: |
---|
704 | n/a | output.append('finally' in str(e)) |
---|
705 | n/a | |
---|
706 | n/a | no_jump_out_of_finally_block.jump = (5, 1) |
---|
707 | n/a | no_jump_out_of_finally_block.output = [3, True] |
---|
708 | n/a | |
---|
709 | n/a | # This verifies the line-numbers-must-be-integers rule. |
---|
710 | n/a | def no_jump_to_non_integers(output): |
---|
711 | n/a | try: |
---|
712 | n/a | output.append(2) |
---|
713 | n/a | except ValueError as e: |
---|
714 | n/a | output.append('integer' in str(e)) |
---|
715 | n/a | |
---|
716 | n/a | no_jump_to_non_integers.jump = (2, "Spam") |
---|
717 | n/a | no_jump_to_non_integers.output = [True] |
---|
718 | n/a | |
---|
719 | n/a | def jump_across_with(output): |
---|
720 | n/a | with open(support.TESTFN, "wb") as fp: |
---|
721 | n/a | pass |
---|
722 | n/a | with open(support.TESTFN, "wb") as fp: |
---|
723 | n/a | pass |
---|
724 | n/a | jump_across_with.jump = (1, 3) |
---|
725 | n/a | jump_across_with.output = [] |
---|
726 | n/a | |
---|
727 | n/a | # This verifies that you can't set f_lineno via _getframe or similar |
---|
728 | n/a | # trickery. |
---|
729 | n/a | def no_jump_without_trace_function(): |
---|
730 | n/a | try: |
---|
731 | n/a | previous_frame = sys._getframe().f_back |
---|
732 | n/a | previous_frame.f_lineno = previous_frame.f_lineno |
---|
733 | n/a | except ValueError as e: |
---|
734 | n/a | # This is the exception we wanted; make sure the error message |
---|
735 | n/a | # talks about trace functions. |
---|
736 | n/a | if 'trace' not in str(e): |
---|
737 | n/a | raise |
---|
738 | n/a | else: |
---|
739 | n/a | # Something's wrong - the expected exception wasn't raised. |
---|
740 | n/a | raise RuntimeError("Trace-function-less jump failed to fail") |
---|
741 | n/a | |
---|
742 | n/a | |
---|
743 | n/a | class JumpTestCase(unittest.TestCase): |
---|
744 | n/a | def setUp(self): |
---|
745 | n/a | self.addCleanup(sys.settrace, sys.gettrace()) |
---|
746 | n/a | sys.settrace(None) |
---|
747 | n/a | |
---|
748 | n/a | def compare_jump_output(self, expected, received): |
---|
749 | n/a | if received != expected: |
---|
750 | n/a | self.fail( "Outputs don't match:\n" + |
---|
751 | n/a | "Expected: " + repr(expected) + "\n" + |
---|
752 | n/a | "Received: " + repr(received)) |
---|
753 | n/a | |
---|
754 | n/a | def run_test(self, func): |
---|
755 | n/a | tracer = JumpTracer(func) |
---|
756 | n/a | sys.settrace(tracer.trace) |
---|
757 | n/a | output = [] |
---|
758 | n/a | func(output) |
---|
759 | n/a | sys.settrace(None) |
---|
760 | n/a | self.compare_jump_output(func.output, output) |
---|
761 | n/a | |
---|
762 | n/a | def test_01_jump_simple_forwards(self): |
---|
763 | n/a | self.run_test(jump_simple_forwards) |
---|
764 | n/a | def test_02_jump_simple_backwards(self): |
---|
765 | n/a | self.run_test(jump_simple_backwards) |
---|
766 | n/a | def test_03_jump_out_of_block_forwards(self): |
---|
767 | n/a | self.run_test(jump_out_of_block_forwards) |
---|
768 | n/a | def test_04_jump_out_of_block_backwards(self): |
---|
769 | n/a | self.run_test(jump_out_of_block_backwards) |
---|
770 | n/a | def test_05_jump_to_codeless_line(self): |
---|
771 | n/a | self.run_test(jump_to_codeless_line) |
---|
772 | n/a | def test_06_jump_to_same_line(self): |
---|
773 | n/a | self.run_test(jump_to_same_line) |
---|
774 | n/a | def test_07_jump_in_nested_finally(self): |
---|
775 | n/a | self.run_test(jump_in_nested_finally) |
---|
776 | n/a | def test_jump_infinite_while_loop(self): |
---|
777 | n/a | self.run_test(jump_infinite_while_loop) |
---|
778 | n/a | def test_08_no_jump_too_far_forwards(self): |
---|
779 | n/a | self.run_test(no_jump_too_far_forwards) |
---|
780 | n/a | def test_09_no_jump_too_far_backwards(self): |
---|
781 | n/a | self.run_test(no_jump_too_far_backwards) |
---|
782 | n/a | def test_10_no_jump_to_except_1(self): |
---|
783 | n/a | self.run_test(no_jump_to_except_1) |
---|
784 | n/a | def test_11_no_jump_to_except_2(self): |
---|
785 | n/a | self.run_test(no_jump_to_except_2) |
---|
786 | n/a | def test_12_no_jump_to_except_3(self): |
---|
787 | n/a | self.run_test(no_jump_to_except_3) |
---|
788 | n/a | def test_13_no_jump_to_except_4(self): |
---|
789 | n/a | self.run_test(no_jump_to_except_4) |
---|
790 | n/a | def test_14_no_jump_forwards_into_block(self): |
---|
791 | n/a | self.run_test(no_jump_forwards_into_block) |
---|
792 | n/a | def test_15_no_jump_backwards_into_block(self): |
---|
793 | n/a | self.run_test(no_jump_backwards_into_block) |
---|
794 | n/a | def test_16_no_jump_into_finally_block(self): |
---|
795 | n/a | self.run_test(no_jump_into_finally_block) |
---|
796 | n/a | def test_17_no_jump_out_of_finally_block(self): |
---|
797 | n/a | self.run_test(no_jump_out_of_finally_block) |
---|
798 | n/a | def test_18_no_jump_to_non_integers(self): |
---|
799 | n/a | self.run_test(no_jump_to_non_integers) |
---|
800 | n/a | def test_19_no_jump_without_trace_function(self): |
---|
801 | n/a | # Must set sys.settrace(None) in setUp(), else condition is not |
---|
802 | n/a | # triggered. |
---|
803 | n/a | no_jump_without_trace_function() |
---|
804 | n/a | def test_jump_across_with(self): |
---|
805 | n/a | self.addCleanup(support.unlink, support.TESTFN) |
---|
806 | n/a | self.run_test(jump_across_with) |
---|
807 | n/a | |
---|
808 | n/a | def test_20_large_function(self): |
---|
809 | n/a | d = {} |
---|
810 | n/a | exec("""def f(output): # line 0 |
---|
811 | n/a | x = 0 # line 1 |
---|
812 | n/a | y = 1 # line 2 |
---|
813 | n/a | ''' # line 3 |
---|
814 | n/a | %s # lines 4-1004 |
---|
815 | n/a | ''' # line 1005 |
---|
816 | n/a | x += 1 # line 1006 |
---|
817 | n/a | output.append(x) # line 1007 |
---|
818 | n/a | return""" % ('\n' * 1000,), d) |
---|
819 | n/a | f = d['f'] |
---|
820 | n/a | |
---|
821 | n/a | f.jump = (2, 1007) |
---|
822 | n/a | f.output = [0] |
---|
823 | n/a | self.run_test(f) |
---|
824 | n/a | |
---|
825 | n/a | def test_jump_to_firstlineno(self): |
---|
826 | n/a | # This tests that PDB can jump back to the first line in a |
---|
827 | n/a | # file. See issue #1689458. It can only be triggered in a |
---|
828 | n/a | # function call if the function is defined on a single line. |
---|
829 | n/a | code = compile(""" |
---|
830 | n/a | # Comments don't count. |
---|
831 | n/a | output.append(2) # firstlineno is here. |
---|
832 | n/a | output.append(3) |
---|
833 | n/a | output.append(4) |
---|
834 | n/a | """, "<fake module>", "exec") |
---|
835 | n/a | class fake_function: |
---|
836 | n/a | __code__ = code |
---|
837 | n/a | jump = (2, 0) |
---|
838 | n/a | tracer = JumpTracer(fake_function) |
---|
839 | n/a | sys.settrace(tracer.trace) |
---|
840 | n/a | namespace = {"output": []} |
---|
841 | n/a | exec(code, namespace) |
---|
842 | n/a | sys.settrace(None) |
---|
843 | n/a | self.compare_jump_output([2, 3, 2, 3, 4], namespace["output"]) |
---|
844 | n/a | |
---|
845 | n/a | |
---|
846 | n/a | def test_main(): |
---|
847 | n/a | support.run_unittest( |
---|
848 | n/a | TraceTestCase, |
---|
849 | n/a | RaisingTraceFuncTestCase, |
---|
850 | n/a | JumpTestCase |
---|
851 | n/a | ) |
---|
852 | n/a | |
---|
853 | n/a | if __name__ == "__main__": |
---|
854 | n/a | test_main() |
---|