1998-10-13 20:51:13 -03:00
|
|
|
import os
|
|
|
|
import bdb
|
|
|
|
import traceback
|
|
|
|
from Tkinter import *
|
1999-01-02 17:28:54 -04:00
|
|
|
from WindowList import ListedToplevel
|
1998-10-13 20:51:13 -03:00
|
|
|
|
1998-10-16 15:59:39 -03:00
|
|
|
import StackViewer
|
|
|
|
|
1998-10-13 20:51:13 -03:00
|
|
|
|
|
|
|
class Debugger(bdb.Bdb):
|
1999-01-02 17:28:54 -04:00
|
|
|
|
1998-10-14 00:43:05 -03:00
|
|
|
interacting = 0
|
1999-01-02 17:28:54 -04:00
|
|
|
|
1998-10-16 18:09:05 -03:00
|
|
|
vstack = vsource = vlocals = vglobals = None
|
1999-01-02 17:28:54 -04:00
|
|
|
|
1998-10-13 20:51:13 -03:00
|
|
|
def __init__(self, pyshell):
|
|
|
|
bdb.Bdb.__init__(self)
|
|
|
|
self.pyshell = pyshell
|
|
|
|
self.make_gui()
|
1999-02-01 15:35:33 -04:00
|
|
|
|
|
|
|
def canonic(self, filename):
|
|
|
|
# Canonicalize filename -- called by Bdb
|
|
|
|
return os.path.normcase(os.path.abspath(filename))
|
1999-01-02 17:28:54 -04:00
|
|
|
|
1999-01-11 10:46:55 -04:00
|
|
|
def close(self, event=None):
|
1998-10-14 00:43:05 -03:00
|
|
|
if self.interacting:
|
|
|
|
self.top.bell()
|
|
|
|
return
|
|
|
|
self.pyshell.close_debugger()
|
1998-10-13 20:51:13 -03:00
|
|
|
self.top.destroy()
|
1999-01-02 17:28:54 -04:00
|
|
|
|
1998-10-14 00:43:05 -03:00
|
|
|
def run(self, *args):
|
|
|
|
try:
|
|
|
|
self.interacting = 1
|
|
|
|
return apply(bdb.Bdb.run, (self,) + args)
|
|
|
|
finally:
|
|
|
|
self.interacting = 0
|
1998-10-13 20:51:13 -03:00
|
|
|
|
|
|
|
def user_line(self, frame):
|
|
|
|
self.interaction(frame)
|
|
|
|
|
|
|
|
def user_return(self, frame, rv):
|
|
|
|
# XXX show rv?
|
1998-10-18 23:24:40 -03:00
|
|
|
##self.interaction(frame)
|
|
|
|
pass
|
1998-10-13 20:51:13 -03:00
|
|
|
|
|
|
|
def user_exception(self, frame, info):
|
|
|
|
self.interaction(frame, info)
|
1999-01-02 17:28:54 -04:00
|
|
|
|
1998-10-13 20:51:13 -03:00
|
|
|
def make_gui(self):
|
|
|
|
pyshell = self.pyshell
|
|
|
|
self.flist = pyshell.flist
|
|
|
|
self.root = root = pyshell.root
|
1999-01-02 17:28:54 -04:00
|
|
|
self.top = top =ListedToplevel(root)
|
|
|
|
self.top.wm_title("Debug Control")
|
|
|
|
self.top.wm_iconname("Debug")
|
1998-10-14 00:43:05 -03:00
|
|
|
top.wm_protocol("WM_DELETE_WINDOW", self.close)
|
1999-01-11 10:46:55 -04:00
|
|
|
self.top.bind("<Escape>", self.close)
|
1998-10-16 13:12:11 -03:00
|
|
|
#
|
1998-10-13 20:51:13 -03:00
|
|
|
self.bframe = bframe = Frame(top)
|
1998-10-14 00:43:05 -03:00
|
|
|
self.bframe.pack(anchor="w")
|
1998-10-13 20:51:13 -03:00
|
|
|
self.buttons = bl = []
|
1998-10-16 13:12:11 -03:00
|
|
|
#
|
1998-10-13 20:51:13 -03:00
|
|
|
self.bcont = b = Button(bframe, text="Go", command=self.cont)
|
|
|
|
bl.append(b)
|
1998-10-16 18:09:05 -03:00
|
|
|
self.bstep = b = Button(bframe, text="Step", command=self.step)
|
1998-10-13 20:51:13 -03:00
|
|
|
bl.append(b)
|
1998-10-16 13:12:11 -03:00
|
|
|
self.bnext = b = Button(bframe, text="Over", command=self.next)
|
1998-10-13 20:51:13 -03:00
|
|
|
bl.append(b)
|
1998-10-16 13:12:11 -03:00
|
|
|
self.bret = b = Button(bframe, text="Out", command=self.ret)
|
1998-10-13 20:51:13 -03:00
|
|
|
bl.append(b)
|
1999-01-28 18:00:30 -04:00
|
|
|
self.bret = b = Button(bframe, text="Quit", command=self.quit)
|
|
|
|
bl.append(b)
|
1998-10-16 13:12:11 -03:00
|
|
|
#
|
1998-10-13 20:51:13 -03:00
|
|
|
for b in bl:
|
|
|
|
b.configure(state="disabled")
|
|
|
|
b.pack(side="left")
|
1998-10-16 13:12:11 -03:00
|
|
|
#
|
|
|
|
self.cframe = cframe = Frame(bframe)
|
|
|
|
self.cframe.pack(side="left")
|
|
|
|
#
|
1998-10-16 18:09:05 -03:00
|
|
|
if not self.vstack:
|
|
|
|
self.__class__.vstack = BooleanVar(top)
|
|
|
|
self.vstack.set(1)
|
1998-10-16 13:12:11 -03:00
|
|
|
self.bstack = Checkbutton(cframe,
|
|
|
|
text="Stack", command=self.show_stack, variable=self.vstack)
|
|
|
|
self.bstack.grid(row=0, column=0)
|
1998-10-16 18:09:05 -03:00
|
|
|
if not self.vsource:
|
|
|
|
self.__class__.vsource = BooleanVar(top)
|
1998-10-18 23:24:40 -03:00
|
|
|
##self.vsource.set(1)
|
1998-10-16 13:12:11 -03:00
|
|
|
self.bsource = Checkbutton(cframe,
|
|
|
|
text="Source", command=self.show_source, variable=self.vsource)
|
1998-10-16 14:16:59 -03:00
|
|
|
self.bsource.grid(row=0, column=1)
|
1998-10-16 18:09:05 -03:00
|
|
|
if not self.vlocals:
|
|
|
|
self.__class__.vlocals = BooleanVar(top)
|
|
|
|
self.vlocals.set(1)
|
1998-10-16 13:12:11 -03:00
|
|
|
self.blocals = Checkbutton(cframe,
|
|
|
|
text="Locals", command=self.show_locals, variable=self.vlocals)
|
|
|
|
self.blocals.grid(row=1, column=0)
|
1998-10-16 18:09:05 -03:00
|
|
|
if not self.vglobals:
|
|
|
|
self.__class__.vglobals = BooleanVar(top)
|
1998-10-18 23:24:40 -03:00
|
|
|
##self.vglobals.set(1)
|
1998-10-16 13:12:11 -03:00
|
|
|
self.bglobals = Checkbutton(cframe,
|
|
|
|
text="Globals", command=self.show_globals, variable=self.vglobals)
|
|
|
|
self.bglobals.grid(row=1, column=1)
|
|
|
|
#
|
1998-10-14 00:43:05 -03:00
|
|
|
self.status = Label(top, anchor="w")
|
|
|
|
self.status.pack(anchor="w")
|
|
|
|
self.error = Label(top, anchor="w")
|
1998-10-18 23:24:40 -03:00
|
|
|
self.error.pack(anchor="w", fill="x")
|
1998-10-16 18:09:05 -03:00
|
|
|
self.errorbg = self.error.cget("background")
|
1998-10-16 15:59:39 -03:00
|
|
|
#
|
|
|
|
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")
|
1998-10-16 18:09:05 -03:00
|
|
|
#
|
|
|
|
if self.vstack.get():
|
|
|
|
self.show_stack()
|
|
|
|
if self.vlocals.get():
|
|
|
|
self.show_locals()
|
|
|
|
if self.vglobals.get():
|
|
|
|
self.show_globals()
|
1999-01-02 17:28:54 -04:00
|
|
|
|
1998-10-16 18:09:05 -03:00
|
|
|
frame = None
|
1999-01-02 17:28:54 -04:00
|
|
|
|
1998-10-13 20:51:13 -03:00
|
|
|
def interaction(self, frame, info=None):
|
|
|
|
self.frame = frame
|
|
|
|
code = frame.f_code
|
|
|
|
file = code.co_filename
|
1998-10-14 00:43:05 -03:00
|
|
|
base = os.path.basename(file)
|
1998-10-13 20:51:13 -03:00
|
|
|
lineno = frame.f_lineno
|
1998-10-16 18:09:05 -03:00
|
|
|
#
|
1998-10-15 09:18:49 -03:00
|
|
|
message = "%s:%s" % (base, lineno)
|
|
|
|
if code.co_name != "?":
|
|
|
|
message = "%s: %s()" % (message, code.co_name)
|
1998-10-14 00:43:05 -03:00
|
|
|
self.status.configure(text=message)
|
1998-10-16 18:09:05 -03:00
|
|
|
#
|
1998-10-13 20:51:13 -03:00
|
|
|
if info:
|
|
|
|
type, value, tb = info
|
1998-10-14 00:43:05 -03:00
|
|
|
try:
|
|
|
|
m1 = type.__name__
|
|
|
|
except AttributeError:
|
|
|
|
m1 = "%s" % str(type)
|
|
|
|
if value is not None:
|
|
|
|
try:
|
|
|
|
m1 = "%s: %s" % (m1, str(value))
|
|
|
|
except:
|
|
|
|
pass
|
1998-10-16 18:09:05 -03:00
|
|
|
bg = "yellow"
|
1998-10-14 00:43:05 -03:00
|
|
|
else:
|
|
|
|
m1 = ""
|
1998-10-16 13:12:11 -03:00
|
|
|
tb = None
|
1998-10-16 18:09:05 -03:00
|
|
|
bg = self.errorbg
|
|
|
|
self.error.configure(text=m1, background=bg)
|
|
|
|
#
|
1998-10-16 13:12:11 -03:00
|
|
|
sv = self.stackviewer
|
|
|
|
if sv:
|
|
|
|
stack, i = self.get_stack(self.frame, tb)
|
|
|
|
sv.load_stack(stack, i)
|
1998-10-16 18:09:05 -03:00
|
|
|
#
|
1998-10-18 23:24:40 -03:00
|
|
|
self.show_variables(1)
|
1998-10-16 18:09:05 -03:00
|
|
|
#
|
1998-10-16 13:12:11 -03:00
|
|
|
if self.vsource.get():
|
|
|
|
self.sync_source_line()
|
1998-10-16 18:09:05 -03:00
|
|
|
#
|
1998-10-13 20:51:13 -03:00
|
|
|
for b in self.buttons:
|
|
|
|
b.configure(state="normal")
|
1998-10-16 18:09:05 -03:00
|
|
|
#
|
1998-10-13 20:51:13 -03:00
|
|
|
self.top.tkraise()
|
|
|
|
self.root.mainloop()
|
1998-10-16 18:09:05 -03:00
|
|
|
#
|
1998-10-13 20:51:13 -03:00
|
|
|
for b in self.buttons:
|
|
|
|
b.configure(state="disabled")
|
|
|
|
self.status.configure(text="")
|
1998-10-16 18:09:05 -03:00
|
|
|
self.error.configure(text="", background=self.errorbg)
|
1998-10-13 20:51:13 -03:00
|
|
|
self.frame = None
|
1999-01-02 17:28:54 -04:00
|
|
|
|
1998-10-16 13:12:11 -03:00
|
|
|
def sync_source_line(self):
|
|
|
|
frame = self.frame
|
|
|
|
if not frame:
|
|
|
|
return
|
|
|
|
code = frame.f_code
|
|
|
|
file = code.co_filename
|
|
|
|
lineno = frame.f_lineno
|
|
|
|
if file[:1] + file[-1:] != "<>" and os.path.exists(file):
|
|
|
|
edit = self.flist.open(file)
|
|
|
|
if edit:
|
|
|
|
edit.gotoline(lineno)
|
1999-01-02 17:28:54 -04:00
|
|
|
|
1998-10-13 20:51:13 -03:00
|
|
|
def cont(self):
|
|
|
|
self.set_continue()
|
|
|
|
self.root.quit()
|
1999-01-02 17:28:54 -04:00
|
|
|
|
1998-10-13 20:51:13 -03:00
|
|
|
def step(self):
|
|
|
|
self.set_step()
|
|
|
|
self.root.quit()
|
1999-01-02 17:28:54 -04:00
|
|
|
|
1998-10-13 20:51:13 -03:00
|
|
|
def next(self):
|
|
|
|
self.set_next(self.frame)
|
|
|
|
self.root.quit()
|
1999-01-02 17:28:54 -04:00
|
|
|
|
1998-10-13 20:51:13 -03:00
|
|
|
def ret(self):
|
|
|
|
self.set_return(self.frame)
|
|
|
|
self.root.quit()
|
1998-10-16 13:12:11 -03:00
|
|
|
|
1999-01-28 18:00:30 -04:00
|
|
|
def quit(self):
|
|
|
|
self.set_quit()
|
|
|
|
self.root.quit()
|
|
|
|
|
1998-10-16 13:12:11 -03:00
|
|
|
stackviewer = None
|
|
|
|
|
|
|
|
def show_stack(self):
|
|
|
|
if not self.stackviewer and self.vstack.get():
|
|
|
|
self.stackviewer = sv = StackViewer.StackViewer(
|
1998-10-16 15:59:39 -03:00
|
|
|
self.fstack, self.flist, self)
|
1998-10-16 13:12:11 -03:00
|
|
|
if self.frame:
|
1998-10-16 14:16:59 -03:00
|
|
|
stack, i = self.get_stack(self.frame, None)
|
|
|
|
sv.load_stack(stack, i)
|
1998-10-16 13:12:11 -03:00
|
|
|
else:
|
|
|
|
sv = self.stackviewer
|
|
|
|
if sv and not self.vstack.get():
|
|
|
|
self.stackviewer = None
|
|
|
|
sv.close()
|
1998-10-16 15:59:39 -03:00
|
|
|
self.fstack['height'] = 1
|
1999-01-02 17:28:54 -04:00
|
|
|
|
1998-10-16 13:12:11 -03:00
|
|
|
def show_source(self):
|
|
|
|
if self.vsource.get():
|
|
|
|
self.sync_source_line()
|
|
|
|
|
|
|
|
def show_frame(self, (frame, lineno)):
|
1998-10-16 18:09:05 -03:00
|
|
|
self.frame = frame
|
|
|
|
self.show_variables()
|
1998-10-16 15:59:39 -03:00
|
|
|
|
|
|
|
localsviewer = None
|
1998-10-16 18:09:05 -03:00
|
|
|
globalsviewer = None
|
1998-10-16 13:12:11 -03:00
|
|
|
|
|
|
|
def show_locals(self):
|
1998-10-16 15:59:39 -03:00
|
|
|
lv = self.localsviewer
|
1998-10-16 18:09:05 -03:00
|
|
|
if self.vlocals.get():
|
|
|
|
if not lv:
|
|
|
|
self.localsviewer = StackViewer.NamespaceViewer(
|
|
|
|
self.flocals, "Locals")
|
|
|
|
else:
|
|
|
|
if lv:
|
|
|
|
self.localsviewer = None
|
|
|
|
lv.close()
|
|
|
|
self.flocals['height'] = 1
|
|
|
|
self.show_variables()
|
1998-10-16 15:59:39 -03:00
|
|
|
|
1998-10-16 13:12:11 -03:00
|
|
|
def show_globals(self):
|
1998-10-16 18:09:05 -03:00
|
|
|
gv = self.globalsviewer
|
|
|
|
if self.vglobals.get():
|
|
|
|
if not gv:
|
|
|
|
self.globalsviewer = StackViewer.NamespaceViewer(
|
|
|
|
self.fglobals, "Globals")
|
|
|
|
else:
|
|
|
|
if gv:
|
|
|
|
self.globalsviewer = None
|
|
|
|
gv.close()
|
|
|
|
self.fglobals['height'] = 1
|
|
|
|
self.show_variables()
|
|
|
|
|
1998-10-18 23:24:40 -03:00
|
|
|
def show_variables(self, force=0):
|
1998-10-16 18:09:05 -03:00
|
|
|
lv = self.localsviewer
|
|
|
|
gv = self.globalsviewer
|
|
|
|
frame = self.frame
|
|
|
|
if not frame:
|
|
|
|
ldict = gdict = None
|
|
|
|
else:
|
|
|
|
ldict = frame.f_locals
|
|
|
|
gdict = frame.f_globals
|
|
|
|
if lv and gv and ldict is gdict:
|
|
|
|
ldict = None
|
|
|
|
if lv:
|
1998-10-18 23:24:40 -03:00
|
|
|
lv.load_dict(ldict, force)
|
1998-10-16 18:09:05 -03:00
|
|
|
if gv:
|
1998-10-18 23:24:40 -03:00
|
|
|
gv.load_dict(gdict, force)
|
|
|
|
|
|
|
|
def set_breakpoint_here(self, edit):
|
|
|
|
text = edit.text
|
|
|
|
filename = edit.io.filename
|
|
|
|
if not filename:
|
|
|
|
text.bell()
|
|
|
|
return
|
|
|
|
lineno = int(float(text.index("insert")))
|
|
|
|
msg = self.set_break(filename, lineno)
|
|
|
|
if msg:
|
|
|
|
text.bell()
|
|
|
|
return
|
|
|
|
text.tag_add("BREAK", "insert linestart", "insert lineend +1char")
|
1999-01-02 17:28:54 -04:00
|
|
|
|
1998-10-18 23:24:40 -03:00
|
|
|
# A literal copy of Bdb.set_break() without the print statement at the end
|
|
|
|
def set_break(self, filename, lineno, temporary=0, cond = None):
|
1999-01-02 17:28:54 -04:00
|
|
|
import linecache # Import as late as possible
|
|
|
|
line = linecache.getline(filename, lineno)
|
|
|
|
if not line:
|
|
|
|
return 'That line does not exist!'
|
|
|
|
if not self.breaks.has_key(filename):
|
|
|
|
self.breaks[filename] = []
|
|
|
|
list = self.breaks[filename]
|
|
|
|
if not lineno in list:
|
|
|
|
list.append(lineno)
|
|
|
|
bp = bdb.Breakpoint(filename, lineno, temporary, cond)
|