from idlelib.WindowList import ListedToplevel
from idlelib.ScrolledList import ScrolledList
from idlelib import macosxSupport
def user_line(self, frame):
if self.in_rpc_code(frame):
message = self.__frame2message(frame)
self.gui.interaction(message, frame)
except TclError: # When closing debugger window with [x] in 3.x
def user_exception(self, frame, info):
if self.in_rpc_code(frame):
message = self.__frame2message(frame)
self.gui.interaction(message, frame, info)
def in_rpc_code(self, frame):
if frame.f_code.co_filename.count('rpc.py'):
prev_frame = frame.f_back
if prev_frame.f_code.co_filename.count('Debugger.py'):
# (that test will catch both Debugger.py and RemoteDebugger.py)
return self.in_rpc_code(prev_frame)
def __frame2message(self, frame):
filename = code.co_filename
basename = os.path.basename(filename)
message = "%s:%s" % (basename, lineno)
message = "%s: %s()" % (message, code.co_name)
vstack = vsource = vlocals = vglobals = None
def __init__(self, pyshell, idb=None):
# Deal with the scenario where we've already got a program running
# in the debugger and we want to start another. If that is the case,
# our second 'run' was invoked from an event dispatched not from
# the main event loop, but from the nested event loop in 'interaction'
# below. So our stack looks something like this:
# <running program with traces>
# callback to debugger's interaction()
# run() for second command
# This kind of nesting of event loops causes all kinds of problems
# (see e.g. issue #24455) especially when dealing with running as a
# subprocess, where there's all kinds of extra stuff happening in
# there - insert a traceback.print_stack() to check it out.
# By this point, we've already called restart_subprocess() in
# ScriptBinding. However, we also need to unwind the stack back to
# that outer event loop. To accomplish this, we:
# - return immediately from the nested run()
# - abort_loop ensures the nested event loop will terminate
# - the debugger's interaction routine completes normally
# - the restart_subprocess() will have taken care of stopping
# the running program, which will also let the outer run complete
# That leaves us back at the outer main event loop, at which point our
# after event can fire, and we'll come back to this routine with a
if self.nesting_level > 0:
self.root.after(100, lambda: self.run(*args))
return self.idb.run(*args)
def close(self, event=None):
self.stackviewer.close(); self.stackviewer = None
# Clean up pyshell if user clicked debugger control close widget.
# (Causes a harmless extra cycle through close_debugger() if user
# toggled debugger from pyshell Debug menu)
self.pyshell.close_debugger()
# Now close the debugger control window....
self.flist = pyshell.flist
self.root = root = pyshell.root
self.top = top = ListedToplevel(root)
self.top.wm_title("Debug Control")
self.top.wm_iconname("Debug")
top.wm_protocol("WM_DELETE_WINDOW", self.close)
self.top.bind("<Escape>", self.close)
self.bframe = bframe = Frame(top)
self.bframe.pack(anchor="w")
self.bcont = b = Button(bframe, text="Go", command=self.cont)
self.bstep = b = Button(bframe, text="Step", command=self.step)
self.bnext = b = Button(bframe, text="Over", command=self.next)
self.bret = b = Button(bframe, text="Out", command=self.ret)
self.bret = b = Button(bframe, text="Quit", command=self.quit)
b.configure(state="disabled")
self.cframe = cframe = Frame(bframe)
self.cframe.pack(side="left")
self.__class__.vstack = BooleanVar(top)
self.bstack = Checkbutton(cframe,
text="Stack", command=self.show_stack, variable=self.vstack)
self.bstack.grid(row=0, column=0)
self.__class__.vsource = BooleanVar(top)
self.bsource = Checkbutton(cframe,
text="Source", command=self.show_source, variable=self.vsource)
self.bsource.grid(row=0, column=1)
self.__class__.vlocals = BooleanVar(top)
self.blocals = Checkbutton(cframe,
text="Locals", command=self.show_locals, variable=self.vlocals)
self.blocals.grid(row=1, column=0)
self.__class__.vglobals = BooleanVar(top)
self.bglobals = Checkbutton(cframe,
text="Globals", command=self.show_globals, variable=self.vglobals)
self.bglobals.grid(row=1, column=1)
self.status = Label(top, anchor="w")
self.status.pack(anchor="w")
self.error = Label(top, anchor="w")
self.error.pack(anchor="w", fill="x")
self.errorbg = self.error.cget("background")
self.fstack = Frame(top, height=1)
self.fstack.pack(expand=1, fill="both")
self.flocals = Frame(top)
self.flocals.pack(expand=1, fill="both")
self.fglobals = Frame(top, height=1)
self.fglobals.pack(expand=1, fill="both")
def interaction(self, message, frame, info=None):
self.status.configure(text=message)
m1 = "%s: %s" % (m1, str(value))
self.error.configure(text=m1, background=bg)
stack, i = self.idb.get_stack(self.frame, tb)
b.configure(state="normal")
# Nested main loop: Tkinter's main loop is not reentrant, so use
# Tcl's vwait facility, which reenters the event loop until an
# event handler sets the variable we're waiting on
self.root.tk.call('vwait', '::idledebugwait')
b.configure(state="disabled")
self.status.configure(text="")
self.error.configure(text="", background=self.errorbg)
def sync_source_line(self):
filename, lineno = self.__frame2fileline(frame)
if filename[:1] + filename[-1:] != "<>" and os.path.exists(filename):
self.flist.gotofileline(filename, lineno)
def __frame2fileline(self, frame):
filename = code.co_filename
self.idb.set_next(self.frame)
self.idb.set_return(self.frame)
self.root.tk.call('set', '::idledebugwait', '1')
if not self.stackviewer and self.vstack.get():
self.stackviewer = sv = StackViewer(self.fstack, self.flist, self)
stack, i = self.idb.get_stack(self.frame, None)
if sv and not self.vstack.get():
self.fstack['height'] = 1
def show_frame(self, stackitem):
self.frame = stackitem[0] # lineno is stackitem[1]
self.localsviewer = NamespaceViewer(self.flocals, "Locals")
self.flocals['height'] = 1
self.globalsviewer = NamespaceViewer(self.fglobals, "Globals")
self.globalsviewer = None
self.fglobals['height'] = 1
def show_variables(self, force=0):
if lv and gv and ldict is gdict:
lv.load_dict(ldict, force, self.pyshell.interp.rpcclt)
gv.load_dict(gdict, force, self.pyshell.interp.rpcclt)
def set_breakpoint_here(self, filename, lineno):
self.idb.set_break(filename, lineno)
def clear_breakpoint_here(self, filename, lineno):
self.idb.clear_break(filename, lineno)
def clear_file_breaks(self, filename):
self.idb.clear_all_file_breaks(filename)
def load_breakpoints(self):
"Load PyShellEditorWindow breakpoints into subprocess debugger"
pyshell_edit_windows = self.pyshell.flist.inversedict.keys()
for editwin in pyshell_edit_windows:
filename = editwin.io.filename
for lineno in editwin.breakpoints:
self.set_breakpoint_here(filename, lineno)
class StackViewer(ScrolledList):
def __init__(self, master, flist, gui):
if macosxSupport.isAquaTk():
# At least on with the stock AquaTk version on OSX 10.4 you'll
# get a shaking GUI that eventually kills IDLE if the width
ScrolledList.__init__(self, master)
ScrolledList.__init__(self, master, width=80)
def load_stack(self, stack, index=None):
for i in range(len(stack)):
modname = frame.f_globals["__name__"]
filename = code.co_filename
sourceline = linecache.getline(filename, lineno)
sourceline = string.strip(sourceline)
if funcname in ("?", "", None):
item = "%s, line %d: %s" % (modname, lineno, sourceline)
item = "%s.%s(), line %d: %s" % (modname, funcname,
def popup_event(self, event):
return ScrolledList.popup_event(self, event)
menu.add_command(label="Go to source line",
command=self.goto_source_line)
menu.add_command(label="Show stack frame",
command=self.show_stack_frame)
def on_select(self, index):
if 0 <= index < len(self.stack):
self.gui.show_frame(self.stack[index])
def on_double(self, index):
def goto_source_line(self):
index = self.listbox.index("active")
def show_stack_frame(self):
index = self.listbox.index("active")
if 0 <= index < len(self.stack):
self.gui.show_frame(self.stack[index])
def show_source(self, index):
if not (0 <= index < len(self.stack)):
frame, lineno = self.stack[index]
filename = code.co_filename
if os.path.isfile(filename):
edit = self.flist.open(filename)
def __init__(self, master, title, dict=None):
height = 20*len(dict) # XXX 20 == observed height of Entry widget
self.frame = frame = Frame(master)
self.frame.pack(expand=1, fill="both")
self.label = Label(frame, text=title, borderwidth=2, relief="groove")
self.label.pack(fill="x")
self.vbar = vbar = Scrollbar(frame, name="vbar")
vbar.pack(side="right", fill="y")
self.canvas = canvas = Canvas(frame,
height=min(300, max(40, height)),
scrollregion=(0, 0, width, height))
canvas.pack(side="left", fill="both", expand=1)
vbar["command"] = canvas.yview
canvas["yscrollcommand"] = vbar.set
self.subframe = subframe = Frame(canvas)
self.sfid = canvas.create_window(0, 0, window=subframe, anchor="nw")
def load_dict(self, dict, force=0, rpc_client=None):
if dict is self.dict and not force:
for c in subframe.children.values():
l = Label(subframe, text="None")