Mercurial > cpython-lru_cache-weakref
annotate Lib/bdb.py @ 5817:48b94de85bf3 legacy-trunk
Extensive changes by AMK.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 17 Jul 1997 16:29:42 +0000 |
| parents | 309aab4c0b98 |
| children | 2656d1eea792 |
| rev | line source |
|---|---|
| 622 | 1 # A generic Python debugger base class. |
| 2 # This class takes care of details of the trace facility; | |
| 3 # a derived class should implement user interaction. | |
| 4 # There are two debuggers based upon this: | |
| 5 # 'pdb', a text-oriented debugger not unlike dbx or gdb; | |
| 6 # and 'wdb', a window-oriented debugger. | |
| 7 # And of course... you can roll your own! | |
| 8 | |
| 9 import sys | |
|
4411
d738490c8da1
Allow code objects to be passed to run() and eval().
Guido van Rossum <guido@python.org>
parents:
2437
diff
changeset
|
10 import types |
| 622 | 11 |
| 12 BdbQuit = 'bdb.BdbQuit' # Exception to give up completely | |
| 13 | |
| 14 | |
| 15 class Bdb: # Basic Debugger | |
| 16 | |
|
1317
ec312b647fcb
pdb.py, bdb.py, cmd.py: use __init__() instead of init()
Guido van Rossum <guido@python.org>
parents:
1110
diff
changeset
|
17 def __init__(self): |
| 622 | 18 self.breaks = {} |
|
5765
d6421b3ed1da
Support for conditional breakpoints (Jim Fulton).
Guido van Rossum <guido@python.org>
parents:
4411
diff
changeset
|
19 self.cbreaks = {} |
| 622 | 20 |
| 21 def reset(self): | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
22 import linecache |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
23 linecache.checkcache() |
| 622 | 24 self.botframe = None |
| 25 self.stopframe = None | |
| 26 self.returnframe = None | |
| 27 self.quitting = 0 | |
| 28 | |
| 29 def trace_dispatch(self, frame, event, arg): | |
| 30 if self.quitting: | |
| 31 return # None | |
| 32 if event == 'line': | |
| 33 return self.dispatch_line(frame) | |
| 34 if event == 'call': | |
| 35 return self.dispatch_call(frame, arg) | |
| 36 if event == 'return': | |
| 37 return self.dispatch_return(frame, arg) | |
| 38 if event == 'exception': | |
| 39 return self.dispatch_exception(frame, arg) | |
| 40 print 'bdb.Bdb.dispatch: unknown debugging event:', `event` | |
| 41 return self.trace_dispatch | |
| 42 | |
| 43 def dispatch_line(self, frame): | |
| 44 if self.stop_here(frame) or self.break_here(frame): | |
| 45 self.user_line(frame) | |
| 46 if self.quitting: raise BdbQuit | |
| 47 return self.trace_dispatch | |
| 48 | |
| 49 def dispatch_call(self, frame, arg): | |
| 50 frame.f_locals['__args__'] = arg | |
| 51 if self.botframe is None: | |
| 52 # First call of dispatch since reset() | |
| 53 self.botframe = frame | |
| 54 return self.trace_dispatch | |
| 55 if not (self.stop_here(frame) or self.break_anywhere(frame)): | |
| 56 # No need to trace this function | |
| 57 return # None | |
| 58 self.user_call(frame, arg) | |
| 59 if self.quitting: raise BdbQuit | |
| 60 return self.trace_dispatch | |
| 61 | |
| 62 def dispatch_return(self, frame, arg): | |
| 63 if self.stop_here(frame) or frame == self.returnframe: | |
| 64 self.user_return(frame, arg) | |
| 65 if self.quitting: raise BdbQuit | |
| 66 | |
| 67 def dispatch_exception(self, frame, arg): | |
| 68 if self.stop_here(frame): | |
| 69 self.user_exception(frame, arg) | |
| 70 if self.quitting: raise BdbQuit | |
| 71 return self.trace_dispatch | |
| 72 | |
| 73 # Normally derived classes don't override the following | |
|
1006
903071377d29
Added a _v21 def to FL.py and added two new input field types
Guido van Rossum <guido@python.org>
parents:
786
diff
changeset
|
74 # methods, but they may if they want to redefine the |
| 622 | 75 # definition of stopping and breakpoints. |
| 76 | |
| 77 def stop_here(self, frame): | |
| 78 if self.stopframe is None: | |
| 79 return 1 | |
| 80 if frame is self.stopframe: | |
| 81 return 1 | |
| 82 while frame is not None and frame is not self.stopframe: | |
| 83 if frame is self.botframe: | |
| 84 return 1 | |
| 85 frame = frame.f_back | |
| 86 return 0 | |
| 87 | |
| 88 def break_here(self, frame): | |
| 5766 | 89 filename=frame.f_code.co_filename |
|
5765
d6421b3ed1da
Support for conditional breakpoints (Jim Fulton).
Guido van Rossum <guido@python.org>
parents:
4411
diff
changeset
|
90 if not self.breaks.has_key(filename): |
| 622 | 91 return 0 |
|
5765
d6421b3ed1da
Support for conditional breakpoints (Jim Fulton).
Guido van Rossum <guido@python.org>
parents:
4411
diff
changeset
|
92 lineno=frame.f_lineno |
|
d6421b3ed1da
Support for conditional breakpoints (Jim Fulton).
Guido van Rossum <guido@python.org>
parents:
4411
diff
changeset
|
93 if not lineno in self.breaks[filename]: |
| 622 | 94 return 0 |
|
5765
d6421b3ed1da
Support for conditional breakpoints (Jim Fulton).
Guido van Rossum <guido@python.org>
parents:
4411
diff
changeset
|
95 if self.cbreaks.has_key((filename, lineno)): |
| 5766 | 96 cond=self.cbreaks[filename, lineno] |
| 97 return eval(cond, frame.f_globals, | |
| 98 frame.f_locals) | |
| 622 | 99 return 1 |
| 100 | |
| 101 def break_anywhere(self, frame): | |
| 102 return self.breaks.has_key(frame.f_code.co_filename) | |
| 103 | |
|
1006
903071377d29
Added a _v21 def to FL.py and added two new input field types
Guido van Rossum <guido@python.org>
parents:
786
diff
changeset
|
104 # Derived classes should override the user_* methods |
| 622 | 105 # to gain control. |
| 106 | |
| 107 def user_call(self, frame, argument_list): | |
|
1006
903071377d29
Added a _v21 def to FL.py and added two new input field types
Guido van Rossum <guido@python.org>
parents:
786
diff
changeset
|
108 # This method is called when there is the remote possibility |
| 622 | 109 # that we ever need to stop in this function |
| 110 pass | |
| 111 | |
| 112 def user_line(self, frame): | |
|
1006
903071377d29
Added a _v21 def to FL.py and added two new input field types
Guido van Rossum <guido@python.org>
parents:
786
diff
changeset
|
113 # This method is called when we stop or break at this line |
| 622 | 114 pass |
| 115 | |
| 116 def user_return(self, frame, return_value): | |
|
1006
903071377d29
Added a _v21 def to FL.py and added two new input field types
Guido van Rossum <guido@python.org>
parents:
786
diff
changeset
|
117 # This method is called when a return trap is set here |
| 622 | 118 pass |
| 119 | |
| 120 def user_exception(self, frame, (exc_type, exc_value, exc_traceback)): | |
|
1006
903071377d29
Added a _v21 def to FL.py and added two new input field types
Guido van Rossum <guido@python.org>
parents:
786
diff
changeset
|
121 # This method is called if an exception occurs, |
| 622 | 122 # but only if we are to stop at or just below this level |
| 123 pass | |
| 124 | |
|
1006
903071377d29
Added a _v21 def to FL.py and added two new input field types
Guido van Rossum <guido@python.org>
parents:
786
diff
changeset
|
125 # Derived classes and clients can call the following methods |
| 622 | 126 # to affect the stepping state. |
| 127 | |
| 128 def set_step(self): | |
| 129 # Stop after one line of code | |
| 130 self.stopframe = None | |
| 131 self.returnframe = None | |
| 132 self.quitting = 0 | |
| 133 | |
| 134 def set_next(self, frame): | |
| 135 # Stop on the next line in or below the given frame | |
| 136 self.stopframe = frame | |
| 137 self.returnframe = None | |
| 138 self.quitting = 0 | |
| 139 | |
| 140 def set_return(self, frame): | |
| 141 # Stop when returning from the given frame | |
| 142 self.stopframe = frame.f_back | |
| 143 self.returnframe = frame | |
| 144 self.quitting = 0 | |
| 145 | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
146 def set_trace(self): |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
147 # Start debugging from here |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
148 try: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
149 1 + '' |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
150 except: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
151 frame = sys.exc_traceback.tb_frame.f_back |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
152 self.reset() |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
153 while frame: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
154 frame.f_trace = self.trace_dispatch |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
155 self.botframe = frame |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
156 frame = frame.f_back |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
157 self.set_step() |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
158 sys.settrace(self.trace_dispatch) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
159 |
| 622 | 160 def set_continue(self): |
| 161 # Don't stop except at breakpoints or when finished | |
| 162 self.stopframe = self.botframe | |
| 163 self.returnframe = None | |
| 164 self.quitting = 0 | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
165 if not self.breaks: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
166 # no breakpoints; run without debugger overhead |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
167 sys.settrace(None) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
168 try: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
169 1 + '' # raise an exception |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
170 except: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
171 frame = sys.exc_traceback.tb_frame.f_back |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
172 while frame and frame is not self.botframe: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
173 del frame.f_trace |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
174 frame = frame.f_back |
| 622 | 175 |
| 176 def set_quit(self): | |
| 177 self.stopframe = self.botframe | |
| 178 self.returnframe = None | |
| 179 self.quitting = 1 | |
|
720
f7278a067fb4
use settrace() intreface
Guido van Rossum <guido@python.org>
parents:
651
diff
changeset
|
180 sys.settrace(None) |
| 622 | 181 |
|
1006
903071377d29
Added a _v21 def to FL.py and added two new input field types
Guido van Rossum <guido@python.org>
parents:
786
diff
changeset
|
182 # Derived classes and clients can call the following methods |
|
903071377d29
Added a _v21 def to FL.py and added two new input field types
Guido van Rossum <guido@python.org>
parents:
786
diff
changeset
|
183 # to manipulate breakpoints. These methods return an |
| 622 | 184 # error message is something went wrong, None if all is well. |
| 185 # Call self.get_*break*() to see the breakpoints. | |
| 186 | |
|
5765
d6421b3ed1da
Support for conditional breakpoints (Jim Fulton).
Guido van Rossum <guido@python.org>
parents:
4411
diff
changeset
|
187 def set_break(self, filename, lineno, cond=None): |
| 622 | 188 import linecache # Import as late as possible |
| 189 line = linecache.getline(filename, lineno) | |
| 190 if not line: | |
| 191 return 'That line does not exist!' | |
| 192 if not self.breaks.has_key(filename): | |
| 193 self.breaks[filename] = [] | |
| 194 list = self.breaks[filename] | |
| 195 if lineno in list: | |
| 196 return 'There is already a breakpoint there!' | |
| 197 list.append(lineno) | |
|
5765
d6421b3ed1da
Support for conditional breakpoints (Jim Fulton).
Guido van Rossum <guido@python.org>
parents:
4411
diff
changeset
|
198 if cond is not None: self.cbreaks[filename, lineno]=cond |
| 622 | 199 |
| 200 def clear_break(self, filename, lineno): | |
| 201 if not self.breaks.has_key(filename): | |
| 202 return 'There are no breakpoints in that file!' | |
| 203 if lineno not in self.breaks[filename]: | |
| 204 return 'There is no breakpoint there!' | |
| 205 self.breaks[filename].remove(lineno) | |
| 206 if not self.breaks[filename]: | |
| 207 del self.breaks[filename] | |
|
5765
d6421b3ed1da
Support for conditional breakpoints (Jim Fulton).
Guido van Rossum <guido@python.org>
parents:
4411
diff
changeset
|
208 try: del self.cbreaks[filename, lineno] |
|
d6421b3ed1da
Support for conditional breakpoints (Jim Fulton).
Guido van Rossum <guido@python.org>
parents:
4411
diff
changeset
|
209 except: pass |
| 622 | 210 |
| 211 def clear_all_file_breaks(self, filename): | |
| 212 if not self.breaks.has_key(filename): | |
| 213 return 'There are no breakpoints in that file!' | |
| 214 del self.breaks[filename] | |
|
5765
d6421b3ed1da
Support for conditional breakpoints (Jim Fulton).
Guido van Rossum <guido@python.org>
parents:
4411
diff
changeset
|
215 for f,l in self.cbreaks.keys(): |
|
d6421b3ed1da
Support for conditional breakpoints (Jim Fulton).
Guido van Rossum <guido@python.org>
parents:
4411
diff
changeset
|
216 if f==filename: del self.cbreaks[f,l] |
| 622 | 217 |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
218 def clear_all_breaks(self): |
| 622 | 219 if not self.breaks: |
| 220 return 'There are no breakpoints!' | |
| 221 self.breaks = {} | |
|
5765
d6421b3ed1da
Support for conditional breakpoints (Jim Fulton).
Guido van Rossum <guido@python.org>
parents:
4411
diff
changeset
|
222 self.cbreaks = {} |
| 622 | 223 |
| 224 def get_break(self, filename, lineno): | |
| 225 return self.breaks.has_key(filename) and \ | |
| 226 lineno in self.breaks[filename] | |
| 227 | |
| 228 def get_file_breaks(self, filename): | |
| 229 if self.breaks.has_key(filename): | |
| 230 return self.breaks[filename] | |
| 231 else: | |
| 232 return [] | |
| 233 | |
| 234 def get_all_breaks(self): | |
| 235 return self.breaks | |
| 236 | |
|
1006
903071377d29
Added a _v21 def to FL.py and added two new input field types
Guido van Rossum <guido@python.org>
parents:
786
diff
changeset
|
237 # Derived classes and clients can call the following method |
| 622 | 238 # to get a data structure representing a stack trace. |
| 239 | |
| 240 def get_stack(self, f, t): | |
| 241 stack = [] | |
| 242 if t and t.tb_frame is f: | |
| 243 t = t.tb_next | |
| 244 while f is not None: | |
| 245 stack.append((f, f.f_lineno)) | |
| 246 if f is self.botframe: | |
| 247 break | |
| 248 f = f.f_back | |
| 249 stack.reverse() | |
| 250 i = max(0, len(stack) - 1) | |
| 251 while t is not None: | |
| 252 stack.append((t.tb_frame, t.tb_lineno)) | |
| 253 t = t.tb_next | |
| 254 return stack, i | |
| 255 | |
|
651
73fe7a1a5dd4
Added a method "format_stack_entry" to be used by derived classes.
Guido van Rossum <guido@python.org>
parents:
623
diff
changeset
|
256 # |
|
73fe7a1a5dd4
Added a method "format_stack_entry" to be used by derived classes.
Guido van Rossum <guido@python.org>
parents:
623
diff
changeset
|
257 |
|
1999
c5a514f8f3e3
improved prompt format
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
258 def format_stack_entry(self, frame_lineno, lprefix=': '): |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
259 import linecache, repr, string |
|
1110
1d06b1499eb9
* Got entirely rid of path.py.
Guido van Rossum <guido@python.org>
parents:
1006
diff
changeset
|
260 frame, lineno = frame_lineno |
|
651
73fe7a1a5dd4
Added a method "format_stack_entry" to be used by derived classes.
Guido van Rossum <guido@python.org>
parents:
623
diff
changeset
|
261 filename = frame.f_code.co_filename |
|
73fe7a1a5dd4
Added a method "format_stack_entry" to be used by derived classes.
Guido van Rossum <guido@python.org>
parents:
623
diff
changeset
|
262 s = filename + '(' + `lineno` + ')' |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
263 if frame.f_code.co_name: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
264 s = s + frame.f_code.co_name |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
265 else: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
266 s = s + "<lambda>" |
|
651
73fe7a1a5dd4
Added a method "format_stack_entry" to be used by derived classes.
Guido van Rossum <guido@python.org>
parents:
623
diff
changeset
|
267 if frame.f_locals.has_key('__args__'): |
|
73fe7a1a5dd4
Added a method "format_stack_entry" to be used by derived classes.
Guido van Rossum <guido@python.org>
parents:
623
diff
changeset
|
268 args = frame.f_locals['__args__'] |
|
2280
650cb67d7659
fix formatting of stack entries
Guido van Rossum <guido@python.org>
parents:
1999
diff
changeset
|
269 else: |
|
650cb67d7659
fix formatting of stack entries
Guido van Rossum <guido@python.org>
parents:
1999
diff
changeset
|
270 args = None |
|
650cb67d7659
fix formatting of stack entries
Guido van Rossum <guido@python.org>
parents:
1999
diff
changeset
|
271 if args: |
|
650cb67d7659
fix formatting of stack entries
Guido van Rossum <guido@python.org>
parents:
1999
diff
changeset
|
272 s = s + repr.repr(args) |
|
650cb67d7659
fix formatting of stack entries
Guido van Rossum <guido@python.org>
parents:
1999
diff
changeset
|
273 else: |
|
650cb67d7659
fix formatting of stack entries
Guido van Rossum <guido@python.org>
parents:
1999
diff
changeset
|
274 s = s + '()' |
|
651
73fe7a1a5dd4
Added a method "format_stack_entry" to be used by derived classes.
Guido van Rossum <guido@python.org>
parents:
623
diff
changeset
|
275 if frame.f_locals.has_key('__return__'): |
|
73fe7a1a5dd4
Added a method "format_stack_entry" to be used by derived classes.
Guido van Rossum <guido@python.org>
parents:
623
diff
changeset
|
276 rv = frame.f_locals['__return__'] |
|
73fe7a1a5dd4
Added a method "format_stack_entry" to be used by derived classes.
Guido van Rossum <guido@python.org>
parents:
623
diff
changeset
|
277 s = s + '->' |
|
73fe7a1a5dd4
Added a method "format_stack_entry" to be used by derived classes.
Guido van Rossum <guido@python.org>
parents:
623
diff
changeset
|
278 s = s + repr.repr(rv) |
|
73fe7a1a5dd4
Added a method "format_stack_entry" to be used by derived classes.
Guido van Rossum <guido@python.org>
parents:
623
diff
changeset
|
279 line = linecache.getline(filename, lineno) |
|
1999
c5a514f8f3e3
improved prompt format
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
280 if line: s = s + lprefix + string.strip(line) |
|
651
73fe7a1a5dd4
Added a method "format_stack_entry" to be used by derived classes.
Guido van Rossum <guido@python.org>
parents:
623
diff
changeset
|
281 return s |
|
73fe7a1a5dd4
Added a method "format_stack_entry" to be used by derived classes.
Guido van Rossum <guido@python.org>
parents:
623
diff
changeset
|
282 |
|
1006
903071377d29
Added a _v21 def to FL.py and added two new input field types
Guido van Rossum <guido@python.org>
parents:
786
diff
changeset
|
283 # The following two methods can be called by clients to use |
| 622 | 284 # a debugger to debug a statement, given as a string. |
| 285 | |
|
2437
62ebbcfea827
handle class exceptions; added runeval; made runctx obsolete
Guido van Rossum <guido@python.org>
parents:
2280
diff
changeset
|
286 def run(self, cmd, globals=None, locals=None): |
|
62ebbcfea827
handle class exceptions; added runeval; made runctx obsolete
Guido van Rossum <guido@python.org>
parents:
2280
diff
changeset
|
287 if globals is None: |
|
62ebbcfea827
handle class exceptions; added runeval; made runctx obsolete
Guido van Rossum <guido@python.org>
parents:
2280
diff
changeset
|
288 import __main__ |
|
62ebbcfea827
handle class exceptions; added runeval; made runctx obsolete
Guido van Rossum <guido@python.org>
parents:
2280
diff
changeset
|
289 globals = __main__.__dict__ |
|
62ebbcfea827
handle class exceptions; added runeval; made runctx obsolete
Guido van Rossum <guido@python.org>
parents:
2280
diff
changeset
|
290 if locals is None: |
|
62ebbcfea827
handle class exceptions; added runeval; made runctx obsolete
Guido van Rossum <guido@python.org>
parents:
2280
diff
changeset
|
291 locals = globals |
| 622 | 292 self.reset() |
|
720
f7278a067fb4
use settrace() intreface
Guido van Rossum <guido@python.org>
parents:
651
diff
changeset
|
293 sys.settrace(self.trace_dispatch) |
|
4411
d738490c8da1
Allow code objects to be passed to run() and eval().
Guido van Rossum <guido@python.org>
parents:
2437
diff
changeset
|
294 if type(cmd) <> types.CodeType: |
|
d738490c8da1
Allow code objects to be passed to run() and eval().
Guido van Rossum <guido@python.org>
parents:
2437
diff
changeset
|
295 cmd = cmd+'\n' |
| 622 | 296 try: |
|
786
63d34e877a69
Different syntax: cannot mix except and finally any more
Guido van Rossum <guido@python.org>
parents:
720
diff
changeset
|
297 try: |
|
4411
d738490c8da1
Allow code objects to be passed to run() and eval().
Guido van Rossum <guido@python.org>
parents:
2437
diff
changeset
|
298 exec cmd in globals, locals |
|
2437
62ebbcfea827
handle class exceptions; added runeval; made runctx obsolete
Guido van Rossum <guido@python.org>
parents:
2280
diff
changeset
|
299 except BdbQuit: |
|
62ebbcfea827
handle class exceptions; added runeval; made runctx obsolete
Guido van Rossum <guido@python.org>
parents:
2280
diff
changeset
|
300 pass |
|
62ebbcfea827
handle class exceptions; added runeval; made runctx obsolete
Guido van Rossum <guido@python.org>
parents:
2280
diff
changeset
|
301 finally: |
|
62ebbcfea827
handle class exceptions; added runeval; made runctx obsolete
Guido van Rossum <guido@python.org>
parents:
2280
diff
changeset
|
302 self.quitting = 1 |
|
62ebbcfea827
handle class exceptions; added runeval; made runctx obsolete
Guido van Rossum <guido@python.org>
parents:
2280
diff
changeset
|
303 sys.settrace(None) |
|
62ebbcfea827
handle class exceptions; added runeval; made runctx obsolete
Guido van Rossum <guido@python.org>
parents:
2280
diff
changeset
|
304 |
|
62ebbcfea827
handle class exceptions; added runeval; made runctx obsolete
Guido van Rossum <guido@python.org>
parents:
2280
diff
changeset
|
305 def runeval(self, expr, globals=None, locals=None): |
|
62ebbcfea827
handle class exceptions; added runeval; made runctx obsolete
Guido van Rossum <guido@python.org>
parents:
2280
diff
changeset
|
306 if globals is None: |
|
62ebbcfea827
handle class exceptions; added runeval; made runctx obsolete
Guido van Rossum <guido@python.org>
parents:
2280
diff
changeset
|
307 import __main__ |
|
62ebbcfea827
handle class exceptions; added runeval; made runctx obsolete
Guido van Rossum <guido@python.org>
parents:
2280
diff
changeset
|
308 globals = __main__.__dict__ |
|
62ebbcfea827
handle class exceptions; added runeval; made runctx obsolete
Guido van Rossum <guido@python.org>
parents:
2280
diff
changeset
|
309 if locals is None: |
|
62ebbcfea827
handle class exceptions; added runeval; made runctx obsolete
Guido van Rossum <guido@python.org>
parents:
2280
diff
changeset
|
310 locals = globals |
|
62ebbcfea827
handle class exceptions; added runeval; made runctx obsolete
Guido van Rossum <guido@python.org>
parents:
2280
diff
changeset
|
311 self.reset() |
|
62ebbcfea827
handle class exceptions; added runeval; made runctx obsolete
Guido van Rossum <guido@python.org>
parents:
2280
diff
changeset
|
312 sys.settrace(self.trace_dispatch) |
|
4411
d738490c8da1
Allow code objects to be passed to run() and eval().
Guido van Rossum <guido@python.org>
parents:
2437
diff
changeset
|
313 if type(expr) <> types.CodeType: |
|
d738490c8da1
Allow code objects to be passed to run() and eval().
Guido van Rossum <guido@python.org>
parents:
2437
diff
changeset
|
314 expr = expr+'\n' |
|
2437
62ebbcfea827
handle class exceptions; added runeval; made runctx obsolete
Guido van Rossum <guido@python.org>
parents:
2280
diff
changeset
|
315 try: |
|
62ebbcfea827
handle class exceptions; added runeval; made runctx obsolete
Guido van Rossum <guido@python.org>
parents:
2280
diff
changeset
|
316 try: |
|
4411
d738490c8da1
Allow code objects to be passed to run() and eval().
Guido van Rossum <guido@python.org>
parents:
2437
diff
changeset
|
317 return eval(expr, globals, locals) |
|
786
63d34e877a69
Different syntax: cannot mix except and finally any more
Guido van Rossum <guido@python.org>
parents:
720
diff
changeset
|
318 except BdbQuit: |
|
63d34e877a69
Different syntax: cannot mix except and finally any more
Guido van Rossum <guido@python.org>
parents:
720
diff
changeset
|
319 pass |
| 622 | 320 finally: |
| 321 self.quitting = 1 | |
|
720
f7278a067fb4
use settrace() intreface
Guido van Rossum <guido@python.org>
parents:
651
diff
changeset
|
322 sys.settrace(None) |
|
1006
903071377d29
Added a _v21 def to FL.py and added two new input field types
Guido van Rossum <guido@python.org>
parents:
786
diff
changeset
|
323 |
|
2437
62ebbcfea827
handle class exceptions; added runeval; made runctx obsolete
Guido van Rossum <guido@python.org>
parents:
2280
diff
changeset
|
324 def runctx(self, cmd, globals, locals): |
|
62ebbcfea827
handle class exceptions; added runeval; made runctx obsolete
Guido van Rossum <guido@python.org>
parents:
2280
diff
changeset
|
325 # B/W compatibility |
|
62ebbcfea827
handle class exceptions; added runeval; made runctx obsolete
Guido van Rossum <guido@python.org>
parents:
2280
diff
changeset
|
326 self.run(cmd, globals, locals) |
|
62ebbcfea827
handle class exceptions; added runeval; made runctx obsolete
Guido van Rossum <guido@python.org>
parents:
2280
diff
changeset
|
327 |
|
1006
903071377d29
Added a _v21 def to FL.py and added two new input field types
Guido van Rossum <guido@python.org>
parents:
786
diff
changeset
|
328 # This method is more useful to debug a single function call. |
|
903071377d29
Added a _v21 def to FL.py and added two new input field types
Guido van Rossum <guido@python.org>
parents:
786
diff
changeset
|
329 |
|
903071377d29
Added a _v21 def to FL.py and added two new input field types
Guido van Rossum <guido@python.org>
parents:
786
diff
changeset
|
330 def runcall(self, func, *args): |
|
903071377d29
Added a _v21 def to FL.py and added two new input field types
Guido van Rossum <guido@python.org>
parents:
786
diff
changeset
|
331 self.reset() |
|
903071377d29
Added a _v21 def to FL.py and added two new input field types
Guido van Rossum <guido@python.org>
parents:
786
diff
changeset
|
332 sys.settrace(self.trace_dispatch) |
|
2437
62ebbcfea827
handle class exceptions; added runeval; made runctx obsolete
Guido van Rossum <guido@python.org>
parents:
2280
diff
changeset
|
333 res = None |
|
1006
903071377d29
Added a _v21 def to FL.py and added two new input field types
Guido van Rossum <guido@python.org>
parents:
786
diff
changeset
|
334 try: |
|
903071377d29
Added a _v21 def to FL.py and added two new input field types
Guido van Rossum <guido@python.org>
parents:
786
diff
changeset
|
335 try: |
|
2437
62ebbcfea827
handle class exceptions; added runeval; made runctx obsolete
Guido van Rossum <guido@python.org>
parents:
2280
diff
changeset
|
336 res = apply(func, args) |
|
1006
903071377d29
Added a _v21 def to FL.py and added two new input field types
Guido van Rossum <guido@python.org>
parents:
786
diff
changeset
|
337 except BdbQuit: |
|
903071377d29
Added a _v21 def to FL.py and added two new input field types
Guido van Rossum <guido@python.org>
parents:
786
diff
changeset
|
338 pass |
|
903071377d29
Added a _v21 def to FL.py and added two new input field types
Guido van Rossum <guido@python.org>
parents:
786
diff
changeset
|
339 finally: |
|
903071377d29
Added a _v21 def to FL.py and added two new input field types
Guido van Rossum <guido@python.org>
parents:
786
diff
changeset
|
340 self.quitting = 1 |
|
903071377d29
Added a _v21 def to FL.py and added two new input field types
Guido van Rossum <guido@python.org>
parents:
786
diff
changeset
|
341 sys.settrace(None) |
|
2437
62ebbcfea827
handle class exceptions; added runeval; made runctx obsolete
Guido van Rossum <guido@python.org>
parents:
2280
diff
changeset
|
342 return res |
| 622 | 343 |
| 344 | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
345 def set_trace(): |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
346 Bdb().set_trace() |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
347 |
| 622 | 348 # -------------------- testing -------------------- |
| 349 | |
| 350 class Tdb(Bdb): | |
| 351 def user_call(self, frame, args): | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
352 name = frame.f_code.co_name |
| 622 | 353 if not name: name = '???' |
| 354 print '+++ call', name, args | |
| 355 def user_line(self, frame): | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
356 import linecache, string |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
357 name = frame.f_code.co_name |
| 622 | 358 if not name: name = '???' |
| 359 fn = frame.f_code.co_filename | |
| 360 line = linecache.getline(fn, frame.f_lineno) | |
| 361 print '+++', fn, frame.f_lineno, name, ':', string.strip(line) | |
| 362 def user_return(self, frame, retval): | |
| 363 print '+++ return', retval | |
| 364 def user_exception(self, frame, exc_stuff): | |
| 365 print '+++ exception', exc_stuff | |
| 366 self.set_continue() | |
| 367 | |
| 368 def foo(n): | |
| 369 print 'foo(', n, ')' | |
| 370 x = bar(n*10) | |
| 371 print 'bar returned', x | |
| 372 | |
| 373 def bar(a): | |
| 374 print 'bar(', a, ')' | |
| 375 return a/2 | |
| 376 | |
| 377 def test(): | |
|
1317
ec312b647fcb
pdb.py, bdb.py, cmd.py: use __init__() instead of init()
Guido van Rossum <guido@python.org>
parents:
1110
diff
changeset
|
378 t = Tdb() |
| 622 | 379 t.run('import bdb; bdb.foo(10)') |
