1 # Format and print Python stack traces
8 def _print(file, str='', terminator
='\n'):
9 file.write(str+terminator
)
12 def print_list(extracted_list
, file=None):
15 for filename
, lineno
, name
, line
in extracted_list
:
17 ' File "%s", line %d, in %s' % (filename
,lineno
,name
))
19 _print(file, ' %s' % string
.strip(line
))
21 def format_list(extracted_list
):
23 for filename
, lineno
, name
, line
in extracted_list
:
24 item
= ' File "%s", line %d, in %s\n' % (filename
,lineno
,name
)
26 item
= item
+ ' %s\n' % string
.strip(line
)
31 def print_tb(tb
, limit
=None, file=None):
35 if hasattr(sys
, 'tracebacklimit'):
36 limit
= sys
.tracebacklimit
38 while tb
is not None and (limit
is None or n
< limit
):
40 lineno
= tb_lineno(tb
)
42 filename
= co
.co_filename
45 ' File "%s", line %d, in %s' % (filename
,lineno
,name
))
46 line
= linecache
.getline(filename
, lineno
)
47 if line
: _print(file, ' ' + string
.strip(line
))
51 def format_tb(tb
, limit
= None):
52 return format_list(extract_tb(tb
, limit
))
54 def extract_tb(tb
, limit
= None):
56 if hasattr(sys
, 'tracebacklimit'):
57 limit
= sys
.tracebacklimit
60 while tb
is not None and (limit
is None or n
< limit
):
62 lineno
= tb_lineno(tb
)
64 filename
= co
.co_filename
66 line
= linecache
.getline(filename
, lineno
)
67 if line
: line
= string
.strip(line
)
69 list.append((filename
, lineno
, name
, line
))
75 def print_exception(etype
, value
, tb
, limit
=None, file=None):
79 _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], '')
86 def format_exception(etype
, value
, tb
, limit
= None):
88 list = ['Traceback (innermost last):\n']
89 list = list + format_tb(tb
, limit
)
92 list = list + format_exception_only(etype
, value
)
95 def format_exception_only(etype
, value
):
97 if type(etype
) == types
.ClassType
:
98 stype
= etype
.__name
__
102 list.append(str(stype
) + '\n')
104 if etype
is SyntaxError:
106 msg
, (filename
, lineno
, offset
, line
) = value
110 if not filename
: filename
= "<string>"
111 list.append(' File "%s", line %d\n' %
114 while i
< len(line
) and \
115 line
[i
] in string
.whitespace
:
117 list.append(' %s\n' % string
.strip(line
))
119 for c
in line
[i
:offset
-1]:
120 if c
in string
.whitespace
:
124 list.append('%s^\n' % s
)
126 list.append('%s: %s\n' % (str(stype
), str(value
)))
130 def print_exc(limit
=None, file=None):
134 etype
, value
, tb
= sys
.exc_info()
135 print_exception(etype
, value
, tb
, limit
, file)
137 etype
= value
= tb
= None
139 def print_last(limit
=None, file=None):
142 print_exception(sys
.last_type
, sys
.last_value
, sys
.last_traceback
,
146 def print_stack(f
=None, limit
=None, file=None):
149 raise ZeroDivisionError
150 except ZeroDivisionError:
151 f
= sys
.exc_info()[2].tb_frame
.f_back
152 print_list(extract_stack(f
, limit
), file)
154 def format_stack(f
=None, limit
=None):
157 raise ZeroDivisionError
158 except ZeroDivisionError:
159 f
= sys
.exc_info()[2].tb_frame
.f_back
160 return format_list(extract_stack(f
, limit
))
162 def extract_stack(f
=None, limit
= None):
165 raise ZeroDivisionError
166 except ZeroDivisionError:
167 f
= sys
.exc_info()[2].tb_frame
.f_back
169 if hasattr(sys
, 'tracebacklimit'):
170 limit
= sys
.tracebacklimit
173 while f
is not None and (limit
is None or n
< limit
):
174 lineno
= f
.f_lineno
# XXX Too bad if -O is used
176 filename
= co
.co_filename
178 line
= linecache
.getline(filename
, lineno
)
179 if line
: line
= string
.strip(line
)
181 list.append((filename
, lineno
, name
, line
))
187 # Calculate the correct line number of the traceback given in tb (even
189 # Coded by Marc-Andre Lemburg from the example of PyCode_Addr2Line()
191 # Revised version by Jim Hugunin to work with JPython too.
194 c
= tb
.tb_frame
.f_code
195 if not hasattr(c
, 'co_lnotab'):
199 line
= c
.co_firstlineno
202 for i
in range(0, len(tab
), 2):
203 addr
= addr
+ ord(tab
[i
])
206 line
= line
+ ord(tab
[i
+1])