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