1 # Print tracebacks, with a dump of local variables.
2 # Also an interactive stack trace browser.
10 def br(): browser(sys
.last_traceback
)
12 def tb(): printtb(sys
.last_traceback
)
30 line
= raw_input('TB: ')
31 except KeyboardInterrupt:
32 print '\n[Interrupted]'
37 cmd
= string
.strip(line
)
44 if ptr
-1 >= 0: ptr
= ptr
-1
45 else: print 'Bottom of stack.'
47 if ptr
+1 < len(tblist
): ptr
= ptr
+1
48 else: print 'Top of stack.'
50 printsymbols(tb
.tb_frame
.f_locals
)
51 elif cmd
== 'globals':
52 printsymbols(tb
.tb_frame
.f_globals
)
53 elif cmd
in ('?', 'help'):
59 filename
= tb
.tb_frame
.f_code
.co_filename
62 first
= max(1, last
-10)
63 for i
in range(first
, last
+1):
64 if i
== lineno
: prefix
= '***' + string
.rjust(`i`
, 4) + ':'
65 else: prefix
= string
.rjust(`i`
, 7) + ':'
66 line
= linecache
.getline(filename
, i
)
67 if line
[-1:] == '\n': line
= line
[:-1]
70 def browserexec(tb
, cmd
):
71 locals = tb
.tb_frame
.f_locals
72 globals = tb
.tb_frame
.f_globals
74 exec(cmd
+'\n', globals, locals)
76 print '*** Exception:',
78 if sys
.exc_value
<> None:
79 print ':', sys
.exc_value
,
81 print 'Type help to get help.'
85 print ' This is the traceback browser. Commands are:'
86 print ' up : move one level up in the call stack'
87 print ' down : move one level down in the call stack'
88 print ' locals : print all local variables at this level'
89 print ' globals : print all global variables at this level'
90 print ' list : list source code around the failure'
91 print ' help : print help (what you are reading now)'
92 print ' quit : back to command interpreter'
93 print ' Typing any other 1-line statement will execute it'
94 print ' using the current level\'s symbol tables'
104 if tb
.tb_frame
.f_locals
is not tb
.tb_frame
.f_globals
:
105 printsymbols(tb
.tb_frame
.f_locals
)
107 def printtbheader(tb
):
108 filename
= tb
.tb_frame
.f_code
.co_filename
109 lineno
= tb
.tb_lineno
110 info
= '"' + filename
+ '"(' + `lineno`
+ ')'
111 line
= linecache
.getline(filename
, lineno
)
113 info
= info
+ ': ' + string
.strip(line
)
120 print ' ' + string
.ljust(name
, 12) + ':',
121 printobject(d
[name
], 4)
124 def printobject(v
, maxlevel
):
127 elif type(v
) in (type(0), type(0.0)):
129 elif type(v
) == type(''):
131 print `v
[:17] + '...'`
,
134 elif type(v
) == type(()):
136 printlist(v
, maxlevel
)
138 elif type(v
) == type([]):
140 printlist(v
, maxlevel
)
142 elif type(v
) == type({}):
144 printdict(v
, maxlevel
)
149 def printlist(v
, maxlevel
):
155 for i
in range(min(6, n
)):
156 printobject(v
[i
], maxlevel
-1)
157 if i
+1 < n
: print ',',
158 if n
> 6: print '...',
160 def printdict(v
, maxlevel
):
168 for i
in range(min(6, n
)):
171 printobject(v
[key
], maxlevel
-1)
172 if i
+1 < n
: print ',',
173 if n
> 6: print '...',