Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../usr/lib64/python2....
File: cgitb.py
"""More comprehensive traceback formatting for Python scripts.
[0] Fix | Delete
[1] Fix | Delete
To enable this module, do:
[2] Fix | Delete
[3] Fix | Delete
import cgitb; cgitb.enable()
[4] Fix | Delete
[5] Fix | Delete
at the top of your script. The optional arguments to enable() are:
[6] Fix | Delete
[7] Fix | Delete
display - if true, tracebacks are displayed in the web browser
[8] Fix | Delete
logdir - if set, tracebacks are written to files in this directory
[9] Fix | Delete
context - number of lines of source code to show for each stack frame
[10] Fix | Delete
format - 'text' or 'html' controls the output format
[11] Fix | Delete
[12] Fix | Delete
By default, tracebacks are displayed but not saved, the context is 5 lines
[13] Fix | Delete
and the output format is 'html' (for backwards compatibility with the
[14] Fix | Delete
original use of this module)
[15] Fix | Delete
[16] Fix | Delete
Alternatively, if you have caught an exception and want cgitb to display it
[17] Fix | Delete
for you, call cgitb.handler(). The optional argument to handler() is a
[18] Fix | Delete
3-item tuple (etype, evalue, etb) just like the value of sys.exc_info().
[19] Fix | Delete
The default handler displays output as HTML.
[20] Fix | Delete
[21] Fix | Delete
"""
[22] Fix | Delete
import inspect
[23] Fix | Delete
import keyword
[24] Fix | Delete
import linecache
[25] Fix | Delete
import os
[26] Fix | Delete
import pydoc
[27] Fix | Delete
import sys
[28] Fix | Delete
import tempfile
[29] Fix | Delete
import time
[30] Fix | Delete
import tokenize
[31] Fix | Delete
import traceback
[32] Fix | Delete
import types
[33] Fix | Delete
[34] Fix | Delete
def reset():
[35] Fix | Delete
"""Return a string that resets the CGI and browser to a known state."""
[36] Fix | Delete
return '''<!--: spam
[37] Fix | Delete
Content-Type: text/html
[38] Fix | Delete
[39] Fix | Delete
<body bgcolor="#f0f0f8"><font color="#f0f0f8" size="-5"> -->
[40] Fix | Delete
<body bgcolor="#f0f0f8"><font color="#f0f0f8" size="-5"> --> -->
[41] Fix | Delete
</font> </font> </font> </script> </object> </blockquote> </pre>
[42] Fix | Delete
</table> </table> </table> </table> </table> </font> </font> </font>'''
[43] Fix | Delete
[44] Fix | Delete
__UNDEF__ = [] # a special sentinel object
[45] Fix | Delete
def small(text):
[46] Fix | Delete
if text:
[47] Fix | Delete
return '<small>' + text + '</small>'
[48] Fix | Delete
else:
[49] Fix | Delete
return ''
[50] Fix | Delete
[51] Fix | Delete
def strong(text):
[52] Fix | Delete
if text:
[53] Fix | Delete
return '<strong>' + text + '</strong>'
[54] Fix | Delete
else:
[55] Fix | Delete
return ''
[56] Fix | Delete
[57] Fix | Delete
def grey(text):
[58] Fix | Delete
if text:
[59] Fix | Delete
return '<font color="#909090">' + text + '</font>'
[60] Fix | Delete
else:
[61] Fix | Delete
return ''
[62] Fix | Delete
[63] Fix | Delete
def lookup(name, frame, locals):
[64] Fix | Delete
"""Find the value for a given name in the given environment."""
[65] Fix | Delete
if name in locals:
[66] Fix | Delete
return 'local', locals[name]
[67] Fix | Delete
if name in frame.f_globals:
[68] Fix | Delete
return 'global', frame.f_globals[name]
[69] Fix | Delete
if '__builtins__' in frame.f_globals:
[70] Fix | Delete
builtins = frame.f_globals['__builtins__']
[71] Fix | Delete
if type(builtins) is type({}):
[72] Fix | Delete
if name in builtins:
[73] Fix | Delete
return 'builtin', builtins[name]
[74] Fix | Delete
else:
[75] Fix | Delete
if hasattr(builtins, name):
[76] Fix | Delete
return 'builtin', getattr(builtins, name)
[77] Fix | Delete
return None, __UNDEF__
[78] Fix | Delete
[79] Fix | Delete
def scanvars(reader, frame, locals):
[80] Fix | Delete
"""Scan one logical line of Python and look up values of variables used."""
[81] Fix | Delete
vars, lasttoken, parent, prefix, value = [], None, None, '', __UNDEF__
[82] Fix | Delete
for ttype, token, start, end, line in tokenize.generate_tokens(reader):
[83] Fix | Delete
if ttype == tokenize.NEWLINE: break
[84] Fix | Delete
if ttype == tokenize.NAME and token not in keyword.kwlist:
[85] Fix | Delete
if lasttoken == '.':
[86] Fix | Delete
if parent is not __UNDEF__:
[87] Fix | Delete
value = getattr(parent, token, __UNDEF__)
[88] Fix | Delete
vars.append((prefix + token, prefix, value))
[89] Fix | Delete
else:
[90] Fix | Delete
where, value = lookup(token, frame, locals)
[91] Fix | Delete
vars.append((token, where, value))
[92] Fix | Delete
elif token == '.':
[93] Fix | Delete
prefix += lasttoken + '.'
[94] Fix | Delete
parent = value
[95] Fix | Delete
else:
[96] Fix | Delete
parent, prefix = None, ''
[97] Fix | Delete
lasttoken = token
[98] Fix | Delete
return vars
[99] Fix | Delete
[100] Fix | Delete
def html(einfo, context=5):
[101] Fix | Delete
"""Return a nice HTML document describing a given traceback."""
[102] Fix | Delete
etype, evalue, etb = einfo
[103] Fix | Delete
if type(etype) is types.ClassType:
[104] Fix | Delete
etype = etype.__name__
[105] Fix | Delete
pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable
[106] Fix | Delete
date = time.ctime(time.time())
[107] Fix | Delete
head = '<body bgcolor="#f0f0f8">' + pydoc.html.heading(
[108] Fix | Delete
'<big><big>%s</big></big>' %
[109] Fix | Delete
strong(pydoc.html.escape(str(etype))),
[110] Fix | Delete
'#ffffff', '#6622aa', pyver + '<br>' + date) + '''
[111] Fix | Delete
<p>A problem occurred in a Python script. Here is the sequence of
[112] Fix | Delete
function calls leading up to the error, in the order they occurred.</p>'''
[113] Fix | Delete
[114] Fix | Delete
indent = '<tt>' + small('&nbsp;' * 5) + '&nbsp;</tt>'
[115] Fix | Delete
frames = []
[116] Fix | Delete
records = inspect.getinnerframes(etb, context)
[117] Fix | Delete
for frame, file, lnum, func, lines, index in records:
[118] Fix | Delete
if file:
[119] Fix | Delete
file = os.path.abspath(file)
[120] Fix | Delete
link = '<a href="file://%s">%s</a>' % (file, pydoc.html.escape(file))
[121] Fix | Delete
else:
[122] Fix | Delete
file = link = '?'
[123] Fix | Delete
args, varargs, varkw, locals = inspect.getargvalues(frame)
[124] Fix | Delete
call = ''
[125] Fix | Delete
if func != '?':
[126] Fix | Delete
call = 'in ' + strong(pydoc.html.escape(func)) + \
[127] Fix | Delete
inspect.formatargvalues(args, varargs, varkw, locals,
[128] Fix | Delete
formatvalue=lambda value: '=' + pydoc.html.repr(value))
[129] Fix | Delete
[130] Fix | Delete
highlight = {}
[131] Fix | Delete
def reader(lnum=[lnum]):
[132] Fix | Delete
highlight[lnum[0]] = 1
[133] Fix | Delete
try: return linecache.getline(file, lnum[0])
[134] Fix | Delete
finally: lnum[0] += 1
[135] Fix | Delete
vars = scanvars(reader, frame, locals)
[136] Fix | Delete
[137] Fix | Delete
rows = ['<tr><td bgcolor="#d8bbff">%s%s %s</td></tr>' %
[138] Fix | Delete
('<big>&nbsp;</big>', link, call)]
[139] Fix | Delete
if index is not None:
[140] Fix | Delete
i = lnum - index
[141] Fix | Delete
for line in lines:
[142] Fix | Delete
num = small('&nbsp;' * (5-len(str(i))) + str(i)) + '&nbsp;'
[143] Fix | Delete
if i in highlight:
[144] Fix | Delete
line = '<tt>=&gt;%s%s</tt>' % (num, pydoc.html.preformat(line))
[145] Fix | Delete
rows.append('<tr><td bgcolor="#ffccee">%s</td></tr>' % line)
[146] Fix | Delete
else:
[147] Fix | Delete
line = '<tt>&nbsp;&nbsp;%s%s</tt>' % (num, pydoc.html.preformat(line))
[148] Fix | Delete
rows.append('<tr><td>%s</td></tr>' % grey(line))
[149] Fix | Delete
i += 1
[150] Fix | Delete
[151] Fix | Delete
done, dump = {}, []
[152] Fix | Delete
for name, where, value in vars:
[153] Fix | Delete
if name in done: continue
[154] Fix | Delete
done[name] = 1
[155] Fix | Delete
if value is not __UNDEF__:
[156] Fix | Delete
if where in ('global', 'builtin'):
[157] Fix | Delete
name = ('<em>%s</em> ' % where) + strong(name)
[158] Fix | Delete
elif where == 'local':
[159] Fix | Delete
name = strong(name)
[160] Fix | Delete
else:
[161] Fix | Delete
name = where + strong(name.split('.')[-1])
[162] Fix | Delete
dump.append('%s&nbsp;= %s' % (name, pydoc.html.repr(value)))
[163] Fix | Delete
else:
[164] Fix | Delete
dump.append(name + ' <em>undefined</em>')
[165] Fix | Delete
[166] Fix | Delete
rows.append('<tr><td>%s</td></tr>' % small(grey(', '.join(dump))))
[167] Fix | Delete
frames.append('''
[168] Fix | Delete
<table width="100%%" cellspacing=0 cellpadding=0 border=0>
[169] Fix | Delete
%s</table>''' % '\n'.join(rows))
[170] Fix | Delete
[171] Fix | Delete
exception = ['<p>%s: %s' % (strong(pydoc.html.escape(str(etype))),
[172] Fix | Delete
pydoc.html.escape(str(evalue)))]
[173] Fix | Delete
if isinstance(evalue, BaseException):
[174] Fix | Delete
for name in dir(evalue):
[175] Fix | Delete
if name[:1] == '_': continue
[176] Fix | Delete
value = pydoc.html.repr(getattr(evalue, name))
[177] Fix | Delete
exception.append('\n<br>%s%s&nbsp;=\n%s' % (indent, name, value))
[178] Fix | Delete
[179] Fix | Delete
return head + ''.join(frames) + ''.join(exception) + '''
[180] Fix | Delete
[181] Fix | Delete
[182] Fix | Delete
<!-- The above is a description of an error in a Python program, formatted
[183] Fix | Delete
for a Web browser because the 'cgitb' module was enabled. In case you
[184] Fix | Delete
are not reading this in a Web browser, here is the original traceback:
[185] Fix | Delete
[186] Fix | Delete
%s
[187] Fix | Delete
-->
[188] Fix | Delete
''' % pydoc.html.escape(
[189] Fix | Delete
''.join(traceback.format_exception(etype, evalue, etb)))
[190] Fix | Delete
[191] Fix | Delete
def text(einfo, context=5):
[192] Fix | Delete
"""Return a plain text document describing a given traceback."""
[193] Fix | Delete
etype, evalue, etb = einfo
[194] Fix | Delete
if type(etype) is types.ClassType:
[195] Fix | Delete
etype = etype.__name__
[196] Fix | Delete
pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable
[197] Fix | Delete
date = time.ctime(time.time())
[198] Fix | Delete
head = "%s\n%s\n%s\n" % (str(etype), pyver, date) + '''
[199] Fix | Delete
A problem occurred in a Python script. Here is the sequence of
[200] Fix | Delete
function calls leading up to the error, in the order they occurred.
[201] Fix | Delete
'''
[202] Fix | Delete
[203] Fix | Delete
frames = []
[204] Fix | Delete
records = inspect.getinnerframes(etb, context)
[205] Fix | Delete
for frame, file, lnum, func, lines, index in records:
[206] Fix | Delete
file = file and os.path.abspath(file) or '?'
[207] Fix | Delete
args, varargs, varkw, locals = inspect.getargvalues(frame)
[208] Fix | Delete
call = ''
[209] Fix | Delete
if func != '?':
[210] Fix | Delete
call = 'in ' + func + \
[211] Fix | Delete
inspect.formatargvalues(args, varargs, varkw, locals,
[212] Fix | Delete
formatvalue=lambda value: '=' + pydoc.text.repr(value))
[213] Fix | Delete
[214] Fix | Delete
highlight = {}
[215] Fix | Delete
def reader(lnum=[lnum]):
[216] Fix | Delete
highlight[lnum[0]] = 1
[217] Fix | Delete
try: return linecache.getline(file, lnum[0])
[218] Fix | Delete
finally: lnum[0] += 1
[219] Fix | Delete
vars = scanvars(reader, frame, locals)
[220] Fix | Delete
[221] Fix | Delete
rows = [' %s %s' % (file, call)]
[222] Fix | Delete
if index is not None:
[223] Fix | Delete
i = lnum - index
[224] Fix | Delete
for line in lines:
[225] Fix | Delete
num = '%5d ' % i
[226] Fix | Delete
rows.append(num+line.rstrip())
[227] Fix | Delete
i += 1
[228] Fix | Delete
[229] Fix | Delete
done, dump = {}, []
[230] Fix | Delete
for name, where, value in vars:
[231] Fix | Delete
if name in done: continue
[232] Fix | Delete
done[name] = 1
[233] Fix | Delete
if value is not __UNDEF__:
[234] Fix | Delete
if where == 'global': name = 'global ' + name
[235] Fix | Delete
elif where != 'local': name = where + name.split('.')[-1]
[236] Fix | Delete
dump.append('%s = %s' % (name, pydoc.text.repr(value)))
[237] Fix | Delete
else:
[238] Fix | Delete
dump.append(name + ' undefined')
[239] Fix | Delete
[240] Fix | Delete
rows.append('\n'.join(dump))
[241] Fix | Delete
frames.append('\n%s\n' % '\n'.join(rows))
[242] Fix | Delete
[243] Fix | Delete
exception = ['%s: %s' % (str(etype), str(evalue))]
[244] Fix | Delete
if isinstance(evalue, BaseException):
[245] Fix | Delete
for name in dir(evalue):
[246] Fix | Delete
value = pydoc.text.repr(getattr(evalue, name))
[247] Fix | Delete
exception.append('\n%s%s = %s' % (" "*4, name, value))
[248] Fix | Delete
[249] Fix | Delete
return head + ''.join(frames) + ''.join(exception) + '''
[250] Fix | Delete
[251] Fix | Delete
The above is a description of an error in a Python program. Here is
[252] Fix | Delete
the original traceback:
[253] Fix | Delete
[254] Fix | Delete
%s
[255] Fix | Delete
''' % ''.join(traceback.format_exception(etype, evalue, etb))
[256] Fix | Delete
[257] Fix | Delete
class Hook:
[258] Fix | Delete
"""A hook to replace sys.excepthook that shows tracebacks in HTML."""
[259] Fix | Delete
[260] Fix | Delete
def __init__(self, display=1, logdir=None, context=5, file=None,
[261] Fix | Delete
format="html"):
[262] Fix | Delete
self.display = display # send tracebacks to browser if true
[263] Fix | Delete
self.logdir = logdir # log tracebacks to files if not None
[264] Fix | Delete
self.context = context # number of source code lines per frame
[265] Fix | Delete
self.file = file or sys.stdout # place to send the output
[266] Fix | Delete
self.format = format
[267] Fix | Delete
[268] Fix | Delete
def __call__(self, etype, evalue, etb):
[269] Fix | Delete
self.handle((etype, evalue, etb))
[270] Fix | Delete
[271] Fix | Delete
def handle(self, info=None):
[272] Fix | Delete
info = info or sys.exc_info()
[273] Fix | Delete
if self.format == "html":
[274] Fix | Delete
self.file.write(reset())
[275] Fix | Delete
[276] Fix | Delete
formatter = (self.format=="html") and html or text
[277] Fix | Delete
plain = False
[278] Fix | Delete
try:
[279] Fix | Delete
doc = formatter(info, self.context)
[280] Fix | Delete
except: # just in case something goes wrong
[281] Fix | Delete
doc = ''.join(traceback.format_exception(*info))
[282] Fix | Delete
plain = True
[283] Fix | Delete
[284] Fix | Delete
if self.display:
[285] Fix | Delete
if plain:
[286] Fix | Delete
doc = pydoc.html.escape(doc)
[287] Fix | Delete
self.file.write('<pre>' + doc + '</pre>\n')
[288] Fix | Delete
else:
[289] Fix | Delete
self.file.write(doc + '\n')
[290] Fix | Delete
else:
[291] Fix | Delete
self.file.write('<p>A problem occurred in a Python script.\n')
[292] Fix | Delete
[293] Fix | Delete
if self.logdir is not None:
[294] Fix | Delete
suffix = ['.txt', '.html'][self.format=="html"]
[295] Fix | Delete
(fd, path) = tempfile.mkstemp(suffix=suffix, dir=self.logdir)
[296] Fix | Delete
[297] Fix | Delete
try:
[298] Fix | Delete
file = os.fdopen(fd, 'w')
[299] Fix | Delete
file.write(doc)
[300] Fix | Delete
file.close()
[301] Fix | Delete
msg = '%s contains the description of this error.' % path
[302] Fix | Delete
except:
[303] Fix | Delete
msg = 'Tried to save traceback to %s, but failed.' % path
[304] Fix | Delete
[305] Fix | Delete
if self.format == 'html':
[306] Fix | Delete
self.file.write('<p>%s</p>\n' % msg)
[307] Fix | Delete
else:
[308] Fix | Delete
self.file.write(msg + '\n')
[309] Fix | Delete
try:
[310] Fix | Delete
self.file.flush()
[311] Fix | Delete
except: pass
[312] Fix | Delete
[313] Fix | Delete
handler = Hook().handle
[314] Fix | Delete
def enable(display=1, logdir=None, context=5, format="html"):
[315] Fix | Delete
"""Install an exception handler that formats tracebacks as HTML.
[316] Fix | Delete
[317] Fix | Delete
The optional argument 'display' can be set to 0 to suppress sending the
[318] Fix | Delete
traceback to the browser, and 'logdir' can be set to a directory to cause
[319] Fix | Delete
tracebacks to be written to files there."""
[320] Fix | Delete
sys.excepthook = Hook(display=display, logdir=logdir,
[321] Fix | Delete
context=context, format=format)
[322] Fix | Delete
[323] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function