5 from TreeWidget
import TreeNode
, TreeItem
, ScrolledCanvas
6 from ObjectBrowser
import ObjectTreeItem
, make_objecttreeitem
8 def StackBrowser(root
, flist
=None, tb
=None, top
=None):
10 from Tkinter
import Toplevel
12 sc
= ScrolledCanvas(top
, bg
="white", highlightthickness
=0)
13 sc
.frame
.pack(expand
=1, fill
="both")
14 item
= StackTreeItem(flist
, tb
)
15 node
= TreeNode(sc
.canvas
, None, item
)
18 class StackTreeItem(TreeItem
):
20 def __init__(self
, flist
=None, tb
=None):
22 self
.stack
= self
.get_stack(tb
)
23 self
.text
= self
.get_exception()
25 def get_stack(self
, tb
):
27 tb
= sys
.last_traceback
29 if tb
and tb
.tb_frame
is None:
32 stack
.append((tb
.tb_frame
, tb
.tb_lineno
))
36 def get_exception(self
):
38 value
= sys
.last_value
39 if hasattr(type, "__name__"):
43 s
= s
+ ": " + str(value
)
51 for info
in self
.stack
:
52 item
= FrameTreeItem(info
, self
.flist
)
56 class FrameTreeItem(TreeItem
):
58 def __init__(self
, info
, flist
):
63 frame
, lineno
= self
.info
65 modname
= frame
.f_globals
["__name__"]
69 filename
= code
.co_filename
70 funcname
= code
.co_name
71 sourceline
= linecache
.getline(filename
, lineno
)
72 sourceline
= sourceline
.strip()
73 if funcname
in ("?", "", None):
74 item
= "%s, line %d: %s" % (modname
, lineno
, sourceline
)
76 item
= "%s.%s(...), line %d: %s" % (modname
, funcname
,
81 frame
, lineno
= self
.info
83 if frame
.f_globals
is not frame
.f_locals
:
84 item
= VariablesTreeItem("<locals>", frame
.f_locals
, self
.flist
)
86 item
= VariablesTreeItem("<globals>", frame
.f_globals
, self
.flist
)
90 def OnDoubleClick(self
):
92 frame
, lineno
= self
.info
93 filename
= frame
.f_code
.co_filename
94 if os
.path
.isfile(filename
):
95 self
.flist
.gotofileline(filename
, lineno
)
97 class VariablesTreeItem(ObjectTreeItem
):
100 return self
.labeltext
102 def GetLabelText(self
):
105 def IsExpandable(self
):
106 return len(self
.object) > 0
109 return self
.object.keys()
111 def GetSubList(self
):
113 for key
in self
.keys():
115 value
= self
.object[key
]
118 def setfunction(value
, key
=key
, object=self
.object):
120 item
= make_objecttreeitem(key
+ " =", value
, setfunction
)
130 sys
.last_type
, sys
.last_value
, sys
.last_traceback
= sys
.exc_info()
131 from Tkinter
import Tk
133 StackBrowser(None, top
=root
)
136 if __name__
== "__main__":