Mercurial > cpython-lru_cache-weakref
annotate Lib/lib-old/tb.py @ 5817:48b94de85bf3 legacy-trunk
Extensive changes by AMK.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 17 Jul 1997 16:29:42 +0000 |
| parents | eb6eb61b606f |
| children | 2656d1eea792 |
| rev | line source |
|---|---|
| 103 | 1 # Print tracebacks, with a dump of local variables. |
| 2 # Also an interactive stack trace browser. | |
|
2959
eb6eb61b606f
noted obsolescence; exec() -> exec
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
3 # Note -- this module is obsolete -- use pdb.pm() instead. |
| 103 | 4 |
| 5 import sys | |
| 572 | 6 import os |
| 103 | 7 from stat import * |
| 8 import string | |
| 572 | 9 import linecache |
| 103 | 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: | |
| 559 | 40 if cmd == 'quit': |
| 103 | 41 break |
| 559 | 42 elif cmd == 'list': |
| 103 | 43 browserlist(tb) |
| 559 | 44 elif cmd == 'up': |
| 103 | 45 if ptr-1 >= 0: ptr = ptr-1 |
| 46 else: print 'Bottom of stack.' | |
| 559 | 47 elif cmd == 'down': |
| 103 | 48 if ptr+1 < len(tblist): ptr = ptr+1 |
| 49 else: print 'Top of stack.' | |
| 559 | 50 elif cmd == 'locals': |
| 103 | 51 printsymbols(tb.tb_frame.f_locals) |
| 559 | 52 elif cmd == 'globals': |
| 103 | 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): | |
| 559 | 65 if i == lineno: prefix = '***' + string.rjust(`i`, 4) + ':' |
| 103 | 66 else: prefix = string.rjust(`i`, 7) + ':' |
| 572 | 67 line = linecache.getline(filename, i) |
| 559 | 68 if line[-1:] == '\n': line = line[:-1] |
| 103 | 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: | |
|
2959
eb6eb61b606f
noted obsolescence; exec() -> exec
Guido van Rossum <guido@python.org>
parents:
2440
diff
changeset
|
75 exec cmd+'\n' in globals, locals |
| 103 | 76 except: |
| 77 print '*** Exception:', | |
|
2440
d577d749918d
handle class exceptions
Guido van Rossum <guido@python.org>
parents:
572
diff
changeset
|
78 if type(sys.exc_type) == type(''): |
|
d577d749918d
handle class exceptions
Guido van Rossum <guido@python.org>
parents:
572
diff
changeset
|
79 print sys.exc_type, |
|
d577d749918d
handle class exceptions
Guido van Rossum <guido@python.org>
parents:
572
diff
changeset
|
80 else: |
|
d577d749918d
handle class exceptions
Guido van Rossum <guido@python.org>
parents:
572
diff
changeset
|
81 print sys.exc_type.__name__, |
| 103 | 82 if sys.exc_value <> None: |
| 83 print ':', sys.exc_value, | |
| 84 print | |
| 85 print 'Type help to get help.' | |
| 86 | |
| 87 def browserhelp(): | |
| 88 print | |
| 89 print ' This is the traceback browser. Commands are:' | |
| 90 print ' up : move one level up in the call stack' | |
| 91 print ' down : move one level down in the call stack' | |
| 92 print ' locals : print all local variables at this level' | |
| 93 print ' globals : print all global variables at this level' | |
| 94 print ' list : list source code around the failure' | |
| 95 print ' help : print help (what you are reading now)' | |
| 96 print ' quit : back to command interpreter' | |
| 97 print ' Typing any other 1-line statement will execute it' | |
| 98 print ' using the current level\'s symbol tables' | |
| 99 print | |
| 100 | |
| 101 def printtb(tb): | |
| 102 while tb: | |
| 103 print1tb(tb) | |
| 104 tb = tb.tb_next | |
| 105 | |
| 106 def print1tb(tb): | |
| 107 printtbheader(tb) | |
| 108 if tb.tb_frame.f_locals is not tb.tb_frame.f_globals: | |
| 109 printsymbols(tb.tb_frame.f_locals) | |
| 110 | |
| 111 def printtbheader(tb): | |
| 112 filename = tb.tb_frame.f_code.co_filename | |
| 113 lineno = tb.tb_lineno | |
| 114 info = '"' + filename + '"(' + `lineno` + ')' | |
| 572 | 115 line = linecache.getline(filename, lineno) |
| 103 | 116 if line: |
| 117 info = info + ': ' + string.strip(line) | |
| 118 print info | |
| 119 | |
| 120 def printsymbols(d): | |
| 121 keys = d.keys() | |
| 122 keys.sort() | |
| 123 for name in keys: | |
| 124 print ' ' + string.ljust(name, 12) + ':', | |
| 125 printobject(d[name], 4) | |
| 126 print | |
| 127 | |
| 128 def printobject(v, maxlevel): | |
| 559 | 129 if v == None: |
| 103 | 130 print 'None', |
| 131 elif type(v) in (type(0), type(0.0)): | |
| 132 print v, | |
| 559 | 133 elif type(v) == type(''): |
| 103 | 134 if len(v) > 20: |
| 135 print `v[:17] + '...'`, | |
| 136 else: | |
| 137 print `v`, | |
| 559 | 138 elif type(v) == type(()): |
| 103 | 139 print '(', |
| 140 printlist(v, maxlevel) | |
| 141 print ')', | |
| 559 | 142 elif type(v) == type([]): |
| 103 | 143 print '[', |
| 144 printlist(v, maxlevel) | |
| 145 print ']', | |
| 559 | 146 elif type(v) == type({}): |
| 103 | 147 print '{', |
| 148 printdict(v, maxlevel) | |
| 149 print '}', | |
| 150 else: | |
| 151 print v, | |
| 152 | |
| 153 def printlist(v, maxlevel): | |
| 154 n = len(v) | |
| 559 | 155 if n == 0: return |
| 103 | 156 if maxlevel <= 0: |
| 157 print '...', | |
| 158 return | |
| 159 for i in range(min(6, n)): | |
| 160 printobject(v[i], maxlevel-1) | |
| 161 if i+1 < n: print ',', | |
| 162 if n > 6: print '...', | |
| 163 | |
| 164 def printdict(v, maxlevel): | |
| 165 keys = v.keys() | |
| 166 n = len(keys) | |
| 559 | 167 if n == 0: return |
| 103 | 168 if maxlevel <= 0: |
| 169 print '...', | |
| 170 return | |
| 171 keys.sort() | |
| 172 for i in range(min(6, n)): | |
| 173 key = keys[i] | |
| 174 print `key` + ':', | |
| 175 printobject(v[key], maxlevel-1) | |
| 176 if i+1 < n: print ',', | |
| 177 if n > 6: print '...', |
