Guido van Rossum | 2bb8e72 | 2000-02-15 18:04:09 +0000 | [diff] [blame] | 1 | import sys |
| 2 | import os |
| 3 | from Tkinter import * |
| 4 | import linecache |
| 5 | from repr import Repr |
| 6 | from WindowList import ListedToplevel |
| 7 | |
| 8 | from ScrolledList import ScrolledList |
| 9 | |
| 10 | |
| 11 | class StackBrowser: |
| 12 | |
| 13 | def __init__(self, root, flist, stack=None): |
| 14 | self.top = top = ListedToplevel(root) |
| 15 | top.protocol("WM_DELETE_WINDOW", self.close) |
| 16 | top.bind("<Key-Escape>", self.close) |
| 17 | top.wm_title("Stack viewer") |
| 18 | top.wm_iconname("Stack") |
| 19 | # Create help label |
| 20 | self.helplabel = Label(top, |
| 21 | text="Click once to view variables; twice for source", |
| 22 | borderwidth=2, relief="groove") |
| 23 | self.helplabel.pack(fill="x") |
| 24 | # |
| 25 | self.sv = StackViewer(top, flist, self) |
| 26 | if stack is None: |
| 27 | stack = get_stack() |
| 28 | self.sv.load_stack(stack) |
| 29 | |
| 30 | def close(self, event=None): |
| 31 | self.top.destroy() |
| 32 | |
| 33 | localsframe = None |
| 34 | localsviewer = None |
| 35 | localsdict = None |
| 36 | globalsframe = None |
| 37 | globalsviewer = None |
| 38 | globalsdict = None |
| 39 | curframe = None |
| 40 | |
| 41 | def show_frame(self, (frame, lineno)): |
| 42 | if frame is self.curframe: |
| 43 | return |
| 44 | self.curframe = None |
| 45 | if frame.f_globals is not self.globalsdict: |
| 46 | self.show_globals(frame) |
| 47 | self.show_locals(frame) |
| 48 | self.curframe = frame |
| 49 | |
| 50 | def show_globals(self, frame): |
| 51 | title = "Global Variables" |
| 52 | if frame.f_globals.has_key("__name__"): |
| 53 | try: |
| 54 | name = str(frame.f_globals["__name__"]) + "" |
| 55 | except: |
| 56 | name = "" |
| 57 | if name: |
| 58 | title = title + " in module " + name |
| 59 | self.globalsdict = None |
| 60 | if self.globalsviewer: |
| 61 | self.globalsviewer.close() |
| 62 | self.globalsviewer = None |
| 63 | if not self.globalsframe: |
| 64 | self.globalsframe = Frame(self.top) |
| 65 | self.globalsdict = frame.f_globals |
| 66 | self.globalsviewer = NamespaceViewer( |
| 67 | self.globalsframe, |
| 68 | title, |
| 69 | self.globalsdict) |
| 70 | self.globalsframe.pack(fill="both", side="bottom") |
| 71 | |
| 72 | def show_locals(self, frame): |
| 73 | self.localsdict = None |
| 74 | if self.localsviewer: |
| 75 | self.localsviewer.close() |
| 76 | self.localsviewer = None |
| 77 | if frame.f_locals is not frame.f_globals: |
| 78 | title = "Local Variables" |
| 79 | code = frame.f_code |
| 80 | funcname = code.co_name |
| 81 | if funcname not in ("?", "", None): |
| 82 | title = title + " in " + funcname |
| 83 | if not self.localsframe: |
| 84 | self.localsframe = Frame(self.top) |
| 85 | self.localsdict = frame.f_locals |
| 86 | self.localsviewer = NamespaceViewer( |
| 87 | self.localsframe, |
| 88 | title, |
| 89 | self.localsdict) |
| 90 | self.localsframe.pack(fill="both", side="top") |
| 91 | else: |
| 92 | if self.localsframe: |
| 93 | self.localsframe.forget() |
| 94 | |
| 95 | |
| 96 | class StackViewer(ScrolledList): |
| 97 | |
| 98 | def __init__(self, master, flist, browser): |
| 99 | ScrolledList.__init__(self, master, width=80) |
| 100 | self.flist = flist |
| 101 | self.browser = browser |
| 102 | self.stack = [] |
| 103 | |
| 104 | def load_stack(self, stack, index=None): |
| 105 | self.stack = stack |
| 106 | self.clear() |
| 107 | ## if len(stack) > 10: |
| 108 | ## l["height"] = 10 |
| 109 | ## self.topframe.pack(expand=1) |
| 110 | ## else: |
| 111 | ## l["height"] = len(stack) |
| 112 | ## self.topframe.pack(expand=0) |
| 113 | for i in range(len(stack)): |
| 114 | frame, lineno = stack[i] |
| 115 | try: |
| 116 | modname = frame.f_globals["__name__"] |
| 117 | except: |
| 118 | modname = "?" |
| 119 | code = frame.f_code |
| 120 | filename = code.co_filename |
| 121 | funcname = code.co_name |
| 122 | sourceline = linecache.getline(filename, lineno) |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 123 | sourceline = sourceline.strip() |
Guido van Rossum | 2bb8e72 | 2000-02-15 18:04:09 +0000 | [diff] [blame] | 124 | if funcname in ("?", "", None): |
| 125 | item = "%s, line %d: %s" % (modname, lineno, sourceline) |
| 126 | else: |
| 127 | item = "%s.%s(), line %d: %s" % (modname, funcname, |
| 128 | lineno, sourceline) |
| 129 | if i == index: |
| 130 | item = "> " + item |
| 131 | self.append(item) |
| 132 | if index is not None: |
| 133 | self.select(index) |
| 134 | |
| 135 | def popup_event(self, event): |
| 136 | if self.stack: |
| 137 | return ScrolledList.popup_event(self, event) |
| 138 | |
| 139 | def fill_menu(self): |
| 140 | menu = self.menu |
| 141 | menu.add_command(label="Go to source line", |
| 142 | command=self.goto_source_line) |
| 143 | menu.add_command(label="Show stack frame", |
| 144 | command=self.show_stack_frame) |
| 145 | |
| 146 | def on_select(self, index): |
| 147 | if 0 <= index < len(self.stack): |
| 148 | self.browser.show_frame(self.stack[index]) |
| 149 | |
| 150 | def on_double(self, index): |
| 151 | self.show_source(index) |
| 152 | |
| 153 | def goto_source_line(self): |
| 154 | index = self.listbox.index("active") |
| 155 | self.show_source(index) |
| 156 | |
| 157 | def show_stack_frame(self): |
| 158 | index = self.listbox.index("active") |
| 159 | if 0 <= index < len(self.stack): |
| 160 | self.browser.show_frame(self.stack[index]) |
| 161 | |
| 162 | def show_source(self, index): |
| 163 | if not (0 <= index < len(self.stack)): |
| 164 | return |
| 165 | frame, lineno = self.stack[index] |
| 166 | code = frame.f_code |
| 167 | filename = code.co_filename |
| 168 | if os.path.isfile(filename): |
| 169 | edit = self.flist.open(filename) |
| 170 | if edit: |
| 171 | edit.gotoline(lineno) |
| 172 | |
| 173 | |
| 174 | def get_stack(t=None, f=None): |
| 175 | if t is None: |
| 176 | t = sys.last_traceback |
| 177 | stack = [] |
| 178 | if t and t.tb_frame is f: |
| 179 | t = t.tb_next |
| 180 | while f is not None: |
| 181 | stack.append((f, f.f_lineno)) |
| 182 | if f is self.botframe: |
| 183 | break |
| 184 | f = f.f_back |
| 185 | stack.reverse() |
| 186 | while t is not None: |
| 187 | stack.append((t.tb_frame, t.tb_lineno)) |
| 188 | t = t.tb_next |
| 189 | return stack |
| 190 | |
| 191 | |
| 192 | def getexception(type=None, value=None): |
| 193 | if type is None: |
| 194 | type = sys.last_type |
| 195 | value = sys.last_value |
| 196 | if hasattr(type, "__name__"): |
| 197 | type = type.__name__ |
| 198 | s = str(type) |
| 199 | if value is not None: |
| 200 | s = s + ": " + str(value) |
| 201 | return s |
| 202 | |
| 203 | |
| 204 | class NamespaceViewer: |
| 205 | |
| 206 | def __init__(self, master, title, dict=None): |
| 207 | width = 0 |
| 208 | height = 40 |
| 209 | if dict: |
| 210 | height = 20*len(dict) # XXX 20 == observed height of Entry widget |
| 211 | self.master = master |
| 212 | self.title = title |
| 213 | self.repr = Repr() |
| 214 | self.repr.maxstring = 60 |
| 215 | self.repr.maxother = 60 |
| 216 | self.frame = frame = Frame(master) |
| 217 | self.frame.pack(expand=1, fill="both") |
| 218 | self.label = Label(frame, text=title, borderwidth=2, relief="groove") |
| 219 | self.label.pack(fill="x") |
| 220 | self.vbar = vbar = Scrollbar(frame, name="vbar") |
| 221 | vbar.pack(side="right", fill="y") |
| 222 | self.canvas = canvas = Canvas(frame, |
| 223 | height=min(300, max(40, height)), |
| 224 | scrollregion=(0, 0, width, height)) |
| 225 | canvas.pack(side="left", fill="both", expand=1) |
| 226 | vbar["command"] = canvas.yview |
| 227 | canvas["yscrollcommand"] = vbar.set |
| 228 | self.subframe = subframe = Frame(canvas) |
| 229 | self.sfid = canvas.create_window(0, 0, window=subframe, anchor="nw") |
| 230 | self.load_dict(dict) |
| 231 | |
| 232 | dict = -1 |
| 233 | |
| 234 | def load_dict(self, dict, force=0): |
| 235 | if dict is self.dict and not force: |
| 236 | return |
| 237 | subframe = self.subframe |
| 238 | frame = self.frame |
| 239 | for c in subframe.children.values(): |
| 240 | c.destroy() |
| 241 | self.dict = None |
| 242 | if not dict: |
| 243 | l = Label(subframe, text="None") |
| 244 | l.grid(row=0, column=0) |
| 245 | else: |
| 246 | names = dict.keys() |
| 247 | names.sort() |
| 248 | row = 0 |
| 249 | for name in names: |
| 250 | value = dict[name] |
| 251 | svalue = self.repr.repr(value) # repr(value) |
| 252 | l = Label(subframe, text=name) |
| 253 | l.grid(row=row, column=0, sticky="nw") |
| 254 | ## l = Label(subframe, text=svalue, justify="l", wraplength=300) |
| 255 | l = Entry(subframe, width=0, borderwidth=0) |
| 256 | l.insert(0, svalue) |
| 257 | ## l["state"] = "disabled" |
| 258 | l.grid(row=row, column=1, sticky="nw") |
| 259 | row = row+1 |
| 260 | self.dict = dict |
| 261 | # XXX Could we use a <Configure> callback for the following? |
| 262 | subframe.update_idletasks() # Alas! |
| 263 | width = subframe.winfo_reqwidth() |
| 264 | height = subframe.winfo_reqheight() |
| 265 | canvas = self.canvas |
| 266 | self.canvas["scrollregion"] = (0, 0, width, height) |
| 267 | if height > 300: |
| 268 | canvas["height"] = 300 |
| 269 | frame.pack(expand=1) |
| 270 | else: |
| 271 | canvas["height"] = height |
| 272 | frame.pack(expand=0) |
| 273 | |
| 274 | def close(self): |
| 275 | self.frame.destroy() |