| 1 | # Testing the line trace facility.
|
|---|
| 2 |
|
|---|
| 3 | from test import test_support
|
|---|
| 4 | import unittest
|
|---|
| 5 | import sys
|
|---|
| 6 | import difflib
|
|---|
| 7 |
|
|---|
| 8 | # A very basic example. If this fails, we're in deep trouble.
|
|---|
| 9 | def basic():
|
|---|
| 10 | return 1
|
|---|
| 11 |
|
|---|
| 12 | basic.events = [(0, 'call'),
|
|---|
| 13 | (1, 'line'),
|
|---|
| 14 | (1, 'return')]
|
|---|
| 15 |
|
|---|
| 16 | # Many of the tests below are tricky because they involve pass statements.
|
|---|
| 17 | # If there is implicit control flow around a pass statement (in an except
|
|---|
| 18 | # clause or else caluse) under what conditions do you set a line number
|
|---|
| 19 | # following that clause?
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 | # The entire "while 0:" statement is optimized away. No code
|
|---|
| 23 | # exists for it, so the line numbers skip directly from "del x"
|
|---|
| 24 | # to "x = 1".
|
|---|
| 25 | def arigo_example():
|
|---|
| 26 | x = 1
|
|---|
| 27 | del x
|
|---|
| 28 | while 0:
|
|---|
| 29 | pass
|
|---|
| 30 | x = 1
|
|---|
| 31 |
|
|---|
| 32 | arigo_example.events = [(0, 'call'),
|
|---|
| 33 | (1, 'line'),
|
|---|
| 34 | (2, 'line'),
|
|---|
| 35 | (5, 'line'),
|
|---|
| 36 | (5, 'return')]
|
|---|
| 37 |
|
|---|
| 38 | # check that lines consisting of just one instruction get traced:
|
|---|
| 39 | def one_instr_line():
|
|---|
| 40 | x = 1
|
|---|
| 41 | del x
|
|---|
| 42 | x = 1
|
|---|
| 43 |
|
|---|
| 44 | one_instr_line.events = [(0, 'call'),
|
|---|
| 45 | (1, 'line'),
|
|---|
| 46 | (2, 'line'),
|
|---|
| 47 | (3, 'line'),
|
|---|
| 48 | (3, 'return')]
|
|---|
| 49 |
|
|---|
| 50 | def no_pop_tops(): # 0
|
|---|
| 51 | x = 1 # 1
|
|---|
| 52 | for a in range(2): # 2
|
|---|
| 53 | if a: # 3
|
|---|
| 54 | x = 1 # 4
|
|---|
| 55 | else: # 5
|
|---|
| 56 | x = 1 # 6
|
|---|
| 57 |
|
|---|
| 58 | no_pop_tops.events = [(0, 'call'),
|
|---|
| 59 | (1, 'line'),
|
|---|
| 60 | (2, 'line'),
|
|---|
| 61 | (3, 'line'),
|
|---|
| 62 | (6, 'line'),
|
|---|
| 63 | (2, 'line'),
|
|---|
| 64 | (3, 'line'),
|
|---|
| 65 | (4, 'line'),
|
|---|
| 66 | (2, 'line'),
|
|---|
| 67 | (2, 'return')]
|
|---|
| 68 |
|
|---|
| 69 | def no_pop_blocks():
|
|---|
| 70 | y = 1
|
|---|
| 71 | while not y:
|
|---|
| 72 | bla
|
|---|
| 73 | x = 1
|
|---|
| 74 |
|
|---|
| 75 | no_pop_blocks.events = [(0, 'call'),
|
|---|
| 76 | (1, 'line'),
|
|---|
| 77 | (2, 'line'),
|
|---|
| 78 | (4, 'line'),
|
|---|
| 79 | (4, 'return')]
|
|---|
| 80 |
|
|---|
| 81 | def called(): # line -3
|
|---|
| 82 | x = 1
|
|---|
| 83 |
|
|---|
| 84 | def call(): # line 0
|
|---|
| 85 | called()
|
|---|
| 86 |
|
|---|
| 87 | call.events = [(0, 'call'),
|
|---|
| 88 | (1, 'line'),
|
|---|
| 89 | (-3, 'call'),
|
|---|
| 90 | (-2, 'line'),
|
|---|
| 91 | (-2, 'return'),
|
|---|
| 92 | (1, 'return')]
|
|---|
| 93 |
|
|---|
| 94 | def raises():
|
|---|
| 95 | raise Exception
|
|---|
| 96 |
|
|---|
| 97 | def test_raise():
|
|---|
| 98 | try:
|
|---|
| 99 | raises()
|
|---|
| 100 | except Exception, exc:
|
|---|
| 101 | x = 1
|
|---|
| 102 |
|
|---|
| 103 | test_raise.events = [(0, 'call'),
|
|---|
| 104 | (1, 'line'),
|
|---|
| 105 | (2, 'line'),
|
|---|
| 106 | (-3, 'call'),
|
|---|
| 107 | (-2, 'line'),
|
|---|
| 108 | (-2, 'exception'),
|
|---|
| 109 | (-2, 'return'),
|
|---|
| 110 | (2, 'exception'),
|
|---|
| 111 | (3, 'line'),
|
|---|
| 112 | (4, 'line'),
|
|---|
| 113 | (4, 'return')]
|
|---|
| 114 |
|
|---|
| 115 | def _settrace_and_return(tracefunc):
|
|---|
| 116 | sys.settrace(tracefunc)
|
|---|
| 117 | sys._getframe().f_back.f_trace = tracefunc
|
|---|
| 118 | def settrace_and_return(tracefunc):
|
|---|
| 119 | _settrace_and_return(tracefunc)
|
|---|
| 120 |
|
|---|
| 121 | settrace_and_return.events = [(1, 'return')]
|
|---|
| 122 |
|
|---|
| 123 | def _settrace_and_raise(tracefunc):
|
|---|
| 124 | sys.settrace(tracefunc)
|
|---|
| 125 | sys._getframe().f_back.f_trace = tracefunc
|
|---|
| 126 | raise RuntimeError
|
|---|
| 127 | def settrace_and_raise(tracefunc):
|
|---|
| 128 | try:
|
|---|
| 129 | _settrace_and_raise(tracefunc)
|
|---|
| 130 | except RuntimeError, exc:
|
|---|
| 131 | pass
|
|---|
| 132 |
|
|---|
| 133 | settrace_and_raise.events = [(2, 'exception'),
|
|---|
| 134 | (3, 'line'),
|
|---|
| 135 | (4, 'line'),
|
|---|
| 136 | (4, 'return')]
|
|---|
| 137 |
|
|---|
| 138 | # implicit return example
|
|---|
| 139 | # This test is interesting because of the else: pass
|
|---|
| 140 | # part of the code. The code generate for the true
|
|---|
| 141 | # part of the if contains a jump past the else branch.
|
|---|
| 142 | # The compiler then generates an implicit "return None"
|
|---|
| 143 | # Internally, the compiler visits the pass statement
|
|---|
| 144 | # and stores its line number for use on the next instruction.
|
|---|
| 145 | # The next instruction is the implicit return None.
|
|---|
| 146 | def ireturn_example():
|
|---|
| 147 | a = 5
|
|---|
| 148 | b = 5
|
|---|
| 149 | if a == b:
|
|---|
| 150 | b = a+1
|
|---|
| 151 | else:
|
|---|
| 152 | pass
|
|---|
| 153 |
|
|---|
| 154 | ireturn_example.events = [(0, 'call'),
|
|---|
| 155 | (1, 'line'),
|
|---|
| 156 | (2, 'line'),
|
|---|
| 157 | (3, 'line'),
|
|---|
| 158 | (4, 'line'),
|
|---|
| 159 | (6, 'line'),
|
|---|
| 160 | (6, 'return')]
|
|---|
| 161 |
|
|---|
| 162 | # Tight loop with while(1) example (SF #765624)
|
|---|
| 163 | def tightloop_example():
|
|---|
| 164 | items = range(0, 3)
|
|---|
| 165 | try:
|
|---|
| 166 | i = 0
|
|---|
| 167 | while 1:
|
|---|
| 168 | b = items[i]; i+=1
|
|---|
| 169 | except IndexError:
|
|---|
| 170 | pass
|
|---|
| 171 |
|
|---|
| 172 | tightloop_example.events = [(0, 'call'),
|
|---|
| 173 | (1, 'line'),
|
|---|
| 174 | (2, 'line'),
|
|---|
| 175 | (3, 'line'),
|
|---|
| 176 | (4, 'line'),
|
|---|
| 177 | (5, 'line'),
|
|---|
| 178 | (5, 'line'),
|
|---|
| 179 | (5, 'line'),
|
|---|
| 180 | (5, 'line'),
|
|---|
| 181 | (5, 'exception'),
|
|---|
| 182 | (6, 'line'),
|
|---|
| 183 | (7, 'line'),
|
|---|
| 184 | (7, 'return')]
|
|---|
| 185 |
|
|---|
| 186 | def tighterloop_example():
|
|---|
| 187 | items = range(1, 4)
|
|---|
| 188 | try:
|
|---|
| 189 | i = 0
|
|---|
| 190 | while 1: i = items[i]
|
|---|
| 191 | except IndexError:
|
|---|
| 192 | pass
|
|---|
| 193 |
|
|---|
| 194 | tighterloop_example.events = [(0, 'call'),
|
|---|
| 195 | (1, 'line'),
|
|---|
| 196 | (2, 'line'),
|
|---|
| 197 | (3, 'line'),
|
|---|
| 198 | (4, 'line'),
|
|---|
| 199 | (4, 'line'),
|
|---|
| 200 | (4, 'line'),
|
|---|
| 201 | (4, 'line'),
|
|---|
| 202 | (4, 'exception'),
|
|---|
| 203 | (5, 'line'),
|
|---|
| 204 | (6, 'line'),
|
|---|
| 205 | (6, 'return')]
|
|---|
| 206 |
|
|---|
| 207 | class Tracer:
|
|---|
| 208 | def __init__(self):
|
|---|
| 209 | self.events = []
|
|---|
| 210 | def trace(self, frame, event, arg):
|
|---|
| 211 | self.events.append((frame.f_lineno, event))
|
|---|
| 212 | return self.trace
|
|---|
| 213 |
|
|---|
| 214 | class TraceTestCase(unittest.TestCase):
|
|---|
| 215 | def compare_events(self, line_offset, events, expected_events):
|
|---|
| 216 | events = [(l - line_offset, e) for (l, e) in events]
|
|---|
| 217 | if events != expected_events:
|
|---|
| 218 | self.fail(
|
|---|
| 219 | "events did not match expectation:\n" +
|
|---|
| 220 | "\n".join(difflib.ndiff(map(str, expected_events),
|
|---|
| 221 | map(str, events))))
|
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 | def run_test(self, func):
|
|---|
| 225 | tracer = Tracer()
|
|---|
| 226 | sys.settrace(tracer.trace)
|
|---|
| 227 | func()
|
|---|
| 228 | sys.settrace(None)
|
|---|
| 229 | self.compare_events(func.func_code.co_firstlineno,
|
|---|
| 230 | tracer.events, func.events)
|
|---|
| 231 |
|
|---|
| 232 | def run_test2(self, func):
|
|---|
| 233 | tracer = Tracer()
|
|---|
| 234 | func(tracer.trace)
|
|---|
| 235 | sys.settrace(None)
|
|---|
| 236 | self.compare_events(func.func_code.co_firstlineno,
|
|---|
| 237 | tracer.events, func.events)
|
|---|
| 238 |
|
|---|
| 239 | def test_01_basic(self):
|
|---|
| 240 | self.run_test(basic)
|
|---|
| 241 | def test_02_arigo(self):
|
|---|
| 242 | self.run_test(arigo_example)
|
|---|
| 243 | def test_03_one_instr(self):
|
|---|
| 244 | self.run_test(one_instr_line)
|
|---|
| 245 | def test_04_no_pop_blocks(self):
|
|---|
| 246 | self.run_test(no_pop_blocks)
|
|---|
| 247 | def test_05_no_pop_tops(self):
|
|---|
| 248 | self.run_test(no_pop_tops)
|
|---|
| 249 | def test_06_call(self):
|
|---|
| 250 | self.run_test(call)
|
|---|
| 251 | def test_07_raise(self):
|
|---|
| 252 | self.run_test(test_raise)
|
|---|
| 253 |
|
|---|
| 254 | def test_08_settrace_and_return(self):
|
|---|
| 255 | self.run_test2(settrace_and_return)
|
|---|
| 256 | def test_09_settrace_and_raise(self):
|
|---|
| 257 | self.run_test2(settrace_and_raise)
|
|---|
| 258 | def test_10_ireturn(self):
|
|---|
| 259 | self.run_test(ireturn_example)
|
|---|
| 260 | def test_11_tightloop(self):
|
|---|
| 261 | self.run_test(tightloop_example)
|
|---|
| 262 | def test_12_tighterloop(self):
|
|---|
| 263 | self.run_test(tighterloop_example)
|
|---|
| 264 |
|
|---|
| 265 | class RaisingTraceFuncTestCase(unittest.TestCase):
|
|---|
| 266 | def trace(self, frame, event, arg):
|
|---|
| 267 | """A trace function that raises an exception in response to a
|
|---|
| 268 | specific trace event."""
|
|---|
| 269 | if event == self.raiseOnEvent:
|
|---|
| 270 | raise ValueError # just something that isn't RuntimeError
|
|---|
| 271 | else:
|
|---|
| 272 | return self.trace
|
|---|
| 273 |
|
|---|
| 274 | def f(self):
|
|---|
| 275 | """The function to trace; raises an exception if that's the case
|
|---|
| 276 | we're testing, so that the 'exception' trace event fires."""
|
|---|
| 277 | if self.raiseOnEvent == 'exception':
|
|---|
| 278 | x = 0
|
|---|
| 279 | y = 1/x
|
|---|
| 280 | else:
|
|---|
| 281 | return 1
|
|---|
| 282 |
|
|---|
| 283 | def run_test_for_event(self, event):
|
|---|
| 284 | """Tests that an exception raised in response to the given event is
|
|---|
| 285 | handled OK."""
|
|---|
| 286 | self.raiseOnEvent = event
|
|---|
| 287 | try:
|
|---|
| 288 | for i in xrange(sys.getrecursionlimit() + 1):
|
|---|
| 289 | sys.settrace(self.trace)
|
|---|
| 290 | try:
|
|---|
| 291 | self.f()
|
|---|
| 292 | except ValueError:
|
|---|
| 293 | pass
|
|---|
| 294 | else:
|
|---|
| 295 | self.fail("exception not thrown!")
|
|---|
| 296 | except RuntimeError:
|
|---|
| 297 | self.fail("recursion counter not reset")
|
|---|
| 298 |
|
|---|
| 299 | # Test the handling of exceptions raised by each kind of trace event.
|
|---|
| 300 | def test_call(self):
|
|---|
| 301 | self.run_test_for_event('call')
|
|---|
| 302 | def test_line(self):
|
|---|
| 303 | self.run_test_for_event('line')
|
|---|
| 304 | def test_return(self):
|
|---|
| 305 | self.run_test_for_event('return')
|
|---|
| 306 | def test_exception(self):
|
|---|
| 307 | self.run_test_for_event('exception')
|
|---|
| 308 |
|
|---|
| 309 | def test_trash_stack(self):
|
|---|
| 310 | def f():
|
|---|
| 311 | for i in range(5):
|
|---|
| 312 | print i # line tracing will raise an exception at this line
|
|---|
| 313 |
|
|---|
| 314 | def g(frame, why, extra):
|
|---|
| 315 | if (why == 'line' and
|
|---|
| 316 | frame.f_lineno == f.func_code.co_firstlineno + 2):
|
|---|
| 317 | raise RuntimeError, "i am crashing"
|
|---|
| 318 | return g
|
|---|
| 319 |
|
|---|
| 320 | sys.settrace(g)
|
|---|
| 321 | try:
|
|---|
| 322 | f()
|
|---|
| 323 | except RuntimeError:
|
|---|
| 324 | # the test is really that this doesn't segfault:
|
|---|
| 325 | import gc
|
|---|
| 326 | gc.collect()
|
|---|
| 327 | else:
|
|---|
| 328 | self.fail("exception not propagated")
|
|---|
| 329 |
|
|---|
| 330 |
|
|---|
| 331 | # 'Jump' tests: assigning to frame.f_lineno within a trace function
|
|---|
| 332 | # moves the execution position - it's how debuggers implement a Jump
|
|---|
| 333 | # command (aka. "Set next statement").
|
|---|
| 334 |
|
|---|
| 335 | class JumpTracer:
|
|---|
| 336 | """Defines a trace function that jumps from one place to another,
|
|---|
| 337 | with the source and destination lines of the jump being defined by
|
|---|
| 338 | the 'jump' property of the function under test."""
|
|---|
| 339 |
|
|---|
| 340 | def __init__(self, function):
|
|---|
| 341 | self.function = function
|
|---|
| 342 | self.jumpFrom = function.jump[0]
|
|---|
| 343 | self.jumpTo = function.jump[1]
|
|---|
| 344 | self.done = False
|
|---|
| 345 |
|
|---|
| 346 | def trace(self, frame, event, arg):
|
|---|
| 347 | if not self.done and frame.f_code == self.function.func_code:
|
|---|
| 348 | firstLine = frame.f_code.co_firstlineno
|
|---|
| 349 | if frame.f_lineno == firstLine + self.jumpFrom:
|
|---|
| 350 | # Cope with non-integer self.jumpTo (because of
|
|---|
| 351 | # no_jump_to_non_integers below).
|
|---|
| 352 | try:
|
|---|
| 353 | frame.f_lineno = firstLine + self.jumpTo
|
|---|
| 354 | except TypeError:
|
|---|
| 355 | frame.f_lineno = self.jumpTo
|
|---|
| 356 | self.done = True
|
|---|
| 357 | return self.trace
|
|---|
| 358 |
|
|---|
| 359 | # The first set of 'jump' tests are for things that are allowed:
|
|---|
| 360 |
|
|---|
| 361 | def jump_simple_forwards(output):
|
|---|
| 362 | output.append(1)
|
|---|
| 363 | output.append(2)
|
|---|
| 364 | output.append(3)
|
|---|
| 365 |
|
|---|
| 366 | jump_simple_forwards.jump = (1, 3)
|
|---|
| 367 | jump_simple_forwards.output = [3]
|
|---|
| 368 |
|
|---|
| 369 | def jump_simple_backwards(output):
|
|---|
| 370 | output.append(1)
|
|---|
| 371 | output.append(2)
|
|---|
| 372 |
|
|---|
| 373 | jump_simple_backwards.jump = (2, 1)
|
|---|
| 374 | jump_simple_backwards.output = [1, 1, 2]
|
|---|
| 375 |
|
|---|
| 376 | def jump_out_of_block_forwards(output):
|
|---|
| 377 | for i in 1, 2:
|
|---|
| 378 | output.append(2)
|
|---|
| 379 | for j in [3]: # Also tests jumping over a block
|
|---|
| 380 | output.append(4)
|
|---|
| 381 | output.append(5)
|
|---|
| 382 |
|
|---|
| 383 | jump_out_of_block_forwards.jump = (3, 5)
|
|---|
| 384 | jump_out_of_block_forwards.output = [2, 5]
|
|---|
| 385 |
|
|---|
| 386 | def jump_out_of_block_backwards(output):
|
|---|
| 387 | output.append(1)
|
|---|
| 388 | for i in [1]:
|
|---|
| 389 | output.append(3)
|
|---|
| 390 | for j in [2]: # Also tests jumping over a block
|
|---|
| 391 | output.append(5)
|
|---|
| 392 | output.append(6)
|
|---|
| 393 | output.append(7)
|
|---|
| 394 |
|
|---|
| 395 | jump_out_of_block_backwards.jump = (6, 1)
|
|---|
| 396 | jump_out_of_block_backwards.output = [1, 3, 5, 1, 3, 5, 6, 7]
|
|---|
| 397 |
|
|---|
| 398 | def jump_to_codeless_line(output):
|
|---|
| 399 | output.append(1)
|
|---|
| 400 | # Jumping to this line should skip to the next one.
|
|---|
| 401 | output.append(3)
|
|---|
| 402 |
|
|---|
| 403 | jump_to_codeless_line.jump = (1, 2)
|
|---|
| 404 | jump_to_codeless_line.output = [3]
|
|---|
| 405 |
|
|---|
| 406 | def jump_to_same_line(output):
|
|---|
| 407 | output.append(1)
|
|---|
| 408 | output.append(2)
|
|---|
| 409 | output.append(3)
|
|---|
| 410 |
|
|---|
| 411 | jump_to_same_line.jump = (2, 2)
|
|---|
| 412 | jump_to_same_line.output = [1, 2, 3]
|
|---|
| 413 |
|
|---|
| 414 | # Tests jumping within a finally block, and over one.
|
|---|
| 415 | def jump_in_nested_finally(output):
|
|---|
| 416 | try:
|
|---|
| 417 | output.append(2)
|
|---|
| 418 | finally:
|
|---|
| 419 | output.append(4)
|
|---|
| 420 | try:
|
|---|
| 421 | output.append(6)
|
|---|
| 422 | finally:
|
|---|
| 423 | output.append(8)
|
|---|
| 424 | output.append(9)
|
|---|
| 425 |
|
|---|
| 426 | jump_in_nested_finally.jump = (4, 9)
|
|---|
| 427 | jump_in_nested_finally.output = [2, 9]
|
|---|
| 428 |
|
|---|
| 429 | # The second set of 'jump' tests are for things that are not allowed:
|
|---|
| 430 |
|
|---|
| 431 | def no_jump_too_far_forwards(output):
|
|---|
| 432 | try:
|
|---|
| 433 | output.append(2)
|
|---|
| 434 | output.append(3)
|
|---|
| 435 | except ValueError, e:
|
|---|
| 436 | output.append('after' in str(e))
|
|---|
| 437 |
|
|---|
| 438 | no_jump_too_far_forwards.jump = (3, 6)
|
|---|
| 439 | no_jump_too_far_forwards.output = [2, True]
|
|---|
| 440 |
|
|---|
| 441 | def no_jump_too_far_backwards(output):
|
|---|
| 442 | try:
|
|---|
| 443 | output.append(2)
|
|---|
| 444 | output.append(3)
|
|---|
| 445 | except ValueError, e:
|
|---|
| 446 | output.append('before' in str(e))
|
|---|
| 447 |
|
|---|
| 448 | no_jump_too_far_backwards.jump = (3, -1)
|
|---|
| 449 | no_jump_too_far_backwards.output = [2, True]
|
|---|
| 450 |
|
|---|
| 451 | # Test each kind of 'except' line.
|
|---|
| 452 | def no_jump_to_except_1(output):
|
|---|
| 453 | try:
|
|---|
| 454 | output.append(2)
|
|---|
| 455 | except:
|
|---|
| 456 | e = sys.exc_info()[1]
|
|---|
| 457 | output.append('except' in str(e))
|
|---|
| 458 |
|
|---|
| 459 | no_jump_to_except_1.jump = (2, 3)
|
|---|
| 460 | no_jump_to_except_1.output = [True]
|
|---|
| 461 |
|
|---|
| 462 | def no_jump_to_except_2(output):
|
|---|
| 463 | try:
|
|---|
| 464 | output.append(2)
|
|---|
| 465 | except ValueError:
|
|---|
| 466 | e = sys.exc_info()[1]
|
|---|
| 467 | output.append('except' in str(e))
|
|---|
| 468 |
|
|---|
| 469 | no_jump_to_except_2.jump = (2, 3)
|
|---|
| 470 | no_jump_to_except_2.output = [True]
|
|---|
| 471 |
|
|---|
| 472 | def no_jump_to_except_3(output):
|
|---|
| 473 | try:
|
|---|
| 474 | output.append(2)
|
|---|
| 475 | except ValueError, e:
|
|---|
| 476 | output.append('except' in str(e))
|
|---|
| 477 |
|
|---|
| 478 | no_jump_to_except_3.jump = (2, 3)
|
|---|
| 479 | no_jump_to_except_3.output = [True]
|
|---|
| 480 |
|
|---|
| 481 | def no_jump_to_except_4(output):
|
|---|
| 482 | try:
|
|---|
| 483 | output.append(2)
|
|---|
| 484 | except (ValueError, RuntimeError), e:
|
|---|
| 485 | output.append('except' in str(e))
|
|---|
| 486 |
|
|---|
| 487 | no_jump_to_except_4.jump = (2, 3)
|
|---|
| 488 | no_jump_to_except_4.output = [True]
|
|---|
| 489 |
|
|---|
| 490 | def no_jump_forwards_into_block(output):
|
|---|
| 491 | try:
|
|---|
| 492 | output.append(2)
|
|---|
| 493 | for i in 1, 2:
|
|---|
| 494 | output.append(4)
|
|---|
| 495 | except ValueError, e:
|
|---|
| 496 | output.append('into' in str(e))
|
|---|
| 497 |
|
|---|
| 498 | no_jump_forwards_into_block.jump = (2, 4)
|
|---|
| 499 | no_jump_forwards_into_block.output = [True]
|
|---|
| 500 |
|
|---|
| 501 | def no_jump_backwards_into_block(output):
|
|---|
| 502 | try:
|
|---|
| 503 | for i in 1, 2:
|
|---|
| 504 | output.append(3)
|
|---|
| 505 | output.append(4)
|
|---|
| 506 | except ValueError, e:
|
|---|
| 507 | output.append('into' in str(e))
|
|---|
| 508 |
|
|---|
| 509 | no_jump_backwards_into_block.jump = (4, 3)
|
|---|
| 510 | no_jump_backwards_into_block.output = [3, 3, True]
|
|---|
| 511 |
|
|---|
| 512 | def no_jump_into_finally_block(output):
|
|---|
| 513 | try:
|
|---|
| 514 | try:
|
|---|
| 515 | output.append(3)
|
|---|
| 516 | x = 1
|
|---|
| 517 | finally:
|
|---|
| 518 | output.append(6)
|
|---|
| 519 | except ValueError, e:
|
|---|
| 520 | output.append('finally' in str(e))
|
|---|
| 521 |
|
|---|
| 522 | no_jump_into_finally_block.jump = (4, 6)
|
|---|
| 523 | no_jump_into_finally_block.output = [3, 6, True] # The 'finally' still runs
|
|---|
| 524 |
|
|---|
| 525 | def no_jump_out_of_finally_block(output):
|
|---|
| 526 | try:
|
|---|
| 527 | try:
|
|---|
| 528 | output.append(3)
|
|---|
| 529 | finally:
|
|---|
| 530 | output.append(5)
|
|---|
| 531 | output.append(6)
|
|---|
| 532 | except ValueError, e:
|
|---|
| 533 | output.append('finally' in str(e))
|
|---|
| 534 |
|
|---|
| 535 | no_jump_out_of_finally_block.jump = (5, 1)
|
|---|
| 536 | no_jump_out_of_finally_block.output = [3, True]
|
|---|
| 537 |
|
|---|
| 538 | # This verifies the line-numbers-must-be-integers rule.
|
|---|
| 539 | def no_jump_to_non_integers(output):
|
|---|
| 540 | try:
|
|---|
| 541 | output.append(2)
|
|---|
| 542 | except ValueError, e:
|
|---|
| 543 | output.append('integer' in str(e))
|
|---|
| 544 |
|
|---|
| 545 | no_jump_to_non_integers.jump = (2, "Spam")
|
|---|
| 546 | no_jump_to_non_integers.output = [True]
|
|---|
| 547 |
|
|---|
| 548 | # This verifies that you can't set f_lineno via _getframe or similar
|
|---|
| 549 | # trickery.
|
|---|
| 550 | def no_jump_without_trace_function():
|
|---|
| 551 | try:
|
|---|
| 552 | previous_frame = sys._getframe().f_back
|
|---|
| 553 | previous_frame.f_lineno = previous_frame.f_lineno
|
|---|
| 554 | except ValueError, e:
|
|---|
| 555 | # This is the exception we wanted; make sure the error message
|
|---|
| 556 | # talks about trace functions.
|
|---|
| 557 | if 'trace' not in str(e):
|
|---|
| 558 | raise
|
|---|
| 559 | else:
|
|---|
| 560 | # Something's wrong - the expected exception wasn't raised.
|
|---|
| 561 | raise RuntimeError, "Trace-function-less jump failed to fail"
|
|---|
| 562 |
|
|---|
| 563 |
|
|---|
| 564 | class JumpTestCase(unittest.TestCase):
|
|---|
| 565 | def compare_jump_output(self, expected, received):
|
|---|
| 566 | if received != expected:
|
|---|
| 567 | self.fail( "Outputs don't match:\n" +
|
|---|
| 568 | "Expected: " + repr(expected) + "\n" +
|
|---|
| 569 | "Received: " + repr(received))
|
|---|
| 570 |
|
|---|
| 571 | def run_test(self, func):
|
|---|
| 572 | tracer = JumpTracer(func)
|
|---|
| 573 | sys.settrace(tracer.trace)
|
|---|
| 574 | output = []
|
|---|
| 575 | func(output)
|
|---|
| 576 | sys.settrace(None)
|
|---|
| 577 | self.compare_jump_output(func.output, output)
|
|---|
| 578 |
|
|---|
| 579 | def test_01_jump_simple_forwards(self):
|
|---|
| 580 | self.run_test(jump_simple_forwards)
|
|---|
| 581 | def test_02_jump_simple_backwards(self):
|
|---|
| 582 | self.run_test(jump_simple_backwards)
|
|---|
| 583 | def test_03_jump_out_of_block_forwards(self):
|
|---|
| 584 | self.run_test(jump_out_of_block_forwards)
|
|---|
| 585 | def test_04_jump_out_of_block_backwards(self):
|
|---|
| 586 | self.run_test(jump_out_of_block_backwards)
|
|---|
| 587 | def test_05_jump_to_codeless_line(self):
|
|---|
| 588 | self.run_test(jump_to_codeless_line)
|
|---|
| 589 | def test_06_jump_to_same_line(self):
|
|---|
| 590 | self.run_test(jump_to_same_line)
|
|---|
| 591 | def test_07_jump_in_nested_finally(self):
|
|---|
| 592 | self.run_test(jump_in_nested_finally)
|
|---|
| 593 | def test_08_no_jump_too_far_forwards(self):
|
|---|
| 594 | self.run_test(no_jump_too_far_forwards)
|
|---|
| 595 | def test_09_no_jump_too_far_backwards(self):
|
|---|
| 596 | self.run_test(no_jump_too_far_backwards)
|
|---|
| 597 | def test_10_no_jump_to_except_1(self):
|
|---|
| 598 | self.run_test(no_jump_to_except_1)
|
|---|
| 599 | def test_11_no_jump_to_except_2(self):
|
|---|
| 600 | self.run_test(no_jump_to_except_2)
|
|---|
| 601 | def test_12_no_jump_to_except_3(self):
|
|---|
| 602 | self.run_test(no_jump_to_except_3)
|
|---|
| 603 | def test_13_no_jump_to_except_4(self):
|
|---|
| 604 | self.run_test(no_jump_to_except_4)
|
|---|
| 605 | def test_14_no_jump_forwards_into_block(self):
|
|---|
| 606 | self.run_test(no_jump_forwards_into_block)
|
|---|
| 607 | def test_15_no_jump_backwards_into_block(self):
|
|---|
| 608 | self.run_test(no_jump_backwards_into_block)
|
|---|
| 609 | def test_16_no_jump_into_finally_block(self):
|
|---|
| 610 | self.run_test(no_jump_into_finally_block)
|
|---|
| 611 | def test_17_no_jump_out_of_finally_block(self):
|
|---|
| 612 | self.run_test(no_jump_out_of_finally_block)
|
|---|
| 613 | def test_18_no_jump_to_non_integers(self):
|
|---|
| 614 | self.run_test(no_jump_to_non_integers)
|
|---|
| 615 | def test_19_no_jump_without_trace_function(self):
|
|---|
| 616 | no_jump_without_trace_function()
|
|---|
| 617 |
|
|---|
| 618 | def test_main():
|
|---|
| 619 | test_support.run_unittest(
|
|---|
| 620 | TraceTestCase,
|
|---|
| 621 | RaisingTraceFuncTestCase,
|
|---|
| 622 | JumpTestCase
|
|---|
| 623 | )
|
|---|
| 624 |
|
|---|
| 625 | if __name__ == "__main__":
|
|---|
| 626 | test_main()
|
|---|