1 # Format and print Python stack traces
8 def print_tb(tb
, limit
= None):
10 if hasattr(sys
, 'tracebacklimit'):
11 limit
= sys
.tracebacklimit
13 while tb
is not None and (limit
is None or n
< limit
):
17 filename
= co
.co_filename
19 print ' File "%s", line %d, in %s' % (filename
, lineno
, name
)
20 line
= linecache
.getline(filename
, lineno
)
21 if line
: print ' ' + string
.strip(line
)
25 def extract_tb(tb
, limit
= None):
27 if hasattr(sys
, 'tracebacklimit'):
28 limit
= sys
.tracebacklimit
31 while tb
is not None and (limit
is None or n
< limit
):
35 filename
= co
.co_filename
37 line
= linecache
.getline(filename
, lineno
)
38 if line
: line
= string
.strip(line
)
40 list.append(filename
, lineno
, name
, line
)
45 def print_exception(etype
, value
, tb
, limit
= None):
47 print 'Traceback (innermost last):'
49 if type(etype
) == types
.ClassType
:
50 stype
= etype
.__name
__
56 if etype
is SyntaxError:
58 msg
, (filename
, lineno
, offset
, line
) = value
62 if not filename
: filename
= "<string>"
63 print ' File "%s", line %d' % \
66 while i
< len(line
) and \
67 line
[i
] in string
.whitespace
:
70 print s
+ string
.strip(line
)
71 for c
in line
[i
:offset
-1]:
72 if c
in string
.whitespace
:
78 print '%s: %s' % (stype
, value
)
80 def print_exc(limit
= None):
81 print_exception(sys
.exc_type
, sys
.exc_value
, sys
.exc_traceback
,
84 def print_last(limit
= None):
85 print_exception(sys
.last_type
, sys
.last_value
, sys
.last_traceback
,