Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame^] | 1 | # wdb.py -- a window-based Python debugger |
| 2 | |
| 3 | import stdwin |
| 4 | from stdwinevents import * |
| 5 | import sys |
| 6 | import basewin |
| 7 | import bdb |
| 8 | import repr |
| 9 | |
| 10 | WIDTH = 40 |
| 11 | HEIGHT = 8 |
| 12 | |
| 13 | WdbDone = 'wdb.WdbDone' # Exception to continue execution |
| 14 | |
| 15 | |
| 16 | class Wdb(bdb.Bdb, basewin.BaseWindow): # Window debugger |
| 17 | |
| 18 | def init(self): |
| 19 | self.sourcewindows = {} |
| 20 | self.framewindows = {} |
| 21 | self = bdb.Bdb.init(self) |
| 22 | width = WIDTH*stdwin.textwidth('0') |
| 23 | height = HEIGHT*stdwin.lineheight() |
| 24 | stdwin.setdefwinsize(width, height) |
| 25 | self = basewin.BaseWindow.init(self, '--Stack--') |
| 26 | self.closed = 0 |
| 27 | return self |
| 28 | |
| 29 | def reset(self): |
| 30 | if self.closed: raise RuntimeError, 'already closed' |
| 31 | bdb.Bdb.reset(self) |
| 32 | self.forget() |
| 33 | |
| 34 | def forget(self): |
| 35 | self.lineno = None |
| 36 | self.stack = [] |
| 37 | self.curindex = 0 |
| 38 | self.curframe = None |
| 39 | for fn in self.sourcewindows.keys(): |
| 40 | self.sourcewindows[fn].resetlineno() |
| 41 | |
| 42 | def setup(self, f, t): |
| 43 | self.forget() |
| 44 | self.stack, self.curindex = self.get_stack(f, t) |
| 45 | self.curframe = self.stack[self.curindex][0] |
| 46 | # Build a list of current frames |
| 47 | cfl = [] |
| 48 | for f, i in self.stack: cfl.append(f) |
| 49 | # Remove deactivated frame windows |
| 50 | for name in self.framewindows.keys(): |
| 51 | fw = self.framewindows[name] |
| 52 | if fw.frame not in cfl: fw.close() |
| 53 | else: fw.refreshframe() |
| 54 | # Refresh the stack window |
| 55 | self.refreshstack() |
| 56 | |
| 57 | # Override Bdb methods (except user_call, for now) |
| 58 | |
| 59 | def user_line(self, frame): |
| 60 | # This function is called when we stop or break at this line |
| 61 | self.interaction(frame, None) |
| 62 | |
| 63 | def user_return(self, frame, return_value): |
| 64 | # This function is called when a return trap is set here |
| 65 | frame.f_locals['__return__'] = return_value |
| 66 | self.settitle('--Return--') |
| 67 | self.interaction(frame, None) |
| 68 | self.settitle('--Stack--') |
| 69 | |
| 70 | def user_exception(self, frame, (exc_type, exc_value, exc_traceback)): |
| 71 | # This function is called if an exception occurs, |
| 72 | # but only if we are to stop at or just below this level |
| 73 | frame.f_locals['__exception__'] = exc_type, exc_value |
| 74 | self.settitle(exc_type + ': ' + repr.repr(exc_value)) |
| 75 | self.interaction(frame, exc_traceback) |
| 76 | self.settitle('--Stack--') |
| 77 | |
| 78 | # Change the title |
| 79 | |
| 80 | def settitle(self, title): |
| 81 | self.savetitle = self.win.gettitle() |
| 82 | self.win.settitle(title) |
| 83 | |
| 84 | # General interaction function |
| 85 | |
| 86 | def interaction(self, frame, traceback): |
| 87 | import mainloop |
| 88 | self.popup() |
| 89 | self.setup(frame, traceback) |
| 90 | try: |
| 91 | mainloop.mainloop() |
| 92 | except WdbDone: |
| 93 | pass |
| 94 | self.forget() |
| 95 | |
| 96 | # Functions whose name is do_X for some character X |
| 97 | # are callable directly from the keyboard. |
| 98 | |
| 99 | def do_up(self): |
| 100 | if self.curindex == 0: |
| 101 | stdwin.fleep() |
| 102 | else: |
| 103 | self.curindex = self.curindex - 1 |
| 104 | self.curframe = self.stack[self.curindex][0] |
| 105 | self.refreshstack() |
| 106 | do_u = do_up |
| 107 | |
| 108 | def do_down(self): |
| 109 | if self.curindex + 1 == len(self.stack): |
| 110 | stdwin.fleep() |
| 111 | else: |
| 112 | self.curindex = self.curindex + 1 |
| 113 | self.curframe = self.stack[self.curindex][0] |
| 114 | self.refreshstack() |
| 115 | do_d = do_down |
| 116 | |
| 117 | def do_step(self): |
| 118 | self.set_step() |
| 119 | raise WdbDone |
| 120 | do_s = do_step |
| 121 | |
| 122 | def do_next(self): |
| 123 | self.set_next(self.curframe) |
| 124 | raise WdbDone |
| 125 | do_n = do_next |
| 126 | |
| 127 | def do_return(self): |
| 128 | self.set_return(self.curframe) |
| 129 | raise WdbDone |
| 130 | do_r = do_return |
| 131 | |
| 132 | def do_continue(self): |
| 133 | self.set_continue() |
| 134 | raise WdbDone |
| 135 | do_c = do_cont = do_continue |
| 136 | |
| 137 | def do_quit(self): |
| 138 | self.close() |
| 139 | raise WdbDone |
| 140 | do_q = do_quit |
| 141 | |
| 142 | def do_list(self): |
| 143 | fn = self.curframe.f_code.co_filename |
| 144 | if not self.sourcewindows.has_key(fn): |
| 145 | import wdbsrcwin |
| 146 | try: |
| 147 | self.sourcewindows[fn] = \ |
| 148 | wdbsrcwin.DebuggerSourceWindow(). \ |
| 149 | init(self, fn) |
| 150 | except IOError: |
| 151 | stdwin.fleep() |
| 152 | return |
| 153 | w = self.sourcewindows[fn] |
| 154 | lineno = self.stack[self.curindex][1] |
| 155 | w.setlineno(lineno) |
| 156 | w.popup() |
| 157 | do_l = do_list |
| 158 | |
| 159 | def do_frame(self): |
| 160 | name = 'locals' + `self.curframe`[16:-1] |
| 161 | if self.framewindows.has_key(name): |
| 162 | self.framewindows[name].popup() |
| 163 | else: |
| 164 | import wdbframewin |
| 165 | self.framewindows[name] = \ |
| 166 | wdbframewin.FrameWindow().init(self, \ |
| 167 | self.curframe, \ |
| 168 | self.curframe.f_locals, name) |
| 169 | do_f = do_frame |
| 170 | |
| 171 | def do_globalframe(self): |
| 172 | name = 'globals' + `self.curframe`[16:-1] |
| 173 | if self.framewindows.has_key(name): |
| 174 | self.framewindows[name].popup() |
| 175 | else: |
| 176 | import wdbframewin |
| 177 | self.framewindows[name] = \ |
| 178 | wdbframewin.FrameWindow().init(self, \ |
| 179 | self.curframe, \ |
| 180 | self.curframe.f_globals, name) |
| 181 | do_g = do_globalframe |
| 182 | |
| 183 | # Link between the debugger and the window |
| 184 | |
| 185 | def refreshstack(self): |
| 186 | height = stdwin.lineheight() * (1 + len(self.stack)) |
| 187 | self.win.setdocsize((0, height)) |
| 188 | self.refreshall() # XXX be more subtle later |
| 189 | # Also pass the information on to the source windows |
| 190 | filename = self.curframe.f_code.co_filename |
| 191 | lineno = self.curframe.f_lineno |
| 192 | for fn in self.sourcewindows.keys(): |
| 193 | w = self.sourcewindows[fn] |
| 194 | if fn == filename: |
| 195 | w.setlineno(lineno) |
| 196 | else: |
| 197 | w.resetlineno() |
| 198 | |
| 199 | # The remaining methods override BaseWindow methods |
| 200 | |
| 201 | def close(self): |
| 202 | if not self.closed: |
| 203 | basewin.BaseWindow.close(self) |
| 204 | self.closed = 1 |
| 205 | for key in self.sourcewindows.keys(): |
| 206 | self.sourcewindows[key].close() |
| 207 | for key in self.framewindows.keys(): |
| 208 | self.framewindows[key].close() |
| 209 | self.set_quit() |
| 210 | |
| 211 | def char(self, detail): |
| 212 | try: |
| 213 | func = eval('self.do_' + detail) |
| 214 | except (AttributeError, SyntaxError): |
| 215 | stdwin.fleep() |
| 216 | return |
| 217 | func() |
| 218 | |
| 219 | def command(self, detail): |
| 220 | if detail == WC_UP: |
| 221 | self.do_up() |
| 222 | elif detail == WC_DOWN: |
| 223 | self.do_down() |
| 224 | |
| 225 | def mouse_down(self, detail): |
| 226 | (h, v), clicks, button, mask = detail |
| 227 | i = v / stdwin.lineheight() |
| 228 | if 0 <= i < len(self.stack): |
| 229 | if i != self.curindex: |
| 230 | self.curindex = i |
| 231 | self.curframe = self.stack[self.curindex][0] |
| 232 | self.refreshstack() |
| 233 | elif clicks == 2: |
| 234 | self.do_frame() |
| 235 | else: |
| 236 | stdwin.fleep() |
| 237 | |
| 238 | def draw(self, detail): |
| 239 | import linecache, codehack, string |
| 240 | d = self.win.begindrawing() |
| 241 | try: |
| 242 | h, v = 0, 0 |
| 243 | for f, lineno in self.stack: |
| 244 | fn = f.f_code.co_filename |
| 245 | if f is self.curframe: |
| 246 | s = '> ' |
| 247 | else: |
| 248 | s = ' ' |
| 249 | s = s + fn + '(' + `lineno` + ')' |
| 250 | s = s + codehack.getcodename(f.f_code) |
| 251 | if f.f_locals.has_key('__args__'): |
| 252 | args = f.f_locals['__args__'] |
| 253 | if args is not None: |
| 254 | s = s + repr.repr(args) |
| 255 | if f.f_locals.has_key('__return__'): |
| 256 | rv = f.f_locals['__return__'] |
| 257 | s = s + '->' |
| 258 | s = s + repr.repr(rv) |
| 259 | line = linecache.getline(fn, lineno) |
| 260 | if line: s = s + ': ' + string.strip(line) |
| 261 | d.text((h, v), s) |
| 262 | v = v + d.lineheight() |
| 263 | finally: |
| 264 | d.close() |
| 265 | |
| 266 | |
| 267 | def run(statement): |
| 268 | x = Wdb().init() |
| 269 | try: x.run(statement) |
| 270 | finally: x.close() |
| 271 | |
| 272 | def runctx(statement, globals, locals): |
| 273 | x = Wdb().init() |
| 274 | try: x.runctx(statement, globals, locals) |
| 275 | finally: x.close() |
| 276 | |
| 277 | TESTCMD = 'import x; x.main()' |
| 278 | |
| 279 | def test(): |
| 280 | import linecache |
| 281 | linecache.checkcache() |
| 282 | run(TESTCMD) |