Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 1 | # Print tracebacks, with a dump of local variables. |
| 2 | # Also an interactive stack trace browser. |
| 3 | |
| 4 | import sys |
Guido van Rossum | 9542c58 | 1992-01-12 23:27:56 +0000 | [diff] [blame] | 5 | import os |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 6 | from stat import * |
| 7 | import string |
Guido van Rossum | 9542c58 | 1992-01-12 23:27:56 +0000 | [diff] [blame] | 8 | import linecache |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 9 | |
| 10 | def br(): browser(sys.last_traceback) |
| 11 | |
| 12 | def tb(): printtb(sys.last_traceback) |
| 13 | |
| 14 | def 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 Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 39 | if cmd == 'quit': |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 40 | break |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 41 | elif cmd == 'list': |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 42 | browserlist(tb) |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 43 | elif cmd == 'up': |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 44 | if ptr-1 >= 0: ptr = ptr-1 |
| 45 | else: print 'Bottom of stack.' |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 46 | elif cmd == 'down': |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 47 | if ptr+1 < len(tblist): ptr = ptr+1 |
| 48 | else: print 'Top of stack.' |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 49 | elif cmd == 'locals': |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 50 | printsymbols(tb.tb_frame.f_locals) |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 51 | elif cmd == 'globals': |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 52 | printsymbols(tb.tb_frame.f_globals) |
| 53 | elif cmd in ('?', 'help'): |
| 54 | browserhelp() |
| 55 | else: |
| 56 | browserexec(tb, cmd) |
| 57 | |
| 58 | def 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 Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 64 | if i == lineno: prefix = '***' + string.rjust(`i`, 4) + ':' |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 65 | else: prefix = string.rjust(`i`, 7) + ':' |
Guido van Rossum | 9542c58 | 1992-01-12 23:27:56 +0000 | [diff] [blame] | 66 | line = linecache.getline(filename, i) |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 67 | if line[-1:] == '\n': line = line[:-1] |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 68 | print prefix + line |
| 69 | |
| 70 | def 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:', |
Guido van Rossum | c7acf2a | 1995-02-27 13:15:45 +0000 | [diff] [blame^] | 77 | if type(sys.exc_type) == type(''): |
| 78 | print sys.exc_type, |
| 79 | else: |
| 80 | print sys.exc_type.__name__, |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 81 | if sys.exc_value <> None: |
| 82 | print ':', sys.exc_value, |
| 83 | print |
| 84 | print 'Type help to get help.' |
| 85 | |
| 86 | def browserhelp(): |
| 87 | print |
| 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' |
| 98 | print |
| 99 | |
| 100 | def printtb(tb): |
| 101 | while tb: |
| 102 | print1tb(tb) |
| 103 | tb = tb.tb_next |
| 104 | |
| 105 | def print1tb(tb): |
| 106 | printtbheader(tb) |
| 107 | if tb.tb_frame.f_locals is not tb.tb_frame.f_globals: |
| 108 | printsymbols(tb.tb_frame.f_locals) |
| 109 | |
| 110 | def printtbheader(tb): |
| 111 | filename = tb.tb_frame.f_code.co_filename |
| 112 | lineno = tb.tb_lineno |
| 113 | info = '"' + filename + '"(' + `lineno` + ')' |
Guido van Rossum | 9542c58 | 1992-01-12 23:27:56 +0000 | [diff] [blame] | 114 | line = linecache.getline(filename, lineno) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 115 | if line: |
| 116 | info = info + ': ' + string.strip(line) |
| 117 | print info |
| 118 | |
| 119 | def printsymbols(d): |
| 120 | keys = d.keys() |
| 121 | keys.sort() |
| 122 | for name in keys: |
| 123 | print ' ' + string.ljust(name, 12) + ':', |
| 124 | printobject(d[name], 4) |
| 125 | print |
| 126 | |
| 127 | def printobject(v, maxlevel): |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 128 | if v == None: |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 129 | print 'None', |
| 130 | elif type(v) in (type(0), type(0.0)): |
| 131 | print v, |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 132 | elif type(v) == type(''): |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 133 | if len(v) > 20: |
| 134 | print `v[:17] + '...'`, |
| 135 | else: |
| 136 | print `v`, |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 137 | elif type(v) == type(()): |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 138 | print '(', |
| 139 | printlist(v, maxlevel) |
| 140 | print ')', |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 141 | elif type(v) == type([]): |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 142 | print '[', |
| 143 | printlist(v, maxlevel) |
| 144 | print ']', |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 145 | elif type(v) == type({}): |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 146 | print '{', |
| 147 | printdict(v, maxlevel) |
| 148 | print '}', |
| 149 | else: |
| 150 | print v, |
| 151 | |
| 152 | def printlist(v, maxlevel): |
| 153 | n = len(v) |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 154 | if n == 0: return |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 155 | if maxlevel <= 0: |
| 156 | print '...', |
| 157 | return |
| 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 '...', |
| 162 | |
| 163 | def printdict(v, maxlevel): |
| 164 | keys = v.keys() |
| 165 | n = len(keys) |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 166 | if n == 0: return |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 167 | if maxlevel <= 0: |
| 168 | print '...', |
| 169 | return |
| 170 | keys.sort() |
| 171 | for i in range(min(6, n)): |
| 172 | key = keys[i] |
| 173 | print `key` + ':', |
| 174 | printobject(v[key], maxlevel-1) |
| 175 | if i+1 < n: print ',', |
| 176 | if n > 6: print '...', |