blob: 80aa54bf52d72adb6077bfd299dfb52735ab9e9d [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 Rossumf15d1591997-09-29 23:22:12 +000083 t, v = sys.exc_info()[:2]
84 if type(t) == type(''):
85 exc_type_name = t
86 else: exc_type_name = t.__name__
87 print '***', exc_type_name + ':', v
Guido van Rossum23efba41992-01-27 16:58:47 +000088
89 # Command definitions, called by cmdloop()
90 # The argument is the remaining string on the command line
91 # Return true to exit from the command loop
Guido van Rossum921c8241992-01-10 14:54:42 +000092
Guido van Rossum23efba41992-01-27 16:58:47 +000093 do_h = cmd.Cmd.do_help
Guido van Rossumb6775db1994-08-01 11:34:53 +000094
Guido van Rossum921c8241992-01-10 14:54:42 +000095 def do_break(self, arg):
96 if not arg:
Guido van Rossum23efba41992-01-27 16:58:47 +000097 print self.get_all_breaks() # XXX
Guido van Rossum921c8241992-01-10 14:54:42 +000098 return
Guido van Rossumb6775db1994-08-01 11:34:53 +000099 # Try line number as argument
Guido van Rossum9e1ee971997-07-11 13:43:53 +0000100 try:
101 arg = eval(arg, self.curframe.f_globals,
102 self.curframe.f_locals)
103 except:
104 print '*** Could not eval argument:', arg
105 return
106
107 # Check for condition
108 try: arg, cond = arg
109 except: arg, cond = arg, None
110
Guido van Rossumb6775db1994-08-01 11:34:53 +0000111 try:
Guido van Rossum9e1ee971997-07-11 13:43:53 +0000112 lineno = int(arg)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000113 filename = self.curframe.f_code.co_filename
Guido van Rossum921c8241992-01-10 14:54:42 +0000114 except:
Guido van Rossumb6775db1994-08-01 11:34:53 +0000115 # Try function name as the argument
Guido van Rossumb6775db1994-08-01 11:34:53 +0000116 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
Guido van Rossumc4448651997-07-18 16:47:40 +0000125 lineno = code.co_firstlineno
Guido van Rossumb6775db1994-08-01 11:34:53 +0000126 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 Rossum46c86bb1998-02-25 20:50:32 +0000204 f = self.curframe
205 co = f.f_code
206 dict = f.f_locals
207 n = co.co_argcount
208 if co.co_flags & 4: n = n+1
209 if co.co_flags & 8: n = n+1
210 for i in range(n):
211 name = co.co_varnames[i]
212 print name, '=',
213 if dict.has_key(name): print dict[name]
214 else: print "*** undefined ***"
Guido van Rossum23efba41992-01-27 16:58:47 +0000215 do_a = do_args
216
217 def do_retval(self, arg):
218 if self.curframe.f_locals.has_key('__return__'):
219 print self.curframe.f_locals['__return__']
220 else:
221 print '*** Not yet returned!'
222 do_rv = do_retval
223
224 def do_p(self, arg):
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000225 self.curframe.f_globals['__privileged__'] = 1
Guido van Rossum23efba41992-01-27 16:58:47 +0000226 try:
227 value = eval(arg, self.curframe.f_globals, \
228 self.curframe.f_locals)
229 except:
Guido van Rossumf15d1591997-09-29 23:22:12 +0000230 t, v = sys.exc_info()[:2]
231 if type(t) == type(''):
232 exc_type_name = t
233 else: exc_type_name = t.__name__
234 print '***', exc_type_name + ':', `v`
Guido van Rossum23efba41992-01-27 16:58:47 +0000235 return
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000236
Guido van Rossum23efba41992-01-27 16:58:47 +0000237 print `value`
238
Guido van Rossum921c8241992-01-10 14:54:42 +0000239 def do_list(self, arg):
Guido van Rossumb9142571992-01-12 23:32:55 +0000240 self.lastcmd = 'list'
Guido van Rossum921c8241992-01-10 14:54:42 +0000241 last = None
242 if arg:
243 try:
244 x = eval(arg, {}, {})
245 if type(x) == type(()):
246 first, last = x
247 first = int(first)
248 last = int(last)
249 if last < first:
250 # Assume it's a count
251 last = first + last
252 else:
Guido van Rossumc629d341992-11-05 10:43:02 +0000253 first = max(1, int(x) - 5)
Guido van Rossum921c8241992-01-10 14:54:42 +0000254 except:
255 print '*** Error in argument:', `arg`
256 return
257 elif self.lineno is None:
258 first = max(1, self.curframe.f_lineno - 5)
259 else:
260 first = self.lineno + 1
Guido van Rossumc629d341992-11-05 10:43:02 +0000261 if last == None:
Guido van Rossum921c8241992-01-10 14:54:42 +0000262 last = first + 10
263 filename = self.curframe.f_code.co_filename
Guido van Rossum23efba41992-01-27 16:58:47 +0000264 breaklist = self.get_file_breaks(filename)
Guido van Rossum921c8241992-01-10 14:54:42 +0000265 try:
266 for lineno in range(first, last+1):
267 line = linecache.getline(filename, lineno)
268 if not line:
269 print '[EOF]'
270 break
271 else:
272 s = string.rjust(`lineno`, 3)
273 if len(s) < 4: s = s + ' '
274 if lineno in breaklist: s = s + 'B'
275 else: s = s + ' '
276 if lineno == self.curframe.f_lineno:
277 s = s + '->'
278 print s + '\t' + line,
279 self.lineno = lineno
280 except KeyboardInterrupt:
281 pass
282 do_l = do_list
Guido van Rossum00230781993-03-29 11:39:45 +0000283
284 def do_whatis(self, arg):
Guido van Rossum00230781993-03-29 11:39:45 +0000285 try:
286 value = eval(arg, self.curframe.f_globals, \
287 self.curframe.f_locals)
288 except:
Guido van Rossumf15d1591997-09-29 23:22:12 +0000289 t, v = sys.exc_info()[:2]
290 if type(t) == type(''):
291 exc_type_name = t
292 else: exc_type_name = t.__name__
293 print '***', exc_type_name + ':', `v`
Guido van Rossum00230781993-03-29 11:39:45 +0000294 return
295 code = None
296 # Is it a function?
297 try: code = value.func_code
298 except: pass
299 if code:
Guido van Rossumb6775db1994-08-01 11:34:53 +0000300 print 'Function', code.co_name
Guido van Rossum00230781993-03-29 11:39:45 +0000301 return
302 # Is it an instance method?
303 try: code = value.im_func.func_code
304 except: pass
305 if code:
Guido van Rossumb6775db1994-08-01 11:34:53 +0000306 print 'Method', code.co_name
Guido van Rossum00230781993-03-29 11:39:45 +0000307 return
308 # None of the above...
309 print type(value)
Guido van Rossumb9142571992-01-12 23:32:55 +0000310
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000311 # Print a traceback starting at the top stack frame.
Guido van Rossum23efba41992-01-27 16:58:47 +0000312 # The most recently entered frame is printed last;
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000313 # this is different from dbx and gdb, but consistent with
314 # the Python interpreter's stack trace.
315 # It is also consistent with the up/down commands (which are
316 # compatible with dbx and gdb: up moves towards 'main()'
317 # and down moves towards the most recent stack frame).
Guido van Rossum921c8241992-01-10 14:54:42 +0000318
Guido van Rossum23efba41992-01-27 16:58:47 +0000319 def print_stack_trace(self):
320 try:
321 for frame_lineno in self.stack:
322 self.print_stack_entry(frame_lineno)
323 except KeyboardInterrupt:
324 pass
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000325
Guido van Rossumb6aa92e1995-02-03 12:50:04 +0000326 def print_stack_entry(self, frame_lineno, prompt_prefix=line_prefix):
Guido van Rossum23efba41992-01-27 16:58:47 +0000327 frame, lineno = frame_lineno
328 if frame is self.curframe:
329 print '>',
330 else:
331 print ' ',
Guido van Rossuma558e371994-11-10 22:27:35 +0000332 print self.format_stack_entry(frame_lineno, prompt_prefix)
Guido van Rossum921c8241992-01-10 14:54:42 +0000333
334
Guido van Rossumb6775db1994-08-01 11:34:53 +0000335 # Help methods (derived from pdb.doc)
336
337 def help_help(self):
338 self.help_h()
339
340 def help_h(self):
341 print """h(elp)
342 Without argument, print the list of available commands.
343 With a command name as argument, print help about that command
344 "help pdb" pipes the full documentation file to the $PAGER
345 "help exec" gives help on the ! command"""
346
347 def help_where(self):
348 self.help_w()
349
350 def help_w(self):
351 print """w(here)
352 Print a stack trace, with the most recent frame at the bottom.
353 An arrow indicates the "current frame", which determines the
354 context of most commands."""
355
356 def help_down(self):
357 self.help_d()
358
359 def help_d(self):
360 print """d(own)
361 Move the current frame one level down in the stack trace
362 (to an older frame)."""
363
364 def help_up(self):
365 self.help_u()
366
367 def help_u(self):
368 print """u(p)
369 Move the current frame one level up in the stack trace
370 (to a newer frame)."""
371
372 def help_break(self):
373 self.help_b()
374
375 def help_b(self):
Guido van Rossum9e1ee971997-07-11 13:43:53 +0000376 print """b(reak) [lineno | function] [, "condition"]
Guido van Rossumb6775db1994-08-01 11:34:53 +0000377 With a line number argument, set a break there in the current
378 file. With a function name, set a break at the entry of that
Guido van Rossum9e1ee971997-07-11 13:43:53 +0000379 function. Without argument, list all breaks. If a second
380 argument is present, it is a string specifying an expression
381 which must evaluate to true before the breakpoint is honored.
382 """
Guido van Rossumb6775db1994-08-01 11:34:53 +0000383
384 def help_clear(self):
385 self.help_cl()
386
387 def help_cl(self):
388 print """cl(ear) [lineno]
389 With a line number argument, clear that break in the current file.
390 Without argument, clear all breaks (but first ask confirmation)."""
391
392 def help_step(self):
393 self.help_s()
394
395 def help_s(self):
396 print """s(tep)
397 Execute the current line, stop at the first possible occasion
398 (either in a function that is called or in the current function)."""
399
400 def help_next(self):
401 self.help_n()
402
403 def help_n(self):
404 print """n(ext)
405 Continue execution until the next line in the current function
406 is reached or it returns."""
407
408 def help_return(self):
409 self.help_r()
410
411 def help_r(self):
412 print """r(eturn)
413 Continue execution until the current function returns."""
414
415 def help_continue(self):
416 self.help_c()
417
418 def help_cont(self):
419 self.help_c()
420
421 def help_c(self):
422 print """c(ont(inue))
423 Continue execution, only stop when a breakpoint is encountered."""
424
425 def help_list(self):
426 self.help_l()
427
428 def help_l(self):
429 print """l(ist) [first [,last]]
430 List source code for the current file.
431 Without arguments, list 11 lines around the current line
432 or continue the previous listing.
433 With one argument, list 11 lines starting at that line.
434 With two arguments, list the given range;
435 if the second argument is less than the first, it is a count."""
436
437 def help_args(self):
438 self.help_a()
439
440 def help_a(self):
441 print """a(rgs)
Guido van Rossum46c86bb1998-02-25 20:50:32 +0000442 Print the arguments of the current function."""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000443
444 def help_p(self):
445 print """p expression
446 Print the value of the expression."""
447
448 def help_exec(self):
449 print """(!) statement
450 Execute the (one-line) statement in the context of
451 the current stack frame.
452 The exclamation point can be omitted unless the first word
453 of the statement resembles a debugger command.
454 To assign to a global variable you must always prefix the
455 command with a 'global' command, e.g.:
456 (Pdb) global list_options; list_options = ['-l']
457 (Pdb)"""
458
459 def help_quit(self):
460 self.help_q()
461
462 def help_q(self):
463 print """q(uit) Quit from the debugger.
464 The program being executed is aborted."""
465
466 def help_pdb(self):
467 help()
468
Guido van Rossum35771131992-09-08 11:59:04 +0000469# Simplified interface
470
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000471def run(statement, globals=None, locals=None):
472 Pdb().run(statement, globals, locals)
473
474def runeval(expression, globals=None, locals=None):
475 return Pdb().runeval(expression, globals, locals)
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000476
477def runctx(statement, globals, locals):
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000478 # B/W compatibility
479 run(statement, globals, locals)
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000480
Guido van Rossum4e160981992-09-02 20:43:20 +0000481def runcall(*args):
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000482 return apply(Pdb().runcall, args)
Guido van Rossum4e160981992-09-02 20:43:20 +0000483
Guido van Rossumb6775db1994-08-01 11:34:53 +0000484def set_trace():
485 Pdb().set_trace()
Guido van Rossum35771131992-09-08 11:59:04 +0000486
487# Post-Mortem interface
488
489def post_mortem(t):
Guido van Rossum5ef74b81993-06-23 11:55:24 +0000490 p = Pdb()
Guido van Rossum35771131992-09-08 11:59:04 +0000491 p.reset()
492 while t.tb_next <> None: t = t.tb_next
493 p.interaction(t.tb_frame, t)
494
495def pm():
496 import sys
497 post_mortem(sys.last_traceback)
498
499
500# Main program for testing
501
Guido van Rossum23efba41992-01-27 16:58:47 +0000502TESTCMD = 'import x; x.main()'
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000503
Guido van Rossum921c8241992-01-10 14:54:42 +0000504def test():
Guido van Rossum23efba41992-01-27 16:58:47 +0000505 run(TESTCMD)
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000506
507# print help
508def help():
Guido van Rossumb37954f1993-10-22 13:57:38 +0000509 import os
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000510 for dirname in sys.path:
511 fullname = os.path.join(dirname, 'pdb.doc')
512 if os.path.exists(fullname):
513 sts = os.system('${PAGER-more} '+fullname)
514 if sts: print '*** Pager exit status:', sts
515 break
516 else:
517 print 'Sorry, can\'t find the help file "pdb.doc"',
518 print 'along the Python search path'
Guido van Rossumf17361d1996-07-30 16:28:13 +0000519
520# When invoked as main program, invoke the debugger on a script
521if __name__=='__main__':
522 import sys
Guido van Rossumec577d51996-09-10 17:39:34 +0000523 import os
Guido van Rossumf17361d1996-07-30 16:28:13 +0000524 if not sys.argv[1:]:
525 print "usage: pdb.py scriptfile [arg] ..."
526 sys.exit(2)
527
Guido van Rossumec577d51996-09-10 17:39:34 +0000528 filename = sys.argv[1] # Get script filename
Guido van Rossumf17361d1996-07-30 16:28:13 +0000529
Guido van Rossumec577d51996-09-10 17:39:34 +0000530 del sys.argv[0] # Hide "pdb.py" from argument list
531
532 # Insert script directory in front of module search path
533 sys.path.insert(0, os.path.dirname(filename))
Guido van Rossumf17361d1996-07-30 16:28:13 +0000534
535 run('execfile(' + `filename` + ')', {'__name__': '__main__'})