Refactoring for fileConfig. Contributed by Shane Hathaway.
[python.git] / Mac / Tools / IDE / Wtraceback.py
blobe84349de62120aea09622cdf3f377e9fa3381930
1 import traceback
2 import sys
3 import W
4 import os
5 import types
6 from Carbon import List
9 class TraceBack:
11 def __init__(self, title = "Traceback"):
12 app = W.getapplication() # checks if W is properly initialized
13 self.title = title
14 self.w = None
15 self.closed = 1
16 self.start = 0
17 self.lastwindowtitle = ""
18 self.bounds = (360, 298)
20 def traceback(self, start = 0, lastwindowtitle = ""):
21 try:
22 self.lastwindowtitle = lastwindowtitle
23 self.start = start
24 self.type, self.value, self.tb = sys.exc_info()
25 if self.type is not SyntaxError:
26 self.show()
27 if type(self.type) == types.ClassType:
28 errortext = self.type.__name__
29 else:
30 errortext = str(self.type)
31 value = str(self.value)
32 if self.value and value:
33 errortext = errortext + ": " + value
34 self.w.text.set(errortext)
35 self.buildtblist()
36 self.w.list.set(self.textlist)
37 self.w.list.setselection([len(self.textlist) - 1])
38 self.w.wid.SelectWindow()
39 self.closed = 0
40 else:
41 self.syntaxerror()
42 except:
43 traceback.print_exc()
45 def syntaxerror(self):
46 try:
47 value, (filename, lineno, charno, line) = self.value
48 except:
49 filename = ""
50 lineno = None
51 value = self.value
52 if not filename and self.lastwindowtitle:
53 filename = self.lastwindowtitle
54 elif not filename:
55 filename = "<unknown>"
56 if filename and os.path.exists(filename):
57 filename = os.path.split(filename)[1]
58 if lineno and charno is not None:
59 charno = charno - 1
60 text = str(value) + '\rFile: "' + str(filename) + '", line ' + str(lineno) + '\r\r' + line[:charno] + "\xa5" + line[charno:-1]
61 else:
62 text = str(value) + '\rFile: "' + str(filename) + '"'
63 self.syntaxdialog = W.ModalDialog((360, 120), "Syntax Error")
64 self.syntaxdialog.text = W.TextBox((10, 10, -10, -40), text)
65 self.syntaxdialog.cancel = W.Button((-190, -32, 80, 16), "Cancel", self.syntaxclose)
66 self.syntaxdialog.edit = W.Button((-100, -32, 80, 16), "Edit", self.syntaxedit)
67 self.syntaxdialog.setdefaultbutton(self.syntaxdialog.edit)
68 self.syntaxdialog.bind("cmd.", self.syntaxdialog.cancel.push)
69 self.syntaxdialog.open()
71 def syntaxclose(self):
72 self.syntaxdialog.close()
73 del self.syntaxdialog
75 def syntaxedit(self):
76 try:
77 value, (filename, lineno, charno, line) = self.value
78 except:
79 filename = ""
80 lineno = None
81 if not filename and self.lastwindowtitle:
82 filename = self.lastwindowtitle
83 elif not filename:
84 filename = "<unknown>"
85 self.syntaxclose()
86 if lineno:
87 if charno is None:
88 charno = 1
89 W.getapplication().openscript(filename, lineno, charno - 1)
90 else:
91 W.getapplication().openscript(filename)
93 def show(self):
94 if self.closed:
95 self.setupwidgets()
96 self.w.open()
97 else:
98 self.w.wid.ShowWindow()
99 self.w.wid.SelectWindow()
101 def hide(self):
102 if self.closed:
103 return
104 self.w.close()
106 def close(self):
107 self.bounds = self.w.getbounds()
108 self.closed = 1
109 self.type, self.value, self.tb = None, None, None
110 self.tblist = None
112 def activate(self, onoff):
113 if onoff:
114 if self.closed:
115 self.traceback()
116 self.closed = 0
117 self.checkbuttons()
119 def setupwidgets(self):
120 self.w = W.Window(self.bounds, self.title, minsize = (316, 168))
121 self.w.text = W.TextBox((10, 10, -10, 30))
122 self.w.tbtitle = W.TextBox((10, 40, -10, 10), "Traceback (innermost last):")
123 self.w.list = W.TwoLineList((10, 60, -10, -40), callback = self.listhit)
125 self.w.editbutton = W.Button((10, -30, 60, 16), "Edit", self.edit)
126 self.w.editbutton.enable(0)
128 self.w.browselocalsbutton = W.Button((80, -30, 100, 16), "Browse locals\xc9", self.browselocals)
129 self.w.browselocalsbutton.enable(0)
131 self.w.postmortembutton = W.Button((190, -30, 100, 16), "Post mortem\xc9", self.postmortem)
133 self.w.setdefaultbutton(self.w.editbutton)
134 self.w.bind("cmdb", self.w.browselocalsbutton.push)
135 self.w.bind("<close>", self.close)
136 self.w.bind("<activate>", self.activate)
138 def buildtblist(self):
139 tb = self.tb
140 for i in range(self.start):
141 if tb.tb_next is None:
142 break
143 tb = tb.tb_next
144 self.tblist = traceback.extract_tb(tb)
145 self.textlist = []
146 for filename, lineno, func, line in self.tblist:
147 tbline = ""
148 if os.path.exists(filename):
149 filename = os.path.split(filename)[1]
150 tbline = 'File "%s", line %r, in %r' % (filename, lineno, func)
151 if line:
152 tbline = tbline + '\r ' + line
153 self.textlist.append(tbline[:255])
155 def edit(self):
156 sel = self.w.list.getselection()
157 for i in sel:
158 filename, lineno, func, line = self.tblist[i]
159 W.getapplication().openscript(filename, lineno)
161 def browselocals(self):
162 sel = self.w.list.getselection()
163 for i in sel:
164 tb = self.tb
165 for j in range(i + self.start):
166 tb = tb.tb_next
167 self.browse(tb.tb_frame.f_locals)
169 def browse(self, object):
170 import PyBrowser
171 PyBrowser.Browser(object)
173 def postmortem(self):
174 import PyDebugger
175 PyDebugger.postmortem(self.type, self.value, self.tb)
177 def listhit(self, isdbl):
178 if isdbl:
179 self.w.editbutton.push()
180 else:
181 self.checkbuttons()
183 def checkbuttons(self):
184 havefile = len(self.w.list.getselection()) > 0
185 self.w.editbutton.enable(havefile)
186 self.w.browselocalsbutton.enable(havefile)
187 self.w.setdefaultbutton(havefile and self.w.editbutton or self.w.postmortembutton)