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:', |
| 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 | |
| 83 | def 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 | |
| 97 | def printtb(tb): |
| 98 | while tb: |
| 99 | print1tb(tb) |
| 100 | tb = tb.tb_next |
| 101 | |
| 102 | def 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 | |
| 107 | def printtbheader(tb): |
| 108 | filename = tb.tb_frame.f_code.co_filename |
| 109 | lineno = tb.tb_lineno |
| 110 | info = '"' + filename + '"(' + `lineno` + ')' |
Guido van Rossum | 9542c58 | 1992-01-12 23:27:56 +0000 | [diff] [blame] | 111 | line = linecache.getline(filename, lineno) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 112 | if line: |
| 113 | info = info + ': ' + string.strip(line) |
| 114 | print info |
| 115 | |
| 116 | def 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 | |
| 124 | def printobject(v, maxlevel): |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 125 | if v == None: |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 126 | print 'None', |
| 127 | elif type(v) in (type(0), type(0.0)): |
| 128 | print v, |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 129 | elif type(v) == type(''): |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 130 | if len(v) > 20: |
| 131 | print `v[:17] + '...'`, |
| 132 | else: |
| 133 | print `v`, |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 134 | elif type(v) == type(()): |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 135 | print '(', |
| 136 | printlist(v, maxlevel) |
| 137 | print ')', |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 138 | elif type(v) == type([]): |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 139 | print '[', |
| 140 | printlist(v, maxlevel) |
| 141 | print ']', |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 142 | elif type(v) == type({}): |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 143 | print '{', |
| 144 | printdict(v, maxlevel) |
| 145 | print '}', |
| 146 | else: |
| 147 | print v, |
| 148 | |
| 149 | def printlist(v, maxlevel): |
| 150 | n = len(v) |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 151 | if n == 0: return |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 152 | 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 | |
| 160 | def printdict(v, maxlevel): |
| 161 | keys = v.keys() |
| 162 | n = len(keys) |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 163 | if n == 0: return |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 164 | 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 '...', |