Guido van Rossum | 526beed | 1994-07-01 15:36:46 +0000 | [diff] [blame] | 1 | # Format and print Python stack traces |
| 2 | |
| 3 | import linecache |
| 4 | import string |
| 5 | import sys |
Guido van Rossum | c7acf2a | 1995-02-27 13:15:45 +0000 | [diff] [blame] | 6 | import types |
Guido van Rossum | 526beed | 1994-07-01 15:36:46 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 194e20a | 1995-09-20 20:31:51 +0000 | [diff] [blame] | 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 |
Guido van Rossum | 526beed | 1994-07-01 15:36:46 +0000 | [diff] [blame] | 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 |
Guido van Rossum | 194e20a | 1995-09-20 20:31:51 +0000 | [diff] [blame] | 25 | _print(file, |
| 26 | ' File "%s", line %d, in %s' % (filename,lineno,name)) |
Guido van Rossum | 526beed | 1994-07-01 15:36:46 +0000 | [diff] [blame] | 27 | line = linecache.getline(filename, lineno) |
Guido van Rossum | 194e20a | 1995-09-20 20:31:51 +0000 | [diff] [blame] | 28 | if line: _print(file, ' ' + string.strip(line)) |
Guido van Rossum | 526beed | 1994-07-01 15:36:46 +0000 | [diff] [blame] | 29 | tb = tb.tb_next |
| 30 | n = n+1 |
| 31 | |
Guido van Rossum | 28e99fe | 1995-08-04 04:30:30 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 526beed | 1994-07-01 15:36:46 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 28e99fe | 1995-08-04 04:30:30 +0000 | [diff] [blame] | 61 | |
Guido van Rossum | 194e20a | 1995-09-20 20:31:51 +0000 | [diff] [blame] | 62 | def print_exception(etype, value, tb, limit=None, file=None): |
| 63 | if not file: |
| 64 | file = sys.stderr |
Guido van Rossum | 7aeb4b9 | 1994-08-23 13:32:20 +0000 | [diff] [blame] | 65 | if tb: |
Guido van Rossum | 194e20a | 1995-09-20 20:31:51 +0000 | [diff] [blame] | 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], '') |
Guido van Rossum | 28e99fe | 1995-08-04 04:30:30 +0000 | [diff] [blame] | 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 = [] |
Guido van Rossum | c7acf2a | 1995-02-27 13:15:45 +0000 | [diff] [blame] | 82 | if type(etype) == types.ClassType: |
| 83 | stype = etype.__name__ |
Guido van Rossum | 7aeb4b9 | 1994-08-23 13:32:20 +0000 | [diff] [blame] | 84 | else: |
Guido van Rossum | c7acf2a | 1995-02-27 13:15:45 +0000 | [diff] [blame] | 85 | stype = etype |
| 86 | if value is None: |
Guido van Rossum | 28e99fe | 1995-08-04 04:30:30 +0000 | [diff] [blame] | 87 | list.append(str(stype) + '\n') |
Guido van Rossum | c7acf2a | 1995-02-27 13:15:45 +0000 | [diff] [blame] | 88 | else: |
| 89 | if etype is SyntaxError: |
Guido van Rossum | 7aeb4b9 | 1994-08-23 13:32:20 +0000 | [diff] [blame] | 90 | try: |
| 91 | msg, (filename, lineno, offset, line) = value |
| 92 | except: |
| 93 | pass |
| 94 | else: |
| 95 | if not filename: filename = "<string>" |
Guido van Rossum | 28e99fe | 1995-08-04 04:30:30 +0000 | [diff] [blame] | 96 | list.append(' File "%s", line %d\n' % |
| 97 | (filename, lineno)) |
Guido van Rossum | 7aeb4b9 | 1994-08-23 13:32:20 +0000 | [diff] [blame] | 98 | i = 0 |
Guido van Rossum | c7acf2a | 1995-02-27 13:15:45 +0000 | [diff] [blame] | 99 | while i < len(line) and \ |
| 100 | line[i] in string.whitespace: |
Guido van Rossum | 7aeb4b9 | 1994-08-23 13:32:20 +0000 | [diff] [blame] | 101 | i = i+1 |
Guido van Rossum | 28e99fe | 1995-08-04 04:30:30 +0000 | [diff] [blame] | 102 | list.append(' %s\n' % string.strip(line)) |
Guido van Rossum | 7aeb4b9 | 1994-08-23 13:32:20 +0000 | [diff] [blame] | 103 | s = ' ' |
Guido van Rossum | 7aeb4b9 | 1994-08-23 13:32:20 +0000 | [diff] [blame] | 104 | for c in line[i:offset-1]: |
| 105 | if c in string.whitespace: |
| 106 | s = s + c |
| 107 | else: |
| 108 | s = s + ' ' |
Guido van Rossum | 28e99fe | 1995-08-04 04:30:30 +0000 | [diff] [blame] | 109 | list.append('%s^\n' % s) |
Guido van Rossum | 7aeb4b9 | 1994-08-23 13:32:20 +0000 | [diff] [blame] | 110 | value = msg |
Guido van Rossum | 28e99fe | 1995-08-04 04:30:30 +0000 | [diff] [blame] | 111 | list.append('%s: %s\n' % (str(stype), str(value))) |
| 112 | return list |
| 113 | |
Guido van Rossum | 526beed | 1994-07-01 15:36:46 +0000 | [diff] [blame] | 114 | |
Guido van Rossum | 194e20a | 1995-09-20 20:31:51 +0000 | [diff] [blame] | 115 | def print_exc(limit=None, file=None): |
| 116 | if not file: |
| 117 | file = sys.stderr |
Guido van Rossum | 526beed | 1994-07-01 15:36:46 +0000 | [diff] [blame] | 118 | print_exception(sys.exc_type, sys.exc_value, sys.exc_traceback, |
Guido van Rossum | 194e20a | 1995-09-20 20:31:51 +0000 | [diff] [blame] | 119 | limit, file) |
Guido van Rossum | 526beed | 1994-07-01 15:36:46 +0000 | [diff] [blame] | 120 | |
Guido van Rossum | 194e20a | 1995-09-20 20:31:51 +0000 | [diff] [blame] | 121 | def print_last(limit=None, file=None): |
| 122 | if not file: |
| 123 | file = sys.stderr |
Guido van Rossum | 526beed | 1994-07-01 15:36:46 +0000 | [diff] [blame] | 124 | print_exception(sys.last_type, sys.last_value, sys.last_traceback, |
Guido van Rossum | 194e20a | 1995-09-20 20:31:51 +0000 | [diff] [blame] | 125 | limit, file) |