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. |
Guido van Rossum | a62b100 | 1995-08-10 19:43:04 +0000 | [diff] [blame] | 3 | # Note -- this module is obsolete -- use pdb.pm() instead. |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 4 | |
| 5 | import sys |
Guido van Rossum | 9542c58 | 1992-01-12 23:27:56 +0000 | [diff] [blame] | 6 | import os |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 7 | from stat import * |
| 8 | import string |
Guido van Rossum | 9542c58 | 1992-01-12 23:27:56 +0000 | [diff] [blame] | 9 | import linecache |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 10 | |
| 11 | def br(): browser(sys.last_traceback) |
| 12 | |
| 13 | def tb(): printtb(sys.last_traceback) |
| 14 | |
| 15 | def browser(tb): |
| 16 | if not tb: |
| 17 | print 'No traceback.' |
| 18 | return |
| 19 | tblist = [] |
| 20 | while tb: |
| 21 | tblist.append(tb) |
| 22 | tb = tb.tb_next |
| 23 | ptr = len(tblist)-1 |
| 24 | tb = tblist[ptr] |
| 25 | while 1: |
| 26 | if tb <> tblist[ptr]: |
| 27 | tb = tblist[ptr] |
| 28 | print `ptr` + ':', |
| 29 | printtbheader(tb) |
| 30 | try: |
| 31 | line = raw_input('TB: ') |
| 32 | except KeyboardInterrupt: |
| 33 | print '\n[Interrupted]' |
| 34 | break |
| 35 | except EOFError: |
| 36 | print '\n[EOF]' |
| 37 | break |
| 38 | cmd = string.strip(line) |
| 39 | if cmd: |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 40 | if cmd == 'quit': |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 41 | break |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 42 | elif cmd == 'list': |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 43 | browserlist(tb) |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 44 | elif cmd == 'up': |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 45 | if ptr-1 >= 0: ptr = ptr-1 |
| 46 | else: print 'Bottom of stack.' |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 47 | elif cmd == 'down': |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 48 | if ptr+1 < len(tblist): ptr = ptr+1 |
| 49 | else: print 'Top of stack.' |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 50 | elif cmd == 'locals': |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 51 | printsymbols(tb.tb_frame.f_locals) |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 52 | elif cmd == 'globals': |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 53 | printsymbols(tb.tb_frame.f_globals) |
| 54 | elif cmd in ('?', 'help'): |
| 55 | browserhelp() |
| 56 | else: |
| 57 | browserexec(tb, cmd) |
| 58 | |
| 59 | def browserlist(tb): |
| 60 | filename = tb.tb_frame.f_code.co_filename |
| 61 | lineno = tb.tb_lineno |
| 62 | last = lineno |
| 63 | first = max(1, last-10) |
| 64 | for i in range(first, last+1): |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 65 | if i == lineno: prefix = '***' + string.rjust(`i`, 4) + ':' |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 66 | else: prefix = string.rjust(`i`, 7) + ':' |
Guido van Rossum | 9542c58 | 1992-01-12 23:27:56 +0000 | [diff] [blame] | 67 | line = linecache.getline(filename, i) |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 68 | if line[-1:] == '\n': line = line[:-1] |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 69 | print prefix + line |
| 70 | |
| 71 | def browserexec(tb, cmd): |
| 72 | locals = tb.tb_frame.f_locals |
| 73 | globals = tb.tb_frame.f_globals |
| 74 | try: |
Guido van Rossum | a62b100 | 1995-08-10 19:43:04 +0000 | [diff] [blame] | 75 | exec cmd+'\n' in globals, locals |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 76 | except: |
Guido van Rossum | f15d159 | 1997-09-29 23:22:12 +0000 | [diff] [blame^] | 77 | t, v = sys.exc_info()[:2] |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 78 | print '*** Exception:', |
Guido van Rossum | f15d159 | 1997-09-29 23:22:12 +0000 | [diff] [blame^] | 79 | if type(t) == type(''): |
| 80 | print t, |
Guido van Rossum | c7acf2a | 1995-02-27 13:15:45 +0000 | [diff] [blame] | 81 | else: |
Guido van Rossum | f15d159 | 1997-09-29 23:22:12 +0000 | [diff] [blame^] | 82 | print t.__name__, |
| 83 | if v <> None: |
| 84 | print ':', v, |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 85 | print |
| 86 | print 'Type help to get help.' |
| 87 | |
| 88 | def browserhelp(): |
| 89 | print |
| 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' |
| 100 | print |
| 101 | |
| 102 | def printtb(tb): |
| 103 | while tb: |
| 104 | print1tb(tb) |
| 105 | tb = tb.tb_next |
| 106 | |
| 107 | def print1tb(tb): |
| 108 | printtbheader(tb) |
| 109 | if tb.tb_frame.f_locals is not tb.tb_frame.f_globals: |
| 110 | printsymbols(tb.tb_frame.f_locals) |
| 111 | |
| 112 | def printtbheader(tb): |
| 113 | filename = tb.tb_frame.f_code.co_filename |
| 114 | lineno = tb.tb_lineno |
| 115 | info = '"' + filename + '"(' + `lineno` + ')' |
Guido van Rossum | 9542c58 | 1992-01-12 23:27:56 +0000 | [diff] [blame] | 116 | line = linecache.getline(filename, lineno) |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 117 | if line: |
| 118 | info = info + ': ' + string.strip(line) |
| 119 | print info |
| 120 | |
| 121 | def printsymbols(d): |
| 122 | keys = d.keys() |
| 123 | keys.sort() |
| 124 | for name in keys: |
| 125 | print ' ' + string.ljust(name, 12) + ':', |
| 126 | printobject(d[name], 4) |
| 127 | print |
| 128 | |
| 129 | def printobject(v, maxlevel): |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 130 | if v == None: |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 131 | print 'None', |
| 132 | elif type(v) in (type(0), type(0.0)): |
| 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 | if len(v) > 20: |
| 136 | print `v[:17] + '...'`, |
| 137 | else: |
| 138 | print `v`, |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 139 | elif type(v) == type(()): |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 140 | print '(', |
| 141 | printlist(v, maxlevel) |
| 142 | print ')', |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 143 | elif type(v) == type([]): |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 144 | print '[', |
| 145 | printlist(v, maxlevel) |
| 146 | print ']', |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 147 | elif type(v) == type({}): |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 148 | print '{', |
| 149 | printdict(v, maxlevel) |
| 150 | print '}', |
| 151 | else: |
| 152 | print v, |
| 153 | |
| 154 | def printlist(v, maxlevel): |
| 155 | n = len(v) |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 156 | if n == 0: return |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 157 | if maxlevel <= 0: |
| 158 | print '...', |
| 159 | return |
| 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 '...', |
| 164 | |
| 165 | def printdict(v, maxlevel): |
| 166 | keys = v.keys() |
| 167 | n = len(keys) |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 168 | if n == 0: return |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 169 | if maxlevel <= 0: |
| 170 | print '...', |
| 171 | return |
| 172 | keys.sort() |
| 173 | for i in range(min(6, n)): |
| 174 | key = keys[i] |
| 175 | print `key` + ':', |
| 176 | printobject(v[key], maxlevel-1) |
| 177 | if i+1 < n: print ',', |
| 178 | if n > 6: print '...', |