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.
11 def br(): browser(sys
.last_traceback
)
13 def tb(): printtb(sys
.last_traceback
)
31 line
= raw_input('TB: ')
32 except KeyboardInterrupt:
33 print '\n[Interrupted]'
38 cmd
= string
.strip(line
)
45 if ptr
-1 >= 0: ptr
= ptr
-1
46 else: print 'Bottom of stack.'
48 if ptr
+1 < len(tblist
): ptr
= ptr
+1
49 else: print 'Top of stack.'
51 printsymbols(tb
.tb_frame
.f_locals
)
52 elif cmd
== 'globals':
53 printsymbols(tb
.tb_frame
.f_globals
)
54 elif cmd
in ('?', 'help'):
60 filename
= tb
.tb_frame
.f_code
.co_filename
63 first
= max(1, last
-10)
64 for i
in range(first
, last
+1):
65 if i
== lineno
: prefix
= '***' + string
.rjust(`i`
, 4) + ':'
66 else: prefix
= string
.rjust(`i`
, 7) + ':'
67 line
= linecache
.getline(filename
, i
)
68 if line
[-1:] == '\n': line
= line
[:-1]
71 def browserexec(tb
, cmd
):
72 locals = tb
.tb_frame
.f_locals
73 globals = tb
.tb_frame
.f_globals
75 exec cmd
+'\n' in globals, locals
77 t
, v
= sys
.exc_info()[:2]
78 print '*** Exception:',
79 if type(t
) == type(''):
86 print 'Type help to get help.'
90 print ' This is the traceback browser. Commands are:'
91 print ' up : move one level up in the call stack'
92 print ' down : move one level down in the call stack'
93 print ' locals : print all local variables at this level'
94 print ' globals : print all global variables at this level'
95 print ' list : list source code around the failure'
96 print ' help : print help (what you are reading now)'
97 print ' quit : back to command interpreter'
98 print ' Typing any other 1-line statement will execute it'
99 print ' using the current level\'s symbol tables'
109 if tb
.tb_frame
.f_locals
is not tb
.tb_frame
.f_globals
:
110 printsymbols(tb
.tb_frame
.f_locals
)
112 def printtbheader(tb
):
113 filename
= tb
.tb_frame
.f_code
.co_filename
114 lineno
= tb
.tb_lineno
115 info
= '"' + filename
+ '"(' + `lineno`
+ ')'
116 line
= linecache
.getline(filename
, lineno
)
118 info
= info
+ ': ' + string
.strip(line
)
125 print ' ' + string
.ljust(name
, 12) + ':',
126 printobject(d
[name
], 4)
129 def printobject(v
, maxlevel
):
132 elif type(v
) in (type(0), type(0.0)):
134 elif type(v
) == type(''):
136 print `v
[:17] + '...'`
,
139 elif type(v
) == type(()):
141 printlist(v
, maxlevel
)
143 elif type(v
) == type([]):
145 printlist(v
, maxlevel
)
147 elif type(v
) == type({}):
149 printdict(v
, maxlevel
)
154 def printlist(v
, maxlevel
):
160 for i
in range(min(6, n
)):
161 printobject(v
[i
], maxlevel
-1)
162 if i
+1 < n
: print ',',
163 if n
> 6: print '...',
165 def printdict(v
, maxlevel
):
173 for i
in range(min(6, n
)):
176 printobject(v
[key
], maxlevel
-1)
177 if i
+1 < n
: print ',',
178 if n
> 6: print '...',