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