blob: c7ec9c4e5353bd910603b4aecd5603ccfa61b1db [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 Rossum4b8c6ea2000-02-04 15:39:30 +00003"""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
Guido van Rossum921c8241992-01-10 14:54:42 +00007import sys
8import linecache
Guido van Rossum23efba41992-01-27 16:58:47 +00009import cmd
10import bdb
Guido van Rossumef1b41b2002-09-10 21:57:14 +000011from repr import Repr
Guido van Rossumb5699c71998-07-20 23:13:54 +000012import os
Barry Warsaw2bee8fe1999-09-09 16:32:41 +000013import re
Barry Warsaw210bd202002-11-05 22:40:20 +000014import pprint
Barry Warsaw2bee8fe1999-09-09 16:32:41 +000015
Guido van Rossumef1b41b2002-09-10 21:57:14 +000016# Create a custom safe Repr instance and increase its maxstring.
17# The default of 30 truncates error messages too easily.
18_repr = Repr()
19_repr.maxstring = 200
20_saferepr = _repr.repr
21
Skip Montanaro352674d2001-02-07 23:14:30 +000022__all__ = ["run", "pm", "Pdb", "runeval", "runctx", "runcall", "set_trace",
23 "post_mortem", "help"]
24
Barry Warsaw2bee8fe1999-09-09 16:32:41 +000025def find_function(funcname, filename):
Tim Peters2344fae2001-01-15 00:50:52 +000026 cre = re.compile(r'def\s+%s\s*[(]' % funcname)
27 try:
28 fp = open(filename)
29 except IOError:
30 return None
31 # consumer of this info expects the first line to be 1
32 lineno = 1
33 answer = None
34 while 1:
35 line = fp.readline()
36 if line == '':
37 break
38 if cre.match(line):
39 answer = funcname, filename, lineno
40 break
41 lineno = lineno + 1
42 fp.close()
43 return answer
Guido van Rossum921c8241992-01-10 14:54:42 +000044
45
Guido van Rossuma558e371994-11-10 22:27:35 +000046# Interaction prompt line will separate file and call info from code
47# text using value of line_prefix string. A newline and arrow may
48# be to your liking. You can set it once pdb is imported using the
49# command "pdb.line_prefix = '\n% '".
Tim Peters2344fae2001-01-15 00:50:52 +000050# line_prefix = ': ' # Use this to get the old situation back
51line_prefix = '\n-> ' # Probably a better default
Guido van Rossuma558e371994-11-10 22:27:35 +000052
Guido van Rossum23efba41992-01-27 16:58:47 +000053class Pdb(bdb.Bdb, cmd.Cmd):
Guido van Rossum2424f851998-09-11 22:50:09 +000054
Tim Peters2344fae2001-01-15 00:50:52 +000055 def __init__(self):
56 bdb.Bdb.__init__(self)
57 cmd.Cmd.__init__(self)
58 self.prompt = '(Pdb) '
59 self.aliases = {}
60 # Try to load readline if it exists
61 try:
62 import readline
63 except ImportError:
64 pass
Guido van Rossum2424f851998-09-11 22:50:09 +000065
Tim Peters2344fae2001-01-15 00:50:52 +000066 # Read $HOME/.pdbrc and ./.pdbrc
67 self.rcLines = []
Raymond Hettinger54f02222002-06-01 14:18:47 +000068 if 'HOME' in os.environ:
Tim Peters2344fae2001-01-15 00:50:52 +000069 envHome = os.environ['HOME']
70 try:
71 rcFile = open(os.path.join(envHome, ".pdbrc"))
72 except IOError:
73 pass
74 else:
75 for line in rcFile.readlines():
76 self.rcLines.append(line)
77 rcFile.close()
78 try:
79 rcFile = open(".pdbrc")
80 except IOError:
81 pass
82 else:
83 for line in rcFile.readlines():
84 self.rcLines.append(line)
85 rcFile.close()
Guido van Rossum23efba41992-01-27 16:58:47 +000086
Tim Peters2344fae2001-01-15 00:50:52 +000087 def reset(self):
88 bdb.Bdb.reset(self)
89 self.forget()
Guido van Rossum23efba41992-01-27 16:58:47 +000090
Tim Peters2344fae2001-01-15 00:50:52 +000091 def forget(self):
92 self.lineno = None
93 self.stack = []
94 self.curindex = 0
95 self.curframe = None
Guido van Rossum2424f851998-09-11 22:50:09 +000096
Tim Peters2344fae2001-01-15 00:50:52 +000097 def setup(self, f, t):
98 self.forget()
99 self.stack, self.curindex = self.get_stack(f, t)
100 self.curframe = self.stack[self.curindex][0]
101 self.execRcLines()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000102
Tim Peters2344fae2001-01-15 00:50:52 +0000103 # Can be executed earlier than 'setup' if desired
104 def execRcLines(self):
105 if self.rcLines:
106 # Make local copy because of recursion
107 rcLines = self.rcLines
108 # executed only once
109 self.rcLines = []
110 for line in rcLines:
111 line = line[:-1]
Guido van Rossum08454592002-07-12 13:10:53 +0000112 if len(line) > 0 and line[0] != '#':
113 self.onecmd(line)
Guido van Rossum2424f851998-09-11 22:50:09 +0000114
Tim Peters280488b2002-08-23 18:19:30 +0000115 # Override Bdb methods
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000116
117 def user_call(self, frame, argument_list):
118 """This method is called when there is the remote possibility
119 that we ever need to stop in this function."""
Michael W. Hudson01eb85c2003-01-31 17:48:29 +0000120 if self.stop_here(frame):
121 print '--Call--'
122 self.interaction(frame, None)
Guido van Rossum2424f851998-09-11 22:50:09 +0000123
Tim Peters2344fae2001-01-15 00:50:52 +0000124 def user_line(self, frame):
125 """This function is called when we stop or break at this line."""
126 self.interaction(frame, None)
Guido van Rossum9e1ee971997-07-11 13:43:53 +0000127
Tim Peters2344fae2001-01-15 00:50:52 +0000128 def user_return(self, frame, return_value):
129 """This function is called when a return trap is set here."""
130 frame.f_locals['__return__'] = return_value
131 print '--Return--'
132 self.interaction(frame, None)
Guido van Rossum2424f851998-09-11 22:50:09 +0000133
Tim Peters2344fae2001-01-15 00:50:52 +0000134 def user_exception(self, frame, (exc_type, exc_value, exc_traceback)):
135 """This function is called if an exception occurs,
136 but only if we are to stop at or just below this level."""
137 frame.f_locals['__exception__'] = exc_type, exc_value
138 if type(exc_type) == type(''):
139 exc_type_name = exc_type
140 else: exc_type_name = exc_type.__name__
Tim Peters6f8ee592001-02-09 23:28:07 +0000141 print exc_type_name + ':', _saferepr(exc_value)
Tim Peters2344fae2001-01-15 00:50:52 +0000142 self.interaction(frame, exc_traceback)
Guido van Rossum2424f851998-09-11 22:50:09 +0000143
Tim Peters2344fae2001-01-15 00:50:52 +0000144 # General interaction function
145
146 def interaction(self, frame, traceback):
147 self.setup(frame, traceback)
148 self.print_stack_entry(self.stack[self.curindex])
149 self.cmdloop()
150 self.forget()
151
152 def default(self, line):
153 if line[:1] == '!': line = line[1:]
154 locals = self.curframe.f_locals
155 globals = self.curframe.f_globals
156 try:
157 code = compile(line + '\n', '<stdin>', 'single')
158 exec code in globals, locals
159 except:
160 t, v = sys.exc_info()[:2]
161 if type(t) == type(''):
162 exc_type_name = t
163 else: exc_type_name = t.__name__
164 print '***', exc_type_name + ':', v
165
166 def precmd(self, line):
167 """Handle alias expansion and ';;' separator."""
Guido van Rossum08454592002-07-12 13:10:53 +0000168 if not line.strip():
Tim Peters2344fae2001-01-15 00:50:52 +0000169 return line
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000170 args = line.split()
Raymond Hettinger54f02222002-06-01 14:18:47 +0000171 while args[0] in self.aliases:
Tim Peters2344fae2001-01-15 00:50:52 +0000172 line = self.aliases[args[0]]
173 ii = 1
174 for tmpArg in args[1:]:
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000175 line = line.replace("%" + str(ii),
Tim Peters2344fae2001-01-15 00:50:52 +0000176 tmpArg)
177 ii = ii + 1
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000178 line = line.replace("%*", ' '.join(args[1:]))
179 args = line.split()
Tim Peters2344fae2001-01-15 00:50:52 +0000180 # split into ';;' separated commands
181 # unless it's an alias command
182 if args[0] != 'alias':
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000183 marker = line.find(';;')
Tim Peters2344fae2001-01-15 00:50:52 +0000184 if marker >= 0:
185 # queue up everything after marker
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000186 next = line[marker+2:].lstrip()
Tim Peters2344fae2001-01-15 00:50:52 +0000187 self.cmdqueue.append(next)
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000188 line = line[:marker].rstrip()
Tim Peters2344fae2001-01-15 00:50:52 +0000189 return line
190
191 # Command definitions, called by cmdloop()
192 # The argument is the remaining string on the command line
193 # Return true to exit from the command loop
194
195 do_h = cmd.Cmd.do_help
196
Tim Peters2344fae2001-01-15 00:50:52 +0000197 def do_break(self, arg, temporary = 0):
198 # break [ ([filename:]lineno | function) [, "condition"] ]
199 if not arg:
200 if self.breaks: # There's at least one
201 print "Num Type Disp Enb Where"
202 for bp in bdb.Breakpoint.bpbynumber:
203 if bp:
204 bp.bpprint()
205 return
206 # parse arguments; comma has lowest precedence
207 # and cannot occur in filename
208 filename = None
209 lineno = None
210 cond = None
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000211 comma = arg.find(',')
Tim Peters2344fae2001-01-15 00:50:52 +0000212 if comma > 0:
213 # parse stuff after comma: "condition"
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000214 cond = arg[comma+1:].lstrip()
215 arg = arg[:comma].rstrip()
Tim Peters2344fae2001-01-15 00:50:52 +0000216 # parse stuff before comma: [filename:]lineno | function
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000217 colon = arg.rfind(':')
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000218 funcname = None
Tim Peters2344fae2001-01-15 00:50:52 +0000219 if colon >= 0:
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000220 filename = arg[:colon].rstrip()
Tim Peters2344fae2001-01-15 00:50:52 +0000221 f = self.lookupmodule(filename)
222 if not f:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000223 print '*** ', repr(filename),
Tim Peters2344fae2001-01-15 00:50:52 +0000224 print 'not found from sys.path'
225 return
226 else:
227 filename = f
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000228 arg = arg[colon+1:].lstrip()
Tim Peters2344fae2001-01-15 00:50:52 +0000229 try:
230 lineno = int(arg)
231 except ValueError, msg:
232 print '*** Bad lineno:', arg
233 return
234 else:
235 # no colon; can be lineno or function
236 try:
237 lineno = int(arg)
238 except ValueError:
239 try:
240 func = eval(arg,
241 self.curframe.f_globals,
242 self.curframe.f_locals)
243 except:
244 func = arg
245 try:
246 if hasattr(func, 'im_func'):
247 func = func.im_func
248 code = func.func_code
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000249 #use co_name to identify the bkpt (function names
250 #could be aliased, but co_name is invariant)
251 funcname = code.co_name
Tim Peters2344fae2001-01-15 00:50:52 +0000252 lineno = code.co_firstlineno
253 filename = code.co_filename
254 except:
255 # last thing to try
256 (ok, filename, ln) = self.lineinfo(arg)
257 if not ok:
258 print '*** The specified object',
Walter Dörwald70a6b492004-02-12 17:35:32 +0000259 print repr(arg),
Tim Peters2344fae2001-01-15 00:50:52 +0000260 print 'is not a function'
261 print ('or was not found '
262 'along sys.path.')
263 return
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000264 funcname = ok # ok contains a function name
Tim Peters2344fae2001-01-15 00:50:52 +0000265 lineno = int(ln)
266 if not filename:
267 filename = self.defaultFile()
268 # Check for reasonable breakpoint
269 line = self.checkline(filename, lineno)
270 if line:
271 # now set the break point
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000272 err = self.set_break(filename, line, temporary, cond, funcname)
Tim Peters2344fae2001-01-15 00:50:52 +0000273 if err: print '***', err
274 else:
275 bp = self.get_breaks(filename, line)[-1]
276 print "Breakpoint %d at %s:%d" % (bp.number,
277 bp.file,
278 bp.line)
279
280 # To be overridden in derived debuggers
281 def defaultFile(self):
282 """Produce a reasonable default."""
283 filename = self.curframe.f_code.co_filename
284 if filename == '<string>' and mainpyfile:
285 filename = mainpyfile
286 return filename
287
288 do_b = do_break
289
290 def do_tbreak(self, arg):
291 self.do_break(arg, 1)
292
293 def lineinfo(self, identifier):
294 failed = (None, None, None)
295 # Input is identifier, may be in single quotes
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000296 idstring = identifier.split("'")
Tim Peters2344fae2001-01-15 00:50:52 +0000297 if len(idstring) == 1:
298 # not in single quotes
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000299 id = idstring[0].strip()
Tim Peters2344fae2001-01-15 00:50:52 +0000300 elif len(idstring) == 3:
301 # quoted
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000302 id = idstring[1].strip()
Tim Peters2344fae2001-01-15 00:50:52 +0000303 else:
304 return failed
305 if id == '': return failed
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000306 parts = id.split('.')
Tim Peters2344fae2001-01-15 00:50:52 +0000307 # Protection for derived debuggers
308 if parts[0] == 'self':
309 del parts[0]
310 if len(parts) == 0:
311 return failed
312 # Best first guess at file to look at
313 fname = self.defaultFile()
314 if len(parts) == 1:
315 item = parts[0]
316 else:
317 # More than one part.
318 # First is module, second is method/class
319 f = self.lookupmodule(parts[0])
320 if f:
321 fname = f
322 item = parts[1]
323 answer = find_function(item, fname)
324 return answer or failed
325
326 def checkline(self, filename, lineno):
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000327 """Check whether specified line seems to be executable.
Tim Peters2344fae2001-01-15 00:50:52 +0000328
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000329 Return `lineno` if it is, 0 if not (e.g. a docstring, comment, blank
330 line or EOF). Warning: testing is not comprehensive.
331 """
Tim Peters2344fae2001-01-15 00:50:52 +0000332 line = linecache.getline(filename, lineno)
333 if not line:
334 print 'End of file'
335 return 0
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000336 line = line.strip()
Tim Peters2344fae2001-01-15 00:50:52 +0000337 # Don't allow setting breakpoint at a blank line
Guido van Rossum08454592002-07-12 13:10:53 +0000338 if (not line or (line[0] == '#') or
339 (line[:3] == '"""') or line[:3] == "'''"):
Tim Peters2344fae2001-01-15 00:50:52 +0000340 print '*** Blank or comment'
341 return 0
Tim Peters2344fae2001-01-15 00:50:52 +0000342 return lineno
343
344 def do_enable(self, arg):
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000345 args = arg.split()
Tim Peters2344fae2001-01-15 00:50:52 +0000346 for i in args:
Andrew M. Kuchlingb1f8bab2003-05-22 14:46:12 +0000347 try:
348 i = int(i)
349 except ValueError:
350 print 'Breakpoint index %r is not a number' % i
351 continue
352
353 if not (0 <= i < len(bdb.Breakpoint.bpbynumber)):
354 print 'No breakpoint numbered', i
355 continue
356
357 bp = bdb.Breakpoint.bpbynumber[i]
Tim Peters2344fae2001-01-15 00:50:52 +0000358 if bp:
359 bp.enable()
360
361 def do_disable(self, arg):
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000362 args = arg.split()
Tim Peters2344fae2001-01-15 00:50:52 +0000363 for i in args:
Andrew M. Kuchlingb1f8bab2003-05-22 14:46:12 +0000364 try:
365 i = int(i)
366 except ValueError:
367 print 'Breakpoint index %r is not a number' % i
368 continue
Tim Petersf545baa2003-06-15 23:26:30 +0000369
Andrew M. Kuchlingb1f8bab2003-05-22 14:46:12 +0000370 if not (0 <= i < len(bdb.Breakpoint.bpbynumber)):
371 print 'No breakpoint numbered', i
372 continue
373
374 bp = bdb.Breakpoint.bpbynumber[i]
Tim Peters2344fae2001-01-15 00:50:52 +0000375 if bp:
376 bp.disable()
377
378 def do_condition(self, arg):
379 # arg is breakpoint number and condition
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000380 args = arg.split(' ', 1)
381 bpnum = int(args[0].strip())
Tim Peters2344fae2001-01-15 00:50:52 +0000382 try:
383 cond = args[1]
384 except:
385 cond = None
386 bp = bdb.Breakpoint.bpbynumber[bpnum]
387 if bp:
388 bp.cond = cond
389 if not cond:
390 print 'Breakpoint', bpnum,
391 print 'is now unconditional.'
392
393 def do_ignore(self,arg):
394 """arg is bp number followed by ignore count."""
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000395 args = arg.split()
396 bpnum = int(args[0].strip())
Tim Peters2344fae2001-01-15 00:50:52 +0000397 try:
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000398 count = int(args[1].strip())
Tim Peters2344fae2001-01-15 00:50:52 +0000399 except:
400 count = 0
401 bp = bdb.Breakpoint.bpbynumber[bpnum]
402 if bp:
403 bp.ignore = count
Guido van Rossum08454592002-07-12 13:10:53 +0000404 if count > 0:
Tim Peters2344fae2001-01-15 00:50:52 +0000405 reply = 'Will ignore next '
Guido van Rossum08454592002-07-12 13:10:53 +0000406 if count > 1:
Tim Peters2344fae2001-01-15 00:50:52 +0000407 reply = reply + '%d crossings' % count
408 else:
409 reply = reply + '1 crossing'
410 print reply + ' of breakpoint %d.' % bpnum
411 else:
412 print 'Will stop next time breakpoint',
413 print bpnum, 'is reached.'
414
415 def do_clear(self, arg):
416 """Three possibilities, tried in this order:
417 clear -> clear all breaks, ask for confirmation
418 clear file:lineno -> clear all breaks at file:lineno
419 clear bpno bpno ... -> clear breakpoints by number"""
420 if not arg:
421 try:
422 reply = raw_input('Clear all breaks? ')
423 except EOFError:
424 reply = 'no'
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000425 reply = reply.strip().lower()
Tim Peters2344fae2001-01-15 00:50:52 +0000426 if reply in ('y', 'yes'):
427 self.clear_all_breaks()
428 return
429 if ':' in arg:
430 # Make sure it works for "clear C:\foo\bar.py:12"
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000431 i = arg.rfind(':')
Tim Peters2344fae2001-01-15 00:50:52 +0000432 filename = arg[:i]
433 arg = arg[i+1:]
434 try:
435 lineno = int(arg)
436 except:
437 err = "Invalid line number (%s)" % arg
438 else:
439 err = self.clear_break(filename, lineno)
440 if err: print '***', err
441 return
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000442 numberlist = arg.split()
Tim Peters2344fae2001-01-15 00:50:52 +0000443 for i in numberlist:
444 err = self.clear_bpbynumber(i)
445 if err:
446 print '***', err
447 else:
448 print 'Deleted breakpoint %s ' % (i,)
449 do_cl = do_clear # 'c' is already an abbreviation for 'continue'
450
451 def do_where(self, arg):
452 self.print_stack_trace()
453 do_w = do_where
Guido van Rossum6bd68352001-01-20 17:57:37 +0000454 do_bt = do_where
Tim Peters2344fae2001-01-15 00:50:52 +0000455
456 def do_up(self, arg):
457 if self.curindex == 0:
458 print '*** Oldest frame'
459 else:
460 self.curindex = self.curindex - 1
461 self.curframe = self.stack[self.curindex][0]
462 self.print_stack_entry(self.stack[self.curindex])
463 self.lineno = None
464 do_u = do_up
465
466 def do_down(self, arg):
467 if self.curindex + 1 == len(self.stack):
468 print '*** Newest frame'
469 else:
470 self.curindex = self.curindex + 1
471 self.curframe = self.stack[self.curindex][0]
472 self.print_stack_entry(self.stack[self.curindex])
473 self.lineno = None
474 do_d = do_down
475
476 def do_step(self, arg):
477 self.set_step()
478 return 1
479 do_s = do_step
480
481 def do_next(self, arg):
482 self.set_next(self.curframe)
483 return 1
484 do_n = do_next
485
486 def do_return(self, arg):
487 self.set_return(self.curframe)
488 return 1
489 do_r = do_return
490
491 def do_continue(self, arg):
492 self.set_continue()
493 return 1
494 do_c = do_cont = do_continue
495
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000496 def do_jump(self, arg):
497 if self.curindex + 1 != len(self.stack):
498 print "*** You can only jump within the bottom frame"
499 return
500 try:
501 arg = int(arg)
502 except ValueError:
503 print "*** The 'jump' command requires a line number."
504 else:
505 try:
506 # Do the jump, fix up our copy of the stack, and display the
507 # new position
508 self.curframe.f_lineno = arg
509 self.stack[self.curindex] = self.stack[self.curindex][0], arg
510 self.print_stack_entry(self.stack[self.curindex])
511 except ValueError, e:
512 print '*** Jump failed:', e
513 do_j = do_jump
514
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000515 def do_debug(self, arg):
516 sys.settrace(None)
517 globals = self.curframe.f_globals
518 locals = self.curframe.f_locals
Guido van Rossumed538d82003-04-09 19:36:34 +0000519 p = Pdb()
520 p.prompt = "(%s) " % self.prompt.strip()
521 print "ENTERING RECURSIVE DEBUGGER"
522 sys.call_tracing(p.run, (arg, globals, locals))
523 print "LEAVING RECURSIVE DEBUGGER"
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000524 sys.settrace(self.trace_dispatch)
525 self.lastcmd = p.lastcmd
526
Tim Peters2344fae2001-01-15 00:50:52 +0000527 def do_quit(self, arg):
528 self.set_quit()
529 return 1
530 do_q = do_quit
Guido van Rossumd1c08f32002-04-15 00:48:24 +0000531 do_exit = do_quit
Tim Peters2344fae2001-01-15 00:50:52 +0000532
Guido van Rossumeef26072003-01-13 21:13:55 +0000533 def do_EOF(self, arg):
534 print
535 self.set_quit()
536 return 1
537
Tim Peters2344fae2001-01-15 00:50:52 +0000538 def do_args(self, arg):
539 f = self.curframe
540 co = f.f_code
541 dict = f.f_locals
542 n = co.co_argcount
543 if co.co_flags & 4: n = n+1
544 if co.co_flags & 8: n = n+1
545 for i in range(n):
546 name = co.co_varnames[i]
547 print name, '=',
Raymond Hettinger54f02222002-06-01 14:18:47 +0000548 if name in dict: print dict[name]
Tim Peters2344fae2001-01-15 00:50:52 +0000549 else: print "*** undefined ***"
550 do_a = do_args
Guido van Rossum2424f851998-09-11 22:50:09 +0000551
Tim Peters2344fae2001-01-15 00:50:52 +0000552 def do_retval(self, arg):
Raymond Hettinger54f02222002-06-01 14:18:47 +0000553 if '__return__' in self.curframe.f_locals:
Tim Peters2344fae2001-01-15 00:50:52 +0000554 print self.curframe.f_locals['__return__']
555 else:
556 print '*** Not yet returned!'
557 do_rv = do_retval
Guido van Rossum2424f851998-09-11 22:50:09 +0000558
Barry Warsaw210bd202002-11-05 22:40:20 +0000559 def _getval(self, arg):
Tim Peters2344fae2001-01-15 00:50:52 +0000560 try:
Barry Warsaw210bd202002-11-05 22:40:20 +0000561 return eval(arg, self.curframe.f_globals,
562 self.curframe.f_locals)
Tim Peters2344fae2001-01-15 00:50:52 +0000563 except:
564 t, v = sys.exc_info()[:2]
Barry Warsaw210bd202002-11-05 22:40:20 +0000565 if isinstance(t, str):
Tim Peters2344fae2001-01-15 00:50:52 +0000566 exc_type_name = t
567 else: exc_type_name = t.__name__
Walter Dörwald70a6b492004-02-12 17:35:32 +0000568 print '***', exc_type_name + ':', repr(v)
Barry Warsaw210bd202002-11-05 22:40:20 +0000569 raise
Guido van Rossum2424f851998-09-11 22:50:09 +0000570
Barry Warsaw210bd202002-11-05 22:40:20 +0000571 def do_p(self, arg):
572 try:
573 print repr(self._getval(arg))
574 except:
575 pass
576
577 def do_pp(self, arg):
578 try:
579 pprint.pprint(self._getval(arg))
580 except:
581 pass
Guido van Rossum2424f851998-09-11 22:50:09 +0000582
Tim Peters2344fae2001-01-15 00:50:52 +0000583 def do_list(self, arg):
584 self.lastcmd = 'list'
585 last = None
586 if arg:
587 try:
588 x = eval(arg, {}, {})
589 if type(x) == type(()):
590 first, last = x
591 first = int(first)
592 last = int(last)
593 if last < first:
594 # Assume it's a count
595 last = first + last
596 else:
597 first = max(1, int(x) - 5)
598 except:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000599 print '*** Error in argument:', repr(arg)
Tim Peters2344fae2001-01-15 00:50:52 +0000600 return
601 elif self.lineno is None:
602 first = max(1, self.curframe.f_lineno - 5)
603 else:
604 first = self.lineno + 1
605 if last is None:
606 last = first + 10
607 filename = self.curframe.f_code.co_filename
608 breaklist = self.get_file_breaks(filename)
609 try:
610 for lineno in range(first, last+1):
611 line = linecache.getline(filename, lineno)
612 if not line:
613 print '[EOF]'
614 break
615 else:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000616 s = repr(lineno).rjust(3)
Tim Peters2344fae2001-01-15 00:50:52 +0000617 if len(s) < 4: s = s + ' '
618 if lineno in breaklist: s = s + 'B'
619 else: s = s + ' '
620 if lineno == self.curframe.f_lineno:
621 s = s + '->'
622 print s + '\t' + line,
623 self.lineno = lineno
624 except KeyboardInterrupt:
625 pass
626 do_l = do_list
Guido van Rossum2424f851998-09-11 22:50:09 +0000627
Tim Peters2344fae2001-01-15 00:50:52 +0000628 def do_whatis(self, arg):
629 try:
630 value = eval(arg, self.curframe.f_globals,
631 self.curframe.f_locals)
632 except:
633 t, v = sys.exc_info()[:2]
634 if type(t) == type(''):
635 exc_type_name = t
636 else: exc_type_name = t.__name__
Walter Dörwald70a6b492004-02-12 17:35:32 +0000637 print '***', exc_type_name + ':', repr(v)
Tim Peters2344fae2001-01-15 00:50:52 +0000638 return
639 code = None
640 # Is it a function?
641 try: code = value.func_code
642 except: pass
643 if code:
644 print 'Function', code.co_name
645 return
646 # Is it an instance method?
647 try: code = value.im_func.func_code
648 except: pass
649 if code:
650 print 'Method', code.co_name
651 return
652 # None of the above...
653 print type(value)
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000654
Tim Peters2344fae2001-01-15 00:50:52 +0000655 def do_alias(self, arg):
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000656 args = arg.split()
Tim Peters2344fae2001-01-15 00:50:52 +0000657 if len(args) == 0:
658 keys = self.aliases.keys()
659 keys.sort()
660 for alias in keys:
661 print "%s = %s" % (alias, self.aliases[alias])
662 return
Guido van Rossum08454592002-07-12 13:10:53 +0000663 if args[0] in self.aliases and len(args) == 1:
Tim Peters2344fae2001-01-15 00:50:52 +0000664 print "%s = %s" % (args[0], self.aliases[args[0]])
665 else:
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000666 self.aliases[args[0]] = ' '.join(args[1:])
Guido van Rossum23efba41992-01-27 16:58:47 +0000667
Tim Peters2344fae2001-01-15 00:50:52 +0000668 def do_unalias(self, arg):
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000669 args = arg.split()
Tim Peters2344fae2001-01-15 00:50:52 +0000670 if len(args) == 0: return
Raymond Hettinger54f02222002-06-01 14:18:47 +0000671 if args[0] in self.aliases:
Tim Peters2344fae2001-01-15 00:50:52 +0000672 del self.aliases[args[0]]
Guido van Rossum00230781993-03-29 11:39:45 +0000673
Tim Peters2344fae2001-01-15 00:50:52 +0000674 # Print a traceback starting at the top stack frame.
675 # The most recently entered frame is printed last;
676 # this is different from dbx and gdb, but consistent with
677 # the Python interpreter's stack trace.
678 # It is also consistent with the up/down commands (which are
679 # compatible with dbx and gdb: up moves towards 'main()'
680 # and down moves towards the most recent stack frame).
Guido van Rossum2424f851998-09-11 22:50:09 +0000681
Tim Peters2344fae2001-01-15 00:50:52 +0000682 def print_stack_trace(self):
683 try:
684 for frame_lineno in self.stack:
685 self.print_stack_entry(frame_lineno)
686 except KeyboardInterrupt:
687 pass
Guido van Rossum2424f851998-09-11 22:50:09 +0000688
Tim Peters2344fae2001-01-15 00:50:52 +0000689 def print_stack_entry(self, frame_lineno, prompt_prefix=line_prefix):
690 frame, lineno = frame_lineno
691 if frame is self.curframe:
692 print '>',
693 else:
694 print ' ',
695 print self.format_stack_entry(frame_lineno, prompt_prefix)
Guido van Rossum2424f851998-09-11 22:50:09 +0000696
Guido van Rossum921c8241992-01-10 14:54:42 +0000697
Tim Peters2344fae2001-01-15 00:50:52 +0000698 # Help methods (derived from pdb.doc)
Guido van Rossum921c8241992-01-10 14:54:42 +0000699
Tim Peters2344fae2001-01-15 00:50:52 +0000700 def help_help(self):
701 self.help_h()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000702
Tim Peters2344fae2001-01-15 00:50:52 +0000703 def help_h(self):
704 print """h(elp)
705Without argument, print the list of available commands.
706With a command name as argument, print help about that command
707"help pdb" pipes the full documentation file to the $PAGER
708"help exec" gives help on the ! command"""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000709
Tim Peters2344fae2001-01-15 00:50:52 +0000710 def help_where(self):
711 self.help_w()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000712
Tim Peters2344fae2001-01-15 00:50:52 +0000713 def help_w(self):
714 print """w(here)
715Print a stack trace, with the most recent frame at the bottom.
716An arrow indicates the "current frame", which determines the
Guido van Rossum6bd68352001-01-20 17:57:37 +0000717context of most commands. 'bt' is an alias for this command."""
718
719 help_bt = help_w
Guido van Rossumb6775db1994-08-01 11:34:53 +0000720
Tim Peters2344fae2001-01-15 00:50:52 +0000721 def help_down(self):
722 self.help_d()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000723
Tim Peters2344fae2001-01-15 00:50:52 +0000724 def help_d(self):
725 print """d(own)
726Move the current frame one level down in the stack trace
Johannes Gijsbers34c41202004-08-14 15:19:28 +0000727(to a newer frame)."""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000728
Tim Peters2344fae2001-01-15 00:50:52 +0000729 def help_up(self):
730 self.help_u()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000731
Tim Peters2344fae2001-01-15 00:50:52 +0000732 def help_u(self):
733 print """u(p)
734Move the current frame one level up in the stack trace
Johannes Gijsbers34c41202004-08-14 15:19:28 +0000735(to an older frame)."""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000736
Tim Peters2344fae2001-01-15 00:50:52 +0000737 def help_break(self):
738 self.help_b()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000739
Tim Peters2344fae2001-01-15 00:50:52 +0000740 def help_b(self):
741 print """b(reak) ([file:]lineno | function) [, condition]
742With a line number argument, set a break there in the current
743file. With a function name, set a break at first executable line
744of that function. Without argument, list all breaks. If a second
745argument is present, it is a string specifying an expression
746which must evaluate to true before the breakpoint is honored.
Guido van Rossumb6775db1994-08-01 11:34:53 +0000747
Tim Peters2344fae2001-01-15 00:50:52 +0000748The line number may be prefixed with a filename and a colon,
749to specify a breakpoint in another file (probably one that
750hasn't been loaded yet). The file is searched for on sys.path;
751the .py suffix may be omitted."""
Guido van Rossumb5699c71998-07-20 23:13:54 +0000752
Tim Peters2344fae2001-01-15 00:50:52 +0000753 def help_clear(self):
754 self.help_cl()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000755
Tim Peters2344fae2001-01-15 00:50:52 +0000756 def help_cl(self):
757 print "cl(ear) filename:lineno"
758 print """cl(ear) [bpnumber [bpnumber...]]
759With a space separated list of breakpoint numbers, clear
760those breakpoints. Without argument, clear all breaks (but
761first ask confirmation). With a filename:lineno argument,
762clear all breaks at that line in that file.
Guido van Rossumb6775db1994-08-01 11:34:53 +0000763
Tim Peters2344fae2001-01-15 00:50:52 +0000764Note that the argument is different from previous versions of
765the debugger (in python distributions 1.5.1 and before) where
766a linenumber was used instead of either filename:lineno or
767breakpoint numbers."""
Guido van Rossumb5699c71998-07-20 23:13:54 +0000768
Tim Peters2344fae2001-01-15 00:50:52 +0000769 def help_tbreak(self):
770 print """tbreak same arguments as break, but breakpoint is
771removed when first hit."""
Guido van Rossum2424f851998-09-11 22:50:09 +0000772
Tim Peters2344fae2001-01-15 00:50:52 +0000773 def help_enable(self):
774 print """enable bpnumber [bpnumber ...]
775Enables the breakpoints given as a space separated list of
776bp numbers."""
Guido van Rossum2424f851998-09-11 22:50:09 +0000777
Tim Peters2344fae2001-01-15 00:50:52 +0000778 def help_disable(self):
779 print """disable bpnumber [bpnumber ...]
780Disables the breakpoints given as a space separated list of
781bp numbers."""
Guido van Rossum2424f851998-09-11 22:50:09 +0000782
Tim Peters2344fae2001-01-15 00:50:52 +0000783 def help_ignore(self):
784 print """ignore bpnumber count
785Sets the ignore count for the given breakpoint number. A breakpoint
786becomes active when the ignore count is zero. When non-zero, the
787count is decremented each time the breakpoint is reached and the
788breakpoint is not disabled and any associated condition evaluates
789to true."""
Guido van Rossum2424f851998-09-11 22:50:09 +0000790
Tim Peters2344fae2001-01-15 00:50:52 +0000791 def help_condition(self):
792 print """condition bpnumber str_condition
793str_condition is a string specifying an expression which
794must evaluate to true before the breakpoint is honored.
795If str_condition is absent, any existing condition is removed;
796i.e., the breakpoint is made unconditional."""
Guido van Rossum2424f851998-09-11 22:50:09 +0000797
Tim Peters2344fae2001-01-15 00:50:52 +0000798 def help_step(self):
799 self.help_s()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000800
Tim Peters2344fae2001-01-15 00:50:52 +0000801 def help_s(self):
802 print """s(tep)
803Execute the current line, stop at the first possible occasion
804(either in a function that is called or in the current function)."""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000805
Tim Peters2344fae2001-01-15 00:50:52 +0000806 def help_next(self):
807 self.help_n()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000808
Tim Peters2344fae2001-01-15 00:50:52 +0000809 def help_n(self):
810 print """n(ext)
811Continue execution until the next line in the current function
812is reached or it returns."""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000813
Tim Peters2344fae2001-01-15 00:50:52 +0000814 def help_return(self):
815 self.help_r()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000816
Tim Peters2344fae2001-01-15 00:50:52 +0000817 def help_r(self):
818 print """r(eturn)
819Continue execution until the current function returns."""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000820
Tim Peters2344fae2001-01-15 00:50:52 +0000821 def help_continue(self):
822 self.help_c()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000823
Tim Peters2344fae2001-01-15 00:50:52 +0000824 def help_cont(self):
825 self.help_c()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000826
Tim Peters2344fae2001-01-15 00:50:52 +0000827 def help_c(self):
828 print """c(ont(inue))
829Continue execution, only stop when a breakpoint is encountered."""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000830
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000831 def help_jump(self):
832 self.help_j()
833
834 def help_j(self):
835 print """j(ump) lineno
836Set the next line that will be executed."""
837
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000838 def help_debug(self):
839 print """debug code
840Enter a recursive debugger that steps through the code argument
841(which is an arbitrary expression or statement to be executed
842in the current environment)."""
843
Tim Peters2344fae2001-01-15 00:50:52 +0000844 def help_list(self):
845 self.help_l()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000846
Tim Peters2344fae2001-01-15 00:50:52 +0000847 def help_l(self):
848 print """l(ist) [first [,last]]
849List source code for the current file.
850Without arguments, list 11 lines around the current line
851or continue the previous listing.
852With one argument, list 11 lines starting at that line.
853With two arguments, list the given range;
854if the second argument is less than the first, it is a count."""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000855
Tim Peters2344fae2001-01-15 00:50:52 +0000856 def help_args(self):
857 self.help_a()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000858
Tim Peters2344fae2001-01-15 00:50:52 +0000859 def help_a(self):
860 print """a(rgs)
861Print the arguments of the current function."""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000862
Tim Peters2344fae2001-01-15 00:50:52 +0000863 def help_p(self):
864 print """p expression
865Print the value of the expression."""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000866
Barry Warsaw210bd202002-11-05 22:40:20 +0000867 def help_pp(self):
868 print """pp expression
869Pretty-print the value of the expression."""
870
Tim Peters2344fae2001-01-15 00:50:52 +0000871 def help_exec(self):
872 print """(!) statement
873Execute the (one-line) statement in the context of
874the current stack frame.
875The exclamation point can be omitted unless the first word
876of the statement resembles a debugger command.
877To assign to a global variable you must always prefix the
878command with a 'global' command, e.g.:
879(Pdb) global list_options; list_options = ['-l']
880(Pdb)"""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000881
Tim Peters2344fae2001-01-15 00:50:52 +0000882 def help_quit(self):
883 self.help_q()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000884
Tim Peters2344fae2001-01-15 00:50:52 +0000885 def help_q(self):
Guido van Rossumd1c08f32002-04-15 00:48:24 +0000886 print """q(uit) or exit - Quit from the debugger.
Tim Peters2344fae2001-01-15 00:50:52 +0000887The program being executed is aborted."""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000888
Guido van Rossumd1c08f32002-04-15 00:48:24 +0000889 help_exit = help_q
890
Tim Peters2344fae2001-01-15 00:50:52 +0000891 def help_whatis(self):
892 print """whatis arg
893Prints the type of the argument."""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000894
Tim Peters2344fae2001-01-15 00:50:52 +0000895 def help_EOF(self):
896 print """EOF
897Handles the receipt of EOF as a command."""
Guido van Rossum2424f851998-09-11 22:50:09 +0000898
Tim Peters2344fae2001-01-15 00:50:52 +0000899 def help_alias(self):
900 print """alias [name [command [parameter parameter ...] ]]
901Creates an alias called 'name' the executes 'command'. The command
902must *not* be enclosed in quotes. Replaceable parameters are
903indicated by %1, %2, and so on, while %* is replaced by all the
904parameters. If no command is given, the current alias for name
905is shown. If no name is given, all aliases are listed.
Guido van Rossum2424f851998-09-11 22:50:09 +0000906
Tim Peters2344fae2001-01-15 00:50:52 +0000907Aliases may be nested and can contain anything that can be
908legally typed at the pdb prompt. Note! You *can* override
909internal pdb commands with aliases! Those internal commands
910are then hidden until the alias is removed. Aliasing is recursively
911applied to the first word of the command line; all other words
912in the line are left alone.
Guido van Rossum2424f851998-09-11 22:50:09 +0000913
Tim Peters2344fae2001-01-15 00:50:52 +0000914Some useful aliases (especially when placed in the .pdbrc file) are:
Guido van Rossum2424f851998-09-11 22:50:09 +0000915
Tim Peters2344fae2001-01-15 00:50:52 +0000916#Print instance variables (usage "pi classInst")
917alias pi for k in %1.__dict__.keys(): print "%1.",k,"=",%1.__dict__[k]
Guido van Rossum2424f851998-09-11 22:50:09 +0000918
Tim Peters2344fae2001-01-15 00:50:52 +0000919#Print instance variables in self
920alias ps pi self
921"""
Guido van Rossum2424f851998-09-11 22:50:09 +0000922
Tim Peters2344fae2001-01-15 00:50:52 +0000923 def help_unalias(self):
924 print """unalias name
925Deletes the specified alias."""
Guido van Rossum2424f851998-09-11 22:50:09 +0000926
Tim Peters2344fae2001-01-15 00:50:52 +0000927 def help_pdb(self):
928 help()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000929
Tim Peters2344fae2001-01-15 00:50:52 +0000930 def lookupmodule(self, filename):
931 """Helper function for break/clear parsing -- may be overridden."""
932 root, ext = os.path.splitext(filename)
933 if ext == '':
934 filename = filename + '.py'
935 if os.path.isabs(filename):
936 return filename
937 for dirname in sys.path:
938 while os.path.islink(dirname):
939 dirname = os.readlink(dirname)
940 fullname = os.path.join(dirname, filename)
941 if os.path.exists(fullname):
942 return fullname
943 return None
Guido van Rossumb5699c71998-07-20 23:13:54 +0000944
Guido van Rossum35771131992-09-08 11:59:04 +0000945# Simplified interface
946
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000947def run(statement, globals=None, locals=None):
Tim Peters2344fae2001-01-15 00:50:52 +0000948 Pdb().run(statement, globals, locals)
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000949
950def runeval(expression, globals=None, locals=None):
Tim Peters2344fae2001-01-15 00:50:52 +0000951 return Pdb().runeval(expression, globals, locals)
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000952
953def runctx(statement, globals, locals):
Tim Peters2344fae2001-01-15 00:50:52 +0000954 # B/W compatibility
955 run(statement, globals, locals)
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000956
Guido van Rossum4e160981992-09-02 20:43:20 +0000957def runcall(*args):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000958 return Pdb().runcall(*args)
Guido van Rossum4e160981992-09-02 20:43:20 +0000959
Guido van Rossumb6775db1994-08-01 11:34:53 +0000960def set_trace():
Tim Peters2344fae2001-01-15 00:50:52 +0000961 Pdb().set_trace()
Guido van Rossum35771131992-09-08 11:59:04 +0000962
963# Post-Mortem interface
964
965def post_mortem(t):
Tim Peters2344fae2001-01-15 00:50:52 +0000966 p = Pdb()
967 p.reset()
968 while t.tb_next is not None:
969 t = t.tb_next
970 p.interaction(t.tb_frame, t)
Guido van Rossum35771131992-09-08 11:59:04 +0000971
972def pm():
Tim Peters2344fae2001-01-15 00:50:52 +0000973 post_mortem(sys.last_traceback)
Guido van Rossum35771131992-09-08 11:59:04 +0000974
975
976# Main program for testing
977
Guido van Rossum23efba41992-01-27 16:58:47 +0000978TESTCMD = 'import x; x.main()'
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000979
Guido van Rossum921c8241992-01-10 14:54:42 +0000980def test():
Tim Peters2344fae2001-01-15 00:50:52 +0000981 run(TESTCMD)
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000982
983# print help
984def help():
Tim Peters2344fae2001-01-15 00:50:52 +0000985 for dirname in sys.path:
986 fullname = os.path.join(dirname, 'pdb.doc')
987 if os.path.exists(fullname):
988 sts = os.system('${PAGER-more} '+fullname)
989 if sts: print '*** Pager exit status:', sts
990 break
991 else:
992 print 'Sorry, can\'t find the help file "pdb.doc"',
993 print 'along the Python search path'
Guido van Rossumf17361d1996-07-30 16:28:13 +0000994
Guido van Rossumb5699c71998-07-20 23:13:54 +0000995mainmodule = ''
996mainpyfile = ''
997
Guido van Rossumf17361d1996-07-30 16:28:13 +0000998# When invoked as main program, invoke the debugger on a script
999if __name__=='__main__':
Tim Peters2344fae2001-01-15 00:50:52 +00001000 if not sys.argv[1:]:
1001 print "usage: pdb.py scriptfile [arg] ..."
1002 sys.exit(2)
Guido van Rossumf17361d1996-07-30 16:28:13 +00001003
Tim Peters2344fae2001-01-15 00:50:52 +00001004 mainpyfile = filename = sys.argv[1] # Get script filename
1005 if not os.path.exists(filename):
Walter Dörwald70a6b492004-02-12 17:35:32 +00001006 print 'Error:', repr(filename), 'does not exist'
Tim Peters2344fae2001-01-15 00:50:52 +00001007 sys.exit(1)
1008 mainmodule = os.path.basename(filename)
1009 del sys.argv[0] # Hide "pdb.py" from argument list
Guido van Rossumec577d51996-09-10 17:39:34 +00001010
Tim Peters2344fae2001-01-15 00:50:52 +00001011 # Insert script directory in front of module search path
1012 sys.path.insert(0, os.path.dirname(filename))
Guido van Rossumf17361d1996-07-30 16:28:13 +00001013
Walter Dörwald70a6b492004-02-12 17:35:32 +00001014 run('execfile(%r)' % (filename,))