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

Python code coverage for Lib/idlelib/debugobj.py

#countcontent
1n/a# XXX TO DO:
2n/a# - popup menu
3n/a# - support partial or total redisplay
4n/a# - more doc strings
5n/a# - tooltips
6n/a
7n/a# object browser
8n/a
9n/a# XXX TO DO:
10n/a# - for classes/modules, add "open source" to object browser
11n/afrom reprlib import Repr
12n/a
13n/afrom idlelib.tree import TreeItem, TreeNode, ScrolledCanvas
14n/a
15n/amyrepr = Repr()
16n/amyrepr.maxstring = 100
17n/amyrepr.maxother = 100
18n/a
19n/aclass ObjectTreeItem(TreeItem):
20n/a def __init__(self, labeltext, object, setfunction=None):
21n/a self.labeltext = labeltext
22n/a self.object = object
23n/a self.setfunction = setfunction
24n/a def GetLabelText(self):
25n/a return self.labeltext
26n/a def GetText(self):
27n/a return myrepr.repr(self.object)
28n/a def GetIconName(self):
29n/a if not self.IsExpandable():
30n/a return "python"
31n/a def IsEditable(self):
32n/a return self.setfunction is not None
33n/a def SetText(self, text):
34n/a try:
35n/a value = eval(text)
36n/a self.setfunction(value)
37n/a except:
38n/a pass
39n/a else:
40n/a self.object = value
41n/a def IsExpandable(self):
42n/a return not not dir(self.object)
43n/a def GetSubList(self):
44n/a keys = dir(self.object)
45n/a sublist = []
46n/a for key in keys:
47n/a try:
48n/a value = getattr(self.object, key)
49n/a except AttributeError:
50n/a continue
51n/a item = make_objecttreeitem(
52n/a str(key) + " =",
53n/a value,
54n/a lambda value, key=key, object=self.object:
55n/a setattr(object, key, value))
56n/a sublist.append(item)
57n/a return sublist
58n/a
59n/aclass ClassTreeItem(ObjectTreeItem):
60n/a def IsExpandable(self):
61n/a return True
62n/a def GetSubList(self):
63n/a sublist = ObjectTreeItem.GetSubList(self)
64n/a if len(self.object.__bases__) == 1:
65n/a item = make_objecttreeitem("__bases__[0] =",
66n/a self.object.__bases__[0])
67n/a else:
68n/a item = make_objecttreeitem("__bases__ =", self.object.__bases__)
69n/a sublist.insert(0, item)
70n/a return sublist
71n/a
72n/aclass AtomicObjectTreeItem(ObjectTreeItem):
73n/a def IsExpandable(self):
74n/a return 0
75n/a
76n/aclass SequenceTreeItem(ObjectTreeItem):
77n/a def IsExpandable(self):
78n/a return len(self.object) > 0
79n/a def keys(self):
80n/a return range(len(self.object))
81n/a def GetSubList(self):
82n/a sublist = []
83n/a for key in self.keys():
84n/a try:
85n/a value = self.object[key]
86n/a except KeyError:
87n/a continue
88n/a def setfunction(value, key=key, object=self.object):
89n/a object[key] = value
90n/a item = make_objecttreeitem("%r:" % (key,), value, setfunction)
91n/a sublist.append(item)
92n/a return sublist
93n/a
94n/aclass DictTreeItem(SequenceTreeItem):
95n/a def keys(self):
96n/a keys = list(self.object.keys())
97n/a try:
98n/a keys.sort()
99n/a except:
100n/a pass
101n/a return keys
102n/a
103n/adispatch = {
104n/a int: AtomicObjectTreeItem,
105n/a float: AtomicObjectTreeItem,
106n/a str: AtomicObjectTreeItem,
107n/a tuple: SequenceTreeItem,
108n/a list: SequenceTreeItem,
109n/a dict: DictTreeItem,
110n/a type: ClassTreeItem,
111n/a}
112n/a
113n/adef make_objecttreeitem(labeltext, object, setfunction=None):
114n/a t = type(object)
115n/a if t in dispatch:
116n/a c = dispatch[t]
117n/a else:
118n/a c = ObjectTreeItem
119n/a return c(labeltext, object, setfunction)
120n/a
121n/a
122n/adef _object_browser(parent): # htest #
123n/a import sys
124n/a from tkinter import Toplevel
125n/a top = Toplevel(parent)
126n/a top.title("Test debug object browser")
127n/a x, y = map(int, parent.geometry().split('+')[1:])
128n/a top.geometry("+%d+%d" % (x + 100, y + 175))
129n/a top.configure(bd=0, bg="yellow")
130n/a top.focus_set()
131n/a sc = ScrolledCanvas(top, bg="white", highlightthickness=0, takefocus=1)
132n/a sc.frame.pack(expand=1, fill="both")
133n/a item = make_objecttreeitem("sys", sys)
134n/a node = TreeNode(sc.canvas, None, item)
135n/a node.update()
136n/a
137n/aif __name__ == '__main__':
138n/a from idlelib.idle_test.htest import run
139n/a run(_object_browser)