Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 1 | # Format and print Python stack traces |
| 2 | |
| 3 | import linecache |
| 4 | import string |
| 5 | import sys |
| 6 | import types |
| 7 | |
| 8 | def _print(file, str='', terminator='\n'): |
| 9 | file.write(str+terminator) |
| 10 | |
| 11 | |
| 12 | def print_tb(tb, limit=None, file=None): |
| 13 | if not file: |
| 14 | file = sys.stderr |
| 15 | if limit is None: |
| 16 | if hasattr(sys, 'tracebacklimit'): |
| 17 | limit = sys.tracebacklimit |
| 18 | n = 0 |
| 19 | while tb is not None and (limit is None or n < limit): |
| 20 | f = tb.tb_frame |
| 21 | lineno = tb.tb_lineno |
| 22 | co = f.f_code |
| 23 | filename = co.co_filename |
| 24 | name = co.co_name |
| 25 | _print(file, |
| 26 | ' File "%s", line %d, in %s' % (filename,lineno,name)) |
| 27 | line = linecache.getline(filename, lineno) |
| 28 | if line: _print(file, ' ' + string.strip(line)) |
| 29 | tb = tb.tb_next |
| 30 | n = n+1 |
| 31 | |
| 32 | def format_tb(tb, limit = None): |
| 33 | list = [] |
| 34 | for filename, lineno, name, line in extract_tb(tb, limit): |
| 35 | item = ' File "%s", line %d, in %s\n' % (filename,lineno,name) |
| 36 | if line: |
| 37 | item = item + ' %s\n' % string.strip(line) |
| 38 | list.append(item) |
| 39 | return list |
| 40 | |
| 41 | def extract_tb(tb, limit = None): |
| 42 | if limit is None: |
| 43 | if hasattr(sys, 'tracebacklimit'): |
| 44 | limit = sys.tracebacklimit |
| 45 | list = [] |
| 46 | n = 0 |
| 47 | while tb is not None and (limit is None or n < limit): |
| 48 | f = tb.tb_frame |
| 49 | lineno = tb.tb_lineno |
| 50 | co = f.f_code |
| 51 | filename = co.co_filename |
| 52 | name = co.co_name |
| 53 | line = linecache.getline(filename, lineno) |
| 54 | if line: line = string.strip(line) |
| 55 | else: line = None |
| 56 | list.append(filename, lineno, name, line) |
| 57 | tb = tb.tb_next |
| 58 | n = n+1 |
| 59 | return list |
| 60 | |
| 61 | |
| 62 | def print_exception(etype, value, tb, limit=None, file=None): |
| 63 | if not file: |
| 64 | file = sys.stderr |
| 65 | if tb: |
| 66 | _print(file, 'Traceback (innermost last):') |
| 67 | print_tb(tb, limit, file) |
| 68 | lines = format_exception_only(etype, value) |
| 69 | for line in lines[:-1]: |
| 70 | _print(file, line, ' ') |
| 71 | _print(file, lines[-1], '') |
| 72 | |
| 73 | def format_exception(etype, value, tb, limit = None): |
| 74 | if tb: |
| 75 | list = ['Traceback (innermost last):\n'] |
| 76 | list = list + format_tb(tb, limit) |
| 77 | list = list + format_exception_only(etype, value) |
| 78 | return list |
| 79 | |
| 80 | def format_exception_only(etype, value): |
| 81 | list = [] |
| 82 | if type(etype) == types.ClassType: |
| 83 | stype = etype.__name__ |
| 84 | else: |
| 85 | stype = etype |
| 86 | if value is None: |
| 87 | list.append(str(stype) + '\n') |
| 88 | else: |
| 89 | if etype is SyntaxError: |
| 90 | try: |
| 91 | msg, (filename, lineno, offset, line) = value |
| 92 | except: |
| 93 | pass |
| 94 | else: |
| 95 | if not filename: filename = "<string>" |
| 96 | list.append(' File "%s", line %d\n' % |
| 97 | (filename, lineno)) |
| 98 | i = 0 |
| 99 | while i < len(line) and \ |
| 100 | line[i] in string.whitespace: |
| 101 | i = i+1 |
| 102 | list.append(' %s\n' % string.strip(line)) |
| 103 | s = ' ' |
| 104 | for c in line[i:offset-1]: |
| 105 | if c in string.whitespace: |
| 106 | s = s + c |
| 107 | else: |
| 108 | s = s + ' ' |
| 109 | list.append('%s^\n' % s) |
| 110 | value = msg |
| 111 | list.append('%s: %s\n' % (str(stype), str(value))) |
| 112 | return list |
| 113 | |
| 114 | |
| 115 | def print_exc(limit=None, file=None): |
| 116 | if not file: |
| 117 | file = sys.stderr |
| 118 | print_exception(sys.exc_type, sys.exc_value, sys.exc_traceback, |
| 119 | limit, file) |
| 120 | |
| 121 | def print_last(limit=None, file=None): |
| 122 | if not file: |
| 123 | file = sys.stderr |
| 124 | print_exception(sys.last_type, sys.last_value, sys.last_traceback, |
| 125 | limit, file) |