Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2..../idlelib
File: StackViewer.py
import os
[0] Fix | Delete
import sys
[1] Fix | Delete
import linecache
[2] Fix | Delete
import re
[3] Fix | Delete
import Tkinter as tk
[4] Fix | Delete
[5] Fix | Delete
from idlelib.TreeWidget import TreeNode, TreeItem, ScrolledCanvas
[6] Fix | Delete
from idlelib.ObjectBrowser import ObjectTreeItem, make_objecttreeitem
[7] Fix | Delete
from idlelib.PyShell import PyShellFileList
[8] Fix | Delete
[9] Fix | Delete
def StackBrowser(root, flist=None, tb=None, top=None):
[10] Fix | Delete
if top is None:
[11] Fix | Delete
top = tk.Toplevel(root)
[12] Fix | Delete
sc = ScrolledCanvas(top, bg="white", highlightthickness=0)
[13] Fix | Delete
sc.frame.pack(expand=1, fill="both")
[14] Fix | Delete
item = StackTreeItem(flist, tb)
[15] Fix | Delete
node = TreeNode(sc.canvas, None, item)
[16] Fix | Delete
node.expand()
[17] Fix | Delete
[18] Fix | Delete
class StackTreeItem(TreeItem):
[19] Fix | Delete
[20] Fix | Delete
def __init__(self, flist=None, tb=None):
[21] Fix | Delete
self.flist = flist
[22] Fix | Delete
self.stack = self.get_stack(tb)
[23] Fix | Delete
self.text = self.get_exception()
[24] Fix | Delete
[25] Fix | Delete
def get_stack(self, tb):
[26] Fix | Delete
if tb is None:
[27] Fix | Delete
tb = sys.last_traceback
[28] Fix | Delete
stack = []
[29] Fix | Delete
if tb and tb.tb_frame is None:
[30] Fix | Delete
tb = tb.tb_next
[31] Fix | Delete
while tb is not None:
[32] Fix | Delete
stack.append((tb.tb_frame, tb.tb_lineno))
[33] Fix | Delete
tb = tb.tb_next
[34] Fix | Delete
return stack
[35] Fix | Delete
[36] Fix | Delete
def get_exception(self):
[37] Fix | Delete
type = sys.last_type
[38] Fix | Delete
value = sys.last_value
[39] Fix | Delete
if hasattr(type, "__name__"):
[40] Fix | Delete
type = type.__name__
[41] Fix | Delete
s = str(type)
[42] Fix | Delete
if value is not None:
[43] Fix | Delete
s = s + ": " + str(value)
[44] Fix | Delete
return s
[45] Fix | Delete
[46] Fix | Delete
def GetText(self):
[47] Fix | Delete
return self.text
[48] Fix | Delete
[49] Fix | Delete
def GetSubList(self):
[50] Fix | Delete
sublist = []
[51] Fix | Delete
for info in self.stack:
[52] Fix | Delete
item = FrameTreeItem(info, self.flist)
[53] Fix | Delete
sublist.append(item)
[54] Fix | Delete
return sublist
[55] Fix | Delete
[56] Fix | Delete
class FrameTreeItem(TreeItem):
[57] Fix | Delete
[58] Fix | Delete
def __init__(self, info, flist):
[59] Fix | Delete
self.info = info
[60] Fix | Delete
self.flist = flist
[61] Fix | Delete
[62] Fix | Delete
def GetText(self):
[63] Fix | Delete
frame, lineno = self.info
[64] Fix | Delete
try:
[65] Fix | Delete
modname = frame.f_globals["__name__"]
[66] Fix | Delete
except:
[67] Fix | Delete
modname = "?"
[68] Fix | Delete
code = frame.f_code
[69] Fix | Delete
filename = code.co_filename
[70] Fix | Delete
funcname = code.co_name
[71] Fix | Delete
sourceline = linecache.getline(filename, lineno)
[72] Fix | Delete
sourceline = sourceline.strip()
[73] Fix | Delete
if funcname in ("?", "", None):
[74] Fix | Delete
item = "%s, line %d: %s" % (modname, lineno, sourceline)
[75] Fix | Delete
else:
[76] Fix | Delete
item = "%s.%s(...), line %d: %s" % (modname, funcname,
[77] Fix | Delete
lineno, sourceline)
[78] Fix | Delete
return item
[79] Fix | Delete
[80] Fix | Delete
def GetSubList(self):
[81] Fix | Delete
frame, lineno = self.info
[82] Fix | Delete
sublist = []
[83] Fix | Delete
if frame.f_globals is not frame.f_locals:
[84] Fix | Delete
item = VariablesTreeItem("<locals>", frame.f_locals, self.flist)
[85] Fix | Delete
sublist.append(item)
[86] Fix | Delete
item = VariablesTreeItem("<globals>", frame.f_globals, self.flist)
[87] Fix | Delete
sublist.append(item)
[88] Fix | Delete
return sublist
[89] Fix | Delete
[90] Fix | Delete
def OnDoubleClick(self):
[91] Fix | Delete
if self.flist:
[92] Fix | Delete
frame, lineno = self.info
[93] Fix | Delete
filename = frame.f_code.co_filename
[94] Fix | Delete
if os.path.isfile(filename):
[95] Fix | Delete
self.flist.gotofileline(filename, lineno)
[96] Fix | Delete
[97] Fix | Delete
class VariablesTreeItem(ObjectTreeItem):
[98] Fix | Delete
[99] Fix | Delete
def GetText(self):
[100] Fix | Delete
return self.labeltext
[101] Fix | Delete
[102] Fix | Delete
def GetLabelText(self):
[103] Fix | Delete
return None
[104] Fix | Delete
[105] Fix | Delete
def IsExpandable(self):
[106] Fix | Delete
return len(self.object) > 0
[107] Fix | Delete
[108] Fix | Delete
def GetSubList(self):
[109] Fix | Delete
sublist = []
[110] Fix | Delete
for key in self.object.keys():
[111] Fix | Delete
try:
[112] Fix | Delete
value = self.object[key]
[113] Fix | Delete
except KeyError:
[114] Fix | Delete
continue
[115] Fix | Delete
def setfunction(value, key=key, object=self.object):
[116] Fix | Delete
object[key] = value
[117] Fix | Delete
item = make_objecttreeitem(key + " =", value, setfunction)
[118] Fix | Delete
sublist.append(item)
[119] Fix | Delete
return sublist
[120] Fix | Delete
[121] Fix | Delete
def keys(self): # unused, left for possible 3rd party use
[122] Fix | Delete
return self.object.keys()
[123] Fix | Delete
[124] Fix | Delete
def _stack_viewer(parent): # htest #
[125] Fix | Delete
root = tk.Tk()
[126] Fix | Delete
root.title("Test StackViewer")
[127] Fix | Delete
width, height, x, y = list(map(int, re.split('[x+]', parent.geometry())))
[128] Fix | Delete
root.geometry("+%d+%d"%(x, y + 150))
[129] Fix | Delete
flist = PyShellFileList(root)
[130] Fix | Delete
try: # to obtain a traceback object
[131] Fix | Delete
intentional_name_error
[132] Fix | Delete
except NameError:
[133] Fix | Delete
exc_type, exc_value, exc_tb = sys.exc_info()
[134] Fix | Delete
[135] Fix | Delete
# inject stack trace to sys
[136] Fix | Delete
sys.last_type = exc_type
[137] Fix | Delete
sys.last_value = exc_value
[138] Fix | Delete
sys.last_traceback = exc_tb
[139] Fix | Delete
[140] Fix | Delete
StackBrowser(root, flist=flist, top=root, tb=exc_tb)
[141] Fix | Delete
[142] Fix | Delete
# restore sys to original state
[143] Fix | Delete
del sys.last_type
[144] Fix | Delete
del sys.last_value
[145] Fix | Delete
del sys.last_traceback
[146] Fix | Delete
[147] Fix | Delete
if __name__ == '__main__':
[148] Fix | Delete
from idlelib.idle_test.htest import run
[149] Fix | Delete
run(_stack_viewer)
[150] Fix | Delete
[151] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function