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