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:',
77 if type(sys
.exc_type
) == type(''):
80 print sys
.exc_type
.__name
__,
81 if sys
.exc_value
<> None:
82 print ':', sys
.exc_value
,
84 print 'Type help to get help.'
88 print ' This is the traceback browser. Commands are:'
89 print ' up : move one level up in the call stack'
90 print ' down : move one level down in the call stack'
91 print ' locals : print all local variables at this level'
92 print ' globals : print all global variables at this level'
93 print ' list : list source code around the failure'
94 print ' help : print help (what you are reading now)'
95 print ' quit : back to command interpreter'
96 print ' Typing any other 1-line statement will execute it'
97 print ' using the current level\'s symbol tables'
107 if tb
.tb_frame
.f_locals
is not tb
.tb_frame
.f_globals
:
108 printsymbols(tb
.tb_frame
.f_locals
)
110 def printtbheader(tb
):
111 filename
= tb
.tb_frame
.f_code
.co_filename
112 lineno
= tb
.tb_lineno
113 info
= '"' + filename
+ '"(' + `lineno`
+ ')'
114 line
= linecache
.getline(filename
, lineno
)
116 info
= info
+ ': ' + string
.strip(line
)
123 print ' ' + string
.ljust(name
, 12) + ':',
124 printobject(d
[name
], 4)
127 def printobject(v
, maxlevel
):
130 elif type(v
) in (type(0), type(0.0)):
132 elif type(v
) == type(''):
134 print `v
[:17] + '...'`
,
137 elif type(v
) == type(()):
139 printlist(v
, maxlevel
)
141 elif type(v
) == type([]):
143 printlist(v
, maxlevel
)
145 elif type(v
) == type({}):
147 printdict(v
, maxlevel
)
152 def printlist(v
, maxlevel
):
158 for i
in range(min(6, n
)):
159 printobject(v
[i
], maxlevel
-1)
160 if i
+1 < n
: print ',',
161 if n
> 6: print '...',
163 def printdict(v
, maxlevel
):
171 for i
in range(min(6, n
)):
174 printobject(v
[key
], maxlevel
-1)
175 if i
+1 < n
: print ',',
176 if n
> 6: print '...',