jpayne@69: import bdb jpayne@69: import os jpayne@69: jpayne@69: from tkinter import * jpayne@69: from tkinter.ttk import Frame, Scrollbar jpayne@69: jpayne@69: from idlelib import macosx jpayne@69: from idlelib.scrolledlist import ScrolledList jpayne@69: from idlelib.window import ListedToplevel jpayne@69: jpayne@69: jpayne@69: class Idb(bdb.Bdb): jpayne@69: jpayne@69: def __init__(self, gui): jpayne@69: self.gui = gui # An instance of Debugger or proxy of remote. jpayne@69: bdb.Bdb.__init__(self) jpayne@69: jpayne@69: def user_line(self, frame): jpayne@69: if self.in_rpc_code(frame): jpayne@69: self.set_step() jpayne@69: return jpayne@69: message = self.__frame2message(frame) jpayne@69: try: jpayne@69: self.gui.interaction(message, frame) jpayne@69: except TclError: # When closing debugger window with [x] in 3.x jpayne@69: pass jpayne@69: jpayne@69: def user_exception(self, frame, info): jpayne@69: if self.in_rpc_code(frame): jpayne@69: self.set_step() jpayne@69: return jpayne@69: message = self.__frame2message(frame) jpayne@69: self.gui.interaction(message, frame, info) jpayne@69: jpayne@69: def in_rpc_code(self, frame): jpayne@69: if frame.f_code.co_filename.count('rpc.py'): jpayne@69: return True jpayne@69: else: jpayne@69: prev_frame = frame.f_back jpayne@69: prev_name = prev_frame.f_code.co_filename jpayne@69: if 'idlelib' in prev_name and 'debugger' in prev_name: jpayne@69: # catch both idlelib/debugger.py and idlelib/debugger_r.py jpayne@69: # on both Posix and Windows jpayne@69: return False jpayne@69: return self.in_rpc_code(prev_frame) jpayne@69: jpayne@69: def __frame2message(self, frame): jpayne@69: code = frame.f_code jpayne@69: filename = code.co_filename jpayne@69: lineno = frame.f_lineno jpayne@69: basename = os.path.basename(filename) jpayne@69: message = "%s:%s" % (basename, lineno) jpayne@69: if code.co_name != "?": jpayne@69: message = "%s: %s()" % (message, code.co_name) jpayne@69: return message jpayne@69: jpayne@69: jpayne@69: class Debugger: jpayne@69: jpayne@69: vstack = vsource = vlocals = vglobals = None jpayne@69: jpayne@69: def __init__(self, pyshell, idb=None): jpayne@69: if idb is None: jpayne@69: idb = Idb(self) jpayne@69: self.pyshell = pyshell jpayne@69: self.idb = idb # If passed, a proxy of remote instance. jpayne@69: self.frame = None jpayne@69: self.make_gui() jpayne@69: self.interacting = 0 jpayne@69: self.nesting_level = 0 jpayne@69: jpayne@69: def run(self, *args): jpayne@69: # Deal with the scenario where we've already got a program running jpayne@69: # in the debugger and we want to start another. If that is the case, jpayne@69: # our second 'run' was invoked from an event dispatched not from jpayne@69: # the main event loop, but from the nested event loop in 'interaction' jpayne@69: # below. So our stack looks something like this: jpayne@69: # outer main event loop jpayne@69: # run() jpayne@69: # jpayne@69: # callback to debugger's interaction() jpayne@69: # nested event loop jpayne@69: # run() for second command jpayne@69: # jpayne@69: # This kind of nesting of event loops causes all kinds of problems jpayne@69: # (see e.g. issue #24455) especially when dealing with running as a jpayne@69: # subprocess, where there's all kinds of extra stuff happening in jpayne@69: # there - insert a traceback.print_stack() to check it out. jpayne@69: # jpayne@69: # By this point, we've already called restart_subprocess() in jpayne@69: # ScriptBinding. However, we also need to unwind the stack back to jpayne@69: # that outer event loop. To accomplish this, we: jpayne@69: # - return immediately from the nested run() jpayne@69: # - abort_loop ensures the nested event loop will terminate jpayne@69: # - the debugger's interaction routine completes normally jpayne@69: # - the restart_subprocess() will have taken care of stopping jpayne@69: # the running program, which will also let the outer run complete jpayne@69: # jpayne@69: # That leaves us back at the outer main event loop, at which point our jpayne@69: # after event can fire, and we'll come back to this routine with a jpayne@69: # clean stack. jpayne@69: if self.nesting_level > 0: jpayne@69: self.abort_loop() jpayne@69: self.root.after(100, lambda: self.run(*args)) jpayne@69: return jpayne@69: try: jpayne@69: self.interacting = 1 jpayne@69: return self.idb.run(*args) jpayne@69: finally: jpayne@69: self.interacting = 0 jpayne@69: jpayne@69: def close(self, event=None): jpayne@69: try: jpayne@69: self.quit() jpayne@69: except Exception: jpayne@69: pass jpayne@69: if self.interacting: jpayne@69: self.top.bell() jpayne@69: return jpayne@69: if self.stackviewer: jpayne@69: self.stackviewer.close(); self.stackviewer = None jpayne@69: # Clean up pyshell if user clicked debugger control close widget. jpayne@69: # (Causes a harmless extra cycle through close_debugger() if user jpayne@69: # toggled debugger from pyshell Debug menu) jpayne@69: self.pyshell.close_debugger() jpayne@69: # Now close the debugger control window.... jpayne@69: self.top.destroy() jpayne@69: jpayne@69: def make_gui(self): jpayne@69: pyshell = self.pyshell jpayne@69: self.flist = pyshell.flist jpayne@69: self.root = root = pyshell.root jpayne@69: self.top = top = ListedToplevel(root) jpayne@69: self.top.wm_title("Debug Control") jpayne@69: self.top.wm_iconname("Debug") jpayne@69: top.wm_protocol("WM_DELETE_WINDOW", self.close) jpayne@69: self.top.bind("", self.close) jpayne@69: # jpayne@69: self.bframe = bframe = Frame(top) jpayne@69: self.bframe.pack(anchor="w") jpayne@69: self.buttons = bl = [] jpayne@69: # jpayne@69: self.bcont = b = Button(bframe, text="Go", command=self.cont) jpayne@69: bl.append(b) jpayne@69: self.bstep = b = Button(bframe, text="Step", command=self.step) jpayne@69: bl.append(b) jpayne@69: self.bnext = b = Button(bframe, text="Over", command=self.next) jpayne@69: bl.append(b) jpayne@69: self.bret = b = Button(bframe, text="Out", command=self.ret) jpayne@69: bl.append(b) jpayne@69: self.bret = b = Button(bframe, text="Quit", command=self.quit) jpayne@69: bl.append(b) jpayne@69: # jpayne@69: for b in bl: jpayne@69: b.configure(state="disabled") jpayne@69: b.pack(side="left") jpayne@69: # jpayne@69: self.cframe = cframe = Frame(bframe) jpayne@69: self.cframe.pack(side="left") jpayne@69: # jpayne@69: if not self.vstack: jpayne@69: self.__class__.vstack = BooleanVar(top) jpayne@69: self.vstack.set(1) jpayne@69: self.bstack = Checkbutton(cframe, jpayne@69: text="Stack", command=self.show_stack, variable=self.vstack) jpayne@69: self.bstack.grid(row=0, column=0) jpayne@69: if not self.vsource: jpayne@69: self.__class__.vsource = BooleanVar(top) jpayne@69: self.bsource = Checkbutton(cframe, jpayne@69: text="Source", command=self.show_source, variable=self.vsource) jpayne@69: self.bsource.grid(row=0, column=1) jpayne@69: if not self.vlocals: jpayne@69: self.__class__.vlocals = BooleanVar(top) jpayne@69: self.vlocals.set(1) jpayne@69: self.blocals = Checkbutton(cframe, jpayne@69: text="Locals", command=self.show_locals, variable=self.vlocals) jpayne@69: self.blocals.grid(row=1, column=0) jpayne@69: if not self.vglobals: jpayne@69: self.__class__.vglobals = BooleanVar(top) jpayne@69: self.bglobals = Checkbutton(cframe, jpayne@69: text="Globals", command=self.show_globals, variable=self.vglobals) jpayne@69: self.bglobals.grid(row=1, column=1) jpayne@69: # jpayne@69: self.status = Label(top, anchor="w") jpayne@69: self.status.pack(anchor="w") jpayne@69: self.error = Label(top, anchor="w") jpayne@69: self.error.pack(anchor="w", fill="x") jpayne@69: self.errorbg = self.error.cget("background") jpayne@69: # jpayne@69: self.fstack = Frame(top, height=1) jpayne@69: self.fstack.pack(expand=1, fill="both") jpayne@69: self.flocals = Frame(top) jpayne@69: self.flocals.pack(expand=1, fill="both") jpayne@69: self.fglobals = Frame(top, height=1) jpayne@69: self.fglobals.pack(expand=1, fill="both") jpayne@69: # jpayne@69: if self.vstack.get(): jpayne@69: self.show_stack() jpayne@69: if self.vlocals.get(): jpayne@69: self.show_locals() jpayne@69: if self.vglobals.get(): jpayne@69: self.show_globals() jpayne@69: jpayne@69: def interaction(self, message, frame, info=None): jpayne@69: self.frame = frame jpayne@69: self.status.configure(text=message) jpayne@69: # jpayne@69: if info: jpayne@69: type, value, tb = info jpayne@69: try: jpayne@69: m1 = type.__name__ jpayne@69: except AttributeError: jpayne@69: m1 = "%s" % str(type) jpayne@69: if value is not None: jpayne@69: try: jpayne@69: m1 = "%s: %s" % (m1, str(value)) jpayne@69: except: jpayne@69: pass jpayne@69: bg = "yellow" jpayne@69: else: jpayne@69: m1 = "" jpayne@69: tb = None jpayne@69: bg = self.errorbg jpayne@69: self.error.configure(text=m1, background=bg) jpayne@69: # jpayne@69: sv = self.stackviewer jpayne@69: if sv: jpayne@69: stack, i = self.idb.get_stack(self.frame, tb) jpayne@69: sv.load_stack(stack, i) jpayne@69: # jpayne@69: self.show_variables(1) jpayne@69: # jpayne@69: if self.vsource.get(): jpayne@69: self.sync_source_line() jpayne@69: # jpayne@69: for b in self.buttons: jpayne@69: b.configure(state="normal") jpayne@69: # jpayne@69: self.top.wakeup() jpayne@69: # Nested main loop: Tkinter's main loop is not reentrant, so use jpayne@69: # Tcl's vwait facility, which reenters the event loop until an jpayne@69: # event handler sets the variable we're waiting on jpayne@69: self.nesting_level += 1 jpayne@69: self.root.tk.call('vwait', '::idledebugwait') jpayne@69: self.nesting_level -= 1 jpayne@69: # jpayne@69: for b in self.buttons: jpayne@69: b.configure(state="disabled") jpayne@69: self.status.configure(text="") jpayne@69: self.error.configure(text="", background=self.errorbg) jpayne@69: self.frame = None jpayne@69: jpayne@69: def sync_source_line(self): jpayne@69: frame = self.frame jpayne@69: if not frame: jpayne@69: return jpayne@69: filename, lineno = self.__frame2fileline(frame) jpayne@69: if filename[:1] + filename[-1:] != "<>" and os.path.exists(filename): jpayne@69: self.flist.gotofileline(filename, lineno) jpayne@69: jpayne@69: def __frame2fileline(self, frame): jpayne@69: code = frame.f_code jpayne@69: filename = code.co_filename jpayne@69: lineno = frame.f_lineno jpayne@69: return filename, lineno jpayne@69: jpayne@69: def cont(self): jpayne@69: self.idb.set_continue() jpayne@69: self.abort_loop() jpayne@69: jpayne@69: def step(self): jpayne@69: self.idb.set_step() jpayne@69: self.abort_loop() jpayne@69: jpayne@69: def next(self): jpayne@69: self.idb.set_next(self.frame) jpayne@69: self.abort_loop() jpayne@69: jpayne@69: def ret(self): jpayne@69: self.idb.set_return(self.frame) jpayne@69: self.abort_loop() jpayne@69: jpayne@69: def quit(self): jpayne@69: self.idb.set_quit() jpayne@69: self.abort_loop() jpayne@69: jpayne@69: def abort_loop(self): jpayne@69: self.root.tk.call('set', '::idledebugwait', '1') jpayne@69: jpayne@69: stackviewer = None jpayne@69: jpayne@69: def show_stack(self): jpayne@69: if not self.stackviewer and self.vstack.get(): jpayne@69: self.stackviewer = sv = StackViewer(self.fstack, self.flist, self) jpayne@69: if self.frame: jpayne@69: stack, i = self.idb.get_stack(self.frame, None) jpayne@69: sv.load_stack(stack, i) jpayne@69: else: jpayne@69: sv = self.stackviewer jpayne@69: if sv and not self.vstack.get(): jpayne@69: self.stackviewer = None jpayne@69: sv.close() jpayne@69: self.fstack['height'] = 1 jpayne@69: jpayne@69: def show_source(self): jpayne@69: if self.vsource.get(): jpayne@69: self.sync_source_line() jpayne@69: jpayne@69: def show_frame(self, stackitem): jpayne@69: self.frame = stackitem[0] # lineno is stackitem[1] jpayne@69: self.show_variables() jpayne@69: jpayne@69: localsviewer = None jpayne@69: globalsviewer = None jpayne@69: jpayne@69: def show_locals(self): jpayne@69: lv = self.localsviewer jpayne@69: if self.vlocals.get(): jpayne@69: if not lv: jpayne@69: self.localsviewer = NamespaceViewer(self.flocals, "Locals") jpayne@69: else: jpayne@69: if lv: jpayne@69: self.localsviewer = None jpayne@69: lv.close() jpayne@69: self.flocals['height'] = 1 jpayne@69: self.show_variables() jpayne@69: jpayne@69: def show_globals(self): jpayne@69: gv = self.globalsviewer jpayne@69: if self.vglobals.get(): jpayne@69: if not gv: jpayne@69: self.globalsviewer = NamespaceViewer(self.fglobals, "Globals") jpayne@69: else: jpayne@69: if gv: jpayne@69: self.globalsviewer = None jpayne@69: gv.close() jpayne@69: self.fglobals['height'] = 1 jpayne@69: self.show_variables() jpayne@69: jpayne@69: def show_variables(self, force=0): jpayne@69: lv = self.localsviewer jpayne@69: gv = self.globalsviewer jpayne@69: frame = self.frame jpayne@69: if not frame: jpayne@69: ldict = gdict = None jpayne@69: else: jpayne@69: ldict = frame.f_locals jpayne@69: gdict = frame.f_globals jpayne@69: if lv and gv and ldict is gdict: jpayne@69: ldict = None jpayne@69: if lv: jpayne@69: lv.load_dict(ldict, force, self.pyshell.interp.rpcclt) jpayne@69: if gv: jpayne@69: gv.load_dict(gdict, force, self.pyshell.interp.rpcclt) jpayne@69: jpayne@69: def set_breakpoint_here(self, filename, lineno): jpayne@69: self.idb.set_break(filename, lineno) jpayne@69: jpayne@69: def clear_breakpoint_here(self, filename, lineno): jpayne@69: self.idb.clear_break(filename, lineno) jpayne@69: jpayne@69: def clear_file_breaks(self, filename): jpayne@69: self.idb.clear_all_file_breaks(filename) jpayne@69: jpayne@69: def load_breakpoints(self): jpayne@69: "Load PyShellEditorWindow breakpoints into subprocess debugger" jpayne@69: for editwin in self.pyshell.flist.inversedict: jpayne@69: filename = editwin.io.filename jpayne@69: try: jpayne@69: for lineno in editwin.breakpoints: jpayne@69: self.set_breakpoint_here(filename, lineno) jpayne@69: except AttributeError: jpayne@69: continue jpayne@69: jpayne@69: class StackViewer(ScrolledList): jpayne@69: jpayne@69: def __init__(self, master, flist, gui): jpayne@69: if macosx.isAquaTk(): jpayne@69: # At least on with the stock AquaTk version on OSX 10.4 you'll jpayne@69: # get a shaking GUI that eventually kills IDLE if the width jpayne@69: # argument is specified. jpayne@69: ScrolledList.__init__(self, master) jpayne@69: else: jpayne@69: ScrolledList.__init__(self, master, width=80) jpayne@69: self.flist = flist jpayne@69: self.gui = gui jpayne@69: self.stack = [] jpayne@69: jpayne@69: def load_stack(self, stack, index=None): jpayne@69: self.stack = stack jpayne@69: self.clear() jpayne@69: for i in range(len(stack)): jpayne@69: frame, lineno = stack[i] jpayne@69: try: jpayne@69: modname = frame.f_globals["__name__"] jpayne@69: except: jpayne@69: modname = "?" jpayne@69: code = frame.f_code jpayne@69: filename = code.co_filename jpayne@69: funcname = code.co_name jpayne@69: import linecache jpayne@69: sourceline = linecache.getline(filename, lineno) jpayne@69: sourceline = sourceline.strip() jpayne@69: if funcname in ("?", "", None): jpayne@69: item = "%s, line %d: %s" % (modname, lineno, sourceline) jpayne@69: else: jpayne@69: item = "%s.%s(), line %d: %s" % (modname, funcname, jpayne@69: lineno, sourceline) jpayne@69: if i == index: jpayne@69: item = "> " + item jpayne@69: self.append(item) jpayne@69: if index is not None: jpayne@69: self.select(index) jpayne@69: jpayne@69: def popup_event(self, event): jpayne@69: "override base method" jpayne@69: if self.stack: jpayne@69: return ScrolledList.popup_event(self, event) jpayne@69: jpayne@69: def fill_menu(self): jpayne@69: "override base method" jpayne@69: menu = self.menu jpayne@69: menu.add_command(label="Go to source line", jpayne@69: command=self.goto_source_line) jpayne@69: menu.add_command(label="Show stack frame", jpayne@69: command=self.show_stack_frame) jpayne@69: jpayne@69: def on_select(self, index): jpayne@69: "override base method" jpayne@69: if 0 <= index < len(self.stack): jpayne@69: self.gui.show_frame(self.stack[index]) jpayne@69: jpayne@69: def on_double(self, index): jpayne@69: "override base method" jpayne@69: self.show_source(index) jpayne@69: jpayne@69: def goto_source_line(self): jpayne@69: index = self.listbox.index("active") jpayne@69: self.show_source(index) jpayne@69: jpayne@69: def show_stack_frame(self): jpayne@69: index = self.listbox.index("active") jpayne@69: if 0 <= index < len(self.stack): jpayne@69: self.gui.show_frame(self.stack[index]) jpayne@69: jpayne@69: def show_source(self, index): jpayne@69: if not (0 <= index < len(self.stack)): jpayne@69: return jpayne@69: frame, lineno = self.stack[index] jpayne@69: code = frame.f_code jpayne@69: filename = code.co_filename jpayne@69: if os.path.isfile(filename): jpayne@69: edit = self.flist.open(filename) jpayne@69: if edit: jpayne@69: edit.gotoline(lineno) jpayne@69: jpayne@69: jpayne@69: class NamespaceViewer: jpayne@69: jpayne@69: def __init__(self, master, title, dict=None): jpayne@69: width = 0 jpayne@69: height = 40 jpayne@69: if dict: jpayne@69: height = 20*len(dict) # XXX 20 == observed height of Entry widget jpayne@69: self.master = master jpayne@69: self.title = title jpayne@69: import reprlib jpayne@69: self.repr = reprlib.Repr() jpayne@69: self.repr.maxstring = 60 jpayne@69: self.repr.maxother = 60 jpayne@69: self.frame = frame = Frame(master) jpayne@69: self.frame.pack(expand=1, fill="both") jpayne@69: self.label = Label(frame, text=title, borderwidth=2, relief="groove") jpayne@69: self.label.pack(fill="x") jpayne@69: self.vbar = vbar = Scrollbar(frame, name="vbar") jpayne@69: vbar.pack(side="right", fill="y") jpayne@69: self.canvas = canvas = Canvas(frame, jpayne@69: height=min(300, max(40, height)), jpayne@69: scrollregion=(0, 0, width, height)) jpayne@69: canvas.pack(side="left", fill="both", expand=1) jpayne@69: vbar["command"] = canvas.yview jpayne@69: canvas["yscrollcommand"] = vbar.set jpayne@69: self.subframe = subframe = Frame(canvas) jpayne@69: self.sfid = canvas.create_window(0, 0, window=subframe, anchor="nw") jpayne@69: self.load_dict(dict) jpayne@69: jpayne@69: dict = -1 jpayne@69: jpayne@69: def load_dict(self, dict, force=0, rpc_client=None): jpayne@69: if dict is self.dict and not force: jpayne@69: return jpayne@69: subframe = self.subframe jpayne@69: frame = self.frame jpayne@69: for c in list(subframe.children.values()): jpayne@69: c.destroy() jpayne@69: self.dict = None jpayne@69: if not dict: jpayne@69: l = Label(subframe, text="None") jpayne@69: l.grid(row=0, column=0) jpayne@69: else: jpayne@69: #names = sorted(dict) jpayne@69: ### jpayne@69: # Because of (temporary) limitations on the dict_keys type (not yet jpayne@69: # public or pickleable), have the subprocess to send a list of jpayne@69: # keys, not a dict_keys object. sorted() will take a dict_keys jpayne@69: # (no subprocess) or a list. jpayne@69: # jpayne@69: # There is also an obscure bug in sorted(dict) where the jpayne@69: # interpreter gets into a loop requesting non-existing dict[0], jpayne@69: # dict[1], dict[2], etc from the debugger_r.DictProxy. jpayne@69: ### jpayne@69: keys_list = dict.keys() jpayne@69: names = sorted(keys_list) jpayne@69: ### jpayne@69: row = 0 jpayne@69: for name in names: jpayne@69: value = dict[name] jpayne@69: svalue = self.repr.repr(value) # repr(value) jpayne@69: # Strip extra quotes caused by calling repr on the (already) jpayne@69: # repr'd value sent across the RPC interface: jpayne@69: if rpc_client: jpayne@69: svalue = svalue[1:-1] jpayne@69: l = Label(subframe, text=name) jpayne@69: l.grid(row=row, column=0, sticky="nw") jpayne@69: l = Entry(subframe, width=0, borderwidth=0) jpayne@69: l.insert(0, svalue) jpayne@69: l.grid(row=row, column=1, sticky="nw") jpayne@69: row = row+1 jpayne@69: self.dict = dict jpayne@69: # XXX Could we use a callback for the following? jpayne@69: subframe.update_idletasks() # Alas! jpayne@69: width = subframe.winfo_reqwidth() jpayne@69: height = subframe.winfo_reqheight() jpayne@69: canvas = self.canvas jpayne@69: self.canvas["scrollregion"] = (0, 0, width, height) jpayne@69: if height > 300: jpayne@69: canvas["height"] = 300 jpayne@69: frame.pack(expand=1) jpayne@69: else: jpayne@69: canvas["height"] = height jpayne@69: frame.pack(expand=0) jpayne@69: jpayne@69: def close(self): jpayne@69: self.frame.destroy() jpayne@69: jpayne@69: if __name__ == "__main__": jpayne@69: from unittest import main jpayne@69: main('idlelib.idle_test.test_debugger', verbosity=2, exit=False) jpayne@69: jpayne@69: # TODO: htest?