jpayne@68
|
1 import bdb
|
jpayne@68
|
2 import os
|
jpayne@68
|
3
|
jpayne@68
|
4 from tkinter import *
|
jpayne@68
|
5 from tkinter.ttk import Frame, Scrollbar
|
jpayne@68
|
6
|
jpayne@68
|
7 from idlelib import macosx
|
jpayne@68
|
8 from idlelib.scrolledlist import ScrolledList
|
jpayne@68
|
9 from idlelib.window import ListedToplevel
|
jpayne@68
|
10
|
jpayne@68
|
11
|
jpayne@68
|
12 class Idb(bdb.Bdb):
|
jpayne@68
|
13
|
jpayne@68
|
14 def __init__(self, gui):
|
jpayne@68
|
15 self.gui = gui # An instance of Debugger or proxy of remote.
|
jpayne@68
|
16 bdb.Bdb.__init__(self)
|
jpayne@68
|
17
|
jpayne@68
|
18 def user_line(self, frame):
|
jpayne@68
|
19 if self.in_rpc_code(frame):
|
jpayne@68
|
20 self.set_step()
|
jpayne@68
|
21 return
|
jpayne@68
|
22 message = self.__frame2message(frame)
|
jpayne@68
|
23 try:
|
jpayne@68
|
24 self.gui.interaction(message, frame)
|
jpayne@68
|
25 except TclError: # When closing debugger window with [x] in 3.x
|
jpayne@68
|
26 pass
|
jpayne@68
|
27
|
jpayne@68
|
28 def user_exception(self, frame, info):
|
jpayne@68
|
29 if self.in_rpc_code(frame):
|
jpayne@68
|
30 self.set_step()
|
jpayne@68
|
31 return
|
jpayne@68
|
32 message = self.__frame2message(frame)
|
jpayne@68
|
33 self.gui.interaction(message, frame, info)
|
jpayne@68
|
34
|
jpayne@68
|
35 def in_rpc_code(self, frame):
|
jpayne@68
|
36 if frame.f_code.co_filename.count('rpc.py'):
|
jpayne@68
|
37 return True
|
jpayne@68
|
38 else:
|
jpayne@68
|
39 prev_frame = frame.f_back
|
jpayne@68
|
40 prev_name = prev_frame.f_code.co_filename
|
jpayne@68
|
41 if 'idlelib' in prev_name and 'debugger' in prev_name:
|
jpayne@68
|
42 # catch both idlelib/debugger.py and idlelib/debugger_r.py
|
jpayne@68
|
43 # on both Posix and Windows
|
jpayne@68
|
44 return False
|
jpayne@68
|
45 return self.in_rpc_code(prev_frame)
|
jpayne@68
|
46
|
jpayne@68
|
47 def __frame2message(self, frame):
|
jpayne@68
|
48 code = frame.f_code
|
jpayne@68
|
49 filename = code.co_filename
|
jpayne@68
|
50 lineno = frame.f_lineno
|
jpayne@68
|
51 basename = os.path.basename(filename)
|
jpayne@68
|
52 message = "%s:%s" % (basename, lineno)
|
jpayne@68
|
53 if code.co_name != "?":
|
jpayne@68
|
54 message = "%s: %s()" % (message, code.co_name)
|
jpayne@68
|
55 return message
|
jpayne@68
|
56
|
jpayne@68
|
57
|
jpayne@68
|
58 class Debugger:
|
jpayne@68
|
59
|
jpayne@68
|
60 vstack = vsource = vlocals = vglobals = None
|
jpayne@68
|
61
|
jpayne@68
|
62 def __init__(self, pyshell, idb=None):
|
jpayne@68
|
63 if idb is None:
|
jpayne@68
|
64 idb = Idb(self)
|
jpayne@68
|
65 self.pyshell = pyshell
|
jpayne@68
|
66 self.idb = idb # If passed, a proxy of remote instance.
|
jpayne@68
|
67 self.frame = None
|
jpayne@68
|
68 self.make_gui()
|
jpayne@68
|
69 self.interacting = 0
|
jpayne@68
|
70 self.nesting_level = 0
|
jpayne@68
|
71
|
jpayne@68
|
72 def run(self, *args):
|
jpayne@68
|
73 # Deal with the scenario where we've already got a program running
|
jpayne@68
|
74 # in the debugger and we want to start another. If that is the case,
|
jpayne@68
|
75 # our second 'run' was invoked from an event dispatched not from
|
jpayne@68
|
76 # the main event loop, but from the nested event loop in 'interaction'
|
jpayne@68
|
77 # below. So our stack looks something like this:
|
jpayne@68
|
78 # outer main event loop
|
jpayne@68
|
79 # run()
|
jpayne@68
|
80 # <running program with traces>
|
jpayne@68
|
81 # callback to debugger's interaction()
|
jpayne@68
|
82 # nested event loop
|
jpayne@68
|
83 # run() for second command
|
jpayne@68
|
84 #
|
jpayne@68
|
85 # This kind of nesting of event loops causes all kinds of problems
|
jpayne@68
|
86 # (see e.g. issue #24455) especially when dealing with running as a
|
jpayne@68
|
87 # subprocess, where there's all kinds of extra stuff happening in
|
jpayne@68
|
88 # there - insert a traceback.print_stack() to check it out.
|
jpayne@68
|
89 #
|
jpayne@68
|
90 # By this point, we've already called restart_subprocess() in
|
jpayne@68
|
91 # ScriptBinding. However, we also need to unwind the stack back to
|
jpayne@68
|
92 # that outer event loop. To accomplish this, we:
|
jpayne@68
|
93 # - return immediately from the nested run()
|
jpayne@68
|
94 # - abort_loop ensures the nested event loop will terminate
|
jpayne@68
|
95 # - the debugger's interaction routine completes normally
|
jpayne@68
|
96 # - the restart_subprocess() will have taken care of stopping
|
jpayne@68
|
97 # the running program, which will also let the outer run complete
|
jpayne@68
|
98 #
|
jpayne@68
|
99 # That leaves us back at the outer main event loop, at which point our
|
jpayne@68
|
100 # after event can fire, and we'll come back to this routine with a
|
jpayne@68
|
101 # clean stack.
|
jpayne@68
|
102 if self.nesting_level > 0:
|
jpayne@68
|
103 self.abort_loop()
|
jpayne@68
|
104 self.root.after(100, lambda: self.run(*args))
|
jpayne@68
|
105 return
|
jpayne@68
|
106 try:
|
jpayne@68
|
107 self.interacting = 1
|
jpayne@68
|
108 return self.idb.run(*args)
|
jpayne@68
|
109 finally:
|
jpayne@68
|
110 self.interacting = 0
|
jpayne@68
|
111
|
jpayne@68
|
112 def close(self, event=None):
|
jpayne@68
|
113 try:
|
jpayne@68
|
114 self.quit()
|
jpayne@68
|
115 except Exception:
|
jpayne@68
|
116 pass
|
jpayne@68
|
117 if self.interacting:
|
jpayne@68
|
118 self.top.bell()
|
jpayne@68
|
119 return
|
jpayne@68
|
120 if self.stackviewer:
|
jpayne@68
|
121 self.stackviewer.close(); self.stackviewer = None
|
jpayne@68
|
122 # Clean up pyshell if user clicked debugger control close widget.
|
jpayne@68
|
123 # (Causes a harmless extra cycle through close_debugger() if user
|
jpayne@68
|
124 # toggled debugger from pyshell Debug menu)
|
jpayne@68
|
125 self.pyshell.close_debugger()
|
jpayne@68
|
126 # Now close the debugger control window....
|
jpayne@68
|
127 self.top.destroy()
|
jpayne@68
|
128
|
jpayne@68
|
129 def make_gui(self):
|
jpayne@68
|
130 pyshell = self.pyshell
|
jpayne@68
|
131 self.flist = pyshell.flist
|
jpayne@68
|
132 self.root = root = pyshell.root
|
jpayne@68
|
133 self.top = top = ListedToplevel(root)
|
jpayne@68
|
134 self.top.wm_title("Debug Control")
|
jpayne@68
|
135 self.top.wm_iconname("Debug")
|
jpayne@68
|
136 top.wm_protocol("WM_DELETE_WINDOW", self.close)
|
jpayne@68
|
137 self.top.bind("<Escape>", self.close)
|
jpayne@68
|
138 #
|
jpayne@68
|
139 self.bframe = bframe = Frame(top)
|
jpayne@68
|
140 self.bframe.pack(anchor="w")
|
jpayne@68
|
141 self.buttons = bl = []
|
jpayne@68
|
142 #
|
jpayne@68
|
143 self.bcont = b = Button(bframe, text="Go", command=self.cont)
|
jpayne@68
|
144 bl.append(b)
|
jpayne@68
|
145 self.bstep = b = Button(bframe, text="Step", command=self.step)
|
jpayne@68
|
146 bl.append(b)
|
jpayne@68
|
147 self.bnext = b = Button(bframe, text="Over", command=self.next)
|
jpayne@68
|
148 bl.append(b)
|
jpayne@68
|
149 self.bret = b = Button(bframe, text="Out", command=self.ret)
|
jpayne@68
|
150 bl.append(b)
|
jpayne@68
|
151 self.bret = b = Button(bframe, text="Quit", command=self.quit)
|
jpayne@68
|
152 bl.append(b)
|
jpayne@68
|
153 #
|
jpayne@68
|
154 for b in bl:
|
jpayne@68
|
155 b.configure(state="disabled")
|
jpayne@68
|
156 b.pack(side="left")
|
jpayne@68
|
157 #
|
jpayne@68
|
158 self.cframe = cframe = Frame(bframe)
|
jpayne@68
|
159 self.cframe.pack(side="left")
|
jpayne@68
|
160 #
|
jpayne@68
|
161 if not self.vstack:
|
jpayne@68
|
162 self.__class__.vstack = BooleanVar(top)
|
jpayne@68
|
163 self.vstack.set(1)
|
jpayne@68
|
164 self.bstack = Checkbutton(cframe,
|
jpayne@68
|
165 text="Stack", command=self.show_stack, variable=self.vstack)
|
jpayne@68
|
166 self.bstack.grid(row=0, column=0)
|
jpayne@68
|
167 if not self.vsource:
|
jpayne@68
|
168 self.__class__.vsource = BooleanVar(top)
|
jpayne@68
|
169 self.bsource = Checkbutton(cframe,
|
jpayne@68
|
170 text="Source", command=self.show_source, variable=self.vsource)
|
jpayne@68
|
171 self.bsource.grid(row=0, column=1)
|
jpayne@68
|
172 if not self.vlocals:
|
jpayne@68
|
173 self.__class__.vlocals = BooleanVar(top)
|
jpayne@68
|
174 self.vlocals.set(1)
|
jpayne@68
|
175 self.blocals = Checkbutton(cframe,
|
jpayne@68
|
176 text="Locals", command=self.show_locals, variable=self.vlocals)
|
jpayne@68
|
177 self.blocals.grid(row=1, column=0)
|
jpayne@68
|
178 if not self.vglobals:
|
jpayne@68
|
179 self.__class__.vglobals = BooleanVar(top)
|
jpayne@68
|
180 self.bglobals = Checkbutton(cframe,
|
jpayne@68
|
181 text="Globals", command=self.show_globals, variable=self.vglobals)
|
jpayne@68
|
182 self.bglobals.grid(row=1, column=1)
|
jpayne@68
|
183 #
|
jpayne@68
|
184 self.status = Label(top, anchor="w")
|
jpayne@68
|
185 self.status.pack(anchor="w")
|
jpayne@68
|
186 self.error = Label(top, anchor="w")
|
jpayne@68
|
187 self.error.pack(anchor="w", fill="x")
|
jpayne@68
|
188 self.errorbg = self.error.cget("background")
|
jpayne@68
|
189 #
|
jpayne@68
|
190 self.fstack = Frame(top, height=1)
|
jpayne@68
|
191 self.fstack.pack(expand=1, fill="both")
|
jpayne@68
|
192 self.flocals = Frame(top)
|
jpayne@68
|
193 self.flocals.pack(expand=1, fill="both")
|
jpayne@68
|
194 self.fglobals = Frame(top, height=1)
|
jpayne@68
|
195 self.fglobals.pack(expand=1, fill="both")
|
jpayne@68
|
196 #
|
jpayne@68
|
197 if self.vstack.get():
|
jpayne@68
|
198 self.show_stack()
|
jpayne@68
|
199 if self.vlocals.get():
|
jpayne@68
|
200 self.show_locals()
|
jpayne@68
|
201 if self.vglobals.get():
|
jpayne@68
|
202 self.show_globals()
|
jpayne@68
|
203
|
jpayne@68
|
204 def interaction(self, message, frame, info=None):
|
jpayne@68
|
205 self.frame = frame
|
jpayne@68
|
206 self.status.configure(text=message)
|
jpayne@68
|
207 #
|
jpayne@68
|
208 if info:
|
jpayne@68
|
209 type, value, tb = info
|
jpayne@68
|
210 try:
|
jpayne@68
|
211 m1 = type.__name__
|
jpayne@68
|
212 except AttributeError:
|
jpayne@68
|
213 m1 = "%s" % str(type)
|
jpayne@68
|
214 if value is not None:
|
jpayne@68
|
215 try:
|
jpayne@68
|
216 m1 = "%s: %s" % (m1, str(value))
|
jpayne@68
|
217 except:
|
jpayne@68
|
218 pass
|
jpayne@68
|
219 bg = "yellow"
|
jpayne@68
|
220 else:
|
jpayne@68
|
221 m1 = ""
|
jpayne@68
|
222 tb = None
|
jpayne@68
|
223 bg = self.errorbg
|
jpayne@68
|
224 self.error.configure(text=m1, background=bg)
|
jpayne@68
|
225 #
|
jpayne@68
|
226 sv = self.stackviewer
|
jpayne@68
|
227 if sv:
|
jpayne@68
|
228 stack, i = self.idb.get_stack(self.frame, tb)
|
jpayne@68
|
229 sv.load_stack(stack, i)
|
jpayne@68
|
230 #
|
jpayne@68
|
231 self.show_variables(1)
|
jpayne@68
|
232 #
|
jpayne@68
|
233 if self.vsource.get():
|
jpayne@68
|
234 self.sync_source_line()
|
jpayne@68
|
235 #
|
jpayne@68
|
236 for b in self.buttons:
|
jpayne@68
|
237 b.configure(state="normal")
|
jpayne@68
|
238 #
|
jpayne@68
|
239 self.top.wakeup()
|
jpayne@68
|
240 # Nested main loop: Tkinter's main loop is not reentrant, so use
|
jpayne@68
|
241 # Tcl's vwait facility, which reenters the event loop until an
|
jpayne@68
|
242 # event handler sets the variable we're waiting on
|
jpayne@68
|
243 self.nesting_level += 1
|
jpayne@68
|
244 self.root.tk.call('vwait', '::idledebugwait')
|
jpayne@68
|
245 self.nesting_level -= 1
|
jpayne@68
|
246 #
|
jpayne@68
|
247 for b in self.buttons:
|
jpayne@68
|
248 b.configure(state="disabled")
|
jpayne@68
|
249 self.status.configure(text="")
|
jpayne@68
|
250 self.error.configure(text="", background=self.errorbg)
|
jpayne@68
|
251 self.frame = None
|
jpayne@68
|
252
|
jpayne@68
|
253 def sync_source_line(self):
|
jpayne@68
|
254 frame = self.frame
|
jpayne@68
|
255 if not frame:
|
jpayne@68
|
256 return
|
jpayne@68
|
257 filename, lineno = self.__frame2fileline(frame)
|
jpayne@68
|
258 if filename[:1] + filename[-1:] != "<>" and os.path.exists(filename):
|
jpayne@68
|
259 self.flist.gotofileline(filename, lineno)
|
jpayne@68
|
260
|
jpayne@68
|
261 def __frame2fileline(self, frame):
|
jpayne@68
|
262 code = frame.f_code
|
jpayne@68
|
263 filename = code.co_filename
|
jpayne@68
|
264 lineno = frame.f_lineno
|
jpayne@68
|
265 return filename, lineno
|
jpayne@68
|
266
|
jpayne@68
|
267 def cont(self):
|
jpayne@68
|
268 self.idb.set_continue()
|
jpayne@68
|
269 self.abort_loop()
|
jpayne@68
|
270
|
jpayne@68
|
271 def step(self):
|
jpayne@68
|
272 self.idb.set_step()
|
jpayne@68
|
273 self.abort_loop()
|
jpayne@68
|
274
|
jpayne@68
|
275 def next(self):
|
jpayne@68
|
276 self.idb.set_next(self.frame)
|
jpayne@68
|
277 self.abort_loop()
|
jpayne@68
|
278
|
jpayne@68
|
279 def ret(self):
|
jpayne@68
|
280 self.idb.set_return(self.frame)
|
jpayne@68
|
281 self.abort_loop()
|
jpayne@68
|
282
|
jpayne@68
|
283 def quit(self):
|
jpayne@68
|
284 self.idb.set_quit()
|
jpayne@68
|
285 self.abort_loop()
|
jpayne@68
|
286
|
jpayne@68
|
287 def abort_loop(self):
|
jpayne@68
|
288 self.root.tk.call('set', '::idledebugwait', '1')
|
jpayne@68
|
289
|
jpayne@68
|
290 stackviewer = None
|
jpayne@68
|
291
|
jpayne@68
|
292 def show_stack(self):
|
jpayne@68
|
293 if not self.stackviewer and self.vstack.get():
|
jpayne@68
|
294 self.stackviewer = sv = StackViewer(self.fstack, self.flist, self)
|
jpayne@68
|
295 if self.frame:
|
jpayne@68
|
296 stack, i = self.idb.get_stack(self.frame, None)
|
jpayne@68
|
297 sv.load_stack(stack, i)
|
jpayne@68
|
298 else:
|
jpayne@68
|
299 sv = self.stackviewer
|
jpayne@68
|
300 if sv and not self.vstack.get():
|
jpayne@68
|
301 self.stackviewer = None
|
jpayne@68
|
302 sv.close()
|
jpayne@68
|
303 self.fstack['height'] = 1
|
jpayne@68
|
304
|
jpayne@68
|
305 def show_source(self):
|
jpayne@68
|
306 if self.vsource.get():
|
jpayne@68
|
307 self.sync_source_line()
|
jpayne@68
|
308
|
jpayne@68
|
309 def show_frame(self, stackitem):
|
jpayne@68
|
310 self.frame = stackitem[0] # lineno is stackitem[1]
|
jpayne@68
|
311 self.show_variables()
|
jpayne@68
|
312
|
jpayne@68
|
313 localsviewer = None
|
jpayne@68
|
314 globalsviewer = None
|
jpayne@68
|
315
|
jpayne@68
|
316 def show_locals(self):
|
jpayne@68
|
317 lv = self.localsviewer
|
jpayne@68
|
318 if self.vlocals.get():
|
jpayne@68
|
319 if not lv:
|
jpayne@68
|
320 self.localsviewer = NamespaceViewer(self.flocals, "Locals")
|
jpayne@68
|
321 else:
|
jpayne@68
|
322 if lv:
|
jpayne@68
|
323 self.localsviewer = None
|
jpayne@68
|
324 lv.close()
|
jpayne@68
|
325 self.flocals['height'] = 1
|
jpayne@68
|
326 self.show_variables()
|
jpayne@68
|
327
|
jpayne@68
|
328 def show_globals(self):
|
jpayne@68
|
329 gv = self.globalsviewer
|
jpayne@68
|
330 if self.vglobals.get():
|
jpayne@68
|
331 if not gv:
|
jpayne@68
|
332 self.globalsviewer = NamespaceViewer(self.fglobals, "Globals")
|
jpayne@68
|
333 else:
|
jpayne@68
|
334 if gv:
|
jpayne@68
|
335 self.globalsviewer = None
|
jpayne@68
|
336 gv.close()
|
jpayne@68
|
337 self.fglobals['height'] = 1
|
jpayne@68
|
338 self.show_variables()
|
jpayne@68
|
339
|
jpayne@68
|
340 def show_variables(self, force=0):
|
jpayne@68
|
341 lv = self.localsviewer
|
jpayne@68
|
342 gv = self.globalsviewer
|
jpayne@68
|
343 frame = self.frame
|
jpayne@68
|
344 if not frame:
|
jpayne@68
|
345 ldict = gdict = None
|
jpayne@68
|
346 else:
|
jpayne@68
|
347 ldict = frame.f_locals
|
jpayne@68
|
348 gdict = frame.f_globals
|
jpayne@68
|
349 if lv and gv and ldict is gdict:
|
jpayne@68
|
350 ldict = None
|
jpayne@68
|
351 if lv:
|
jpayne@68
|
352 lv.load_dict(ldict, force, self.pyshell.interp.rpcclt)
|
jpayne@68
|
353 if gv:
|
jpayne@68
|
354 gv.load_dict(gdict, force, self.pyshell.interp.rpcclt)
|
jpayne@68
|
355
|
jpayne@68
|
356 def set_breakpoint_here(self, filename, lineno):
|
jpayne@68
|
357 self.idb.set_break(filename, lineno)
|
jpayne@68
|
358
|
jpayne@68
|
359 def clear_breakpoint_here(self, filename, lineno):
|
jpayne@68
|
360 self.idb.clear_break(filename, lineno)
|
jpayne@68
|
361
|
jpayne@68
|
362 def clear_file_breaks(self, filename):
|
jpayne@68
|
363 self.idb.clear_all_file_breaks(filename)
|
jpayne@68
|
364
|
jpayne@68
|
365 def load_breakpoints(self):
|
jpayne@68
|
366 "Load PyShellEditorWindow breakpoints into subprocess debugger"
|
jpayne@68
|
367 for editwin in self.pyshell.flist.inversedict:
|
jpayne@68
|
368 filename = editwin.io.filename
|
jpayne@68
|
369 try:
|
jpayne@68
|
370 for lineno in editwin.breakpoints:
|
jpayne@68
|
371 self.set_breakpoint_here(filename, lineno)
|
jpayne@68
|
372 except AttributeError:
|
jpayne@68
|
373 continue
|
jpayne@68
|
374
|
jpayne@68
|
375 class StackViewer(ScrolledList):
|
jpayne@68
|
376
|
jpayne@68
|
377 def __init__(self, master, flist, gui):
|
jpayne@68
|
378 if macosx.isAquaTk():
|
jpayne@68
|
379 # At least on with the stock AquaTk version on OSX 10.4 you'll
|
jpayne@68
|
380 # get a shaking GUI that eventually kills IDLE if the width
|
jpayne@68
|
381 # argument is specified.
|
jpayne@68
|
382 ScrolledList.__init__(self, master)
|
jpayne@68
|
383 else:
|
jpayne@68
|
384 ScrolledList.__init__(self, master, width=80)
|
jpayne@68
|
385 self.flist = flist
|
jpayne@68
|
386 self.gui = gui
|
jpayne@68
|
387 self.stack = []
|
jpayne@68
|
388
|
jpayne@68
|
389 def load_stack(self, stack, index=None):
|
jpayne@68
|
390 self.stack = stack
|
jpayne@68
|
391 self.clear()
|
jpayne@68
|
392 for i in range(len(stack)):
|
jpayne@68
|
393 frame, lineno = stack[i]
|
jpayne@68
|
394 try:
|
jpayne@68
|
395 modname = frame.f_globals["__name__"]
|
jpayne@68
|
396 except:
|
jpayne@68
|
397 modname = "?"
|
jpayne@68
|
398 code = frame.f_code
|
jpayne@68
|
399 filename = code.co_filename
|
jpayne@68
|
400 funcname = code.co_name
|
jpayne@68
|
401 import linecache
|
jpayne@68
|
402 sourceline = linecache.getline(filename, lineno)
|
jpayne@68
|
403 sourceline = sourceline.strip()
|
jpayne@68
|
404 if funcname in ("?", "", None):
|
jpayne@68
|
405 item = "%s, line %d: %s" % (modname, lineno, sourceline)
|
jpayne@68
|
406 else:
|
jpayne@68
|
407 item = "%s.%s(), line %d: %s" % (modname, funcname,
|
jpayne@68
|
408 lineno, sourceline)
|
jpayne@68
|
409 if i == index:
|
jpayne@68
|
410 item = "> " + item
|
jpayne@68
|
411 self.append(item)
|
jpayne@68
|
412 if index is not None:
|
jpayne@68
|
413 self.select(index)
|
jpayne@68
|
414
|
jpayne@68
|
415 def popup_event(self, event):
|
jpayne@68
|
416 "override base method"
|
jpayne@68
|
417 if self.stack:
|
jpayne@68
|
418 return ScrolledList.popup_event(self, event)
|
jpayne@68
|
419
|
jpayne@68
|
420 def fill_menu(self):
|
jpayne@68
|
421 "override base method"
|
jpayne@68
|
422 menu = self.menu
|
jpayne@68
|
423 menu.add_command(label="Go to source line",
|
jpayne@68
|
424 command=self.goto_source_line)
|
jpayne@68
|
425 menu.add_command(label="Show stack frame",
|
jpayne@68
|
426 command=self.show_stack_frame)
|
jpayne@68
|
427
|
jpayne@68
|
428 def on_select(self, index):
|
jpayne@68
|
429 "override base method"
|
jpayne@68
|
430 if 0 <= index < len(self.stack):
|
jpayne@68
|
431 self.gui.show_frame(self.stack[index])
|
jpayne@68
|
432
|
jpayne@68
|
433 def on_double(self, index):
|
jpayne@68
|
434 "override base method"
|
jpayne@68
|
435 self.show_source(index)
|
jpayne@68
|
436
|
jpayne@68
|
437 def goto_source_line(self):
|
jpayne@68
|
438 index = self.listbox.index("active")
|
jpayne@68
|
439 self.show_source(index)
|
jpayne@68
|
440
|
jpayne@68
|
441 def show_stack_frame(self):
|
jpayne@68
|
442 index = self.listbox.index("active")
|
jpayne@68
|
443 if 0 <= index < len(self.stack):
|
jpayne@68
|
444 self.gui.show_frame(self.stack[index])
|
jpayne@68
|
445
|
jpayne@68
|
446 def show_source(self, index):
|
jpayne@68
|
447 if not (0 <= index < len(self.stack)):
|
jpayne@68
|
448 return
|
jpayne@68
|
449 frame, lineno = self.stack[index]
|
jpayne@68
|
450 code = frame.f_code
|
jpayne@68
|
451 filename = code.co_filename
|
jpayne@68
|
452 if os.path.isfile(filename):
|
jpayne@68
|
453 edit = self.flist.open(filename)
|
jpayne@68
|
454 if edit:
|
jpayne@68
|
455 edit.gotoline(lineno)
|
jpayne@68
|
456
|
jpayne@68
|
457
|
jpayne@68
|
458 class NamespaceViewer:
|
jpayne@68
|
459
|
jpayne@68
|
460 def __init__(self, master, title, dict=None):
|
jpayne@68
|
461 width = 0
|
jpayne@68
|
462 height = 40
|
jpayne@68
|
463 if dict:
|
jpayne@68
|
464 height = 20*len(dict) # XXX 20 == observed height of Entry widget
|
jpayne@68
|
465 self.master = master
|
jpayne@68
|
466 self.title = title
|
jpayne@68
|
467 import reprlib
|
jpayne@68
|
468 self.repr = reprlib.Repr()
|
jpayne@68
|
469 self.repr.maxstring = 60
|
jpayne@68
|
470 self.repr.maxother = 60
|
jpayne@68
|
471 self.frame = frame = Frame(master)
|
jpayne@68
|
472 self.frame.pack(expand=1, fill="both")
|
jpayne@68
|
473 self.label = Label(frame, text=title, borderwidth=2, relief="groove")
|
jpayne@68
|
474 self.label.pack(fill="x")
|
jpayne@68
|
475 self.vbar = vbar = Scrollbar(frame, name="vbar")
|
jpayne@68
|
476 vbar.pack(side="right", fill="y")
|
jpayne@68
|
477 self.canvas = canvas = Canvas(frame,
|
jpayne@68
|
478 height=min(300, max(40, height)),
|
jpayne@68
|
479 scrollregion=(0, 0, width, height))
|
jpayne@68
|
480 canvas.pack(side="left", fill="both", expand=1)
|
jpayne@68
|
481 vbar["command"] = canvas.yview
|
jpayne@68
|
482 canvas["yscrollcommand"] = vbar.set
|
jpayne@68
|
483 self.subframe = subframe = Frame(canvas)
|
jpayne@68
|
484 self.sfid = canvas.create_window(0, 0, window=subframe, anchor="nw")
|
jpayne@68
|
485 self.load_dict(dict)
|
jpayne@68
|
486
|
jpayne@68
|
487 dict = -1
|
jpayne@68
|
488
|
jpayne@68
|
489 def load_dict(self, dict, force=0, rpc_client=None):
|
jpayne@68
|
490 if dict is self.dict and not force:
|
jpayne@68
|
491 return
|
jpayne@68
|
492 subframe = self.subframe
|
jpayne@68
|
493 frame = self.frame
|
jpayne@68
|
494 for c in list(subframe.children.values()):
|
jpayne@68
|
495 c.destroy()
|
jpayne@68
|
496 self.dict = None
|
jpayne@68
|
497 if not dict:
|
jpayne@68
|
498 l = Label(subframe, text="None")
|
jpayne@68
|
499 l.grid(row=0, column=0)
|
jpayne@68
|
500 else:
|
jpayne@68
|
501 #names = sorted(dict)
|
jpayne@68
|
502 ###
|
jpayne@68
|
503 # Because of (temporary) limitations on the dict_keys type (not yet
|
jpayne@68
|
504 # public or pickleable), have the subprocess to send a list of
|
jpayne@68
|
505 # keys, not a dict_keys object. sorted() will take a dict_keys
|
jpayne@68
|
506 # (no subprocess) or a list.
|
jpayne@68
|
507 #
|
jpayne@68
|
508 # There is also an obscure bug in sorted(dict) where the
|
jpayne@68
|
509 # interpreter gets into a loop requesting non-existing dict[0],
|
jpayne@68
|
510 # dict[1], dict[2], etc from the debugger_r.DictProxy.
|
jpayne@68
|
511 ###
|
jpayne@68
|
512 keys_list = dict.keys()
|
jpayne@68
|
513 names = sorted(keys_list)
|
jpayne@68
|
514 ###
|
jpayne@68
|
515 row = 0
|
jpayne@68
|
516 for name in names:
|
jpayne@68
|
517 value = dict[name]
|
jpayne@68
|
518 svalue = self.repr.repr(value) # repr(value)
|
jpayne@68
|
519 # Strip extra quotes caused by calling repr on the (already)
|
jpayne@68
|
520 # repr'd value sent across the RPC interface:
|
jpayne@68
|
521 if rpc_client:
|
jpayne@68
|
522 svalue = svalue[1:-1]
|
jpayne@68
|
523 l = Label(subframe, text=name)
|
jpayne@68
|
524 l.grid(row=row, column=0, sticky="nw")
|
jpayne@68
|
525 l = Entry(subframe, width=0, borderwidth=0)
|
jpayne@68
|
526 l.insert(0, svalue)
|
jpayne@68
|
527 l.grid(row=row, column=1, sticky="nw")
|
jpayne@68
|
528 row = row+1
|
jpayne@68
|
529 self.dict = dict
|
jpayne@68
|
530 # XXX Could we use a <Configure> callback for the following?
|
jpayne@68
|
531 subframe.update_idletasks() # Alas!
|
jpayne@68
|
532 width = subframe.winfo_reqwidth()
|
jpayne@68
|
533 height = subframe.winfo_reqheight()
|
jpayne@68
|
534 canvas = self.canvas
|
jpayne@68
|
535 self.canvas["scrollregion"] = (0, 0, width, height)
|
jpayne@68
|
536 if height > 300:
|
jpayne@68
|
537 canvas["height"] = 300
|
jpayne@68
|
538 frame.pack(expand=1)
|
jpayne@68
|
539 else:
|
jpayne@68
|
540 canvas["height"] = height
|
jpayne@68
|
541 frame.pack(expand=0)
|
jpayne@68
|
542
|
jpayne@68
|
543 def close(self):
|
jpayne@68
|
544 self.frame.destroy()
|
jpayne@68
|
545
|
jpayne@68
|
546 if __name__ == "__main__":
|
jpayne@68
|
547 from unittest import main
|
jpayne@68
|
548 main('idlelib.idle_test.test_debugger', verbosity=2, exit=False)
|
jpayne@68
|
549
|
jpayne@68
|
550 # TODO: htest?
|