from idlelib.TreeWidget import TreeNode, TreeItem, ScrolledCanvas
from idlelib.ObjectBrowser import ObjectTreeItem, make_objecttreeitem
from idlelib.PyShell import PyShellFileList
def StackBrowser(root, flist=None, tb=None, top=None):
sc = ScrolledCanvas(top, bg="white", highlightthickness=0)
sc.frame.pack(expand=1, fill="both")
item = StackTreeItem(flist, tb)
node = TreeNode(sc.canvas, None, item)
class StackTreeItem(TreeItem):
def __init__(self, flist=None, tb=None):
self.stack = self.get_stack(tb)
self.text = self.get_exception()
if tb and tb.tb_frame is None:
stack.append((tb.tb_frame, tb.tb_lineno))
if hasattr(type, "__name__"):
s = s + ": " + str(value)
item = FrameTreeItem(info, self.flist)
class FrameTreeItem(TreeItem):
def __init__(self, info, flist):
frame, lineno = self.info
modname = frame.f_globals["__name__"]
filename = code.co_filename
sourceline = linecache.getline(filename, lineno)
sourceline = sourceline.strip()
if funcname in ("?", "", None):
item = "%s, line %d: %s" % (modname, lineno, sourceline)
item = "%s.%s(...), line %d: %s" % (modname, funcname,
frame, lineno = self.info
if frame.f_globals is not frame.f_locals:
item = VariablesTreeItem("<locals>", frame.f_locals, self.flist)
item = VariablesTreeItem("<globals>", frame.f_globals, self.flist)
frame, lineno = self.info
filename = frame.f_code.co_filename
if os.path.isfile(filename):
self.flist.gotofileline(filename, lineno)
class VariablesTreeItem(ObjectTreeItem):
return len(self.object) > 0
for key in self.object.keys():
def setfunction(value, key=key, object=self.object):
item = make_objecttreeitem(key + " =", value, setfunction)
def keys(self): # unused, left for possible 3rd party use
return self.object.keys()
def _stack_viewer(parent): # htest #
root.title("Test StackViewer")
width, height, x, y = list(map(int, re.split('[x+]', parent.geometry())))
root.geometry("+%d+%d"%(x, y + 150))
flist = PyShellFileList(root)
try: # to obtain a traceback object
exc_type, exc_value, exc_tb = sys.exc_info()
# inject stack trace to sys
sys.last_value = exc_value
sys.last_traceback = exc_tb
StackBrowser(root, flist=flist, top=root, tb=exc_tb)
# restore sys to original state
if __name__ == '__main__':
from idlelib.idle_test.htest import run