Back to index

python3.2  3.2.2
Classes | Functions
test.test_sys_settrace Namespace Reference

Classes

class  Tracer
class  TraceTestCase
class  RaisingTraceFuncTestCase
class  JumpTracer
class  JumpTestCase

Functions

def basic
def arigo_example
def one_instr_line
def no_pop_tops
def no_pop_blocks
def called
def call
def raises
def test_raise
def _settrace_and_return
def settrace_and_return
def _settrace_and_raise
def settrace_and_raise
def ireturn_example
def tightloop_example
def tighterloop_example
def generator_function
def generator_example
def jump_simple_forwards
def jump_simple_backwards
def jump_out_of_block_forwards
def jump_out_of_block_backwards
def jump_to_codeless_line
def jump_to_same_line
def jump_in_nested_finally
def no_jump_too_far_forwards
def no_jump_too_far_backwards
def no_jump_to_except_1
def no_jump_to_except_2
def no_jump_to_except_3
def no_jump_to_except_4
def no_jump_forwards_into_block
def no_jump_backwards_into_block
def no_jump_into_finally_block
def no_jump_out_of_finally_block
def no_jump_to_non_integers
def no_jump_without_trace_function
def test_main

Function Documentation

def test.test_sys_settrace._settrace_and_raise (   tracefunc) [private]

Definition at line 124 of file test_sys_settrace.py.

00124 
00125 def _settrace_and_raise(tracefunc):
00126     sys.settrace(tracefunc)
00127     sys._getframe().f_back.f_trace = tracefunc
    raise RuntimeError

Here is the caller graph for this function:

def test.test_sys_settrace._settrace_and_return (   tracefunc) [private]

Definition at line 116 of file test_sys_settrace.py.

00116 
00117 def _settrace_and_return(tracefunc):
00118     sys.settrace(tracefunc)
    sys._getframe().f_back.f_trace = tracefunc

Here is the caller graph for this function:

Definition at line 26 of file test_sys_settrace.py.

00026 
00027 def arigo_example():
00028     x = 1
00029     del x
00030     while 0:
00031         pass
00032     x = 1
00033 
00034 arigo_example.events = [(0, 'call'),
00035                         (1, 'line'),
00036                         (2, 'line'),
00037                         (5, 'line'),
00038                         (5, 'return')]
00039 
# check that lines consisting of just one instruction get traced:

Definition at line 10 of file test_sys_settrace.py.

00010 
00011 def basic():
00012     return 1
00013 
00014 basic.events = [(0, 'call'),
00015                 (1, 'line'),
00016                 (1, 'return')]
00017 
00018 # Many of the tests below are tricky because they involve pass statements.
00019 # If there is implicit control flow around a pass statement (in an except
00020 # clause or else caluse) under what conditions do you set a line number
00021 # following that clause?
00022 
00023 
00024 # The entire "while 0:" statement is optimized away.  No code
00025 # exists for it, so the line numbers skip directly from "del x"
# to "x = 1".

Definition at line 85 of file test_sys_settrace.py.

00085 
00086 def call():   # line 0
00087     called()
00088 
00089 call.events = [(0, 'call'),
00090                (1, 'line'),
00091                (-3, 'call'),
00092                (-2, 'line'),
00093                (-2, 'return'),
00094                (1, 'return')]

Here is the call graph for this function:

Definition at line 82 of file test_sys_settrace.py.

00082 
00083 def called(): # line -3
00084     x = 1

Here is the caller graph for this function:

Definition at line 214 of file test_sys_settrace.py.

00214 
00215 def generator_example():
00216     # any() will leave the generator before its end
00217     x = any(generator_function())
00218 
00219     # the following lines were not traced
00220     for x in range(10):
00221         y = x
00222 
00223 generator_example.events = ([(0, 'call'),
00224                              (2, 'line'),
00225                              (-6, 'call'),
00226                              (-5, 'line'),
00227                              (-4, 'line'),
00228                              (-4, 'return'),
00229                              (-4, 'call'),
00230                              (-4, 'exception'),
00231                              (-1, 'line'),
00232                              (-1, 'return')] +
00233                             [(5, 'line'), (6, 'line')] * 10 +
00234                             [(5, 'line'), (5, 'return')])
00235 

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 208 of file test_sys_settrace.py.

00208 
00209 def generator_function():
00210     try:
00211         yield True
00212         "continued"
00213     finally:
        "finally"

Here is the caller graph for this function:

Definition at line 147 of file test_sys_settrace.py.

00147 
00148 def ireturn_example():
00149     a = 5
00150     b = 5
00151     if a == b:
00152         b = a+1
00153     else:
00154         pass
00155 
00156 ireturn_example.events = [(0, 'call'),
00157                           (1, 'line'),
00158                           (2, 'line'),
00159                           (3, 'line'),
00160                           (4, 'line'),
00161                           (6, 'line'),
00162                           (6, 'return')]
00163 
# Tight loop with while(1) example (SF #765624)

Definition at line 541 of file test_sys_settrace.py.

00541 
00542 def jump_in_nested_finally(output):
00543     try:
00544         output.append(2)
00545     finally:
00546         output.append(4)
00547         try:
00548             output.append(6)
00549         finally:
00550             output.append(8)
00551         output.append(9)
00552 
00553 jump_in_nested_finally.jump = (4, 9)
00554 jump_in_nested_finally.output = [2, 9]
00555 
00556 # The second set of 'jump' tests are for things that are not allowed:

Definition at line 512 of file test_sys_settrace.py.

00512 
00513 def jump_out_of_block_backwards(output):
00514     output.append(1)
00515     for i in [1]:
00516         output.append(3)
00517         for j in [2]:  # Also tests jumping over a block
00518             output.append(5)
00519         output.append(6)
00520     output.append(7)
00521 
00522 jump_out_of_block_backwards.jump = (6, 1)
00523 jump_out_of_block_backwards.output = [1, 3, 5, 1, 3, 5, 6, 7]

Definition at line 502 of file test_sys_settrace.py.

00502 
00503 def jump_out_of_block_forwards(output):
00504     for i in 1, 2:
00505         output.append(2)
00506         for j in [3]:  # Also tests jumping over a block
00507             output.append(4)
00508     output.append(5)
00509 
00510 jump_out_of_block_forwards.jump = (3, 5)
00511 jump_out_of_block_forwards.output = [2, 5]

Definition at line 495 of file test_sys_settrace.py.

00495 
00496 def jump_simple_backwards(output):
00497     output.append(1)
00498     output.append(2)
00499 
00500 jump_simple_backwards.jump = (2, 1)
00501 jump_simple_backwards.output = [1, 1, 2]

Definition at line 487 of file test_sys_settrace.py.

00487 
00488 def jump_simple_forwards(output):
00489     output.append(1)
00490     output.append(2)
00491     output.append(3)
00492 
00493 jump_simple_forwards.jump = (1, 3)
00494 jump_simple_forwards.output = [3]

Definition at line 524 of file test_sys_settrace.py.

00524 
00525 def jump_to_codeless_line(output):
00526     output.append(1)
00527     # Jumping to this line should skip to the next one.
00528     output.append(3)
00529 
00530 jump_to_codeless_line.jump = (1, 2)
00531 jump_to_codeless_line.output = [3]

Definition at line 532 of file test_sys_settrace.py.

00532 
00533 def jump_to_same_line(output):
00534     output.append(1)
00535     output.append(2)
00536     output.append(3)
00537 
00538 jump_to_same_line.jump = (2, 2)
00539 jump_to_same_line.output = [1, 2, 3]
00540 
# Tests jumping within a finally block, and over one.

Definition at line 627 of file test_sys_settrace.py.

00627 
00628 def no_jump_backwards_into_block(output):
00629     try:
00630         for i in 1, 2:
00631             output.append(3)
00632         output.append(4)
00633     except ValueError as e:
00634         output.append('into' in str(e))
00635 
00636 no_jump_backwards_into_block.jump = (4, 3)
00637 no_jump_backwards_into_block.output = [3, 3, True]

Definition at line 616 of file test_sys_settrace.py.

00616 
00617 def no_jump_forwards_into_block(output):
00618     try:
00619         output.append(2)
00620         for i in 1, 2:
00621             output.append(4)
00622     except ValueError as e:
00623         output.append('into' in str(e))
00624 
00625 no_jump_forwards_into_block.jump = (2, 4)
00626 no_jump_forwards_into_block.output = [True]

Definition at line 638 of file test_sys_settrace.py.

00638 
00639 def no_jump_into_finally_block(output):
00640     try:
00641         try:
00642             output.append(3)
00643             x = 1
00644         finally:
00645             output.append(6)
00646     except ValueError as e:
00647         output.append('finally' in str(e))
00648 
00649 no_jump_into_finally_block.jump = (4, 6)
00650 no_jump_into_finally_block.output = [3, 6, True]  # The 'finally' still runs

Definition at line 651 of file test_sys_settrace.py.

00651 
00652 def no_jump_out_of_finally_block(output):
00653     try:
00654         try:
00655             output.append(3)
00656         finally:
00657             output.append(5)
00658             output.append(6)
00659     except ValueError as e:
00660         output.append('finally' in str(e))
00661 
00662 no_jump_out_of_finally_block.jump = (5, 1)
00663 no_jump_out_of_finally_block.output = [3, True]
00664 
# This verifies the line-numbers-must-be-integers rule.

Definition at line 578 of file test_sys_settrace.py.

00578 
00579 def no_jump_to_except_1(output):
00580     try:
00581         output.append(2)
00582     except:
00583         e = sys.exc_info()[1]
00584         output.append('except' in str(e))
00585 
00586 no_jump_to_except_1.jump = (2, 3)
00587 no_jump_to_except_1.output = [True]

Definition at line 588 of file test_sys_settrace.py.

00588 
00589 def no_jump_to_except_2(output):
00590     try:
00591         output.append(2)
00592     except ValueError:
00593         e = sys.exc_info()[1]
00594         output.append('except' in str(e))
00595 
00596 no_jump_to_except_2.jump = (2, 3)
00597 no_jump_to_except_2.output = [True]

Definition at line 598 of file test_sys_settrace.py.

00598 
00599 def no_jump_to_except_3(output):
00600     try:
00601         output.append(2)
00602     except ValueError as e:
00603         output.append('except' in str(e))
00604 
00605 no_jump_to_except_3.jump = (2, 3)
00606 no_jump_to_except_3.output = [True]

Definition at line 607 of file test_sys_settrace.py.

00607 
00608 def no_jump_to_except_4(output):
00609     try:
00610         output.append(2)
00611     except (ValueError, RuntimeError) as e:
00612         output.append('except' in str(e))
00613 
00614 no_jump_to_except_4.jump = (2, 3)
00615 no_jump_to_except_4.output = [True]

Definition at line 665 of file test_sys_settrace.py.

00665 
00666 def no_jump_to_non_integers(output):
00667     try:
00668         output.append(2)
00669     except ValueError as e:
00670         output.append('integer' in str(e))
00671 
00672 no_jump_to_non_integers.jump = (2, "Spam")
00673 no_jump_to_non_integers.output = [True]
00674 
00675 # This verifies that you can't set f_lineno via _getframe or similar
# trickery.

Definition at line 567 of file test_sys_settrace.py.

00567 
00568 def no_jump_too_far_backwards(output):
00569     try:
00570         output.append(2)
00571         output.append(3)
00572     except ValueError as e:
00573         output.append('before' in str(e))
00574 
00575 no_jump_too_far_backwards.jump = (3, -1)
00576 no_jump_too_far_backwards.output = [2, True]
00577 
# Test each kind of 'except' line.

Definition at line 557 of file test_sys_settrace.py.

00557 
00558 def no_jump_too_far_forwards(output):
00559     try:
00560         output.append(2)
00561         output.append(3)
00562     except ValueError as e:
00563         output.append('after' in str(e))
00564 
00565 no_jump_too_far_forwards.jump = (3, 6)
00566 no_jump_too_far_forwards.output = [2, True]

Definition at line 676 of file test_sys_settrace.py.

00676 
00677 def no_jump_without_trace_function():
00678     try:
00679         previous_frame = sys._getframe().f_back
00680         previous_frame.f_lineno = previous_frame.f_lineno
00681     except ValueError as e:
00682         # This is the exception we wanted; make sure the error message
00683         # talks about trace functions.
00684         if 'trace' not in str(e):
00685             raise
00686     else:
00687         # Something's wrong - the expected exception wasn't raised.
00688         raise RuntimeError("Trace-function-less jump failed to fail")
00689 

Here is the caller graph for this function:

Definition at line 70 of file test_sys_settrace.py.

00070 
00071 def no_pop_blocks():
00072     y = 1
00073     while not y:
00074         bla
00075     x = 1
00076 
00077 no_pop_blocks.events = [(0, 'call'),
00078                         (1, 'line'),
00079                         (2, 'line'),
00080                         (4, 'line'),
00081                         (4, 'return')]

Definition at line 51 of file test_sys_settrace.py.

00051 
00052 def no_pop_tops():      # 0
00053     x = 1               # 1
00054     for a in range(2):  # 2
00055         if a:           # 3
00056             x = 1       # 4
00057         else:           # 5
00058             x = 1       # 6
00059 
00060 no_pop_tops.events = [(0, 'call'),
00061                       (1, 'line'),
00062                       (2, 'line'),
00063                       (3, 'line'),
00064                       (6, 'line'),
00065                       (2, 'line'),
00066                       (3, 'line'),
00067                       (4, 'line'),
00068                       (2, 'line'),
00069                       (2, 'return')]

Definition at line 40 of file test_sys_settrace.py.

00040 
00041 def one_instr_line():
00042     x = 1
00043     del x
00044     x = 1
00045 
00046 one_instr_line.events = [(0, 'call'),
00047                          (1, 'line'),
00048                          (2, 'line'),
00049                          (3, 'line'),
00050                          (3, 'return')]

Definition at line 95 of file test_sys_settrace.py.

00095 
00096 def raises():
00097     raise Exception

Here is the caller graph for this function:

Definition at line 128 of file test_sys_settrace.py.

00128 
00129 def settrace_and_raise(tracefunc):
00130     try:
00131         _settrace_and_raise(tracefunc)
00132     except RuntimeError as exc:
00133         pass
00134 
00135 settrace_and_raise.events = [(2, 'exception'),
00136                              (3, 'line'),
00137                              (4, 'line'),
00138                              (4, 'return')]
00139 
00140 # implicit return example
00141 # This test is interesting because of the else: pass
00142 # part of the code.  The code generate for the true
00143 # part of the if contains a jump past the else branch.
00144 # The compiler then generates an implicit "return None"
00145 # Internally, the compiler visits the pass statement
00146 # and stores its line number for use on the next instruction.
# The next instruction is the implicit return None.

Here is the call graph for this function:

Definition at line 119 of file test_sys_settrace.py.

00119 
00120 def settrace_and_return(tracefunc):
00121     _settrace_and_return(tracefunc)
00122 
00123 settrace_and_return.events = [(1, 'return')]

Here is the call graph for this function:

Definition at line 782 of file test_sys_settrace.py.

00782 
00783 def test_main():
00784     support.run_unittest(
00785         TraceTestCase,
00786         RaisingTraceFuncTestCase,
00787         JumpTestCase
00788     )

Definition at line 98 of file test_sys_settrace.py.

00098 
00099 def test_raise():
00100     try:
00101         raises()
00102     except Exception as exc:
00103         x = 1
00104 
00105 test_raise.events = [(0, 'call'),
00106                      (1, 'line'),
00107                      (2, 'line'),
00108                      (-3, 'call'),
00109                      (-2, 'line'),
00110                      (-2, 'exception'),
00111                      (-2, 'return'),
00112                      (2, 'exception'),
00113                      (3, 'line'),
00114                      (4, 'line'),
00115                      (4, 'return')]

Here is the call graph for this function:

Definition at line 187 of file test_sys_settrace.py.

00187 
00188 def tighterloop_example():
00189     items = range(1, 4)
00190     try:
00191         i = 0
00192         while 1: i = items[i]
00193     except IndexError:
00194         pass
00195 
00196 tighterloop_example.events = [(0, 'call'),
00197                             (1, 'line'),
00198                             (2, 'line'),
00199                             (3, 'line'),
00200                             (4, 'line'),
00201                             (4, 'line'),
00202                             (4, 'line'),
00203                             (4, 'line'),
00204                             (4, 'exception'),
00205                             (5, 'line'),
00206                             (6, 'line'),
00207                             (6, 'return')]

Definition at line 164 of file test_sys_settrace.py.

00164 
00165 def tightloop_example():
00166     items = range(0, 3)
00167     try:
00168         i = 0
00169         while 1:
00170             b = items[i]; i+=1
00171     except IndexError:
00172         pass
00173 
00174 tightloop_example.events = [(0, 'call'),
00175                             (1, 'line'),
00176                             (2, 'line'),
00177                             (3, 'line'),
00178                             (4, 'line'),
00179                             (5, 'line'),
00180                             (5, 'line'),
00181                             (5, 'line'),
00182                             (5, 'line'),
00183                             (5, 'exception'),
00184                             (6, 'line'),
00185                             (7, 'line'),
00186                             (7, 'return')]