David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame^] | 1 | import string |
| 2 | from Tkinter import * |
| 3 | import linecache |
| 4 | |
| 5 | from TreeWidget import TreeNode, TreeItem, ScrolledCanvas |
| 6 | from ObjectBrowser import ObjectTreeItem, make_objecttreeitem |
| 7 | from OldStackViewer import StackViewer, NamespaceViewer |
| 8 | |
| 9 | def StackBrowser(root, flist=None, stack=None): |
| 10 | top = Toplevel(root) |
| 11 | sc = ScrolledCanvas(top, bg="white", highlightthickness=0) |
| 12 | sc.frame.pack(expand=1, fill="both") |
| 13 | item = StackTreeItem(flist) |
| 14 | node = TreeNode(sc.canvas, None, item) |
| 15 | node.expand() |
| 16 | |
| 17 | class StackTreeItem(TreeItem): |
| 18 | |
| 19 | def __init__(self, flist=None): |
| 20 | self.flist = flist |
| 21 | self.stack = get_stack() |
| 22 | self.text = get_exception() |
| 23 | |
| 24 | def GetText(self): |
| 25 | return self.text |
| 26 | |
| 27 | def GetSubList(self): |
| 28 | sublist = [] |
| 29 | for info in self.stack: |
| 30 | item = FrameTreeItem(info, self.flist) |
| 31 | sublist.append(item) |
| 32 | return sublist |
| 33 | |
| 34 | class FrameTreeItem(TreeItem): |
| 35 | |
| 36 | def __init__(self, info, flist): |
| 37 | self.info = info |
| 38 | self.flist = flist |
| 39 | |
| 40 | def GetText(self): |
| 41 | frame, lineno = self.info |
| 42 | try: |
| 43 | modname = frame.f_globals["__name__"] |
| 44 | except: |
| 45 | modname = "?" |
| 46 | code = frame.f_code |
| 47 | filename = code.co_filename |
| 48 | funcname = code.co_name |
| 49 | sourceline = linecache.getline(filename, lineno) |
| 50 | sourceline = string.strip(sourceline) |
| 51 | if funcname in ("?", "", None): |
| 52 | item = "%s, line %d: %s" % (modname, lineno, sourceline) |
| 53 | else: |
| 54 | item = "%s.%s(...), line %d: %s" % (modname, funcname, |
| 55 | lineno, sourceline) |
| 56 | ## if i == index: |
| 57 | ## item = "> " + item |
| 58 | return item |
| 59 | |
| 60 | def GetSubList(self): |
| 61 | frame, lineno = self.info |
| 62 | sublist = [] |
| 63 | if frame.f_globals is not frame.f_locals: |
| 64 | item = VariablesTreeItem("<locals>", frame.f_locals, self.flist) |
| 65 | sublist.append(item) |
| 66 | item = VariablesTreeItem("<globals>", frame.f_globals, self.flist) |
| 67 | sublist.append(item) |
| 68 | return sublist |
| 69 | |
| 70 | def OnDoubleClick(self): |
| 71 | if self.flist: |
| 72 | frame, lineno = self.info |
| 73 | filename = frame.f_code.co_filename |
| 74 | edit = self.flist.open(filename) |
| 75 | edit.gotoline(lineno) |
| 76 | |
| 77 | class VariablesTreeItem(ObjectTreeItem): |
| 78 | |
| 79 | def GetText(self): |
| 80 | return self.labeltext |
| 81 | |
| 82 | def GetLabelText(self): |
| 83 | return None |
| 84 | |
| 85 | def IsExpandable(self): |
| 86 | return len(self.object) > 0 |
| 87 | |
| 88 | def keys(self): |
| 89 | return self.object.keys() |
| 90 | |
| 91 | def GetSubList(self): |
| 92 | sublist = [] |
| 93 | for key in self.keys(): |
| 94 | try: |
| 95 | value = self.object[key] |
| 96 | except KeyError: |
| 97 | continue |
| 98 | def setfunction(value, key=key, object=self.object): |
| 99 | object[key] = value |
| 100 | item = make_objecttreeitem(key + " =", value, setfunction) |
| 101 | sublist.append(item) |
| 102 | return sublist |
| 103 | |
| 104 | def get_stack(t=None, f=None): |
| 105 | if t is None: |
| 106 | t = sys.last_traceback |
| 107 | stack = [] |
| 108 | if t and t.tb_frame is f: |
| 109 | t = t.tb_next |
| 110 | while f is not None: |
| 111 | stack.append((f, f.f_lineno)) |
| 112 | if f is self.botframe: |
| 113 | break |
| 114 | f = f.f_back |
| 115 | stack.reverse() |
| 116 | while t is not None: |
| 117 | stack.append((t.tb_frame, t.tb_lineno)) |
| 118 | t = t.tb_next |
| 119 | return stack |
| 120 | |
| 121 | def get_exception(type=None, value=None): |
| 122 | if type is None: |
| 123 | type = sys.last_type |
| 124 | value = sys.last_value |
| 125 | if hasattr(type, "__name__"): |
| 126 | type = type.__name__ |
| 127 | s = str(type) |
| 128 | if value is not None: |
| 129 | s = s + ": " + str(value) |
| 130 | return s |
| 131 | |
| 132 | if __name__ == "__main__": |
| 133 | root = Tk() |
| 134 | root.withdraw() |
| 135 | StackBrowser(root) |