1 | n/a | import linecache |
---|
2 | n/a | import os |
---|
3 | n/a | import sys |
---|
4 | n/a | |
---|
5 | n/a | import tkinter as tk |
---|
6 | n/a | |
---|
7 | n/a | from idlelib.debugobj import ObjectTreeItem, make_objecttreeitem |
---|
8 | n/a | from idlelib.tree import TreeNode, TreeItem, ScrolledCanvas |
---|
9 | n/a | |
---|
10 | n/a | def StackBrowser(root, flist=None, tb=None, top=None): |
---|
11 | n/a | if top is None: |
---|
12 | n/a | top = tk.Toplevel(root) |
---|
13 | n/a | sc = ScrolledCanvas(top, bg="white", highlightthickness=0) |
---|
14 | n/a | sc.frame.pack(expand=1, fill="both") |
---|
15 | n/a | item = StackTreeItem(flist, tb) |
---|
16 | n/a | node = TreeNode(sc.canvas, None, item) |
---|
17 | n/a | node.expand() |
---|
18 | n/a | |
---|
19 | n/a | |
---|
20 | n/a | class StackTreeItem(TreeItem): |
---|
21 | n/a | |
---|
22 | n/a | def __init__(self, flist=None, tb=None): |
---|
23 | n/a | self.flist = flist |
---|
24 | n/a | self.stack = self.get_stack(tb) |
---|
25 | n/a | self.text = self.get_exception() |
---|
26 | n/a | |
---|
27 | n/a | def get_stack(self, tb): |
---|
28 | n/a | if tb is None: |
---|
29 | n/a | tb = sys.last_traceback |
---|
30 | n/a | stack = [] |
---|
31 | n/a | if tb and tb.tb_frame is None: |
---|
32 | n/a | tb = tb.tb_next |
---|
33 | n/a | while tb is not None: |
---|
34 | n/a | stack.append((tb.tb_frame, tb.tb_lineno)) |
---|
35 | n/a | tb = tb.tb_next |
---|
36 | n/a | return stack |
---|
37 | n/a | |
---|
38 | n/a | def get_exception(self): |
---|
39 | n/a | type = sys.last_type |
---|
40 | n/a | value = sys.last_value |
---|
41 | n/a | if hasattr(type, "__name__"): |
---|
42 | n/a | type = type.__name__ |
---|
43 | n/a | s = str(type) |
---|
44 | n/a | if value is not None: |
---|
45 | n/a | s = s + ": " + str(value) |
---|
46 | n/a | return s |
---|
47 | n/a | |
---|
48 | n/a | def GetText(self): |
---|
49 | n/a | return self.text |
---|
50 | n/a | |
---|
51 | n/a | def GetSubList(self): |
---|
52 | n/a | sublist = [] |
---|
53 | n/a | for info in self.stack: |
---|
54 | n/a | item = FrameTreeItem(info, self.flist) |
---|
55 | n/a | sublist.append(item) |
---|
56 | n/a | return sublist |
---|
57 | n/a | |
---|
58 | n/a | |
---|
59 | n/a | class FrameTreeItem(TreeItem): |
---|
60 | n/a | |
---|
61 | n/a | def __init__(self, info, flist): |
---|
62 | n/a | self.info = info |
---|
63 | n/a | self.flist = flist |
---|
64 | n/a | |
---|
65 | n/a | def GetText(self): |
---|
66 | n/a | frame, lineno = self.info |
---|
67 | n/a | try: |
---|
68 | n/a | modname = frame.f_globals["__name__"] |
---|
69 | n/a | except: |
---|
70 | n/a | modname = "?" |
---|
71 | n/a | code = frame.f_code |
---|
72 | n/a | filename = code.co_filename |
---|
73 | n/a | funcname = code.co_name |
---|
74 | n/a | sourceline = linecache.getline(filename, lineno) |
---|
75 | n/a | sourceline = sourceline.strip() |
---|
76 | n/a | if funcname in ("?", "", None): |
---|
77 | n/a | item = "%s, line %d: %s" % (modname, lineno, sourceline) |
---|
78 | n/a | else: |
---|
79 | n/a | item = "%s.%s(...), line %d: %s" % (modname, funcname, |
---|
80 | n/a | lineno, sourceline) |
---|
81 | n/a | return item |
---|
82 | n/a | |
---|
83 | n/a | def GetSubList(self): |
---|
84 | n/a | frame, lineno = self.info |
---|
85 | n/a | sublist = [] |
---|
86 | n/a | if frame.f_globals is not frame.f_locals: |
---|
87 | n/a | item = VariablesTreeItem("<locals>", frame.f_locals, self.flist) |
---|
88 | n/a | sublist.append(item) |
---|
89 | n/a | item = VariablesTreeItem("<globals>", frame.f_globals, self.flist) |
---|
90 | n/a | sublist.append(item) |
---|
91 | n/a | return sublist |
---|
92 | n/a | |
---|
93 | n/a | def OnDoubleClick(self): |
---|
94 | n/a | if self.flist: |
---|
95 | n/a | frame, lineno = self.info |
---|
96 | n/a | filename = frame.f_code.co_filename |
---|
97 | n/a | if os.path.isfile(filename): |
---|
98 | n/a | self.flist.gotofileline(filename, lineno) |
---|
99 | n/a | |
---|
100 | n/a | |
---|
101 | n/a | class VariablesTreeItem(ObjectTreeItem): |
---|
102 | n/a | |
---|
103 | n/a | def GetText(self): |
---|
104 | n/a | return self.labeltext |
---|
105 | n/a | |
---|
106 | n/a | def GetLabelText(self): |
---|
107 | n/a | return None |
---|
108 | n/a | |
---|
109 | n/a | def IsExpandable(self): |
---|
110 | n/a | return len(self.object) > 0 |
---|
111 | n/a | |
---|
112 | n/a | def GetSubList(self): |
---|
113 | n/a | sublist = [] |
---|
114 | n/a | for key in self.object.keys(): |
---|
115 | n/a | try: |
---|
116 | n/a | value = self.object[key] |
---|
117 | n/a | except KeyError: |
---|
118 | n/a | continue |
---|
119 | n/a | def setfunction(value, key=key, object=self.object): |
---|
120 | n/a | object[key] = value |
---|
121 | n/a | item = make_objecttreeitem(key + " =", value, setfunction) |
---|
122 | n/a | sublist.append(item) |
---|
123 | n/a | return sublist |
---|
124 | n/a | |
---|
125 | n/a | |
---|
126 | n/a | def _stack_viewer(parent): # htest # |
---|
127 | n/a | from idlelib.pyshell import PyShellFileList |
---|
128 | n/a | top = tk.Toplevel(parent) |
---|
129 | n/a | top.title("Test StackViewer") |
---|
130 | n/a | x, y = map(int, parent.geometry().split('+')[1:]) |
---|
131 | n/a | top.geometry("+%d+%d" % (x + 50, y + 175)) |
---|
132 | n/a | flist = PyShellFileList(top) |
---|
133 | n/a | try: # to obtain a traceback object |
---|
134 | n/a | intentional_name_error |
---|
135 | n/a | except NameError: |
---|
136 | n/a | exc_type, exc_value, exc_tb = sys.exc_info() |
---|
137 | n/a | |
---|
138 | n/a | # inject stack trace to sys |
---|
139 | n/a | sys.last_type = exc_type |
---|
140 | n/a | sys.last_value = exc_value |
---|
141 | n/a | sys.last_traceback = exc_tb |
---|
142 | n/a | |
---|
143 | n/a | StackBrowser(top, flist=flist, top=top, tb=exc_tb) |
---|
144 | n/a | |
---|
145 | n/a | # restore sys to original state |
---|
146 | n/a | del sys.last_type |
---|
147 | n/a | del sys.last_value |
---|
148 | n/a | del sys.last_traceback |
---|
149 | n/a | |
---|
150 | n/a | if __name__ == '__main__': |
---|
151 | n/a | from idlelib.idle_test.htest import run |
---|
152 | n/a | run(_stack_viewer) |
---|