blob: 70d323034ff5a62301268694f51b05851ed9eeac [file] [log] [blame]
Guido van Rossum526beed1994-07-01 15:36:46 +00001# Format and print Python stack traces
2
3import linecache
4import string
5import sys
Guido van Rossumc7acf2a1995-02-27 13:15:45 +00006import types
Guido van Rossum526beed1994-07-01 15:36:46 +00007
Guido van Rossum194e20a1995-09-20 20:31:51 +00008def _print(file, str='', terminator='\n'):
9 file.write(str+terminator)
Guido van Rossumdcc057a1996-08-12 23:18:13 +000010
11
12def print_list(extracted_list, file=None):
13 if not file:
14 file = sys.stderr
15 for filename, lineno, name, line in extracted_list:
16 _print(file,
17 ' File "%s", line %d, in %s' % (filename,lineno,name))
18 if line:
19 _print(file, ' %s' % string.strip(line))
20
21def format_list(extracted_list):
22 list = []
23 for filename, lineno, name, line in extracted_list:
24 item = ' File "%s", line %d, in %s\n' % (filename,lineno,name)
25 if line:
26 item = item + ' %s\n' % string.strip(line)
27 list.append(item)
28 return list
Guido van Rossum194e20a1995-09-20 20:31:51 +000029
30
31def print_tb(tb, limit=None, file=None):
32 if not file:
33 file = sys.stderr
Guido van Rossum526beed1994-07-01 15:36:46 +000034 if limit is None:
35 if hasattr(sys, 'tracebacklimit'):
36 limit = sys.tracebacklimit
37 n = 0
38 while tb is not None and (limit is None or n < limit):
39 f = tb.tb_frame
Guido van Rossum47529661997-09-26 22:43:02 +000040 lineno = tb_lineno(tb)
Guido van Rossum526beed1994-07-01 15:36:46 +000041 co = f.f_code
42 filename = co.co_filename
43 name = co.co_name
Guido van Rossum194e20a1995-09-20 20:31:51 +000044 _print(file,
45 ' File "%s", line %d, in %s' % (filename,lineno,name))
Guido van Rossum526beed1994-07-01 15:36:46 +000046 line = linecache.getline(filename, lineno)
Guido van Rossum194e20a1995-09-20 20:31:51 +000047 if line: _print(file, ' ' + string.strip(line))
Guido van Rossum526beed1994-07-01 15:36:46 +000048 tb = tb.tb_next
49 n = n+1
50
Guido van Rossum28e99fe1995-08-04 04:30:30 +000051def format_tb(tb, limit = None):
Guido van Rossumdcc057a1996-08-12 23:18:13 +000052 return format_list(extract_tb(tb, limit))
Guido van Rossum28e99fe1995-08-04 04:30:30 +000053
Guido van Rossum526beed1994-07-01 15:36:46 +000054def extract_tb(tb, limit = None):
55 if limit is None:
56 if hasattr(sys, 'tracebacklimit'):
57 limit = sys.tracebacklimit
58 list = []
59 n = 0
60 while tb is not None and (limit is None or n < limit):
61 f = tb.tb_frame
Guido van Rossum47529661997-09-26 22:43:02 +000062 lineno = tb_lineno(tb)
Guido van Rossum526beed1994-07-01 15:36:46 +000063 co = f.f_code
64 filename = co.co_filename
65 name = co.co_name
66 line = linecache.getline(filename, lineno)
67 if line: line = string.strip(line)
68 else: line = None
Guido van Rossum2715bb21996-10-08 14:06:35 +000069 list.append((filename, lineno, name, line))
Guido van Rossum526beed1994-07-01 15:36:46 +000070 tb = tb.tb_next
71 n = n+1
72 return list
73
Guido van Rossum28e99fe1995-08-04 04:30:30 +000074
Guido van Rossum194e20a1995-09-20 20:31:51 +000075def print_exception(etype, value, tb, limit=None, file=None):
76 if not file:
77 file = sys.stderr
Guido van Rossum7aeb4b91994-08-23 13:32:20 +000078 if tb:
Guido van Rossum194e20a1995-09-20 20:31:51 +000079 _print(file, 'Traceback (innermost last):')
80 print_tb(tb, limit, file)
81 lines = format_exception_only(etype, value)
82 for line in lines[:-1]:
83 _print(file, line, ' ')
84 _print(file, lines[-1], '')
Guido van Rossum28e99fe1995-08-04 04:30:30 +000085
86def format_exception(etype, value, tb, limit = None):
87 if tb:
88 list = ['Traceback (innermost last):\n']
89 list = list + format_tb(tb, limit)
Guido van Rossum3f0666c1998-03-18 17:48:06 +000090 else:
91 list = []
Guido van Rossum28e99fe1995-08-04 04:30:30 +000092 list = list + format_exception_only(etype, value)
93 return list
94
95def format_exception_only(etype, value):
96 list = []
Guido van Rossumc7acf2a1995-02-27 13:15:45 +000097 if type(etype) == types.ClassType:
98 stype = etype.__name__
Guido van Rossum7aeb4b91994-08-23 13:32:20 +000099 else:
Guido van Rossumc7acf2a1995-02-27 13:15:45 +0000100 stype = etype
101 if value is None:
Guido van Rossum28e99fe1995-08-04 04:30:30 +0000102 list.append(str(stype) + '\n')
Guido van Rossumc7acf2a1995-02-27 13:15:45 +0000103 else:
104 if etype is SyntaxError:
Guido van Rossum7aeb4b91994-08-23 13:32:20 +0000105 try:
106 msg, (filename, lineno, offset, line) = value
107 except:
108 pass
109 else:
110 if not filename: filename = "<string>"
Guido van Rossum28e99fe1995-08-04 04:30:30 +0000111 list.append(' File "%s", line %d\n' %
112 (filename, lineno))
Guido van Rossum7aeb4b91994-08-23 13:32:20 +0000113 i = 0
Guido van Rossumc7acf2a1995-02-27 13:15:45 +0000114 while i < len(line) and \
115 line[i] in string.whitespace:
Guido van Rossum7aeb4b91994-08-23 13:32:20 +0000116 i = i+1
Guido van Rossum28e99fe1995-08-04 04:30:30 +0000117 list.append(' %s\n' % string.strip(line))
Guido van Rossum7aeb4b91994-08-23 13:32:20 +0000118 s = ' '
Guido van Rossum7aeb4b91994-08-23 13:32:20 +0000119 for c in line[i:offset-1]:
120 if c in string.whitespace:
121 s = s + c
122 else:
123 s = s + ' '
Guido van Rossum28e99fe1995-08-04 04:30:30 +0000124 list.append('%s^\n' % s)
Guido van Rossum7aeb4b91994-08-23 13:32:20 +0000125 value = msg
Guido van Rossum28e99fe1995-08-04 04:30:30 +0000126 list.append('%s: %s\n' % (str(stype), str(value)))
127 return list
128
Guido van Rossum526beed1994-07-01 15:36:46 +0000129
Guido van Rossum194e20a1995-09-20 20:31:51 +0000130def print_exc(limit=None, file=None):
131 if not file:
132 file = sys.stderr
Guido van Rossum2deb73a1997-07-18 16:46:36 +0000133 try:
134 etype, value, tb = sys.exc_info()
135 print_exception(etype, value, tb, limit, file)
136 finally:
137 etype = value = tb = None
Guido van Rossum526beed1994-07-01 15:36:46 +0000138
Guido van Rossum194e20a1995-09-20 20:31:51 +0000139def print_last(limit=None, file=None):
140 if not file:
141 file = sys.stderr
Guido van Rossum526beed1994-07-01 15:36:46 +0000142 print_exception(sys.last_type, sys.last_value, sys.last_traceback,
Guido van Rossum194e20a1995-09-20 20:31:51 +0000143 limit, file)
Guido van Rossumdcc057a1996-08-12 23:18:13 +0000144
145
146def print_stack(f=None, limit=None, file=None):
147 if f is None:
148 try:
149 raise ZeroDivisionError
150 except ZeroDivisionError:
Guido van Rossum2deb73a1997-07-18 16:46:36 +0000151 f = sys.exc_info()[2].tb_frame.f_back
Guido van Rossumdcc057a1996-08-12 23:18:13 +0000152 print_list(extract_stack(f, limit), file)
153
154def format_stack(f=None, limit=None):
155 if f is None:
156 try:
157 raise ZeroDivisionError
158 except ZeroDivisionError:
Guido van Rossum2deb73a1997-07-18 16:46:36 +0000159 f = sys.exc_info()[2].tb_frame.f_back
160 return format_list(extract_stack(f, limit))
Guido van Rossumdcc057a1996-08-12 23:18:13 +0000161
162def extract_stack(f=None, limit = None):
163 if f is None:
164 try:
165 raise ZeroDivisionError
166 except ZeroDivisionError:
Guido van Rossum2deb73a1997-07-18 16:46:36 +0000167 f = sys.exc_info()[2].tb_frame.f_back
Guido van Rossumdcc057a1996-08-12 23:18:13 +0000168 if limit is None:
169 if hasattr(sys, 'tracebacklimit'):
170 limit = sys.tracebacklimit
171 list = []
172 n = 0
173 while f is not None and (limit is None or n < limit):
Guido van Rossum47529661997-09-26 22:43:02 +0000174 lineno = f.f_lineno # XXX Too bad if -O is used
Guido van Rossumdcc057a1996-08-12 23:18:13 +0000175 co = f.f_code
176 filename = co.co_filename
177 name = co.co_name
178 line = linecache.getline(filename, lineno)
179 if line: line = string.strip(line)
180 else: line = None
Guido van Rossum2715bb21996-10-08 14:06:35 +0000181 list.append((filename, lineno, name, line))
Guido van Rossumdcc057a1996-08-12 23:18:13 +0000182 f = f.f_back
183 n = n+1
184 list.reverse()
185 return list
Guido van Rossum47529661997-09-26 22:43:02 +0000186
187# Calculate the correct line number of the traceback given in tb (even
188# with -O on).
189# Coded by Marc-Andre Lemburg from the example of PyCode_Addr2Line()
190# in compile.c.
Guido van Rossum6e73af71998-02-26 17:25:02 +0000191# Revised version by Jim Hugunin to work with JPython too.
Guido van Rossum47529661997-09-26 22:43:02 +0000192
193def tb_lineno(tb):
Guido van Rossum6e73af71998-02-26 17:25:02 +0000194 c = tb.tb_frame.f_code
195 if not hasattr(c, 'co_lnotab'):
196 return tb.tb_lineno
197
198 tab = c.co_lnotab
199 line = c.co_firstlineno
200 stopat = tb.tb_lasti
Guido van Rossum47529661997-09-26 22:43:02 +0000201 addr = 0
202 for i in range(0, len(tab), 2):
203 addr = addr + ord(tab[i])
204 if addr > stopat:
205 break
206 line = line + ord(tab[i+1])
207 return line