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