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