blob: 2824f4c22d3969e2a248538b0f1e81dd5b30353a [file] [log] [blame]
Guido van Rossumf06ee5f1996-11-27 19:52:01 +00001#! /usr/bin/env python
Guido van Rossumf17361d1996-07-30 16:28:13 +00002
Guido van Rossum6fe08b01992-01-16 13:50:21 +00003# pdb.py -- finally, a Python debugger!
Guido van Rossum92df0c61992-01-14 18:30:15 +00004
Guido van Rossum23efba41992-01-27 16:58:47 +00005# (See pdb.doc for documentation.)
Guido van Rossum921c8241992-01-10 14:54:42 +00006
7import string
8import sys
9import linecache
Guido van Rossum23efba41992-01-27 16:58:47 +000010import cmd
11import bdb
12import repr
Guido van Rossumb5699c71998-07-20 23:13:54 +000013import os
Guido van Rossum921c8241992-01-10 14:54:42 +000014
15
Guido van Rossuma558e371994-11-10 22:27:35 +000016# Interaction prompt line will separate file and call info from code
17# text using value of line_prefix string. A newline and arrow may
18# be to your liking. You can set it once pdb is imported using the
19# command "pdb.line_prefix = '\n% '".
20# line_prefix = ': ' # Use this to get the old situation back
21line_prefix = '\n-> ' # Probably a better default
22
Guido van Rossum23efba41992-01-27 16:58:47 +000023class Pdb(bdb.Bdb, cmd.Cmd):
Guido van Rossum921c8241992-01-10 14:54:42 +000024
Guido van Rossum5ef74b81993-06-23 11:55:24 +000025 def __init__(self):
26 bdb.Bdb.__init__(self)
27 cmd.Cmd.__init__(self)
Guido van Rossum921c8241992-01-10 14:54:42 +000028 self.prompt = '(Pdb) '
Guido van Rossumb5699c71998-07-20 23:13:54 +000029 # Try to load readline if it exists
30 try:
31 import readline
32 except ImportError:
33 pass
Guido van Rossum921c8241992-01-10 14:54:42 +000034
35 def reset(self):
Guido van Rossum23efba41992-01-27 16:58:47 +000036 bdb.Bdb.reset(self)
Guido van Rossum921c8241992-01-10 14:54:42 +000037 self.forget()
38
39 def forget(self):
Guido van Rossum921c8241992-01-10 14:54:42 +000040 self.lineno = None
Guido van Rossum6fe08b01992-01-16 13:50:21 +000041 self.stack = []
Guido van Rossum7ac1c811992-01-16 13:55:21 +000042 self.curindex = 0
43 self.curframe = None
Guido van Rossum23efba41992-01-27 16:58:47 +000044
45 def setup(self, f, t):
46 self.forget()
47 self.stack, self.curindex = self.get_stack(f, t)
Guido van Rossum7ac1c811992-01-16 13:55:21 +000048 self.curframe = self.stack[self.curindex][0]
Guido van Rossum921c8241992-01-10 14:54:42 +000049
Guido van Rossum23efba41992-01-27 16:58:47 +000050 # Override Bdb methods (except user_call, for now)
Guido van Rossumb9142571992-01-12 23:32:55 +000051
Guido van Rossum23efba41992-01-27 16:58:47 +000052 def user_line(self, frame):
53 # This function is called when we stop or break at this line
54 self.interaction(frame, None)
Guido van Rossum7ac1c811992-01-16 13:55:21 +000055
Guido van Rossum23efba41992-01-27 16:58:47 +000056 def user_return(self, frame, return_value):
57 # This function is called when a return trap is set here
58 frame.f_locals['__return__'] = return_value
59 print '--Return--'
60 self.interaction(frame, None)
Guido van Rossum921c8241992-01-10 14:54:42 +000061
Guido van Rossum23efba41992-01-27 16:58:47 +000062 def user_exception(self, frame, (exc_type, exc_value, exc_traceback)):
63 # This function is called if an exception occurs,
64 # but only if we are to stop at or just below this level
65 frame.f_locals['__exception__'] = exc_type, exc_value
Guido van Rossum5e38b6f1995-02-27 13:13:40 +000066 if type(exc_type) == type(''):
67 exc_type_name = exc_type
68 else: exc_type_name = exc_type.__name__
69 print exc_type_name + ':', repr.repr(exc_value)
Guido van Rossum23efba41992-01-27 16:58:47 +000070 self.interaction(frame, exc_traceback)
Guido van Rossum921c8241992-01-10 14:54:42 +000071
Guido van Rossum23efba41992-01-27 16:58:47 +000072 # General interaction function
Guido van Rossumb9142571992-01-12 23:32:55 +000073
Guido van Rossum23efba41992-01-27 16:58:47 +000074 def interaction(self, frame, traceback):
Guido van Rossum6fe08b01992-01-16 13:50:21 +000075 self.setup(frame, traceback)
Guido van Rossumb6aa92e1995-02-03 12:50:04 +000076 self.print_stack_entry(self.stack[self.curindex])
Guido van Rossum6fe08b01992-01-16 13:50:21 +000077 self.cmdloop()
Guido van Rossumb9142571992-01-12 23:32:55 +000078 self.forget()
Guido van Rossum23efba41992-01-27 16:58:47 +000079
Guido van Rossum921c8241992-01-10 14:54:42 +000080 def default(self, line):
Guido van Rossum23efba41992-01-27 16:58:47 +000081 if line[:1] == '!': line = line[1:]
82 locals = self.curframe.f_locals
83 globals = self.curframe.f_globals
84 try:
Guido van Rossumf17361d1996-07-30 16:28:13 +000085 code = compile(line + '\n', '<stdin>', 'single')
Guido van Rossumec8fd941995-08-07 20:16:05 +000086 exec code in globals, locals
Guido van Rossum23efba41992-01-27 16:58:47 +000087 except:
Guido van Rossumf15d1591997-09-29 23:22:12 +000088 t, v = sys.exc_info()[:2]
89 if type(t) == type(''):
90 exc_type_name = t
91 else: exc_type_name = t.__name__
92 print '***', exc_type_name + ':', v
Guido van Rossum23efba41992-01-27 16:58:47 +000093
94 # Command definitions, called by cmdloop()
95 # The argument is the remaining string on the command line
96 # Return true to exit from the command loop
Guido van Rossum921c8241992-01-10 14:54:42 +000097
Guido van Rossum23efba41992-01-27 16:58:47 +000098 do_h = cmd.Cmd.do_help
Guido van Rossumb6775db1994-08-01 11:34:53 +000099
Guido van Rossum921c8241992-01-10 14:54:42 +0000100 def do_break(self, arg):
Guido van Rossumb5699c71998-07-20 23:13:54 +0000101 # break [ ([filename:]lineno | function) [, "condition"] ]
Guido van Rossum921c8241992-01-10 14:54:42 +0000102 if not arg:
Guido van Rossum23efba41992-01-27 16:58:47 +0000103 print self.get_all_breaks() # XXX
Guido van Rossum921c8241992-01-10 14:54:42 +0000104 return
Guido van Rossumb5699c71998-07-20 23:13:54 +0000105 # parse arguments; comma has lowest precendence
106 # and cannot occur in filename
107 filename = None
108 lineno = None
109 cond = None
110 comma = string.find(arg, ',')
111 if comma > 0:
112 # parse stuff after comma: "condition"
113 cond = string.lstrip(arg[comma+1:])
114 arg = string.rstrip(arg[:comma])
Guido van Rossumb6775db1994-08-01 11:34:53 +0000115 try:
Guido van Rossumb5699c71998-07-20 23:13:54 +0000116 cond = eval(
117 cond,
118 self.curframe.f_globals,
119 self.curframe.f_locals)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000120 except:
Guido van Rossumb5699c71998-07-20 23:13:54 +0000121 print '*** Could not eval condition:', cond
Guido van Rossumb6775db1994-08-01 11:34:53 +0000122 return
Guido van Rossumb5699c71998-07-20 23:13:54 +0000123 # parse stuff before comma: [filename:]lineno | function
124 colon = string.rfind(arg, ':')
125 if colon >= 0:
126 filename = string.rstrip(arg[:colon])
127 filename = self.lookupmodule(filename)
128 arg = string.lstrip(arg[colon+1:])
129 try:
130 lineno = int(arg)
131 except ValueError, msg:
132 print '*** Bad lineno:', arg
133 return
134 else:
135 # no colon; can be lineno or function
136 try:
137 lineno = int(arg)
138 except ValueError:
139 try:
140 func = eval(arg,
141 self.curframe.f_globals,
142 self.curframe.f_locals)
143 except:
144 print '*** Could not eval argument:',
145 print arg
146 return
147 try:
148 if hasattr(func, 'im_func'):
149 func = func.im_func
150 code = func.func_code
151 except:
152 print '*** The specified object',
153 print 'is not a function', arg
154 return
155 lineno = code.co_firstlineno
156 if not filename:
157 filename = code.co_filename
158 # supply default filename if necessary
159 if not filename:
160 filename = self.curframe.f_code.co_filename
Guido van Rossumb6775db1994-08-01 11:34:53 +0000161 # now set the break point
Guido van Rossum9e1ee971997-07-11 13:43:53 +0000162 err = self.set_break(filename, lineno, cond)
Guido van Rossum23efba41992-01-27 16:58:47 +0000163 if err: print '***', err
Guido van Rossum9e1ee971997-07-11 13:43:53 +0000164
Guido van Rossum921c8241992-01-10 14:54:42 +0000165 do_b = do_break
166
167 def do_clear(self, arg):
168 if not arg:
Guido van Rossumb9142571992-01-12 23:32:55 +0000169 try:
170 reply = raw_input('Clear all breaks? ')
171 except EOFError:
172 reply = 'no'
173 reply = string.lower(string.strip(reply))
174 if reply in ('y', 'yes'):
Guido van Rossum23efba41992-01-27 16:58:47 +0000175 self.clear_all_breaks()
Guido van Rossum921c8241992-01-10 14:54:42 +0000176 return
Guido van Rossumb5699c71998-07-20 23:13:54 +0000177 filename = None
178 colon = string.rfind(arg, ':')
179 if colon >= 0:
180 filename = string.rstrip(arg[:colon])
181 filename = self.lookupmodule(filename)
182 arg = string.lstrip(arg[colon+1:])
Guido van Rossum921c8241992-01-10 14:54:42 +0000183 try:
Guido van Rossumb5699c71998-07-20 23:13:54 +0000184 lineno = int(arg)
Guido van Rossum921c8241992-01-10 14:54:42 +0000185 except:
Guido van Rossumb5699c71998-07-20 23:13:54 +0000186 print '*** Bad lineno:', `arg`
Guido van Rossum921c8241992-01-10 14:54:42 +0000187 return
Guido van Rossumb5699c71998-07-20 23:13:54 +0000188 if not filename:
189 filename = self.curframe.f_code.co_filename
Guido van Rossum89a78691992-12-14 12:57:56 +0000190 err = self.clear_break(filename, lineno)
Guido van Rossum23efba41992-01-27 16:58:47 +0000191 if err: print '***', err
Guido van Rossumb9142571992-01-12 23:32:55 +0000192 do_cl = do_clear # 'c' is already an abbreviation for 'continue'
Guido van Rossum921c8241992-01-10 14:54:42 +0000193
194 def do_where(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000195 self.print_stack_trace()
Guido van Rossum921c8241992-01-10 14:54:42 +0000196 do_w = do_where
197
198 def do_up(self, arg):
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000199 if self.curindex == 0:
200 print '*** Oldest frame'
Guido van Rossum921c8241992-01-10 14:54:42 +0000201 else:
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000202 self.curindex = self.curindex - 1
203 self.curframe = self.stack[self.curindex][0]
Guido van Rossum23efba41992-01-27 16:58:47 +0000204 self.print_stack_entry(self.stack[self.curindex])
Guido van Rossumc629d341992-11-05 10:43:02 +0000205 self.lineno = None
Guido van Rossumb9142571992-01-12 23:32:55 +0000206 do_u = do_up
207
208 def do_down(self, arg):
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000209 if self.curindex + 1 == len(self.stack):
210 print '*** Newest frame'
Guido van Rossumb9142571992-01-12 23:32:55 +0000211 else:
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000212 self.curindex = self.curindex + 1
213 self.curframe = self.stack[self.curindex][0]
Guido van Rossum23efba41992-01-27 16:58:47 +0000214 self.print_stack_entry(self.stack[self.curindex])
Guido van Rossumc629d341992-11-05 10:43:02 +0000215 self.lineno = None
Guido van Rossum921c8241992-01-10 14:54:42 +0000216 do_d = do_down
217
218 def do_step(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000219 self.set_step()
Guido van Rossumb9142571992-01-12 23:32:55 +0000220 return 1
Guido van Rossum921c8241992-01-10 14:54:42 +0000221 do_s = do_step
222
223 def do_next(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000224 self.set_next(self.curframe)
Guido van Rossumb9142571992-01-12 23:32:55 +0000225 return 1
Guido van Rossum921c8241992-01-10 14:54:42 +0000226 do_n = do_next
227
Guido van Rossumb9142571992-01-12 23:32:55 +0000228 def do_return(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000229 self.set_return(self.curframe)
Guido van Rossumb9142571992-01-12 23:32:55 +0000230 return 1
231 do_r = do_return
232
Guido van Rossum921c8241992-01-10 14:54:42 +0000233 def do_continue(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000234 self.set_continue()
Guido van Rossumb9142571992-01-12 23:32:55 +0000235 return 1
Guido van Rossum921c8241992-01-10 14:54:42 +0000236 do_c = do_cont = do_continue
237
238 def do_quit(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000239 self.set_quit()
240 return 1
Guido van Rossum921c8241992-01-10 14:54:42 +0000241 do_q = do_quit
242
Guido van Rossum23efba41992-01-27 16:58:47 +0000243 def do_args(self, arg):
Guido van Rossum46c86bb1998-02-25 20:50:32 +0000244 f = self.curframe
245 co = f.f_code
246 dict = f.f_locals
247 n = co.co_argcount
248 if co.co_flags & 4: n = n+1
249 if co.co_flags & 8: n = n+1
250 for i in range(n):
251 name = co.co_varnames[i]
252 print name, '=',
253 if dict.has_key(name): print dict[name]
254 else: print "*** undefined ***"
Guido van Rossum23efba41992-01-27 16:58:47 +0000255 do_a = do_args
256
257 def do_retval(self, arg):
258 if self.curframe.f_locals.has_key('__return__'):
259 print self.curframe.f_locals['__return__']
260 else:
261 print '*** Not yet returned!'
262 do_rv = do_retval
263
264 def do_p(self, arg):
265 try:
266 value = eval(arg, self.curframe.f_globals, \
267 self.curframe.f_locals)
268 except:
Guido van Rossumf15d1591997-09-29 23:22:12 +0000269 t, v = sys.exc_info()[:2]
270 if type(t) == type(''):
271 exc_type_name = t
272 else: exc_type_name = t.__name__
273 print '***', exc_type_name + ':', `v`
Guido van Rossum23efba41992-01-27 16:58:47 +0000274 return
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000275
Guido van Rossum23efba41992-01-27 16:58:47 +0000276 print `value`
277
Guido van Rossum921c8241992-01-10 14:54:42 +0000278 def do_list(self, arg):
Guido van Rossumb9142571992-01-12 23:32:55 +0000279 self.lastcmd = 'list'
Guido van Rossum921c8241992-01-10 14:54:42 +0000280 last = None
281 if arg:
282 try:
283 x = eval(arg, {}, {})
284 if type(x) == type(()):
285 first, last = x
286 first = int(first)
287 last = int(last)
288 if last < first:
289 # Assume it's a count
290 last = first + last
291 else:
Guido van Rossumc629d341992-11-05 10:43:02 +0000292 first = max(1, int(x) - 5)
Guido van Rossum921c8241992-01-10 14:54:42 +0000293 except:
294 print '*** Error in argument:', `arg`
295 return
296 elif self.lineno is None:
297 first = max(1, self.curframe.f_lineno - 5)
298 else:
299 first = self.lineno + 1
Guido van Rossumc629d341992-11-05 10:43:02 +0000300 if last == None:
Guido van Rossum921c8241992-01-10 14:54:42 +0000301 last = first + 10
302 filename = self.curframe.f_code.co_filename
Guido van Rossum23efba41992-01-27 16:58:47 +0000303 breaklist = self.get_file_breaks(filename)
Guido van Rossum921c8241992-01-10 14:54:42 +0000304 try:
305 for lineno in range(first, last+1):
306 line = linecache.getline(filename, lineno)
307 if not line:
308 print '[EOF]'
309 break
310 else:
311 s = string.rjust(`lineno`, 3)
312 if len(s) < 4: s = s + ' '
313 if lineno in breaklist: s = s + 'B'
314 else: s = s + ' '
315 if lineno == self.curframe.f_lineno:
316 s = s + '->'
317 print s + '\t' + line,
318 self.lineno = lineno
319 except KeyboardInterrupt:
320 pass
321 do_l = do_list
Guido van Rossum00230781993-03-29 11:39:45 +0000322
323 def do_whatis(self, arg):
Guido van Rossum00230781993-03-29 11:39:45 +0000324 try:
325 value = eval(arg, self.curframe.f_globals, \
326 self.curframe.f_locals)
327 except:
Guido van Rossumf15d1591997-09-29 23:22:12 +0000328 t, v = sys.exc_info()[:2]
329 if type(t) == type(''):
330 exc_type_name = t
331 else: exc_type_name = t.__name__
332 print '***', exc_type_name + ':', `v`
Guido van Rossum00230781993-03-29 11:39:45 +0000333 return
334 code = None
335 # Is it a function?
336 try: code = value.func_code
337 except: pass
338 if code:
Guido van Rossumb6775db1994-08-01 11:34:53 +0000339 print 'Function', code.co_name
Guido van Rossum00230781993-03-29 11:39:45 +0000340 return
341 # Is it an instance method?
342 try: code = value.im_func.func_code
343 except: pass
344 if code:
Guido van Rossumb6775db1994-08-01 11:34:53 +0000345 print 'Method', code.co_name
Guido van Rossum00230781993-03-29 11:39:45 +0000346 return
347 # None of the above...
348 print type(value)
Guido van Rossumb9142571992-01-12 23:32:55 +0000349
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000350 # Print a traceback starting at the top stack frame.
Guido van Rossum23efba41992-01-27 16:58:47 +0000351 # The most recently entered frame is printed last;
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000352 # this is different from dbx and gdb, but consistent with
353 # the Python interpreter's stack trace.
354 # It is also consistent with the up/down commands (which are
355 # compatible with dbx and gdb: up moves towards 'main()'
356 # and down moves towards the most recent stack frame).
Guido van Rossum921c8241992-01-10 14:54:42 +0000357
Guido van Rossum23efba41992-01-27 16:58:47 +0000358 def print_stack_trace(self):
359 try:
360 for frame_lineno in self.stack:
361 self.print_stack_entry(frame_lineno)
362 except KeyboardInterrupt:
363 pass
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000364
Guido van Rossumb6aa92e1995-02-03 12:50:04 +0000365 def print_stack_entry(self, frame_lineno, prompt_prefix=line_prefix):
Guido van Rossum23efba41992-01-27 16:58:47 +0000366 frame, lineno = frame_lineno
367 if frame is self.curframe:
368 print '>',
369 else:
370 print ' ',
Guido van Rossuma558e371994-11-10 22:27:35 +0000371 print self.format_stack_entry(frame_lineno, prompt_prefix)
Guido van Rossum921c8241992-01-10 14:54:42 +0000372
373
Guido van Rossumb6775db1994-08-01 11:34:53 +0000374 # Help methods (derived from pdb.doc)
375
376 def help_help(self):
377 self.help_h()
378
379 def help_h(self):
380 print """h(elp)
381 Without argument, print the list of available commands.
382 With a command name as argument, print help about that command
383 "help pdb" pipes the full documentation file to the $PAGER
384 "help exec" gives help on the ! command"""
385
386 def help_where(self):
387 self.help_w()
388
389 def help_w(self):
390 print """w(here)
391 Print a stack trace, with the most recent frame at the bottom.
392 An arrow indicates the "current frame", which determines the
393 context of most commands."""
394
395 def help_down(self):
396 self.help_d()
397
398 def help_d(self):
399 print """d(own)
400 Move the current frame one level down in the stack trace
401 (to an older frame)."""
402
403 def help_up(self):
404 self.help_u()
405
406 def help_u(self):
407 print """u(p)
408 Move the current frame one level up in the stack trace
409 (to a newer frame)."""
410
411 def help_break(self):
412 self.help_b()
413
414 def help_b(self):
Guido van Rossumb5699c71998-07-20 23:13:54 +0000415 print """b(reak) ([file:]lineno | function) [, "condition"]
Guido van Rossumb6775db1994-08-01 11:34:53 +0000416 With a line number argument, set a break there in the current
417 file. With a function name, set a break at the entry of that
Guido van Rossum9e1ee971997-07-11 13:43:53 +0000418 function. Without argument, list all breaks. If a second
419 argument is present, it is a string specifying an expression
420 which must evaluate to true before the breakpoint is honored.
Guido van Rossumb5699c71998-07-20 23:13:54 +0000421
422 The line number may be prefixed with a filename and a colon,
423 to specify a breakpoint in another file (probably one that
424 hasn't been loaded yet). The file is searched on sys.path."""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000425
426 def help_clear(self):
427 self.help_cl()
428
429 def help_cl(self):
430 print """cl(ear) [lineno]
431 With a line number argument, clear that break in the current file.
Guido van Rossumb5699c71998-07-20 23:13:54 +0000432 Without argument, clear all breaks (but first ask confirmation).
433
434 The line number may be prefixed with a filename and a colon,
435 to specify a breakpoint in another file (probably one that
436 hasn't been loaded yet). The file is searched on sys.path."""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000437
438 def help_step(self):
439 self.help_s()
440
441 def help_s(self):
442 print """s(tep)
443 Execute the current line, stop at the first possible occasion
444 (either in a function that is called or in the current function)."""
445
446 def help_next(self):
447 self.help_n()
448
449 def help_n(self):
450 print """n(ext)
451 Continue execution until the next line in the current function
452 is reached or it returns."""
453
454 def help_return(self):
455 self.help_r()
456
457 def help_r(self):
458 print """r(eturn)
459 Continue execution until the current function returns."""
460
461 def help_continue(self):
462 self.help_c()
463
464 def help_cont(self):
465 self.help_c()
466
467 def help_c(self):
468 print """c(ont(inue))
469 Continue execution, only stop when a breakpoint is encountered."""
470
471 def help_list(self):
472 self.help_l()
473
474 def help_l(self):
475 print """l(ist) [first [,last]]
476 List source code for the current file.
477 Without arguments, list 11 lines around the current line
478 or continue the previous listing.
479 With one argument, list 11 lines starting at that line.
480 With two arguments, list the given range;
481 if the second argument is less than the first, it is a count."""
482
483 def help_args(self):
484 self.help_a()
485
486 def help_a(self):
487 print """a(rgs)
Guido van Rossum46c86bb1998-02-25 20:50:32 +0000488 Print the arguments of the current function."""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000489
490 def help_p(self):
491 print """p expression
492 Print the value of the expression."""
493
494 def help_exec(self):
495 print """(!) statement
496 Execute the (one-line) statement in the context of
497 the current stack frame.
498 The exclamation point can be omitted unless the first word
499 of the statement resembles a debugger command.
500 To assign to a global variable you must always prefix the
501 command with a 'global' command, e.g.:
502 (Pdb) global list_options; list_options = ['-l']
503 (Pdb)"""
504
505 def help_quit(self):
506 self.help_q()
507
508 def help_q(self):
509 print """q(uit) Quit from the debugger.
510 The program being executed is aborted."""
511
512 def help_pdb(self):
513 help()
514
Guido van Rossumb5699c71998-07-20 23:13:54 +0000515 # Helper function for break/clear parsing -- may be overridden
516
517 def lookupmodule(self, filename):
518 if filename == mainmodule:
519 return mainpyfile
520 for dirname in sys.path:
521 fullname = os.path.join(dirname, filename)
522 if os.path.exists(fullname):
523 return fullname
524 print 'Warning:', `filename`, 'not found from sys.path'
525 return filename
526
527
Guido van Rossum35771131992-09-08 11:59:04 +0000528# Simplified interface
529
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000530def run(statement, globals=None, locals=None):
531 Pdb().run(statement, globals, locals)
532
533def runeval(expression, globals=None, locals=None):
534 return Pdb().runeval(expression, globals, locals)
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000535
536def runctx(statement, globals, locals):
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000537 # B/W compatibility
538 run(statement, globals, locals)
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000539
Guido van Rossum4e160981992-09-02 20:43:20 +0000540def runcall(*args):
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000541 return apply(Pdb().runcall, args)
Guido van Rossum4e160981992-09-02 20:43:20 +0000542
Guido van Rossumb6775db1994-08-01 11:34:53 +0000543def set_trace():
544 Pdb().set_trace()
Guido van Rossum35771131992-09-08 11:59:04 +0000545
546# Post-Mortem interface
547
548def post_mortem(t):
Guido van Rossum5ef74b81993-06-23 11:55:24 +0000549 p = Pdb()
Guido van Rossum35771131992-09-08 11:59:04 +0000550 p.reset()
551 while t.tb_next <> None: t = t.tb_next
552 p.interaction(t.tb_frame, t)
553
554def pm():
Guido van Rossum35771131992-09-08 11:59:04 +0000555 post_mortem(sys.last_traceback)
556
557
558# Main program for testing
559
Guido van Rossum23efba41992-01-27 16:58:47 +0000560TESTCMD = 'import x; x.main()'
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000561
Guido van Rossum921c8241992-01-10 14:54:42 +0000562def test():
Guido van Rossum23efba41992-01-27 16:58:47 +0000563 run(TESTCMD)
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000564
565# print help
566def help():
567 for dirname in sys.path:
568 fullname = os.path.join(dirname, 'pdb.doc')
569 if os.path.exists(fullname):
570 sts = os.system('${PAGER-more} '+fullname)
571 if sts: print '*** Pager exit status:', sts
572 break
573 else:
574 print 'Sorry, can\'t find the help file "pdb.doc"',
575 print 'along the Python search path'
Guido van Rossumf17361d1996-07-30 16:28:13 +0000576
Guido van Rossumb5699c71998-07-20 23:13:54 +0000577mainmodule = ''
578mainpyfile = ''
579
Guido van Rossumf17361d1996-07-30 16:28:13 +0000580# When invoked as main program, invoke the debugger on a script
581if __name__=='__main__':
Guido van Rossumb5699c71998-07-20 23:13:54 +0000582 global mainmodule, mainpyfile
Guido van Rossumf17361d1996-07-30 16:28:13 +0000583 if not sys.argv[1:]:
584 print "usage: pdb.py scriptfile [arg] ..."
585 sys.exit(2)
586
Guido van Rossumb5699c71998-07-20 23:13:54 +0000587 mainpyfile = filename = sys.argv[1] # Get script filename
588 if not os.path.exists(filename):
589 print 'Error:', `filename`, 'does not exist'
590 sys.exit(1)
591 mainmodule = os.path.basename(filename)
Guido van Rossumec577d51996-09-10 17:39:34 +0000592 del sys.argv[0] # Hide "pdb.py" from argument list
593
594 # Insert script directory in front of module search path
595 sys.path.insert(0, os.path.dirname(filename))
Guido van Rossumf17361d1996-07-30 16:28:13 +0000596
597 run('execfile(' + `filename` + ')', {'__name__': '__main__'})