0001 import os 0002 import sys 0003 import linecache 0004 0005 from TreeWidget import TreeNode, TreeItem, ScrolledCanvas 0006 from ObjectBrowser import ObjectTreeItem, make_objecttreeitem 0007 0008 def StackBrowser(root, flist=None, tb=None, top=None): 0009 if top is None: 0010 from Tkinter import Toplevel 0011 top = Toplevel(root) 0012 sc = ScrolledCanvas(top, bg="white", highlightthickness=0) 0013 sc.frame.pack(expand=1, fill="both") 0014 item = StackTreeItem(flist, tb) 0015 node = TreeNode(sc.canvas, None, item) 0016 node.expand() 0017 0018 class StackTreeItem(TreeItem): 0019 0020 def __init__(self, flist=None, tb=None): 0021 self.flist = flist 0022 self.stack = self.get_stack(tb) 0023 self.text = self.get_exception() 0024 0025 def get_stack(self, tb): 0026 if tb is None: 0027 tb = sys.last_traceback 0028 stack = [] 0029 if tb and tb.tb_frame is None: 0030 tb = tb.tb_next 0031 while tb is not None: 0032 stack.append((tb.tb_frame, tb.tb_lineno)) 0033 tb = tb.tb_next 0034 return stack 0035 0036 def get_exception(self): 0037 type = sys.last_type 0038 value = sys.last_value 0039 if hasattr(type, "__name__"): 0040 type = type.__name__ 0041 s = str(type) 0042 if value is not None: 0043 s = s + ": " + str(value) 0044 return s 0045 0046 def GetText(self): 0047 return self.text 0048 0049 def GetSubList(self): 0050 sublist = [] 0051 for info in self.stack: 0052 item = FrameTreeItem(info, self.flist) 0053 sublist.append(item) 0054 return sublist 0055 0056 class FrameTreeItem(TreeItem): 0057 0058 def __init__(self, info, flist): 0059 self.info = info 0060 self.flist = flist 0061 0062 def GetText(self): 0063 frame, lineno = self.info 0064 try: 0065 modname = frame.f_globals["__name__"] 0066 except: 0067 modname = "?" 0068 code = frame.f_code 0069 filename = code.co_filename 0070 funcname = code.co_name 0071 sourceline = linecache.getline(filename, lineno) 0072 sourceline = sourceline.strip() 0073 if funcname in ("?", "", None): 0074 item = "%s, line %d: %s" % (modname, lineno, sourceline) 0075 else: 0076 item = "%s.%s(...), line %d: %s" % (modname, funcname, 0077 lineno, sourceline) 0078 return item 0079 0080 def GetSubList(self): 0081 frame, lineno = self.info 0082 sublist = [] 0083 if frame.f_globals is not frame.f_locals: 0084 item = VariablesTreeItem("<locals>", frame.f_locals, self.flist) 0085 sublist.append(item) 0086 item = VariablesTreeItem("<globals>", frame.f_globals, self.flist) 0087 sublist.append(item) 0088 return sublist 0089 0090 def OnDoubleClick(self): 0091 if self.flist: 0092 frame, lineno = self.info 0093 filename = frame.f_code.co_filename 0094 if os.path.isfile(filename): 0095 self.flist.gotofileline(filename, lineno) 0096 0097 class VariablesTreeItem(ObjectTreeItem): 0098 0099 def GetText(self): 0100 return self.labeltext 0101 0102 def GetLabelText(self): 0103 return None 0104 0105 def IsExpandable(self): 0106 return len(self.object) > 0 0107 0108 def keys(self): 0109 return self.object.keys() 0110 0111 def GetSubList(self): 0112 sublist = [] 0113 for key in self.keys(): 0114 try: 0115 value = self.object[key] 0116 except KeyError: 0117 continue 0118 def setfunction(value, key=key, object=self.object): 0119 object[key] = value 0120 item = make_objecttreeitem(key + " =", value, setfunction) 0121 sublist.append(item) 0122 return sublist 0123 0124 0125 def _test(): 0126 try: 0127 import testcode 0128 reload(testcode) 0129 except: 0130 sys.last_type, sys.last_value, sys.last_traceback = sys.exc_info() 0131 from Tkinter import Tk 0132 root = Tk() 0133 StackBrowser(None, top=root) 0134 root.mainloop() 0135 0136 if __name__ == "__main__": 0137 _test() 0138
Generated by PyXR 0.9.4