blob: ec6a2abf432e7be9bd5acced966b361597d229d0 [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 Rossum921c8241992-01-10 14:54:42 +000013
14
Guido van Rossuma558e371994-11-10 22:27:35 +000015# Interaction prompt line will separate file and call info from code
16# text using value of line_prefix string. A newline and arrow may
17# be to your liking. You can set it once pdb is imported using the
18# command "pdb.line_prefix = '\n% '".
19# line_prefix = ': ' # Use this to get the old situation back
20line_prefix = '\n-> ' # Probably a better default
21
Guido van Rossum23efba41992-01-27 16:58:47 +000022class Pdb(bdb.Bdb, cmd.Cmd):
Guido van Rossum921c8241992-01-10 14:54:42 +000023
Guido van Rossum5ef74b81993-06-23 11:55:24 +000024 def __init__(self):
25 bdb.Bdb.__init__(self)
26 cmd.Cmd.__init__(self)
Guido van Rossum921c8241992-01-10 14:54:42 +000027 self.prompt = '(Pdb) '
Guido van Rossum921c8241992-01-10 14:54:42 +000028
29 def reset(self):
Guido van Rossum23efba41992-01-27 16:58:47 +000030 bdb.Bdb.reset(self)
Guido van Rossum921c8241992-01-10 14:54:42 +000031 self.forget()
32
33 def forget(self):
Guido van Rossum921c8241992-01-10 14:54:42 +000034 self.lineno = None
Guido van Rossum6fe08b01992-01-16 13:50:21 +000035 self.stack = []
Guido van Rossum7ac1c811992-01-16 13:55:21 +000036 self.curindex = 0
37 self.curframe = None
Guido van Rossum23efba41992-01-27 16:58:47 +000038
39 def setup(self, f, t):
40 self.forget()
41 self.stack, self.curindex = self.get_stack(f, t)
Guido van Rossum7ac1c811992-01-16 13:55:21 +000042 self.curframe = self.stack[self.curindex][0]
Guido van Rossum921c8241992-01-10 14:54:42 +000043
Guido van Rossum23efba41992-01-27 16:58:47 +000044 # Override Bdb methods (except user_call, for now)
Guido van Rossumb9142571992-01-12 23:32:55 +000045
Guido van Rossum23efba41992-01-27 16:58:47 +000046 def user_line(self, frame):
47 # This function is called when we stop or break at this line
48 self.interaction(frame, None)
Guido van Rossum7ac1c811992-01-16 13:55:21 +000049
Guido van Rossum23efba41992-01-27 16:58:47 +000050 def user_return(self, frame, return_value):
51 # This function is called when a return trap is set here
52 frame.f_locals['__return__'] = return_value
53 print '--Return--'
54 self.interaction(frame, None)
Guido van Rossum921c8241992-01-10 14:54:42 +000055
Guido van Rossum23efba41992-01-27 16:58:47 +000056 def user_exception(self, frame, (exc_type, exc_value, exc_traceback)):
57 # This function is called if an exception occurs,
58 # but only if we are to stop at or just below this level
59 frame.f_locals['__exception__'] = exc_type, exc_value
Guido van Rossum5e38b6f1995-02-27 13:13:40 +000060 if type(exc_type) == type(''):
61 exc_type_name = exc_type
62 else: exc_type_name = exc_type.__name__
63 print exc_type_name + ':', repr.repr(exc_value)
Guido van Rossum23efba41992-01-27 16:58:47 +000064 self.interaction(frame, exc_traceback)
Guido van Rossum921c8241992-01-10 14:54:42 +000065
Guido van Rossum23efba41992-01-27 16:58:47 +000066 # General interaction function
Guido van Rossumb9142571992-01-12 23:32:55 +000067
Guido van Rossum23efba41992-01-27 16:58:47 +000068 def interaction(self, frame, traceback):
Guido van Rossum6fe08b01992-01-16 13:50:21 +000069 self.setup(frame, traceback)
Guido van Rossumb6aa92e1995-02-03 12:50:04 +000070 self.print_stack_entry(self.stack[self.curindex])
Guido van Rossum6fe08b01992-01-16 13:50:21 +000071 self.cmdloop()
Guido van Rossumb9142571992-01-12 23:32:55 +000072 self.forget()
Guido van Rossum23efba41992-01-27 16:58:47 +000073
Guido van Rossum921c8241992-01-10 14:54:42 +000074 def default(self, line):
Guido van Rossum23efba41992-01-27 16:58:47 +000075 if line[:1] == '!': line = line[1:]
76 locals = self.curframe.f_locals
77 globals = self.curframe.f_globals
Guido van Rossum8e2ec561993-07-29 09:37:38 +000078 globals['__privileged__'] = 1
Guido van Rossum23efba41992-01-27 16:58:47 +000079 try:
Guido van Rossumf17361d1996-07-30 16:28:13 +000080 code = compile(line + '\n', '<stdin>', 'single')
Guido van Rossumec8fd941995-08-07 20:16:05 +000081 exec code in globals, locals
Guido van Rossum23efba41992-01-27 16:58:47 +000082 except:
Guido van Rossum5e38b6f1995-02-27 13:13:40 +000083 if type(sys.exc_type) == type(''):
84 exc_type_name = sys.exc_type
85 else: exc_type_name = sys.exc_type.__name__
86 print '***', exc_type_name + ':', sys.exc_value
Guido van Rossum23efba41992-01-27 16:58:47 +000087
88 # Command definitions, called by cmdloop()
89 # The argument is the remaining string on the command line
90 # Return true to exit from the command loop
Guido van Rossum921c8241992-01-10 14:54:42 +000091
Guido van Rossum23efba41992-01-27 16:58:47 +000092 do_h = cmd.Cmd.do_help
Guido van Rossumb6775db1994-08-01 11:34:53 +000093
Guido van Rossum921c8241992-01-10 14:54:42 +000094 def do_break(self, arg):
95 if not arg:
Guido van Rossum23efba41992-01-27 16:58:47 +000096 print self.get_all_breaks() # XXX
Guido van Rossum921c8241992-01-10 14:54:42 +000097 return
Guido van Rossumb6775db1994-08-01 11:34:53 +000098 # Try line number as argument
Guido van Rossum9e1ee971997-07-11 13:43:53 +000099 try:
100 arg = eval(arg, self.curframe.f_globals,
101 self.curframe.f_locals)
102 except:
103 print '*** Could not eval argument:', arg
104 return
105
106 # Check for condition
107 try: arg, cond = arg
108 except: arg, cond = arg, None
109
Guido van Rossumb6775db1994-08-01 11:34:53 +0000110 try:
Guido van Rossum9e1ee971997-07-11 13:43:53 +0000111 lineno = int(arg)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000112 filename = self.curframe.f_code.co_filename
Guido van Rossum921c8241992-01-10 14:54:42 +0000113 except:
Guido van Rossumb6775db1994-08-01 11:34:53 +0000114 # Try function name as the argument
115 import codehack
116 try:
Guido van Rossum9e1ee971997-07-11 13:43:53 +0000117 func = arg
Guido van Rossumb6775db1994-08-01 11:34:53 +0000118 if hasattr(func, 'im_func'):
119 func = func.im_func
120 code = func.func_code
121 except:
Guido van Rossum9e1ee971997-07-11 13:43:53 +0000122 print '*** The specified object',
123 print 'is not a function', arg
Guido van Rossumb6775db1994-08-01 11:34:53 +0000124 return
125 lineno = codehack.getlineno(code)
126 filename = code.co_filename
127
128 # now set the break point
Guido van Rossum9e1ee971997-07-11 13:43:53 +0000129 err = self.set_break(filename, lineno, cond)
Guido van Rossum23efba41992-01-27 16:58:47 +0000130 if err: print '***', err
Guido van Rossum9e1ee971997-07-11 13:43:53 +0000131
Guido van Rossum921c8241992-01-10 14:54:42 +0000132 do_b = do_break
133
134 def do_clear(self, arg):
135 if not arg:
Guido van Rossumb9142571992-01-12 23:32:55 +0000136 try:
137 reply = raw_input('Clear all breaks? ')
138 except EOFError:
139 reply = 'no'
140 reply = string.lower(string.strip(reply))
141 if reply in ('y', 'yes'):
Guido van Rossum23efba41992-01-27 16:58:47 +0000142 self.clear_all_breaks()
Guido van Rossum921c8241992-01-10 14:54:42 +0000143 return
144 try:
145 lineno = int(eval(arg))
146 except:
147 print '*** Error in argument:', `arg`
148 return
149 filename = self.curframe.f_code.co_filename
Guido van Rossum89a78691992-12-14 12:57:56 +0000150 err = self.clear_break(filename, lineno)
Guido van Rossum23efba41992-01-27 16:58:47 +0000151 if err: print '***', err
Guido van Rossumb9142571992-01-12 23:32:55 +0000152 do_cl = do_clear # 'c' is already an abbreviation for 'continue'
Guido van Rossum921c8241992-01-10 14:54:42 +0000153
154 def do_where(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000155 self.print_stack_trace()
Guido van Rossum921c8241992-01-10 14:54:42 +0000156 do_w = do_where
157
158 def do_up(self, arg):
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000159 if self.curindex == 0:
160 print '*** Oldest frame'
Guido van Rossum921c8241992-01-10 14:54:42 +0000161 else:
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000162 self.curindex = self.curindex - 1
163 self.curframe = self.stack[self.curindex][0]
Guido van Rossum23efba41992-01-27 16:58:47 +0000164 self.print_stack_entry(self.stack[self.curindex])
Guido van Rossumc629d341992-11-05 10:43:02 +0000165 self.lineno = None
Guido van Rossumb9142571992-01-12 23:32:55 +0000166 do_u = do_up
167
168 def do_down(self, arg):
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000169 if self.curindex + 1 == len(self.stack):
170 print '*** Newest frame'
Guido van Rossumb9142571992-01-12 23:32:55 +0000171 else:
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000172 self.curindex = self.curindex + 1
173 self.curframe = self.stack[self.curindex][0]
Guido van Rossum23efba41992-01-27 16:58:47 +0000174 self.print_stack_entry(self.stack[self.curindex])
Guido van Rossumc629d341992-11-05 10:43:02 +0000175 self.lineno = None
Guido van Rossum921c8241992-01-10 14:54:42 +0000176 do_d = do_down
177
178 def do_step(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000179 self.set_step()
Guido van Rossumb9142571992-01-12 23:32:55 +0000180 return 1
Guido van Rossum921c8241992-01-10 14:54:42 +0000181 do_s = do_step
182
183 def do_next(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000184 self.set_next(self.curframe)
Guido van Rossumb9142571992-01-12 23:32:55 +0000185 return 1
Guido van Rossum921c8241992-01-10 14:54:42 +0000186 do_n = do_next
187
Guido van Rossumb9142571992-01-12 23:32:55 +0000188 def do_return(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000189 self.set_return(self.curframe)
Guido van Rossumb9142571992-01-12 23:32:55 +0000190 return 1
191 do_r = do_return
192
Guido van Rossum921c8241992-01-10 14:54:42 +0000193 def do_continue(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000194 self.set_continue()
Guido van Rossumb9142571992-01-12 23:32:55 +0000195 return 1
Guido van Rossum921c8241992-01-10 14:54:42 +0000196 do_c = do_cont = do_continue
197
198 def do_quit(self, arg):
Guido van Rossum23efba41992-01-27 16:58:47 +0000199 self.set_quit()
200 return 1
Guido van Rossum921c8241992-01-10 14:54:42 +0000201 do_q = do_quit
202
Guido van Rossum23efba41992-01-27 16:58:47 +0000203 def do_args(self, arg):
Guido van Rossumb6775db1994-08-01 11:34:53 +0000204 if self.curframe.f_locals.has_key('__args__'):
205 print `self.curframe.f_locals['__args__']`
Guido van Rossum23efba41992-01-27 16:58:47 +0000206 else:
Guido van Rossumb6775db1994-08-01 11:34:53 +0000207 print '*** No arguments?!'
Guido van Rossum23efba41992-01-27 16:58:47 +0000208 do_a = do_args
209
210 def do_retval(self, arg):
211 if self.curframe.f_locals.has_key('__return__'):
212 print self.curframe.f_locals['__return__']
213 else:
214 print '*** Not yet returned!'
215 do_rv = do_retval
216
217 def do_p(self, arg):
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000218 self.curframe.f_globals['__privileged__'] = 1
Guido van Rossum23efba41992-01-27 16:58:47 +0000219 try:
220 value = eval(arg, self.curframe.f_globals, \
221 self.curframe.f_locals)
222 except:
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000223 if type(sys.exc_type) == type(''):
224 exc_type_name = sys.exc_type
225 else: exc_type_name = sys.exc_type.__name__
226 print '***', exc_type_name + ':', `sys.exc_value`
Guido van Rossum23efba41992-01-27 16:58:47 +0000227 return
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000228
Guido van Rossum23efba41992-01-27 16:58:47 +0000229 print `value`
230
Guido van Rossum921c8241992-01-10 14:54:42 +0000231 def do_list(self, arg):
Guido van Rossumb9142571992-01-12 23:32:55 +0000232 self.lastcmd = 'list'
Guido van Rossum921c8241992-01-10 14:54:42 +0000233 last = None
234 if arg:
235 try:
236 x = eval(arg, {}, {})
237 if type(x) == type(()):
238 first, last = x
239 first = int(first)
240 last = int(last)
241 if last < first:
242 # Assume it's a count
243 last = first + last
244 else:
Guido van Rossumc629d341992-11-05 10:43:02 +0000245 first = max(1, int(x) - 5)
Guido van Rossum921c8241992-01-10 14:54:42 +0000246 except:
247 print '*** Error in argument:', `arg`
248 return
249 elif self.lineno is None:
250 first = max(1, self.curframe.f_lineno - 5)
251 else:
252 first = self.lineno + 1
Guido van Rossumc629d341992-11-05 10:43:02 +0000253 if last == None:
Guido van Rossum921c8241992-01-10 14:54:42 +0000254 last = first + 10
255 filename = self.curframe.f_code.co_filename
Guido van Rossum23efba41992-01-27 16:58:47 +0000256 breaklist = self.get_file_breaks(filename)
Guido van Rossum921c8241992-01-10 14:54:42 +0000257 try:
258 for lineno in range(first, last+1):
259 line = linecache.getline(filename, lineno)
260 if not line:
261 print '[EOF]'
262 break
263 else:
264 s = string.rjust(`lineno`, 3)
265 if len(s) < 4: s = s + ' '
266 if lineno in breaklist: s = s + 'B'
267 else: s = s + ' '
268 if lineno == self.curframe.f_lineno:
269 s = s + '->'
270 print s + '\t' + line,
271 self.lineno = lineno
272 except KeyboardInterrupt:
273 pass
274 do_l = do_list
Guido van Rossum00230781993-03-29 11:39:45 +0000275
276 def do_whatis(self, arg):
Guido van Rossum00230781993-03-29 11:39:45 +0000277 try:
278 value = eval(arg, self.curframe.f_globals, \
279 self.curframe.f_locals)
280 except:
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000281 if type(sys.exc_type) == type(''):
282 exc_type_name = sys.exc_type
283 else: exc_type_name = sys.exc_type.__name__
284 print '***', exc_type_name + ':', `sys.exc_value`
Guido van Rossum00230781993-03-29 11:39:45 +0000285 return
286 code = None
287 # Is it a function?
288 try: code = value.func_code
289 except: pass
290 if code:
Guido van Rossumb6775db1994-08-01 11:34:53 +0000291 print 'Function', code.co_name
Guido van Rossum00230781993-03-29 11:39:45 +0000292 return
293 # Is it an instance method?
294 try: code = value.im_func.func_code
295 except: pass
296 if code:
Guido van Rossumb6775db1994-08-01 11:34:53 +0000297 print 'Method', code.co_name
Guido van Rossum00230781993-03-29 11:39:45 +0000298 return
299 # None of the above...
300 print type(value)
Guido van Rossumb9142571992-01-12 23:32:55 +0000301
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000302 # Print a traceback starting at the top stack frame.
Guido van Rossum23efba41992-01-27 16:58:47 +0000303 # The most recently entered frame is printed last;
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000304 # this is different from dbx and gdb, but consistent with
305 # the Python interpreter's stack trace.
306 # It is also consistent with the up/down commands (which are
307 # compatible with dbx and gdb: up moves towards 'main()'
308 # and down moves towards the most recent stack frame).
Guido van Rossum921c8241992-01-10 14:54:42 +0000309
Guido van Rossum23efba41992-01-27 16:58:47 +0000310 def print_stack_trace(self):
311 try:
312 for frame_lineno in self.stack:
313 self.print_stack_entry(frame_lineno)
314 except KeyboardInterrupt:
315 pass
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000316
Guido van Rossumb6aa92e1995-02-03 12:50:04 +0000317 def print_stack_entry(self, frame_lineno, prompt_prefix=line_prefix):
Guido van Rossum23efba41992-01-27 16:58:47 +0000318 frame, lineno = frame_lineno
319 if frame is self.curframe:
320 print '>',
321 else:
322 print ' ',
Guido van Rossuma558e371994-11-10 22:27:35 +0000323 print self.format_stack_entry(frame_lineno, prompt_prefix)
Guido van Rossum921c8241992-01-10 14:54:42 +0000324
325
Guido van Rossumb6775db1994-08-01 11:34:53 +0000326 # Help methods (derived from pdb.doc)
327
328 def help_help(self):
329 self.help_h()
330
331 def help_h(self):
332 print """h(elp)
333 Without argument, print the list of available commands.
334 With a command name as argument, print help about that command
335 "help pdb" pipes the full documentation file to the $PAGER
336 "help exec" gives help on the ! command"""
337
338 def help_where(self):
339 self.help_w()
340
341 def help_w(self):
342 print """w(here)
343 Print a stack trace, with the most recent frame at the bottom.
344 An arrow indicates the "current frame", which determines the
345 context of most commands."""
346
347 def help_down(self):
348 self.help_d()
349
350 def help_d(self):
351 print """d(own)
352 Move the current frame one level down in the stack trace
353 (to an older frame)."""
354
355 def help_up(self):
356 self.help_u()
357
358 def help_u(self):
359 print """u(p)
360 Move the current frame one level up in the stack trace
361 (to a newer frame)."""
362
363 def help_break(self):
364 self.help_b()
365
366 def help_b(self):
Guido van Rossum9e1ee971997-07-11 13:43:53 +0000367 print """b(reak) [lineno | function] [, "condition"]
Guido van Rossumb6775db1994-08-01 11:34:53 +0000368 With a line number argument, set a break there in the current
369 file. With a function name, set a break at the entry of that
Guido van Rossum9e1ee971997-07-11 13:43:53 +0000370 function. Without argument, list all breaks. If a second
371 argument is present, it is a string specifying an expression
372 which must evaluate to true before the breakpoint is honored.
373 """
Guido van Rossumb6775db1994-08-01 11:34:53 +0000374
375 def help_clear(self):
376 self.help_cl()
377
378 def help_cl(self):
379 print """cl(ear) [lineno]
380 With a line number argument, clear that break in the current file.
381 Without argument, clear all breaks (but first ask confirmation)."""
382
383 def help_step(self):
384 self.help_s()
385
386 def help_s(self):
387 print """s(tep)
388 Execute the current line, stop at the first possible occasion
389 (either in a function that is called or in the current function)."""
390
391 def help_next(self):
392 self.help_n()
393
394 def help_n(self):
395 print """n(ext)
396 Continue execution until the next line in the current function
397 is reached or it returns."""
398
399 def help_return(self):
400 self.help_r()
401
402 def help_r(self):
403 print """r(eturn)
404 Continue execution until the current function returns."""
405
406 def help_continue(self):
407 self.help_c()
408
409 def help_cont(self):
410 self.help_c()
411
412 def help_c(self):
413 print """c(ont(inue))
414 Continue execution, only stop when a breakpoint is encountered."""
415
416 def help_list(self):
417 self.help_l()
418
419 def help_l(self):
420 print """l(ist) [first [,last]]
421 List source code for the current file.
422 Without arguments, list 11 lines around the current line
423 or continue the previous listing.
424 With one argument, list 11 lines starting at that line.
425 With two arguments, list the given range;
426 if the second argument is less than the first, it is a count."""
427
428 def help_args(self):
429 self.help_a()
430
431 def help_a(self):
432 print """a(rgs)
433 Print the argument list of the current function."""
434
435 def help_p(self):
436 print """p expression
437 Print the value of the expression."""
438
439 def help_exec(self):
440 print """(!) statement
441 Execute the (one-line) statement in the context of
442 the current stack frame.
443 The exclamation point can be omitted unless the first word
444 of the statement resembles a debugger command.
445 To assign to a global variable you must always prefix the
446 command with a 'global' command, e.g.:
447 (Pdb) global list_options; list_options = ['-l']
448 (Pdb)"""
449
450 def help_quit(self):
451 self.help_q()
452
453 def help_q(self):
454 print """q(uit) Quit from the debugger.
455 The program being executed is aborted."""
456
457 def help_pdb(self):
458 help()
459
Guido van Rossum35771131992-09-08 11:59:04 +0000460# Simplified interface
461
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000462def run(statement, globals=None, locals=None):
463 Pdb().run(statement, globals, locals)
464
465def runeval(expression, globals=None, locals=None):
466 return Pdb().runeval(expression, globals, locals)
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000467
468def runctx(statement, globals, locals):
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000469 # B/W compatibility
470 run(statement, globals, locals)
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000471
Guido van Rossum4e160981992-09-02 20:43:20 +0000472def runcall(*args):
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000473 return apply(Pdb().runcall, args)
Guido van Rossum4e160981992-09-02 20:43:20 +0000474
Guido van Rossumb6775db1994-08-01 11:34:53 +0000475def set_trace():
476 Pdb().set_trace()
Guido van Rossum35771131992-09-08 11:59:04 +0000477
478# Post-Mortem interface
479
480def post_mortem(t):
Guido van Rossum5ef74b81993-06-23 11:55:24 +0000481 p = Pdb()
Guido van Rossum35771131992-09-08 11:59:04 +0000482 p.reset()
483 while t.tb_next <> None: t = t.tb_next
484 p.interaction(t.tb_frame, t)
485
486def pm():
487 import sys
488 post_mortem(sys.last_traceback)
489
490
491# Main program for testing
492
Guido van Rossum23efba41992-01-27 16:58:47 +0000493TESTCMD = 'import x; x.main()'
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000494
Guido van Rossum921c8241992-01-10 14:54:42 +0000495def test():
Guido van Rossum23efba41992-01-27 16:58:47 +0000496 run(TESTCMD)
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000497
498# print help
499def help():
Guido van Rossumb37954f1993-10-22 13:57:38 +0000500 import os
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000501 for dirname in sys.path:
502 fullname = os.path.join(dirname, 'pdb.doc')
503 if os.path.exists(fullname):
504 sts = os.system('${PAGER-more} '+fullname)
505 if sts: print '*** Pager exit status:', sts
506 break
507 else:
508 print 'Sorry, can\'t find the help file "pdb.doc"',
509 print 'along the Python search path'
Guido van Rossumf17361d1996-07-30 16:28:13 +0000510
511# When invoked as main program, invoke the debugger on a script
512if __name__=='__main__':
513 import sys
Guido van Rossumec577d51996-09-10 17:39:34 +0000514 import os
Guido van Rossumf17361d1996-07-30 16:28:13 +0000515 if not sys.argv[1:]:
516 print "usage: pdb.py scriptfile [arg] ..."
517 sys.exit(2)
518
Guido van Rossumec577d51996-09-10 17:39:34 +0000519 filename = sys.argv[1] # Get script filename
Guido van Rossumf17361d1996-07-30 16:28:13 +0000520
Guido van Rossumec577d51996-09-10 17:39:34 +0000521 del sys.argv[0] # Hide "pdb.py" from argument list
522
523 # Insert script directory in front of module search path
524 sys.path.insert(0, os.path.dirname(filename))
Guido van Rossumf17361d1996-07-30 16:28:13 +0000525
526 run('execfile(' + `filename` + ')', {'__name__': '__main__'})