blob: 6b9cd9a53fcd06c34da5648125ff6725a8b59dee [file] [log] [blame]
Guido van Rossum217a5fa1990-12-26 15:40:07 +00001# Print tracebacks, with a dump of local variables.
2# Also an interactive stack trace browser.
3
4import sys
Guido van Rossum9542c581992-01-12 23:27:56 +00005import os
Guido van Rossum217a5fa1990-12-26 15:40:07 +00006from stat import *
7import string
Guido van Rossum9542c581992-01-12 23:27:56 +00008import linecache
Guido van Rossum217a5fa1990-12-26 15:40:07 +00009
10def br(): browser(sys.last_traceback)
11
12def tb(): printtb(sys.last_traceback)
13
14def browser(tb):
15 if not tb:
16 print 'No traceback.'
17 return
18 tblist = []
19 while tb:
20 tblist.append(tb)
21 tb = tb.tb_next
22 ptr = len(tblist)-1
23 tb = tblist[ptr]
24 while 1:
25 if tb <> tblist[ptr]:
26 tb = tblist[ptr]
27 print `ptr` + ':',
28 printtbheader(tb)
29 try:
30 line = raw_input('TB: ')
31 except KeyboardInterrupt:
32 print '\n[Interrupted]'
33 break
34 except EOFError:
35 print '\n[EOF]'
36 break
37 cmd = string.strip(line)
38 if cmd:
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000039 if cmd == 'quit':
Guido van Rossum217a5fa1990-12-26 15:40:07 +000040 break
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000041 elif cmd == 'list':
Guido van Rossum217a5fa1990-12-26 15:40:07 +000042 browserlist(tb)
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000043 elif cmd == 'up':
Guido van Rossum217a5fa1990-12-26 15:40:07 +000044 if ptr-1 >= 0: ptr = ptr-1
45 else: print 'Bottom of stack.'
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000046 elif cmd == 'down':
Guido van Rossum217a5fa1990-12-26 15:40:07 +000047 if ptr+1 < len(tblist): ptr = ptr+1
48 else: print 'Top of stack.'
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000049 elif cmd == 'locals':
Guido van Rossum217a5fa1990-12-26 15:40:07 +000050 printsymbols(tb.tb_frame.f_locals)
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000051 elif cmd == 'globals':
Guido van Rossum217a5fa1990-12-26 15:40:07 +000052 printsymbols(tb.tb_frame.f_globals)
53 elif cmd in ('?', 'help'):
54 browserhelp()
55 else:
56 browserexec(tb, cmd)
57
58def browserlist(tb):
59 filename = tb.tb_frame.f_code.co_filename
60 lineno = tb.tb_lineno
61 last = lineno
62 first = max(1, last-10)
63 for i in range(first, last+1):
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000064 if i == lineno: prefix = '***' + string.rjust(`i`, 4) + ':'
Guido van Rossum217a5fa1990-12-26 15:40:07 +000065 else: prefix = string.rjust(`i`, 7) + ':'
Guido van Rossum9542c581992-01-12 23:27:56 +000066 line = linecache.getline(filename, i)
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000067 if line[-1:] == '\n': line = line[:-1]
Guido van Rossum217a5fa1990-12-26 15:40:07 +000068 print prefix + line
69
70def browserexec(tb, cmd):
71 locals = tb.tb_frame.f_locals
72 globals = tb.tb_frame.f_globals
73 try:
74 exec(cmd+'\n', globals, locals)
75 except:
76 print '*** Exception:',
77 print sys.exc_type,
78 if sys.exc_value <> None:
79 print ':', sys.exc_value,
80 print
81 print 'Type help to get help.'
82
83def browserhelp():
84 print
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'
95 print
96
97def printtb(tb):
98 while tb:
99 print1tb(tb)
100 tb = tb.tb_next
101
102def print1tb(tb):
103 printtbheader(tb)
104 if tb.tb_frame.f_locals is not tb.tb_frame.f_globals:
105 printsymbols(tb.tb_frame.f_locals)
106
107def printtbheader(tb):
108 filename = tb.tb_frame.f_code.co_filename
109 lineno = tb.tb_lineno
110 info = '"' + filename + '"(' + `lineno` + ')'
Guido van Rossum9542c581992-01-12 23:27:56 +0000111 line = linecache.getline(filename, lineno)
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000112 if line:
113 info = info + ': ' + string.strip(line)
114 print info
115
116def printsymbols(d):
117 keys = d.keys()
118 keys.sort()
119 for name in keys:
120 print ' ' + string.ljust(name, 12) + ':',
121 printobject(d[name], 4)
122 print
123
124def printobject(v, maxlevel):
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000125 if v == None:
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000126 print 'None',
127 elif type(v) in (type(0), type(0.0)):
128 print v,
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000129 elif type(v) == type(''):
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000130 if len(v) > 20:
131 print `v[:17] + '...'`,
132 else:
133 print `v`,
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000134 elif type(v) == type(()):
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000135 print '(',
136 printlist(v, maxlevel)
137 print ')',
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000138 elif type(v) == type([]):
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000139 print '[',
140 printlist(v, maxlevel)
141 print ']',
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000142 elif type(v) == type({}):
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000143 print '{',
144 printdict(v, maxlevel)
145 print '}',
146 else:
147 print v,
148
149def printlist(v, maxlevel):
150 n = len(v)
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000151 if n == 0: return
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000152 if maxlevel <= 0:
153 print '...',
154 return
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 '...',
159
160def printdict(v, maxlevel):
161 keys = v.keys()
162 n = len(keys)
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +0000163 if n == 0: return
Guido van Rossum217a5fa1990-12-26 15:40:07 +0000164 if maxlevel <= 0:
165 print '...',
166 return
167 keys.sort()
168 for i in range(min(6, n)):
169 key = keys[i]
170 print `key` + ':',
171 printobject(v[key], maxlevel-1)
172 if i+1 < n: print ',',
173 if n > 6: print '...',