5 from WindowList
import ListedToplevel
10 class Debugger(bdb
.Bdb
):
14 vstack
= vsource
= vlocals
= vglobals
= None
16 def __init__(self
, pyshell
):
17 bdb
.Bdb
.__init
__(self
)
18 self
.pyshell
= pyshell
21 def canonic(self
, filename
):
22 # Canonicalize filename -- called by Bdb
23 return os
.path
.normcase(os
.path
.abspath(filename
))
25 def close(self
, event
=None):
30 self
.stackviewer
.close(); self
.stackviewer
= None
31 self
.pyshell
.close_debugger()
37 return apply(bdb
.Bdb
.run
, (self
,) + args
)
41 def user_line(self
, frame
):
42 self
.interaction(frame
)
44 def user_return(self
, frame
, rv
):
46 ##self.interaction(frame)
49 def user_exception(self
, frame
, info
):
50 self
.interaction(frame
, info
)
53 pyshell
= self
.pyshell
54 self
.flist
= pyshell
.flist
55 self
.root
= root
= pyshell
.root
56 self
.top
= top
=ListedToplevel(root
)
57 self
.top
.wm_title("Debug Control")
58 self
.top
.wm_iconname("Debug")
59 top
.wm_protocol("WM_DELETE_WINDOW", self
.close
)
60 self
.top
.bind("<Escape>", self
.close
)
62 self
.bframe
= bframe
= Frame(top
)
63 self
.bframe
.pack(anchor
="w")
64 self
.buttons
= bl
= []
66 self
.bcont
= b
= Button(bframe
, text
="Go", command
=self
.cont
)
68 self
.bstep
= b
= Button(bframe
, text
="Step", command
=self
.step
)
70 self
.bnext
= b
= Button(bframe
, text
="Over", command
=self
.next
)
72 self
.bret
= b
= Button(bframe
, text
="Out", command
=self
.ret
)
74 self
.bret
= b
= Button(bframe
, text
="Quit", command
=self
.quit
)
78 b
.configure(state
="disabled")
81 self
.cframe
= cframe
= Frame(bframe
)
82 self
.cframe
.pack(side
="left")
85 self
.__class
__.vstack
= BooleanVar(top
)
87 self
.bstack
= Checkbutton(cframe
,
88 text
="Stack", command
=self
.show_stack
, variable
=self
.vstack
)
89 self
.bstack
.grid(row
=0, column
=0)
91 self
.__class
__.vsource
= BooleanVar(top
)
93 self
.bsource
= Checkbutton(cframe
,
94 text
="Source", command
=self
.show_source
, variable
=self
.vsource
)
95 self
.bsource
.grid(row
=0, column
=1)
97 self
.__class
__.vlocals
= BooleanVar(top
)
99 self
.blocals
= Checkbutton(cframe
,
100 text
="Locals", command
=self
.show_locals
, variable
=self
.vlocals
)
101 self
.blocals
.grid(row
=1, column
=0)
102 if not self
.vglobals
:
103 self
.__class
__.vglobals
= BooleanVar(top
)
104 ##self.vglobals.set(1)
105 self
.bglobals
= Checkbutton(cframe
,
106 text
="Globals", command
=self
.show_globals
, variable
=self
.vglobals
)
107 self
.bglobals
.grid(row
=1, column
=1)
109 self
.status
= Label(top
, anchor
="w")
110 self
.status
.pack(anchor
="w")
111 self
.error
= Label(top
, anchor
="w")
112 self
.error
.pack(anchor
="w", fill
="x")
113 self
.errorbg
= self
.error
.cget("background")
115 self
.fstack
= Frame(top
, height
=1)
116 self
.fstack
.pack(expand
=1, fill
="both")
117 self
.flocals
= Frame(top
)
118 self
.flocals
.pack(expand
=1, fill
="both")
119 self
.fglobals
= Frame(top
, height
=1)
120 self
.fglobals
.pack(expand
=1, fill
="both")
122 if self
.vstack
.get():
124 if self
.vlocals
.get():
126 if self
.vglobals
.get():
131 def interaction(self
, frame
, info
=None):
134 file = code
.co_filename
135 base
= os
.path
.basename(file)
136 lineno
= frame
.f_lineno
138 message
= "%s:%s" % (base
, lineno
)
139 if code
.co_name
!= "?":
140 message
= "%s: %s()" % (message
, code
.co_name
)
141 self
.status
.configure(text
=message
)
144 type, value
, tb
= info
147 except AttributeError:
148 m1
= "%s" % str(type)
149 if value
is not None:
151 m1
= "%s: %s" % (m1
, str(value
))
159 self
.error
.configure(text
=m1
, background
=bg
)
161 sv
= self
.stackviewer
163 stack
, i
= self
.get_stack(self
.frame
, tb
)
164 sv
.load_stack(stack
, i
)
166 self
.show_variables(1)
168 if self
.vsource
.get():
169 self
.sync_source_line()
171 for b
in self
.buttons
:
172 b
.configure(state
="normal")
177 for b
in self
.buttons
:
178 b
.configure(state
="disabled")
179 self
.status
.configure(text
="")
180 self
.error
.configure(text
="", background
=self
.errorbg
)
183 def sync_source_line(self
):
188 file = code
.co_filename
189 lineno
= frame
.f_lineno
190 if file[:1] + file[-1:] != "<>" and os
.path
.exists(file):
191 edit
= self
.flist
.open(file)
193 edit
.gotoline(lineno
)
204 self
.set_next(self
.frame
)
208 self
.set_return(self
.frame
)
217 def show_stack(self
):
218 if not self
.stackviewer
and self
.vstack
.get():
219 self
.stackviewer
= sv
= StackViewer
.StackViewer(
220 self
.fstack
, self
.flist
, self
)
222 stack
, i
= self
.get_stack(self
.frame
, None)
223 sv
.load_stack(stack
, i
)
225 sv
= self
.stackviewer
226 if sv
and not self
.vstack
.get():
227 self
.stackviewer
= None
229 self
.fstack
['height'] = 1
231 def show_source(self
):
232 if self
.vsource
.get():
233 self
.sync_source_line()
235 def show_frame(self
, (frame
, lineno
)):
237 self
.show_variables()
242 def show_locals(self
):
243 lv
= self
.localsviewer
244 if self
.vlocals
.get():
246 self
.localsviewer
= StackViewer
.NamespaceViewer(
247 self
.flocals
, "Locals")
250 self
.localsviewer
= None
252 self
.flocals
['height'] = 1
253 self
.show_variables()
255 def show_globals(self
):
256 gv
= self
.globalsviewer
257 if self
.vglobals
.get():
259 self
.globalsviewer
= StackViewer
.NamespaceViewer(
260 self
.fglobals
, "Globals")
263 self
.globalsviewer
= None
265 self
.fglobals
['height'] = 1
266 self
.show_variables()
268 def show_variables(self
, force
=0):
269 lv
= self
.localsviewer
270 gv
= self
.globalsviewer
275 ldict
= frame
.f_locals
276 gdict
= frame
.f_globals
277 if lv
and gv
and ldict
is gdict
:
280 lv
.load_dict(ldict
, force
)
282 gv
.load_dict(gdict
, force
)
284 def set_breakpoint_here(self
, edit
):
286 filename
= edit
.io
.filename
290 lineno
= int(float(text
.index("insert")))
291 msg
= self
.set_break(filename
, lineno
)
295 text
.tag_add("BREAK", "insert linestart", "insert lineend +1char")
297 # A literal copy of Bdb.set_break() without the print statement at the end
298 def set_break(self
, filename
, lineno
, temporary
=0, cond
= None):
299 import linecache
# Import as late as possible
300 line
= linecache
.getline(filename
, lineno
)
302 return 'That line does not exist!'
303 if not self
.breaks
.has_key(filename
):
304 self
.breaks
[filename
] = []
305 list = self
.breaks
[filename
]
306 if not lineno
in list:
308 bp
= bdb
.Breakpoint(filename
, lineno
, temporary
, cond
)