Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../usr/lib64/python2....
File: traceback.py
"""Extract, format and print information about Python stack traces."""
[0] Fix | Delete
[1] Fix | Delete
import linecache
[2] Fix | Delete
import sys
[3] Fix | Delete
import types
[4] Fix | Delete
[5] Fix | Delete
__all__ = ['extract_stack', 'extract_tb', 'format_exception',
[6] Fix | Delete
'format_exception_only', 'format_list', 'format_stack',
[7] Fix | Delete
'format_tb', 'print_exc', 'format_exc', 'print_exception',
[8] Fix | Delete
'print_last', 'print_stack', 'print_tb', 'tb_lineno']
[9] Fix | Delete
[10] Fix | Delete
def _print(file, str='', terminator='\n'):
[11] Fix | Delete
file.write(str+terminator)
[12] Fix | Delete
[13] Fix | Delete
[14] Fix | Delete
def print_list(extracted_list, file=None):
[15] Fix | Delete
"""Print the list of tuples as returned by extract_tb() or
[16] Fix | Delete
extract_stack() as a formatted stack trace to the given file."""
[17] Fix | Delete
if file is None:
[18] Fix | Delete
file = sys.stderr
[19] Fix | Delete
for filename, lineno, name, line in extracted_list:
[20] Fix | Delete
_print(file,
[21] Fix | Delete
' File "%s", line %d, in %s' % (filename,lineno,name))
[22] Fix | Delete
if line:
[23] Fix | Delete
_print(file, ' %s' % line.strip())
[24] Fix | Delete
[25] Fix | Delete
def format_list(extracted_list):
[26] Fix | Delete
"""Format a list of traceback entry tuples for printing.
[27] Fix | Delete
[28] Fix | Delete
Given a list of tuples as returned by extract_tb() or
[29] Fix | Delete
extract_stack(), return a list of strings ready for printing.
[30] Fix | Delete
Each string in the resulting list corresponds to the item with the
[31] Fix | Delete
same index in the argument list. Each string ends in a newline;
[32] Fix | Delete
the strings may contain internal newlines as well, for those items
[33] Fix | Delete
whose source text line is not None.
[34] Fix | Delete
"""
[35] Fix | Delete
list = []
[36] Fix | Delete
for filename, lineno, name, line in extracted_list:
[37] Fix | Delete
item = ' File "%s", line %d, in %s\n' % (filename,lineno,name)
[38] Fix | Delete
if line:
[39] Fix | Delete
item = item + ' %s\n' % line.strip()
[40] Fix | Delete
list.append(item)
[41] Fix | Delete
return list
[42] Fix | Delete
[43] Fix | Delete
[44] Fix | Delete
def print_tb(tb, limit=None, file=None):
[45] Fix | Delete
"""Print up to 'limit' stack trace entries from the traceback 'tb'.
[46] Fix | Delete
[47] Fix | Delete
If 'limit' is omitted or None, all entries are printed. If 'file'
[48] Fix | Delete
is omitted or None, the output goes to sys.stderr; otherwise
[49] Fix | Delete
'file' should be an open file or file-like object with a write()
[50] Fix | Delete
method.
[51] Fix | Delete
"""
[52] Fix | Delete
if file is None:
[53] Fix | Delete
file = sys.stderr
[54] Fix | Delete
if limit is None:
[55] Fix | Delete
if hasattr(sys, 'tracebacklimit'):
[56] Fix | Delete
limit = sys.tracebacklimit
[57] Fix | Delete
n = 0
[58] Fix | Delete
while tb is not None and (limit is None or n < limit):
[59] Fix | Delete
f = tb.tb_frame
[60] Fix | Delete
lineno = tb.tb_lineno
[61] Fix | Delete
co = f.f_code
[62] Fix | Delete
filename = co.co_filename
[63] Fix | Delete
name = co.co_name
[64] Fix | Delete
_print(file,
[65] Fix | Delete
' File "%s", line %d, in %s' % (filename, lineno, name))
[66] Fix | Delete
linecache.checkcache(filename)
[67] Fix | Delete
line = linecache.getline(filename, lineno, f.f_globals)
[68] Fix | Delete
if line: _print(file, ' ' + line.strip())
[69] Fix | Delete
tb = tb.tb_next
[70] Fix | Delete
n = n+1
[71] Fix | Delete
[72] Fix | Delete
def format_tb(tb, limit = None):
[73] Fix | Delete
"""A shorthand for 'format_list(extract_tb(tb, limit))'."""
[74] Fix | Delete
return format_list(extract_tb(tb, limit))
[75] Fix | Delete
[76] Fix | Delete
def extract_tb(tb, limit = None):
[77] Fix | Delete
"""Return list of up to limit pre-processed entries from traceback.
[78] Fix | Delete
[79] Fix | Delete
This is useful for alternate formatting of stack traces. If
[80] Fix | Delete
'limit' is omitted or None, all entries are extracted. A
[81] Fix | Delete
pre-processed stack trace entry is a quadruple (filename, line
[82] Fix | Delete
number, function name, text) representing the information that is
[83] Fix | Delete
usually printed for a stack trace. The text is a string with
[84] Fix | Delete
leading and trailing whitespace stripped; if the source is not
[85] Fix | Delete
available it is None.
[86] Fix | Delete
"""
[87] Fix | Delete
if limit is None:
[88] Fix | Delete
if hasattr(sys, 'tracebacklimit'):
[89] Fix | Delete
limit = sys.tracebacklimit
[90] Fix | Delete
list = []
[91] Fix | Delete
n = 0
[92] Fix | Delete
while tb is not None and (limit is None or n < limit):
[93] Fix | Delete
f = tb.tb_frame
[94] Fix | Delete
lineno = tb.tb_lineno
[95] Fix | Delete
co = f.f_code
[96] Fix | Delete
filename = co.co_filename
[97] Fix | Delete
name = co.co_name
[98] Fix | Delete
linecache.checkcache(filename)
[99] Fix | Delete
line = linecache.getline(filename, lineno, f.f_globals)
[100] Fix | Delete
if line: line = line.strip()
[101] Fix | Delete
else: line = None
[102] Fix | Delete
list.append((filename, lineno, name, line))
[103] Fix | Delete
tb = tb.tb_next
[104] Fix | Delete
n = n+1
[105] Fix | Delete
return list
[106] Fix | Delete
[107] Fix | Delete
[108] Fix | Delete
def print_exception(etype, value, tb, limit=None, file=None):
[109] Fix | Delete
"""Print exception up to 'limit' stack trace entries from 'tb' to 'file'.
[110] Fix | Delete
[111] Fix | Delete
This differs from print_tb() in the following ways: (1) if
[112] Fix | Delete
traceback is not None, it prints a header "Traceback (most recent
[113] Fix | Delete
call last):"; (2) it prints the exception type and value after the
[114] Fix | Delete
stack trace; (3) if type is SyntaxError and value has the
[115] Fix | Delete
appropriate format, it prints the line where the syntax error
[116] Fix | Delete
occurred with a caret on the next line indicating the approximate
[117] Fix | Delete
position of the error.
[118] Fix | Delete
"""
[119] Fix | Delete
if file is None:
[120] Fix | Delete
file = sys.stderr
[121] Fix | Delete
if tb:
[122] Fix | Delete
_print(file, 'Traceback (most recent call last):')
[123] Fix | Delete
print_tb(tb, limit, file)
[124] Fix | Delete
lines = format_exception_only(etype, value)
[125] Fix | Delete
for line in lines:
[126] Fix | Delete
_print(file, line, '')
[127] Fix | Delete
[128] Fix | Delete
def format_exception(etype, value, tb, limit = None):
[129] Fix | Delete
"""Format a stack trace and the exception information.
[130] Fix | Delete
[131] Fix | Delete
The arguments have the same meaning as the corresponding arguments
[132] Fix | Delete
to print_exception(). The return value is a list of strings, each
[133] Fix | Delete
ending in a newline and some containing internal newlines. When
[134] Fix | Delete
these lines are concatenated and printed, exactly the same text is
[135] Fix | Delete
printed as does print_exception().
[136] Fix | Delete
"""
[137] Fix | Delete
if tb:
[138] Fix | Delete
list = ['Traceback (most recent call last):\n']
[139] Fix | Delete
list = list + format_tb(tb, limit)
[140] Fix | Delete
else:
[141] Fix | Delete
list = []
[142] Fix | Delete
list = list + format_exception_only(etype, value)
[143] Fix | Delete
return list
[144] Fix | Delete
[145] Fix | Delete
def format_exception_only(etype, value):
[146] Fix | Delete
"""Format the exception part of a traceback.
[147] Fix | Delete
[148] Fix | Delete
The arguments are the exception type and value such as given by
[149] Fix | Delete
sys.last_type and sys.last_value. The return value is a list of
[150] Fix | Delete
strings, each ending in a newline.
[151] Fix | Delete
[152] Fix | Delete
Normally, the list contains a single string; however, for
[153] Fix | Delete
SyntaxError exceptions, it contains several lines that (when
[154] Fix | Delete
printed) display detailed information about where the syntax
[155] Fix | Delete
error occurred.
[156] Fix | Delete
[157] Fix | Delete
The message indicating which exception occurred is always the last
[158] Fix | Delete
string in the list.
[159] Fix | Delete
[160] Fix | Delete
"""
[161] Fix | Delete
[162] Fix | Delete
# An instance should not have a meaningful value parameter, but
[163] Fix | Delete
# sometimes does, particularly for string exceptions, such as
[164] Fix | Delete
# >>> raise string1, string2 # deprecated
[165] Fix | Delete
#
[166] Fix | Delete
# Clear these out first because issubtype(string1, SyntaxError)
[167] Fix | Delete
# would raise another exception and mask the original problem.
[168] Fix | Delete
if (isinstance(etype, BaseException) or
[169] Fix | Delete
isinstance(etype, types.InstanceType) or
[170] Fix | Delete
etype is None or type(etype) is str):
[171] Fix | Delete
return [_format_final_exc_line(etype, value)]
[172] Fix | Delete
[173] Fix | Delete
stype = etype.__name__
[174] Fix | Delete
[175] Fix | Delete
if not issubclass(etype, SyntaxError):
[176] Fix | Delete
return [_format_final_exc_line(stype, value)]
[177] Fix | Delete
[178] Fix | Delete
# It was a syntax error; show exactly where the problem was found.
[179] Fix | Delete
lines = []
[180] Fix | Delete
try:
[181] Fix | Delete
msg, (filename, lineno, offset, badline) = value.args
[182] Fix | Delete
except Exception:
[183] Fix | Delete
pass
[184] Fix | Delete
else:
[185] Fix | Delete
filename = filename or "<string>"
[186] Fix | Delete
lines.append(' File "%s", line %d\n' % (filename, lineno))
[187] Fix | Delete
if badline is not None:
[188] Fix | Delete
lines.append(' %s\n' % badline.strip())
[189] Fix | Delete
if offset is not None:
[190] Fix | Delete
caretspace = badline.rstrip('\n')
[191] Fix | Delete
offset = min(len(caretspace), offset) - 1
[192] Fix | Delete
caretspace = caretspace[:offset].lstrip()
[193] Fix | Delete
# non-space whitespace (likes tabs) must be kept for alignment
[194] Fix | Delete
caretspace = ((c.isspace() and c or ' ') for c in caretspace)
[195] Fix | Delete
lines.append(' %s^\n' % ''.join(caretspace))
[196] Fix | Delete
value = msg
[197] Fix | Delete
[198] Fix | Delete
lines.append(_format_final_exc_line(stype, value))
[199] Fix | Delete
return lines
[200] Fix | Delete
[201] Fix | Delete
def _format_final_exc_line(etype, value):
[202] Fix | Delete
"""Return a list of a single line -- normal case for format_exception_only"""
[203] Fix | Delete
valuestr = _some_str(value)
[204] Fix | Delete
if value is None or not valuestr:
[205] Fix | Delete
line = "%s\n" % etype
[206] Fix | Delete
else:
[207] Fix | Delete
line = "%s: %s\n" % (etype, valuestr)
[208] Fix | Delete
return line
[209] Fix | Delete
[210] Fix | Delete
def _some_str(value):
[211] Fix | Delete
try:
[212] Fix | Delete
return str(value)
[213] Fix | Delete
except Exception:
[214] Fix | Delete
pass
[215] Fix | Delete
try:
[216] Fix | Delete
value = unicode(value)
[217] Fix | Delete
return value.encode("ascii", "backslashreplace")
[218] Fix | Delete
except Exception:
[219] Fix | Delete
pass
[220] Fix | Delete
return '<unprintable %s object>' % type(value).__name__
[221] Fix | Delete
[222] Fix | Delete
[223] Fix | Delete
def print_exc(limit=None, file=None):
[224] Fix | Delete
"""Shorthand for 'print_exception(sys.exc_type, sys.exc_value, sys.exc_traceback, limit, file)'.
[225] Fix | Delete
(In fact, it uses sys.exc_info() to retrieve the same information
[226] Fix | Delete
in a thread-safe way.)"""
[227] Fix | Delete
if file is None:
[228] Fix | Delete
file = sys.stderr
[229] Fix | Delete
try:
[230] Fix | Delete
etype, value, tb = sys.exc_info()
[231] Fix | Delete
print_exception(etype, value, tb, limit, file)
[232] Fix | Delete
finally:
[233] Fix | Delete
etype = value = tb = None
[234] Fix | Delete
[235] Fix | Delete
[236] Fix | Delete
def format_exc(limit=None):
[237] Fix | Delete
"""Like print_exc() but return a string."""
[238] Fix | Delete
try:
[239] Fix | Delete
etype, value, tb = sys.exc_info()
[240] Fix | Delete
return ''.join(format_exception(etype, value, tb, limit))
[241] Fix | Delete
finally:
[242] Fix | Delete
etype = value = tb = None
[243] Fix | Delete
[244] Fix | Delete
[245] Fix | Delete
def print_last(limit=None, file=None):
[246] Fix | Delete
"""This is a shorthand for 'print_exception(sys.last_type,
[247] Fix | Delete
sys.last_value, sys.last_traceback, limit, file)'."""
[248] Fix | Delete
if not hasattr(sys, "last_type"):
[249] Fix | Delete
raise ValueError("no last exception")
[250] Fix | Delete
if file is None:
[251] Fix | Delete
file = sys.stderr
[252] Fix | Delete
print_exception(sys.last_type, sys.last_value, sys.last_traceback,
[253] Fix | Delete
limit, file)
[254] Fix | Delete
[255] Fix | Delete
[256] Fix | Delete
def print_stack(f=None, limit=None, file=None):
[257] Fix | Delete
"""Print a stack trace from its invocation point.
[258] Fix | Delete
[259] Fix | Delete
The optional 'f' argument can be used to specify an alternate
[260] Fix | Delete
stack frame at which to start. The optional 'limit' and 'file'
[261] Fix | Delete
arguments have the same meaning as for print_exception().
[262] Fix | Delete
"""
[263] Fix | Delete
if f is None:
[264] Fix | Delete
try:
[265] Fix | Delete
raise ZeroDivisionError
[266] Fix | Delete
except ZeroDivisionError:
[267] Fix | Delete
f = sys.exc_info()[2].tb_frame.f_back
[268] Fix | Delete
print_list(extract_stack(f, limit), file)
[269] Fix | Delete
[270] Fix | Delete
def format_stack(f=None, limit=None):
[271] Fix | Delete
"""Shorthand for 'format_list(extract_stack(f, limit))'."""
[272] Fix | Delete
if f is None:
[273] Fix | Delete
try:
[274] Fix | Delete
raise ZeroDivisionError
[275] Fix | Delete
except ZeroDivisionError:
[276] Fix | Delete
f = sys.exc_info()[2].tb_frame.f_back
[277] Fix | Delete
return format_list(extract_stack(f, limit))
[278] Fix | Delete
[279] Fix | Delete
def extract_stack(f=None, limit = None):
[280] Fix | Delete
"""Extract the raw traceback from the current stack frame.
[281] Fix | Delete
[282] Fix | Delete
The return value has the same format as for extract_tb(). The
[283] Fix | Delete
optional 'f' and 'limit' arguments have the same meaning as for
[284] Fix | Delete
print_stack(). Each item in the list is a quadruple (filename,
[285] Fix | Delete
line number, function name, text), and the entries are in order
[286] Fix | Delete
from oldest to newest stack frame.
[287] Fix | Delete
"""
[288] Fix | Delete
if f is None:
[289] Fix | Delete
try:
[290] Fix | Delete
raise ZeroDivisionError
[291] Fix | Delete
except ZeroDivisionError:
[292] Fix | Delete
f = sys.exc_info()[2].tb_frame.f_back
[293] Fix | Delete
if limit is None:
[294] Fix | Delete
if hasattr(sys, 'tracebacklimit'):
[295] Fix | Delete
limit = sys.tracebacklimit
[296] Fix | Delete
list = []
[297] Fix | Delete
n = 0
[298] Fix | Delete
while f is not None and (limit is None or n < limit):
[299] Fix | Delete
lineno = f.f_lineno
[300] Fix | Delete
co = f.f_code
[301] Fix | Delete
filename = co.co_filename
[302] Fix | Delete
name = co.co_name
[303] Fix | Delete
linecache.checkcache(filename)
[304] Fix | Delete
line = linecache.getline(filename, lineno, f.f_globals)
[305] Fix | Delete
if line: line = line.strip()
[306] Fix | Delete
else: line = None
[307] Fix | Delete
list.append((filename, lineno, name, line))
[308] Fix | Delete
f = f.f_back
[309] Fix | Delete
n = n+1
[310] Fix | Delete
list.reverse()
[311] Fix | Delete
return list
[312] Fix | Delete
[313] Fix | Delete
def tb_lineno(tb):
[314] Fix | Delete
"""Calculate correct line number of traceback given in tb.
[315] Fix | Delete
[316] Fix | Delete
Obsolete in 2.3.
[317] Fix | Delete
"""
[318] Fix | Delete
return tb.tb_lineno
[319] Fix | Delete
[320] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function