fixed mem leak
[swftools.git] / wx / lib / error.py
blobed7e5813e60cd78ac9eeb4757d867012deb154b3
1 import os, sys
2 import wx
3 import wx.lib.mixins.listctrl as listmix
4 import traceback
7 class BoldedStaticText(wx.StaticText):
8 def __init__(self, *args, **kwargs):
9 wx.StaticText.__init__(self, *args, **kwargs)
10 font = self.GetFont()
11 font.SetWeight(wx.BOLD)
12 self.SetFont(font)
15 class ListCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
16 def __init__(self, parent, size=wx.DefaultSize, style=0):
17 wx.ListCtrl.__init__(self, parent, size=size, style=style)
18 listmix.ListCtrlAutoWidthMixin.__init__(self)
21 class ErrorPanel(wx.Panel):
22 def __init__(self, parent):
23 wx.Panel.__init__(self, parent)
25 self.box = wx.BoxSizer(wx.VERTICAL)
27 # Main Label
28 self.box.Add(BoldedStaticText(self, label="An error has occurred")
29 , 0, wx.ALIGN_CENTER | wx.ALL, 10)
31 # Exception Information
32 boxInfoGrid = wx.FlexGridSizer(2, 2, 0, 0)
33 textFlags = wx.ALIGN_LEFT | wx.LEFT | wx.RIGHT | wx.TOP
35 boxInfoGrid.Add(BoldedStaticText(self, label="Type: "), 0, textFlags, 5 )
36 self.error_type = wx.StaticText(self, label="")
37 boxInfoGrid.Add(self.error_type, 0, textFlags, 5 )
39 textFlags = wx.ALIGN_LEFT | wx.ALL
40 boxInfoGrid.Add(BoldedStaticText(self, label="Value: ") , 0, textFlags, 5 )
41 self.error_details = wx.StaticText(self, label="")
42 boxInfoGrid.Add(self.error_details , 0, textFlags, 5 )
44 bbox = wx.BoxSizer(wx.HORIZONTAL)
45 self.copy = wx.Button(self, label="Copy to Clipboard")
46 self.close = wx.Button(self, label="&Close")
47 bbox.Add(self.copy)
48 bbox.Add(self.close)
50 # Set up the traceback list
51 # This one automatically resizes last column to take up remaining space
52 self.__list = ListCtrl(self, style=wx.LC_REPORT | wx.SUNKEN_BORDER)
53 self.__list.InsertColumn(0, "Filename")
54 self.__list.InsertColumn(1, "Line", wx.LIST_FORMAT_RIGHT)
55 self.__list.InsertColumn(2, "Function")
56 self.__list.InsertColumn(3, "Code")
58 self.box.Add(boxInfoGrid)
59 self.box.Add((-1,10))
60 self.box.Add(BoldedStaticText(self, label="Traceback:")
61 , 0, wx.ALIGN_LEFT | wx.LEFT | wx.TOP, 5)
62 self.box.Add(self.__list, 1, wx.EXPAND | wx.ALIGN_CENTER | wx.ALL, 5)
63 self.box.Add(bbox, 0, wx.ALIGN_RIGHT | wx.ALL, 5)
65 self.box.Fit(self)
66 self.SetSizer(self.box)
68 self.tb_info = []
70 self.copy.Bind(wx.EVT_BUTTON, self.OnCopy)
73 def repr_(self, val):
74 return repr(val)[1:-1]
76 def show(self, type_, value, tb):
77 sys.__excepthook__(type_, value, tb)
79 self.error_type.SetLabel(self.repr_(type_))
80 self.error_details.SetLabel(repr(value))
82 self.tb_info = traceback.format_exception(type_, value, tb)
83 x = 0
84 self.__list.DeleteAllItems()
85 for filename, line, func, text in traceback.extract_tb(tb):
86 self.__list.InsertStringItem(x,
87 self.repr_(os.path.basename(filename))) # Filename
88 self.__list.SetStringItem(x, 1, unicode(line)) # Line
89 self.__list.SetStringItem(x, 2, unicode(func)) # Function
90 self.__list.SetStringItem(x, 3, self.repr_(text)) # Code
92 x += 1
93 self.Show()
95 def OnCopy(self, event):
96 data = wx.TextDataObject()
97 data.SetText(''.join(self.tb_info))
98 if wx.TheClipboard.Open():
99 wx.TheClipboard.SetData(data)
100 wx.TheClipboard.Close()
101 event.GetEventObject().Enable(False)
102 else:
103 wx.MessageBox("Unable to open the clipboard", "Error")
106 class ErrorFrame:
107 def __init__(self, parent=None):
108 self.frame = None
109 self.parent = parent
111 def __call__(self, type_, value, tb):
112 if not self.frame:
113 self.frame = wx.Frame(self.parent, size=(600,400))
114 self.error_panel = ErrorPanel(self.frame)
116 self.error_panel.close.Bind(wx.EVT_BUTTON, self.OnClose)
117 self.frame.Bind(wx.EVT_CLOSE, self.OnClose)
119 self.error_panel.show(type_, value, tb)
121 self.frame.CenterOnParent()
122 self.frame.Show()
124 def OnClose(self, event):
125 self.frame.Hide()
126 #event.Skip()
129 if __name__ == '__main__':
130 def raise_():
131 raise Exception("Teste")
133 app = wx.App()
134 f = wx.Frame(None)
135 sys.excepthook = ErrorFrame(f)
136 f.Show()
137 wx.FutureCall(500, raise_)
138 app.MainLoop()