Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 1 | """Debugger basics""" |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 2 | |
| 3 | import sys |
Barry Warsaw | 148ffbc | 1999-09-09 23:24:33 +0000 | [diff] [blame] | 4 | import os |
Guido van Rossum | 4808dcb | 1996-10-15 14:40:21 +0000 | [diff] [blame] | 5 | import types |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 6 | |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 7 | __all__ = ["BdbQuit","Bdb","Breakpoint"] |
| 8 | |
Neal Norwitz | 93cf79f | 2002-03-31 14:06:41 +0000 | [diff] [blame] | 9 | class BdbQuit(Exception): |
| 10 | """Exception to give up completely""" |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 11 | |
| 12 | |
Guido van Rossum | 6ea27cc | 1999-01-25 20:51:34 +0000 | [diff] [blame] | 13 | class Bdb: |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 15 | """Generic Python debugger base class. |
Guido van Rossum | 6ea27cc | 1999-01-25 20:51:34 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 17 | This class takes care of details of the trace facility; |
| 18 | a derived class should implement user interaction. |
| 19 | The standard debugger class (pdb.Pdb) is an example. |
| 20 | """ |
Guido van Rossum | 6ea27cc | 1999-01-25 20:51:34 +0000 | [diff] [blame] | 21 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 22 | def __init__(self): |
| 23 | self.breaks = {} |
| 24 | self.fncache = {} |
Barry Warsaw | 148ffbc | 1999-09-09 23:24:33 +0000 | [diff] [blame] | 25 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 26 | def canonic(self, filename): |
Guido van Rossum | 42f5332 | 2001-11-29 02:50:15 +0000 | [diff] [blame] | 27 | if filename == "<" + filename[1:-1] + ">": |
| 28 | return filename |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 29 | canonic = self.fncache.get(filename) |
| 30 | if not canonic: |
| 31 | canonic = os.path.abspath(filename) |
Guido van Rossum | bdba320 | 2002-02-25 23:23:24 +0000 | [diff] [blame] | 32 | canonic = os.path.normcase(canonic) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 33 | self.fncache[filename] = canonic |
| 34 | return canonic |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 35 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 36 | def reset(self): |
| 37 | import linecache |
| 38 | linecache.checkcache() |
| 39 | self.botframe = None |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 40 | self._set_stopinfo(None, None) |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 42 | def trace_dispatch(self, frame, event, arg): |
| 43 | if self.quitting: |
| 44 | return # None |
| 45 | if event == 'line': |
| 46 | return self.dispatch_line(frame) |
| 47 | if event == 'call': |
| 48 | return self.dispatch_call(frame, arg) |
| 49 | if event == 'return': |
| 50 | return self.dispatch_return(frame, arg) |
| 51 | if event == 'exception': |
| 52 | return self.dispatch_exception(frame, arg) |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 53 | if event == 'c_call': |
| 54 | return self.trace_dispatch |
| 55 | if event == 'c_exception': |
| 56 | return self.trace_dispatch |
| 57 | if event == 'c_return': |
| 58 | return self.trace_dispatch |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 59 | print('bdb.Bdb.dispatch: unknown debugging event:', repr(event)) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 60 | return self.trace_dispatch |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 61 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 62 | def dispatch_line(self, frame): |
| 63 | if self.stop_here(frame) or self.break_here(frame): |
| 64 | self.user_line(frame) |
| 65 | if self.quitting: raise BdbQuit |
| 66 | return self.trace_dispatch |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 67 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 68 | def dispatch_call(self, frame, arg): |
| 69 | # XXX 'arg' is no longer used |
| 70 | if self.botframe is None: |
| 71 | # First call of dispatch since reset() |
Christian Tismer | 313a751 | 2002-05-28 08:04:00 +0000 | [diff] [blame] | 72 | self.botframe = frame.f_back # (CT) Note that this may also be None! |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 73 | return self.trace_dispatch |
| 74 | if not (self.stop_here(frame) or self.break_anywhere(frame)): |
| 75 | # No need to trace this function |
| 76 | return # None |
| 77 | self.user_call(frame, arg) |
| 78 | if self.quitting: raise BdbQuit |
| 79 | return self.trace_dispatch |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 80 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 81 | def dispatch_return(self, frame, arg): |
| 82 | if self.stop_here(frame) or frame == self.returnframe: |
| 83 | self.user_return(frame, arg) |
| 84 | if self.quitting: raise BdbQuit |
Just van Rossum | ae1f65f | 2001-06-25 18:01:24 +0000 | [diff] [blame] | 85 | return self.trace_dispatch |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 86 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 87 | def dispatch_exception(self, frame, arg): |
| 88 | if self.stop_here(frame): |
| 89 | self.user_exception(frame, arg) |
| 90 | if self.quitting: raise BdbQuit |
| 91 | return self.trace_dispatch |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 92 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 93 | # Normally derived classes don't override the following |
| 94 | # methods, but they may if they want to redefine the |
| 95 | # definition of stopping and breakpoints. |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 96 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 97 | def stop_here(self, frame): |
Neal Norwitz | 72a2b4d | 2002-05-29 00:54:38 +0000 | [diff] [blame] | 98 | # (CT) stopframe may now also be None, see dispatch_call. |
| 99 | # (CT) the former test for None is therefore removed from here. |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 100 | if frame is self.stopframe: |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 101 | return frame.f_lineno >= self.stoplineno |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 102 | while frame is not None and frame is not self.stopframe: |
| 103 | if frame is self.botframe: |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 104 | return True |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 105 | frame = frame.f_back |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 106 | return False |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 107 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 108 | def break_here(self, frame): |
| 109 | filename = self.canonic(frame.f_code.co_filename) |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 110 | if not filename in self.breaks: |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 111 | return False |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 112 | lineno = frame.f_lineno |
| 113 | if not lineno in self.breaks[filename]: |
Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 114 | # The line itself has no breakpoint, but maybe the line is the |
| 115 | # first line of a function with breakpoint set by function name. |
| 116 | lineno = frame.f_code.co_firstlineno |
| 117 | if not lineno in self.breaks[filename]: |
| 118 | return False |
| 119 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 120 | # flag says ok to delete temp. bp |
| 121 | (bp, flag) = effective(filename, lineno, frame) |
| 122 | if bp: |
| 123 | self.currentbp = bp.number |
| 124 | if (flag and bp.temporary): |
| 125 | self.do_clear(str(bp.number)) |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 126 | return True |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 127 | else: |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 128 | return False |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 129 | |
Guido van Rossum | 9cec8fb | 2001-04-08 15:05:16 +0000 | [diff] [blame] | 130 | def do_clear(self, arg): |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 131 | raise NotImplementedError("subclass of bdb must implement do_clear()") |
Guido van Rossum | 9cec8fb | 2001-04-08 15:05:16 +0000 | [diff] [blame] | 132 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 133 | def break_anywhere(self, frame): |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 134 | return self.canonic(frame.f_code.co_filename) in self.breaks |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 135 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 136 | # Derived classes should override the user_* methods |
| 137 | # to gain control. |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 138 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 139 | def user_call(self, frame, argument_list): |
| 140 | """This method is called when there is the remote possibility |
| 141 | that we ever need to stop in this function.""" |
| 142 | pass |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 143 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 144 | def user_line(self, frame): |
| 145 | """This method is called when we stop or break at this line.""" |
| 146 | pass |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 147 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 148 | def user_return(self, frame, return_value): |
| 149 | """This method is called when a return trap is set here.""" |
| 150 | pass |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 151 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 152 | def user_exception(self, frame, exc_info): |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 153 | """This method is called if an exception occurs, |
| 154 | but only if we are to stop at or just below this level.""" |
| 155 | pass |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 156 | |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 157 | def _set_stopinfo(self, stopframe, returnframe, stoplineno=-1): |
| 158 | self.stopframe = stopframe |
| 159 | self.returnframe = returnframe |
| 160 | self.quitting = 0 |
| 161 | self.stoplineno = stoplineno |
| 162 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 163 | # Derived classes and clients can call the following methods |
| 164 | # to affect the stepping state. |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 165 | |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 166 | def set_until(self, frame): #the name "until" is borrowed from gdb |
| 167 | """Stop when the line with the line no greater than the current one is |
| 168 | reached or when returning from current frame""" |
| 169 | self._set_stopinfo(frame, frame, frame.f_lineno+1) |
| 170 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 171 | def set_step(self): |
| 172 | """Stop after one line of code.""" |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 173 | self._set_stopinfo(None,None) |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 174 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 175 | def set_next(self, frame): |
| 176 | """Stop on the next line in or below the given frame.""" |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 177 | self._set_stopinfo(frame, None) |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 178 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 179 | def set_return(self, frame): |
| 180 | """Stop when returning from the given frame.""" |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 181 | self._set_stopinfo(frame.f_back, frame) |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 182 | |
Johannes Gijsbers | 84a6c20 | 2004-11-07 11:35:30 +0000 | [diff] [blame] | 183 | def set_trace(self, frame=None): |
| 184 | """Start debugging from `frame`. |
| 185 | |
| 186 | If frame is not specified, debugging starts from caller's frame. |
| 187 | """ |
| 188 | if frame is None: |
| 189 | frame = sys._getframe().f_back |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 190 | self.reset() |
| 191 | while frame: |
| 192 | frame.f_trace = self.trace_dispatch |
| 193 | self.botframe = frame |
| 194 | frame = frame.f_back |
| 195 | self.set_step() |
| 196 | sys.settrace(self.trace_dispatch) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 197 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 198 | def set_continue(self): |
| 199 | # Don't stop except at breakpoints or when finished |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 200 | self._set_stopinfo(self.botframe, None) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 201 | if not self.breaks: |
| 202 | # no breakpoints; run without debugger overhead |
| 203 | sys.settrace(None) |
Christian Tismer | 313a751 | 2002-05-28 08:04:00 +0000 | [diff] [blame] | 204 | frame = sys._getframe().f_back |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 205 | while frame and frame is not self.botframe: |
| 206 | del frame.f_trace |
| 207 | frame = frame.f_back |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 208 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 209 | def set_quit(self): |
| 210 | self.stopframe = self.botframe |
| 211 | self.returnframe = None |
| 212 | self.quitting = 1 |
| 213 | sys.settrace(None) |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 214 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 215 | # Derived classes and clients can call the following methods |
| 216 | # to manipulate breakpoints. These methods return an |
| 217 | # error message is something went wrong, None if all is well. |
| 218 | # Set_break prints out the breakpoint line and file:lineno. |
| 219 | # Call self.get_*break*() to see the breakpoints or better |
| 220 | # for bp in Breakpoint.bpbynumber: if bp: bp.bpprint(). |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 221 | |
Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 222 | def set_break(self, filename, lineno, temporary=0, cond = None, |
| 223 | funcname=None): |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 224 | filename = self.canonic(filename) |
| 225 | import linecache # Import as late as possible |
| 226 | line = linecache.getline(filename, lineno) |
| 227 | if not line: |
| 228 | return 'Line %s:%d does not exist' % (filename, |
| 229 | lineno) |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 230 | if not filename in self.breaks: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 231 | self.breaks[filename] = [] |
| 232 | list = self.breaks[filename] |
| 233 | if not lineno in list: |
| 234 | list.append(lineno) |
Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 235 | bp = Breakpoint(filename, lineno, temporary, cond, funcname) |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 236 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 237 | def clear_break(self, filename, lineno): |
| 238 | filename = self.canonic(filename) |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 239 | if not filename in self.breaks: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 240 | return 'There are no breakpoints in %s' % filename |
| 241 | if lineno not in self.breaks[filename]: |
| 242 | return 'There is no breakpoint at %s:%d' % (filename, |
| 243 | lineno) |
| 244 | # If there's only one bp in the list for that file,line |
| 245 | # pair, then remove the breaks entry |
| 246 | for bp in Breakpoint.bplist[filename, lineno][:]: |
| 247 | bp.deleteMe() |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 248 | if (filename, lineno) not in Breakpoint.bplist: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 249 | self.breaks[filename].remove(lineno) |
| 250 | if not self.breaks[filename]: |
| 251 | del self.breaks[filename] |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 252 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 253 | def clear_bpbynumber(self, arg): |
| 254 | try: |
| 255 | number = int(arg) |
| 256 | except: |
| 257 | return 'Non-numeric breakpoint number (%s)' % arg |
| 258 | try: |
| 259 | bp = Breakpoint.bpbynumber[number] |
| 260 | except IndexError: |
| 261 | return 'Breakpoint number (%d) out of range' % number |
| 262 | if not bp: |
| 263 | return 'Breakpoint (%d) already deleted' % number |
| 264 | self.clear_break(bp.file, bp.line) |
Guido van Rossum | 6ea27cc | 1999-01-25 20:51:34 +0000 | [diff] [blame] | 265 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 266 | def clear_all_file_breaks(self, filename): |
| 267 | filename = self.canonic(filename) |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 268 | if not filename in self.breaks: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 269 | return 'There are no breakpoints in %s' % filename |
| 270 | for line in self.breaks[filename]: |
| 271 | blist = Breakpoint.bplist[filename, line] |
| 272 | for bp in blist: |
| 273 | bp.deleteMe() |
| 274 | del self.breaks[filename] |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 275 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 276 | def clear_all_breaks(self): |
| 277 | if not self.breaks: |
| 278 | return 'There are no breakpoints' |
| 279 | for bp in Breakpoint.bpbynumber: |
| 280 | if bp: |
| 281 | bp.deleteMe() |
| 282 | self.breaks = {} |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 283 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 284 | def get_break(self, filename, lineno): |
| 285 | filename = self.canonic(filename) |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 286 | return filename in self.breaks and \ |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 287 | lineno in self.breaks[filename] |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 288 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 289 | def get_breaks(self, filename, lineno): |
| 290 | filename = self.canonic(filename) |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 291 | return filename in self.breaks and \ |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 292 | lineno in self.breaks[filename] and \ |
| 293 | Breakpoint.bplist[filename, lineno] or [] |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 294 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 295 | def get_file_breaks(self, filename): |
| 296 | filename = self.canonic(filename) |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 297 | if filename in self.breaks: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 298 | return self.breaks[filename] |
| 299 | else: |
| 300 | return [] |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 301 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 302 | def get_all_breaks(self): |
| 303 | return self.breaks |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 304 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 305 | # Derived classes and clients can call the following method |
| 306 | # to get a data structure representing a stack trace. |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 307 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 308 | def get_stack(self, f, t): |
| 309 | stack = [] |
| 310 | if t and t.tb_frame is f: |
| 311 | t = t.tb_next |
| 312 | while f is not None: |
| 313 | stack.append((f, f.f_lineno)) |
| 314 | if f is self.botframe: |
| 315 | break |
| 316 | f = f.f_back |
| 317 | stack.reverse() |
| 318 | i = max(0, len(stack) - 1) |
| 319 | while t is not None: |
| 320 | stack.append((t.tb_frame, t.tb_lineno)) |
| 321 | t = t.tb_next |
| 322 | return stack, i |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 323 | |
| 324 | # |
| 325 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 326 | def format_stack_entry(self, frame_lineno, lprefix=': '): |
Alexandre Vassalotti | 1f2ba4b | 2008-05-16 07:12:44 +0000 | [diff] [blame] | 327 | import linecache, reprlib |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 328 | frame, lineno = frame_lineno |
| 329 | filename = self.canonic(frame.f_code.co_filename) |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 330 | s = '%s(%r)' % (filename, lineno) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 331 | if frame.f_code.co_name: |
| 332 | s = s + frame.f_code.co_name |
| 333 | else: |
| 334 | s = s + "<lambda>" |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 335 | if '__args__' in frame.f_locals: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 336 | args = frame.f_locals['__args__'] |
| 337 | else: |
| 338 | args = None |
| 339 | if args: |
Alexandre Vassalotti | 1f2ba4b | 2008-05-16 07:12:44 +0000 | [diff] [blame] | 340 | s = s + reprlib.repr(args) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 341 | else: |
| 342 | s = s + '()' |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 343 | if '__return__' in frame.f_locals: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 344 | rv = frame.f_locals['__return__'] |
| 345 | s = s + '->' |
Alexandre Vassalotti | 1f2ba4b | 2008-05-16 07:12:44 +0000 | [diff] [blame] | 346 | s = s + reprlib.repr(rv) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 347 | line = linecache.getline(filename, lineno) |
Eric S. Raymond | b49f4a4 | 2001-02-09 05:07:04 +0000 | [diff] [blame] | 348 | if line: s = s + lprefix + line.strip() |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 349 | return s |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 350 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 351 | # The following two methods can be called by clients to use |
| 352 | # a debugger to debug a statement, given as a string. |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 353 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 354 | def run(self, cmd, globals=None, locals=None): |
| 355 | if globals is None: |
| 356 | import __main__ |
| 357 | globals = __main__.__dict__ |
| 358 | if locals is None: |
| 359 | locals = globals |
| 360 | self.reset() |
| 361 | sys.settrace(self.trace_dispatch) |
| 362 | if not isinstance(cmd, types.CodeType): |
| 363 | cmd = cmd+'\n' |
| 364 | try: |
Martin v. Löwis | ede8ffb | 2008-03-30 20:43:50 +0000 | [diff] [blame] | 365 | exec(cmd, globals, locals) |
| 366 | except BdbQuit: |
| 367 | pass |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 368 | finally: |
| 369 | self.quitting = 1 |
| 370 | sys.settrace(None) |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 371 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 372 | def runeval(self, expr, globals=None, locals=None): |
| 373 | if globals is None: |
| 374 | import __main__ |
| 375 | globals = __main__.__dict__ |
| 376 | if locals is None: |
| 377 | locals = globals |
| 378 | self.reset() |
| 379 | sys.settrace(self.trace_dispatch) |
| 380 | if not isinstance(expr, types.CodeType): |
| 381 | expr = expr+'\n' |
| 382 | try: |
Martin v. Löwis | ede8ffb | 2008-03-30 20:43:50 +0000 | [diff] [blame] | 383 | return eval(expr, globals, locals) |
| 384 | except BdbQuit: |
| 385 | pass |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 386 | finally: |
| 387 | self.quitting = 1 |
| 388 | sys.settrace(None) |
Guido van Rossum | 5e38b6f | 1995-02-27 13:13:40 +0000 | [diff] [blame] | 389 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 390 | def runctx(self, cmd, globals, locals): |
| 391 | # B/W compatibility |
| 392 | self.run(cmd, globals, locals) |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 393 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 394 | # This method is more useful to debug a single function call. |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 395 | |
Raymond Hettinger | 2ef7e6c | 2004-10-24 00:32:24 +0000 | [diff] [blame] | 396 | def runcall(self, func, *args, **kwds): |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 397 | self.reset() |
| 398 | sys.settrace(self.trace_dispatch) |
| 399 | res = None |
| 400 | try: |
Martin v. Löwis | ede8ffb | 2008-03-30 20:43:50 +0000 | [diff] [blame] | 401 | res = func(*args, **kwds) |
| 402 | except BdbQuit: |
| 403 | pass |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 404 | finally: |
| 405 | self.quitting = 1 |
| 406 | sys.settrace(None) |
| 407 | return res |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 408 | |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 409 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 410 | def set_trace(): |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 411 | Bdb().set_trace() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 412 | |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 413 | |
| 414 | class Breakpoint: |
| 415 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 416 | """Breakpoint class |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 417 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 418 | Implements temporary breakpoints, ignore counts, disabling and |
| 419 | (re)-enabling, and conditionals. |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 420 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 421 | Breakpoints are indexed by number through bpbynumber and by |
| 422 | the file,line tuple using bplist. The former points to a |
| 423 | single instance of class Breakpoint. The latter points to a |
| 424 | list of such instances since there may be more than one |
| 425 | breakpoint per line. |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 426 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 427 | """ |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 428 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 429 | # XXX Keeping state in the class is a mistake -- this means |
| 430 | # you cannot have more than one active Bdb instance. |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 431 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 432 | next = 1 # Next bp to be assigned |
| 433 | bplist = {} # indexed by (file, lineno) tuple |
| 434 | bpbynumber = [None] # Each entry is None or an instance of Bpt |
| 435 | # index 0 is unused, except for marking an |
| 436 | # effective break .... see effective() |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 437 | |
Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 438 | def __init__(self, file, line, temporary=0, cond=None, funcname=None): |
| 439 | self.funcname = funcname |
| 440 | # Needed if funcname is not None. |
| 441 | self.func_first_executable_line = None |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 442 | self.file = file # This better be in canonical form! |
| 443 | self.line = line |
| 444 | self.temporary = temporary |
| 445 | self.cond = cond |
| 446 | self.enabled = 1 |
| 447 | self.ignore = 0 |
| 448 | self.hits = 0 |
| 449 | self.number = Breakpoint.next |
| 450 | Breakpoint.next = Breakpoint.next + 1 |
| 451 | # Build the two lists |
| 452 | self.bpbynumber.append(self) |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 453 | if (file, line) in self.bplist: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 454 | self.bplist[file, line].append(self) |
| 455 | else: |
| 456 | self.bplist[file, line] = [self] |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 457 | |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 458 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 459 | def deleteMe(self): |
| 460 | index = (self.file, self.line) |
| 461 | self.bpbynumber[self.number] = None # No longer in list |
| 462 | self.bplist[index].remove(self) |
| 463 | if not self.bplist[index]: |
| 464 | # No more bp for this f:l combo |
| 465 | del self.bplist[index] |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 466 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 467 | def enable(self): |
| 468 | self.enabled = 1 |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 469 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 470 | def disable(self): |
| 471 | self.enabled = 0 |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 472 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 473 | def bpprint(self, out=None): |
| 474 | if out is None: |
| 475 | out = sys.stdout |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 476 | if self.temporary: |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 477 | disp = 'del ' |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 478 | else: |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 479 | disp = 'keep ' |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 480 | if self.enabled: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 481 | disp = disp + 'yes ' |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 482 | else: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 483 | disp = disp + 'no ' |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 484 | print('%-4dbreakpoint %s at %s:%d' % (self.number, disp, |
| 485 | self.file, self.line), file=out) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 486 | if self.cond: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 487 | print('\tstop only if %s' % (self.cond,), file=out) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 488 | if self.ignore: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 489 | print('\tignore next %d hits' % (self.ignore), file=out) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 490 | if (self.hits): |
| 491 | if (self.hits > 1): ss = 's' |
| 492 | else: ss = '' |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 493 | print(('\tbreakpoint already hit %d time%s' % |
| 494 | (self.hits, ss)), file=out) |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 495 | |
| 496 | # -----------end of Breakpoint class---------- |
| 497 | |
Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 498 | def checkfuncname(b, frame): |
| 499 | """Check whether we should break here because of `b.funcname`.""" |
| 500 | if not b.funcname: |
| 501 | # Breakpoint was set via line number. |
| 502 | if b.line != frame.f_lineno: |
| 503 | # Breakpoint was set at a line with a def statement and the function |
| 504 | # defined is called: don't break. |
| 505 | return False |
| 506 | return True |
| 507 | |
| 508 | # Breakpoint set via function name. |
| 509 | |
| 510 | if frame.f_code.co_name != b.funcname: |
| 511 | # It's not a function call, but rather execution of def statement. |
| 512 | return False |
| 513 | |
| 514 | # We are in the right frame. |
| 515 | if not b.func_first_executable_line: |
| 516 | # The function is entered for the 1st time. |
| 517 | b.func_first_executable_line = frame.f_lineno |
| 518 | |
| 519 | if b.func_first_executable_line != frame.f_lineno: |
| 520 | # But we are not at the first line number: don't break. |
| 521 | return False |
| 522 | return True |
| 523 | |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 524 | # Determines if there is an effective (active) breakpoint at this |
| 525 | # line of code. Returns breakpoint number or 0 if none |
| 526 | def effective(file, line, frame): |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 527 | """Determine which breakpoint for this file:line is to be acted upon. |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 528 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 529 | Called only if we know there is a bpt at this |
| 530 | location. Returns breakpoint that was triggered and a flag |
| 531 | that indicates if it is ok to delete a temporary bp. |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 532 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 533 | """ |
| 534 | possibles = Breakpoint.bplist[file,line] |
| 535 | for i in range(0, len(possibles)): |
| 536 | b = possibles[i] |
| 537 | if b.enabled == 0: |
| 538 | continue |
Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 539 | if not checkfuncname(b, frame): |
| 540 | continue |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 541 | # Count every hit when bp is enabled |
| 542 | b.hits = b.hits + 1 |
| 543 | if not b.cond: |
| 544 | # If unconditional, and ignoring, |
| 545 | # go on to next, else break |
| 546 | if b.ignore > 0: |
| 547 | b.ignore = b.ignore -1 |
| 548 | continue |
| 549 | else: |
| 550 | # breakpoint and marker that's ok |
| 551 | # to delete if temporary |
| 552 | return (b,1) |
| 553 | else: |
| 554 | # Conditional bp. |
| 555 | # Ignore count applies only to those bpt hits where the |
| 556 | # condition evaluates to true. |
| 557 | try: |
| 558 | val = eval(b.cond, frame.f_globals, |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 559 | frame.f_locals) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 560 | if val: |
| 561 | if b.ignore > 0: |
| 562 | b.ignore = b.ignore -1 |
| 563 | # continue |
| 564 | else: |
| 565 | return (b,1) |
| 566 | # else: |
| 567 | # continue |
| 568 | except: |
| 569 | # if eval fails, most conservative |
| 570 | # thing is to stop on breakpoint |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 571 | # regardless of ignore count. |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 572 | # Don't delete temporary, |
| 573 | # as another hint to user. |
| 574 | return (b,0) |
| 575 | return (None, None) |
Guido van Rossum | d93643f | 1998-09-11 22:38:35 +0000 | [diff] [blame] | 576 | |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 577 | # -------------------- testing -------------------- |
| 578 | |
| 579 | class Tdb(Bdb): |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 580 | def user_call(self, frame, args): |
| 581 | name = frame.f_code.co_name |
| 582 | if not name: name = '???' |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 583 | print('+++ call', name, args) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 584 | def user_line(self, frame): |
Eric S. Raymond | b49f4a4 | 2001-02-09 05:07:04 +0000 | [diff] [blame] | 585 | import linecache |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 586 | name = frame.f_code.co_name |
| 587 | if not name: name = '???' |
| 588 | fn = self.canonic(frame.f_code.co_filename) |
| 589 | line = linecache.getline(fn, frame.f_lineno) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 590 | print('+++', fn, frame.f_lineno, name, ':', line.strip()) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 591 | def user_return(self, frame, retval): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 592 | print('+++ return', retval) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 593 | def user_exception(self, frame, exc_stuff): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 594 | print('+++ exception', exc_stuff) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 595 | self.set_continue() |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 596 | |
| 597 | def foo(n): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 598 | print('foo(', n, ')') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 599 | x = bar(n*10) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 600 | print('bar returned', x) |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 601 | |
| 602 | def bar(a): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 603 | print('bar(', a, ')') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 604 | return a/2 |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 605 | |
| 606 | def test(): |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 607 | t = Tdb() |
| 608 | t.run('import bdb; bdb.foo(10)') |
Eric S. Raymond | b49f4a4 | 2001-02-09 05:07:04 +0000 | [diff] [blame] | 609 | |
| 610 | # end |