ยปCore Development>Code coverage>Lib/idlelib/stackviewer.py

Python code coverage for Lib/idlelib/stackviewer.py

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