1 # Print tracebacks, with a dump of local variables.
2 # Also an interactive stack trace browser.
3 # Note -- this module is obsolete -- use pdb.pm() instead.
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]'
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
= '***' + `i`
.rjust(4) + ':'
65 else: prefix
= `i`
.rjust(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' in globals, locals
76 t
, v
= sys
.exc_info()[:2]
77 print '*** Exception:',
78 if type(t
) is type(''):
85 print 'Type help to get help.'
89 print ' This is the traceback browser. Commands are:'
90 print ' up : move one level up in the call stack'
91 print ' down : move one level down in the call stack'
92 print ' locals : print all local variables at this level'
93 print ' globals : print all global variables at this level'
94 print ' list : list source code around the failure'
95 print ' help : print help (what you are reading now)'
96 print ' quit : back to command interpreter'
97 print ' Typing any other 1-line statement will execute it'
98 print ' using the current level\'s symbol tables'
108 if tb
.tb_frame
.f_locals
is not tb
.tb_frame
.f_globals
:
109 printsymbols(tb
.tb_frame
.f_locals
)
111 def printtbheader(tb
):
112 filename
= tb
.tb_frame
.f_code
.co_filename
113 lineno
= tb
.tb_lineno
114 info
= '"' + filename
+ '"(' + `lineno`
+ ')'
115 line
= linecache
.getline(filename
, lineno
)
117 info
= info
+ ': ' + line
.strip()
124 print ' ' + name
.ljust(12) + ':',
125 printobject(d
[name
], 4)
128 def printobject(v
, maxlevel
):
131 elif type(v
) in (type(0), type(0.0)):
133 elif type(v
) is type(''):
135 print `v
[:17] + '...'`
,
138 elif type(v
) is type(()):
140 printlist(v
, maxlevel
)
142 elif type(v
) is type([]):
144 printlist(v
, maxlevel
)
146 elif type(v
) is type({}):
148 printdict(v
, maxlevel
)
153 def printlist(v
, maxlevel
):
159 for i
in range(min(6, n
)):
160 printobject(v
[i
], maxlevel
-1)
161 if i
+1 < n
: print ',',
162 if n
> 6: print '...',
164 def printdict(v
, maxlevel
):
172 for i
in range(min(6, n
)):
175 printobject(v
[key
], maxlevel
-1)
176 if i
+1 < n
: print ',',
177 if n
> 6: print '...',