blob: 468b1d321dffa935ee3b9076a1c99f09b73bf89c [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
Johannes Gijsbers25b38c82004-10-12 18:12:09 +000015import traceback
Georg Brandl8e84c652007-03-13 21:08:15 +000016
17
18class Restart(Exception):
19 """Causes a debugger to be restarted for the debugged python program."""
20 pass
21
Guido van Rossumef1b41b2002-09-10 21:57:14 +000022# Create a custom safe Repr instance and increase its maxstring.
23# The default of 30 truncates error messages too easily.
24_repr = Repr()
25_repr.maxstring = 200
26_saferepr = _repr.repr
27
Skip Montanaro352674d2001-02-07 23:14:30 +000028__all__ = ["run", "pm", "Pdb", "runeval", "runctx", "runcall", "set_trace",
29 "post_mortem", "help"]
30
Barry Warsaw2bee8fe1999-09-09 16:32:41 +000031def find_function(funcname, filename):
Andrew M. Kuchlinge6728252006-09-05 13:19:18 +000032 cre = re.compile(r'def\s+%s\s*[(]' % re.escape(funcname))
Tim Peters2344fae2001-01-15 00:50:52 +000033 try:
34 fp = open(filename)
35 except IOError:
36 return None
37 # consumer of this info expects the first line to be 1
38 lineno = 1
39 answer = None
40 while 1:
41 line = fp.readline()
42 if line == '':
43 break
44 if cre.match(line):
45 answer = funcname, filename, lineno
46 break
47 lineno = lineno + 1
48 fp.close()
49 return answer
Guido van Rossum921c8241992-01-10 14:54:42 +000050
51
Guido van Rossuma558e371994-11-10 22:27:35 +000052# Interaction prompt line will separate file and call info from code
53# text using value of line_prefix string. A newline and arrow may
54# be to your liking. You can set it once pdb is imported using the
55# command "pdb.line_prefix = '\n% '".
Tim Peters2344fae2001-01-15 00:50:52 +000056# line_prefix = ': ' # Use this to get the old situation back
57line_prefix = '\n-> ' # Probably a better default
Guido van Rossuma558e371994-11-10 22:27:35 +000058
Guido van Rossum23efba41992-01-27 16:58:47 +000059class Pdb(bdb.Bdb, cmd.Cmd):
Guido van Rossum2424f851998-09-11 22:50:09 +000060
Georg Brandl19564802006-05-10 17:13:20 +000061 def __init__(self, completekey='tab', stdin=None, stdout=None):
Tim Peters2344fae2001-01-15 00:50:52 +000062 bdb.Bdb.__init__(self)
Georg Brandl19564802006-05-10 17:13:20 +000063 cmd.Cmd.__init__(self, completekey, stdin, stdout)
64 if stdout:
65 self.use_rawinput = 0
Tim Peters2344fae2001-01-15 00:50:52 +000066 self.prompt = '(Pdb) '
67 self.aliases = {}
Johannes Gijsbers25b38c82004-10-12 18:12:09 +000068 self.mainpyfile = ''
69 self._wait_for_mainpyfile = 0
Tim Peters2344fae2001-01-15 00:50:52 +000070 # Try to load readline if it exists
71 try:
72 import readline
73 except ImportError:
74 pass
Guido van Rossum2424f851998-09-11 22:50:09 +000075
Tim Peters2344fae2001-01-15 00:50:52 +000076 # Read $HOME/.pdbrc and ./.pdbrc
77 self.rcLines = []
Raymond Hettinger54f02222002-06-01 14:18:47 +000078 if 'HOME' in os.environ:
Tim Peters2344fae2001-01-15 00:50:52 +000079 envHome = os.environ['HOME']
80 try:
81 rcFile = open(os.path.join(envHome, ".pdbrc"))
82 except IOError:
83 pass
84 else:
85 for line in rcFile.readlines():
86 self.rcLines.append(line)
87 rcFile.close()
88 try:
89 rcFile = open(".pdbrc")
90 except IOError:
91 pass
92 else:
93 for line in rcFile.readlines():
94 self.rcLines.append(line)
95 rcFile.close()
Guido van Rossum23efba41992-01-27 16:58:47 +000096
Martin v. Löwisbd30f522006-04-17 17:08:37 +000097 self.commands = {} # associates a command list to breakpoint numbers
98 self.commands_doprompt = {} # for each bp num, tells if the prompt must be disp. after execing the cmd list
99 self.commands_silent = {} # for each bp num, tells if the stack trace must be disp. after execing the cmd list
100 self.commands_defining = False # True while in the process of defining a command list
101 self.commands_bnum = None # The breakpoint number for which we are defining a list
102
Tim Peters2344fae2001-01-15 00:50:52 +0000103 def reset(self):
104 bdb.Bdb.reset(self)
105 self.forget()
Guido van Rossum23efba41992-01-27 16:58:47 +0000106
Tim Peters2344fae2001-01-15 00:50:52 +0000107 def forget(self):
108 self.lineno = None
109 self.stack = []
110 self.curindex = 0
111 self.curframe = None
Guido van Rossum2424f851998-09-11 22:50:09 +0000112
Tim Peters2344fae2001-01-15 00:50:52 +0000113 def setup(self, f, t):
114 self.forget()
115 self.stack, self.curindex = self.get_stack(f, t)
116 self.curframe = self.stack[self.curindex][0]
117 self.execRcLines()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000118
Tim Peters2344fae2001-01-15 00:50:52 +0000119 # Can be executed earlier than 'setup' if desired
120 def execRcLines(self):
121 if self.rcLines:
122 # Make local copy because of recursion
123 rcLines = self.rcLines
124 # executed only once
125 self.rcLines = []
126 for line in rcLines:
127 line = line[:-1]
Guido van Rossum08454592002-07-12 13:10:53 +0000128 if len(line) > 0 and line[0] != '#':
129 self.onecmd(line)
Guido van Rossum2424f851998-09-11 22:50:09 +0000130
Tim Peters280488b2002-08-23 18:19:30 +0000131 # Override Bdb methods
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000132
133 def user_call(self, frame, argument_list):
134 """This method is called when there is the remote possibility
135 that we ever need to stop in this function."""
Johannes Gijsbers25b38c82004-10-12 18:12:09 +0000136 if self._wait_for_mainpyfile:
137 return
Michael W. Hudson01eb85c2003-01-31 17:48:29 +0000138 if self.stop_here(frame):
Georg Brandl19564802006-05-10 17:13:20 +0000139 print >>self.stdout, '--Call--'
Michael W. Hudson01eb85c2003-01-31 17:48:29 +0000140 self.interaction(frame, None)
Guido van Rossum2424f851998-09-11 22:50:09 +0000141
Tim Peters2344fae2001-01-15 00:50:52 +0000142 def user_line(self, frame):
143 """This function is called when we stop or break at this line."""
Johannes Gijsbers25b38c82004-10-12 18:12:09 +0000144 if self._wait_for_mainpyfile:
145 if (self.mainpyfile != self.canonic(frame.f_code.co_filename)
146 or frame.f_lineno<= 0):
147 return
148 self._wait_for_mainpyfile = 0
Martin v. Löwisbd30f522006-04-17 17:08:37 +0000149 if self.bp_commands(frame):
150 self.interaction(frame, None)
Martin v. Löwis1a00e182006-04-17 19:18:18 +0000151
Martin v. Löwisbd30f522006-04-17 17:08:37 +0000152 def bp_commands(self,frame):
153 """ Call every command that was set for the current active breakpoint (if there is one)
154 Returns True if the normal interaction function must be called, False otherwise """
155 #self.currentbp is set in bdb.py in bdb.break_here if a breakpoint was hit
156 if getattr(self,"currentbp",False) and self.currentbp in self.commands:
157 currentbp = self.currentbp
158 self.currentbp = 0
159 lastcmd_back = self.lastcmd
160 self.setup(frame, None)
161 for line in self.commands[currentbp]:
162 self.onecmd(line)
163 self.lastcmd = lastcmd_back
164 if not self.commands_silent[currentbp]:
165 self.print_stack_entry(self.stack[self.curindex])
166 if self.commands_doprompt[currentbp]:
167 self.cmdloop()
168 self.forget()
Martin v. Löwis1a00e182006-04-17 19:18:18 +0000169 return
Martin v. Löwisbd30f522006-04-17 17:08:37 +0000170 return 1
Guido van Rossum9e1ee971997-07-11 13:43:53 +0000171
Tim Peters2344fae2001-01-15 00:50:52 +0000172 def user_return(self, frame, return_value):
173 """This function is called when a return trap is set here."""
174 frame.f_locals['__return__'] = return_value
Georg Brandl19564802006-05-10 17:13:20 +0000175 print >>self.stdout, '--Return--'
Tim Peters2344fae2001-01-15 00:50:52 +0000176 self.interaction(frame, None)
Guido van Rossum2424f851998-09-11 22:50:09 +0000177
Tim Peters2344fae2001-01-15 00:50:52 +0000178 def user_exception(self, frame, (exc_type, exc_value, exc_traceback)):
179 """This function is called if an exception occurs,
180 but only if we are to stop at or just below this level."""
181 frame.f_locals['__exception__'] = exc_type, exc_value
182 if type(exc_type) == type(''):
183 exc_type_name = exc_type
184 else: exc_type_name = exc_type.__name__
Georg Brandl19564802006-05-10 17:13:20 +0000185 print >>self.stdout, exc_type_name + ':', _saferepr(exc_value)
Tim Peters2344fae2001-01-15 00:50:52 +0000186 self.interaction(frame, exc_traceback)
Guido van Rossum2424f851998-09-11 22:50:09 +0000187
Tim Peters2344fae2001-01-15 00:50:52 +0000188 # General interaction function
189
190 def interaction(self, frame, traceback):
191 self.setup(frame, traceback)
192 self.print_stack_entry(self.stack[self.curindex])
193 self.cmdloop()
194 self.forget()
195
196 def default(self, line):
197 if line[:1] == '!': line = line[1:]
198 locals = self.curframe.f_locals
199 globals = self.curframe.f_globals
200 try:
201 code = compile(line + '\n', '<stdin>', 'single')
202 exec code in globals, locals
203 except:
204 t, v = sys.exc_info()[:2]
205 if type(t) == type(''):
206 exc_type_name = t
207 else: exc_type_name = t.__name__
Georg Brandl19564802006-05-10 17:13:20 +0000208 print >>self.stdout, '***', exc_type_name + ':', v
Tim Peters2344fae2001-01-15 00:50:52 +0000209
210 def precmd(self, line):
211 """Handle alias expansion and ';;' separator."""
Guido van Rossum08454592002-07-12 13:10:53 +0000212 if not line.strip():
Tim Peters2344fae2001-01-15 00:50:52 +0000213 return line
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000214 args = line.split()
Raymond Hettinger54f02222002-06-01 14:18:47 +0000215 while args[0] in self.aliases:
Tim Peters2344fae2001-01-15 00:50:52 +0000216 line = self.aliases[args[0]]
217 ii = 1
218 for tmpArg in args[1:]:
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000219 line = line.replace("%" + str(ii),
Tim Peters2344fae2001-01-15 00:50:52 +0000220 tmpArg)
221 ii = ii + 1
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000222 line = line.replace("%*", ' '.join(args[1:]))
223 args = line.split()
Tim Peters2344fae2001-01-15 00:50:52 +0000224 # split into ';;' separated commands
225 # unless it's an alias command
226 if args[0] != 'alias':
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000227 marker = line.find(';;')
Tim Peters2344fae2001-01-15 00:50:52 +0000228 if marker >= 0:
229 # queue up everything after marker
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000230 next = line[marker+2:].lstrip()
Tim Peters2344fae2001-01-15 00:50:52 +0000231 self.cmdqueue.append(next)
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000232 line = line[:marker].rstrip()
Tim Peters2344fae2001-01-15 00:50:52 +0000233 return line
234
Martin v. Löwisbd30f522006-04-17 17:08:37 +0000235 def onecmd(self, line):
236 """Interpret the argument as though it had been typed in response
Martin v. Löwis1a00e182006-04-17 19:18:18 +0000237 to the prompt.
238
Andrew M. Kuchling9aed98f2006-07-27 12:18:20 +0000239 Checks whether this line is typed at the normal prompt or in
Tim Petersdaea0352006-07-27 15:11:00 +0000240 a breakpoint command list definition.
Martin v. Löwisbd30f522006-04-17 17:08:37 +0000241 """
242 if not self.commands_defining:
243 return cmd.Cmd.onecmd(self, line)
244 else:
245 return self.handle_command_def(line)
246
Martin v. Löwis1a00e182006-04-17 19:18:18 +0000247 def handle_command_def(self,line):
Martin v. Löwisbd30f522006-04-17 17:08:37 +0000248 """ Handles one command line during command list definition. """
249 cmd, arg, line = self.parseline(line)
250 if cmd == 'silent':
251 self.commands_silent[self.commands_bnum] = True
252 return # continue to handle other cmd def in the cmd list
253 elif cmd == 'end':
254 self.cmdqueue = []
255 return 1 # end of cmd list
256 cmdlist = self.commands[self.commands_bnum]
257 if (arg):
258 cmdlist.append(cmd+' '+arg)
259 else:
260 cmdlist.append(cmd)
261 # Determine if we must stop
262 try:
263 func = getattr(self, 'do_' + cmd)
264 except AttributeError:
265 func = self.default
Martin v. Löwis1a00e182006-04-17 19:18:18 +0000266 if func.func_name in self.commands_resuming : # one of the resuming commands.
Martin v. Löwisbd30f522006-04-17 17:08:37 +0000267 self.commands_doprompt[self.commands_bnum] = False
268 self.cmdqueue = []
269 return 1
Martin v. Löwis1a00e182006-04-17 19:18:18 +0000270 return
Martin v. Löwisbd30f522006-04-17 17:08:37 +0000271
Tim Peters2344fae2001-01-15 00:50:52 +0000272 # Command definitions, called by cmdloop()
273 # The argument is the remaining string on the command line
274 # Return true to exit from the command loop
275
276 do_h = cmd.Cmd.do_help
277
Martin v. Löwisbd30f522006-04-17 17:08:37 +0000278 def do_commands(self, arg):
279 """Defines a list of commands associated to a breakpoint
280 Those commands will be executed whenever the breakpoint causes the program to stop execution."""
281 if not arg:
282 bnum = len(bdb.Breakpoint.bpbynumber)-1
283 else:
284 try:
285 bnum = int(arg)
286 except:
Georg Brandl19564802006-05-10 17:13:20 +0000287 print >>self.stdout, "Usage : commands [bnum]\n ...\n end"
Martin v. Löwisbd30f522006-04-17 17:08:37 +0000288 return
289 self.commands_bnum = bnum
290 self.commands[bnum] = []
291 self.commands_doprompt[bnum] = True
292 self.commands_silent[bnum] = False
293 prompt_back = self.prompt
294 self.prompt = '(com) '
295 self.commands_defining = True
296 self.cmdloop()
297 self.commands_defining = False
298 self.prompt = prompt_back
299
Tim Peters2344fae2001-01-15 00:50:52 +0000300 def do_break(self, arg, temporary = 0):
301 # break [ ([filename:]lineno | function) [, "condition"] ]
302 if not arg:
303 if self.breaks: # There's at least one
Georg Brandl19564802006-05-10 17:13:20 +0000304 print >>self.stdout, "Num Type Disp Enb Where"
Tim Peters2344fae2001-01-15 00:50:52 +0000305 for bp in bdb.Breakpoint.bpbynumber:
306 if bp:
Georg Brandl19564802006-05-10 17:13:20 +0000307 bp.bpprint(self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000308 return
309 # parse arguments; comma has lowest precedence
310 # and cannot occur in filename
311 filename = None
312 lineno = None
313 cond = None
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000314 comma = arg.find(',')
Tim Peters2344fae2001-01-15 00:50:52 +0000315 if comma > 0:
316 # parse stuff after comma: "condition"
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000317 cond = arg[comma+1:].lstrip()
318 arg = arg[:comma].rstrip()
Tim Peters2344fae2001-01-15 00:50:52 +0000319 # parse stuff before comma: [filename:]lineno | function
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000320 colon = arg.rfind(':')
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000321 funcname = None
Tim Peters2344fae2001-01-15 00:50:52 +0000322 if colon >= 0:
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000323 filename = arg[:colon].rstrip()
Tim Peters2344fae2001-01-15 00:50:52 +0000324 f = self.lookupmodule(filename)
325 if not f:
Georg Brandl19564802006-05-10 17:13:20 +0000326 print >>self.stdout, '*** ', repr(filename),
327 print >>self.stdout, 'not found from sys.path'
Tim Peters2344fae2001-01-15 00:50:52 +0000328 return
329 else:
330 filename = f
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000331 arg = arg[colon+1:].lstrip()
Tim Peters2344fae2001-01-15 00:50:52 +0000332 try:
333 lineno = int(arg)
334 except ValueError, msg:
Georg Brandl19564802006-05-10 17:13:20 +0000335 print >>self.stdout, '*** Bad lineno:', arg
Tim Peters2344fae2001-01-15 00:50:52 +0000336 return
337 else:
338 # no colon; can be lineno or function
339 try:
340 lineno = int(arg)
341 except ValueError:
342 try:
343 func = eval(arg,
344 self.curframe.f_globals,
345 self.curframe.f_locals)
346 except:
347 func = arg
348 try:
349 if hasattr(func, 'im_func'):
350 func = func.im_func
351 code = func.func_code
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000352 #use co_name to identify the bkpt (function names
353 #could be aliased, but co_name is invariant)
354 funcname = code.co_name
Tim Peters2344fae2001-01-15 00:50:52 +0000355 lineno = code.co_firstlineno
356 filename = code.co_filename
357 except:
358 # last thing to try
359 (ok, filename, ln) = self.lineinfo(arg)
360 if not ok:
Georg Brandl19564802006-05-10 17:13:20 +0000361 print >>self.stdout, '*** The specified object',
362 print >>self.stdout, repr(arg),
363 print >>self.stdout, 'is not a function'
364 print >>self.stdout, 'or was not found along sys.path.'
Tim Peters2344fae2001-01-15 00:50:52 +0000365 return
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000366 funcname = ok # ok contains a function name
Tim Peters2344fae2001-01-15 00:50:52 +0000367 lineno = int(ln)
368 if not filename:
369 filename = self.defaultFile()
370 # Check for reasonable breakpoint
371 line = self.checkline(filename, lineno)
372 if line:
373 # now set the break point
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000374 err = self.set_break(filename, line, temporary, cond, funcname)
Georg Brandl19564802006-05-10 17:13:20 +0000375 if err: print >>self.stdout, '***', err
Tim Peters2344fae2001-01-15 00:50:52 +0000376 else:
377 bp = self.get_breaks(filename, line)[-1]
Georg Brandl19564802006-05-10 17:13:20 +0000378 print >>self.stdout, "Breakpoint %d at %s:%d" % (bp.number,
379 bp.file,
380 bp.line)
Tim Peters2344fae2001-01-15 00:50:52 +0000381
382 # To be overridden in derived debuggers
383 def defaultFile(self):
384 """Produce a reasonable default."""
385 filename = self.curframe.f_code.co_filename
Johannes Gijsbers25b38c82004-10-12 18:12:09 +0000386 if filename == '<string>' and self.mainpyfile:
387 filename = self.mainpyfile
Tim Peters2344fae2001-01-15 00:50:52 +0000388 return filename
389
390 do_b = do_break
391
392 def do_tbreak(self, arg):
393 self.do_break(arg, 1)
394
395 def lineinfo(self, identifier):
396 failed = (None, None, None)
397 # Input is identifier, may be in single quotes
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000398 idstring = identifier.split("'")
Tim Peters2344fae2001-01-15 00:50:52 +0000399 if len(idstring) == 1:
400 # not in single quotes
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000401 id = idstring[0].strip()
Tim Peters2344fae2001-01-15 00:50:52 +0000402 elif len(idstring) == 3:
403 # quoted
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000404 id = idstring[1].strip()
Tim Peters2344fae2001-01-15 00:50:52 +0000405 else:
406 return failed
407 if id == '': return failed
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000408 parts = id.split('.')
Tim Peters2344fae2001-01-15 00:50:52 +0000409 # Protection for derived debuggers
410 if parts[0] == 'self':
411 del parts[0]
412 if len(parts) == 0:
413 return failed
414 # Best first guess at file to look at
415 fname = self.defaultFile()
416 if len(parts) == 1:
417 item = parts[0]
418 else:
419 # More than one part.
420 # First is module, second is method/class
421 f = self.lookupmodule(parts[0])
422 if f:
423 fname = f
424 item = parts[1]
425 answer = find_function(item, fname)
426 return answer or failed
427
428 def checkline(self, filename, lineno):
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000429 """Check whether specified line seems to be executable.
Tim Peters2344fae2001-01-15 00:50:52 +0000430
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000431 Return `lineno` if it is, 0 if not (e.g. a docstring, comment, blank
432 line or EOF). Warning: testing is not comprehensive.
433 """
Tim Peters2344fae2001-01-15 00:50:52 +0000434 line = linecache.getline(filename, lineno)
435 if not line:
Georg Brandl19564802006-05-10 17:13:20 +0000436 print >>self.stdout, 'End of file'
Tim Peters2344fae2001-01-15 00:50:52 +0000437 return 0
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000438 line = line.strip()
Tim Peters2344fae2001-01-15 00:50:52 +0000439 # Don't allow setting breakpoint at a blank line
Guido van Rossum08454592002-07-12 13:10:53 +0000440 if (not line or (line[0] == '#') or
441 (line[:3] == '"""') or line[:3] == "'''"):
Georg Brandl19564802006-05-10 17:13:20 +0000442 print >>self.stdout, '*** Blank or comment'
Tim Peters2344fae2001-01-15 00:50:52 +0000443 return 0
Tim Peters2344fae2001-01-15 00:50:52 +0000444 return lineno
445
446 def do_enable(self, arg):
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000447 args = arg.split()
Tim Peters2344fae2001-01-15 00:50:52 +0000448 for i in args:
Andrew M. Kuchlingb1f8bab2003-05-22 14:46:12 +0000449 try:
450 i = int(i)
451 except ValueError:
Georg Brandl19564802006-05-10 17:13:20 +0000452 print >>self.stdout, 'Breakpoint index %r is not a number' % i
Andrew M. Kuchlingb1f8bab2003-05-22 14:46:12 +0000453 continue
454
455 if not (0 <= i < len(bdb.Breakpoint.bpbynumber)):
Georg Brandl19564802006-05-10 17:13:20 +0000456 print >>self.stdout, 'No breakpoint numbered', i
Andrew M. Kuchlingb1f8bab2003-05-22 14:46:12 +0000457 continue
458
459 bp = bdb.Breakpoint.bpbynumber[i]
Tim Peters2344fae2001-01-15 00:50:52 +0000460 if bp:
461 bp.enable()
462
463 def do_disable(self, arg):
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000464 args = arg.split()
Tim Peters2344fae2001-01-15 00:50:52 +0000465 for i in args:
Andrew M. Kuchlingb1f8bab2003-05-22 14:46:12 +0000466 try:
467 i = int(i)
468 except ValueError:
Georg Brandl19564802006-05-10 17:13:20 +0000469 print >>self.stdout, 'Breakpoint index %r is not a number' % i
Andrew M. Kuchlingb1f8bab2003-05-22 14:46:12 +0000470 continue
Tim Petersf545baa2003-06-15 23:26:30 +0000471
Andrew M. Kuchlingb1f8bab2003-05-22 14:46:12 +0000472 if not (0 <= i < len(bdb.Breakpoint.bpbynumber)):
Georg Brandl19564802006-05-10 17:13:20 +0000473 print >>self.stdout, 'No breakpoint numbered', i
Andrew M. Kuchlingb1f8bab2003-05-22 14:46:12 +0000474 continue
475
476 bp = bdb.Breakpoint.bpbynumber[i]
Tim Peters2344fae2001-01-15 00:50:52 +0000477 if bp:
478 bp.disable()
479
480 def do_condition(self, arg):
481 # arg is breakpoint number and condition
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000482 args = arg.split(' ', 1)
Georg Brandle4980832007-01-22 21:23:41 +0000483 try:
484 bpnum = int(args[0].strip())
485 except ValueError:
486 # something went wrong
487 print >>self.stdout, \
488 'Breakpoint index %r is not a number' % args[0]
Georg Brandlb2783182007-03-11 08:28:46 +0000489 return
Tim Peters2344fae2001-01-15 00:50:52 +0000490 try:
491 cond = args[1]
492 except:
493 cond = None
Collin Winter2faa9e12007-03-11 16:00:20 +0000494 try:
495 bp = bdb.Breakpoint.bpbynumber[bpnum]
496 except IndexError:
497 print >>self.stdout, 'Breakpoint index %r is not valid' % args[0]
498 return
Tim Peters2344fae2001-01-15 00:50:52 +0000499 if bp:
500 bp.cond = cond
501 if not cond:
Georg Brandl19564802006-05-10 17:13:20 +0000502 print >>self.stdout, 'Breakpoint', bpnum,
503 print >>self.stdout, 'is now unconditional.'
Tim Peters2344fae2001-01-15 00:50:52 +0000504
505 def do_ignore(self,arg):
506 """arg is bp number followed by ignore count."""
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000507 args = arg.split()
Georg Brandle4980832007-01-22 21:23:41 +0000508 try:
509 bpnum = int(args[0].strip())
510 except ValueError:
511 # something went wrong
512 print >>self.stdout, \
513 'Breakpoint index %r is not a number' % args[0]
Georg Brandlb2783182007-03-11 08:28:46 +0000514 return
Tim Peters2344fae2001-01-15 00:50:52 +0000515 try:
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000516 count = int(args[1].strip())
Tim Peters2344fae2001-01-15 00:50:52 +0000517 except:
518 count = 0
Collin Winter2faa9e12007-03-11 16:00:20 +0000519 try:
520 bp = bdb.Breakpoint.bpbynumber[bpnum]
521 except IndexError:
522 print >>self.stdout, 'Breakpoint index %r is not valid' % args[0]
523 return
Tim Peters2344fae2001-01-15 00:50:52 +0000524 if bp:
525 bp.ignore = count
Guido van Rossum08454592002-07-12 13:10:53 +0000526 if count > 0:
Tim Peters2344fae2001-01-15 00:50:52 +0000527 reply = 'Will ignore next '
Guido van Rossum08454592002-07-12 13:10:53 +0000528 if count > 1:
Tim Peters2344fae2001-01-15 00:50:52 +0000529 reply = reply + '%d crossings' % count
530 else:
531 reply = reply + '1 crossing'
Georg Brandl19564802006-05-10 17:13:20 +0000532 print >>self.stdout, reply + ' of breakpoint %d.' % bpnum
Tim Peters2344fae2001-01-15 00:50:52 +0000533 else:
Georg Brandl19564802006-05-10 17:13:20 +0000534 print >>self.stdout, 'Will stop next time breakpoint',
535 print >>self.stdout, bpnum, 'is reached.'
Tim Peters2344fae2001-01-15 00:50:52 +0000536
537 def do_clear(self, arg):
538 """Three possibilities, tried in this order:
539 clear -> clear all breaks, ask for confirmation
540 clear file:lineno -> clear all breaks at file:lineno
541 clear bpno bpno ... -> clear breakpoints by number"""
542 if not arg:
543 try:
544 reply = raw_input('Clear all breaks? ')
545 except EOFError:
546 reply = 'no'
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000547 reply = reply.strip().lower()
Tim Peters2344fae2001-01-15 00:50:52 +0000548 if reply in ('y', 'yes'):
549 self.clear_all_breaks()
550 return
551 if ':' in arg:
552 # Make sure it works for "clear C:\foo\bar.py:12"
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000553 i = arg.rfind(':')
Tim Peters2344fae2001-01-15 00:50:52 +0000554 filename = arg[:i]
555 arg = arg[i+1:]
556 try:
557 lineno = int(arg)
Georg Brandl23d9d452006-05-03 18:12:33 +0000558 except ValueError:
Tim Peters2344fae2001-01-15 00:50:52 +0000559 err = "Invalid line number (%s)" % arg
560 else:
561 err = self.clear_break(filename, lineno)
Georg Brandl19564802006-05-10 17:13:20 +0000562 if err: print >>self.stdout, '***', err
Tim Peters2344fae2001-01-15 00:50:52 +0000563 return
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000564 numberlist = arg.split()
Tim Peters2344fae2001-01-15 00:50:52 +0000565 for i in numberlist:
Georg Brandl23d9d452006-05-03 18:12:33 +0000566 try:
567 i = int(i)
568 except ValueError:
Georg Brandl19564802006-05-10 17:13:20 +0000569 print >>self.stdout, 'Breakpoint index %r is not a number' % i
Georg Brandl23d9d452006-05-03 18:12:33 +0000570 continue
571
Georg Brandl6d2b3462005-08-24 07:36:17 +0000572 if not (0 <= i < len(bdb.Breakpoint.bpbynumber)):
Georg Brandl19564802006-05-10 17:13:20 +0000573 print >>self.stdout, 'No breakpoint numbered', i
Georg Brandl6d2b3462005-08-24 07:36:17 +0000574 continue
Tim Peters2344fae2001-01-15 00:50:52 +0000575 err = self.clear_bpbynumber(i)
576 if err:
Georg Brandl19564802006-05-10 17:13:20 +0000577 print >>self.stdout, '***', err
Tim Peters2344fae2001-01-15 00:50:52 +0000578 else:
Georg Brandl19564802006-05-10 17:13:20 +0000579 print >>self.stdout, 'Deleted breakpoint', i
Tim Peters2344fae2001-01-15 00:50:52 +0000580 do_cl = do_clear # 'c' is already an abbreviation for 'continue'
581
582 def do_where(self, arg):
583 self.print_stack_trace()
584 do_w = do_where
Guido van Rossum6bd68352001-01-20 17:57:37 +0000585 do_bt = do_where
Tim Peters2344fae2001-01-15 00:50:52 +0000586
587 def do_up(self, arg):
588 if self.curindex == 0:
Georg Brandl19564802006-05-10 17:13:20 +0000589 print >>self.stdout, '*** Oldest frame'
Tim Peters2344fae2001-01-15 00:50:52 +0000590 else:
591 self.curindex = self.curindex - 1
592 self.curframe = self.stack[self.curindex][0]
593 self.print_stack_entry(self.stack[self.curindex])
594 self.lineno = None
595 do_u = do_up
596
597 def do_down(self, arg):
598 if self.curindex + 1 == len(self.stack):
Georg Brandl19564802006-05-10 17:13:20 +0000599 print >>self.stdout, '*** Newest frame'
Tim Peters2344fae2001-01-15 00:50:52 +0000600 else:
601 self.curindex = self.curindex + 1
602 self.curframe = self.stack[self.curindex][0]
603 self.print_stack_entry(self.stack[self.curindex])
604 self.lineno = None
605 do_d = do_down
606
607 def do_step(self, arg):
608 self.set_step()
609 return 1
610 do_s = do_step
611
612 def do_next(self, arg):
613 self.set_next(self.curframe)
614 return 1
615 do_n = do_next
616
Georg Brandl8e84c652007-03-13 21:08:15 +0000617 def do_run(self, arg):
618 """Restart program by raising an exception to be caught in the main debugger
619 loop. If arguments were given, set them in sys.argv."""
620 if arg:
621 import shlex
622 argv0 = sys.argv[0:1]
623 sys.argv = shlex.split(arg)
624 sys.argv[:0] = argv0
625 raise Restart
626
627 do_restart = do_run
628
Tim Peters2344fae2001-01-15 00:50:52 +0000629 def do_return(self, arg):
630 self.set_return(self.curframe)
631 return 1
632 do_r = do_return
633
634 def do_continue(self, arg):
635 self.set_continue()
636 return 1
637 do_c = do_cont = do_continue
638
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000639 def do_jump(self, arg):
640 if self.curindex + 1 != len(self.stack):
Georg Brandl19564802006-05-10 17:13:20 +0000641 print >>self.stdout, "*** You can only jump within the bottom frame"
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000642 return
643 try:
644 arg = int(arg)
645 except ValueError:
Georg Brandl19564802006-05-10 17:13:20 +0000646 print >>self.stdout, "*** The 'jump' command requires a line number."
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000647 else:
648 try:
649 # Do the jump, fix up our copy of the stack, and display the
650 # new position
651 self.curframe.f_lineno = arg
652 self.stack[self.curindex] = self.stack[self.curindex][0], arg
653 self.print_stack_entry(self.stack[self.curindex])
654 except ValueError, e:
Georg Brandl19564802006-05-10 17:13:20 +0000655 print >>self.stdout, '*** Jump failed:', e
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000656 do_j = do_jump
657
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000658 def do_debug(self, arg):
659 sys.settrace(None)
660 globals = self.curframe.f_globals
661 locals = self.curframe.f_locals
Guido van Rossumed538d82003-04-09 19:36:34 +0000662 p = Pdb()
663 p.prompt = "(%s) " % self.prompt.strip()
Georg Brandl19564802006-05-10 17:13:20 +0000664 print >>self.stdout, "ENTERING RECURSIVE DEBUGGER"
Guido van Rossumed538d82003-04-09 19:36:34 +0000665 sys.call_tracing(p.run, (arg, globals, locals))
Georg Brandl19564802006-05-10 17:13:20 +0000666 print >>self.stdout, "LEAVING RECURSIVE DEBUGGER"
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000667 sys.settrace(self.trace_dispatch)
668 self.lastcmd = p.lastcmd
669
Tim Peters2344fae2001-01-15 00:50:52 +0000670 def do_quit(self, arg):
Johannes Gijsbers25b38c82004-10-12 18:12:09 +0000671 self._user_requested_quit = 1
Tim Peters2344fae2001-01-15 00:50:52 +0000672 self.set_quit()
673 return 1
Johannes Gijsbers25b38c82004-10-12 18:12:09 +0000674
Tim Peters2344fae2001-01-15 00:50:52 +0000675 do_q = do_quit
Guido van Rossumd1c08f32002-04-15 00:48:24 +0000676 do_exit = do_quit
Tim Peters2344fae2001-01-15 00:50:52 +0000677
Guido van Rossumeef26072003-01-13 21:13:55 +0000678 def do_EOF(self, arg):
Georg Brandl19564802006-05-10 17:13:20 +0000679 print >>self.stdout
Johannes Gijsbers25b38c82004-10-12 18:12:09 +0000680 self._user_requested_quit = 1
Guido van Rossumeef26072003-01-13 21:13:55 +0000681 self.set_quit()
682 return 1
683
Tim Peters2344fae2001-01-15 00:50:52 +0000684 def do_args(self, arg):
685 f = self.curframe
686 co = f.f_code
687 dict = f.f_locals
688 n = co.co_argcount
689 if co.co_flags & 4: n = n+1
690 if co.co_flags & 8: n = n+1
691 for i in range(n):
692 name = co.co_varnames[i]
Georg Brandl19564802006-05-10 17:13:20 +0000693 print >>self.stdout, name, '=',
694 if name in dict: print >>self.stdout, dict[name]
695 else: print >>self.stdout, "*** undefined ***"
Tim Peters2344fae2001-01-15 00:50:52 +0000696 do_a = do_args
Guido van Rossum2424f851998-09-11 22:50:09 +0000697
Tim Peters2344fae2001-01-15 00:50:52 +0000698 def do_retval(self, arg):
Raymond Hettinger54f02222002-06-01 14:18:47 +0000699 if '__return__' in self.curframe.f_locals:
Georg Brandl19564802006-05-10 17:13:20 +0000700 print >>self.stdout, self.curframe.f_locals['__return__']
Tim Peters2344fae2001-01-15 00:50:52 +0000701 else:
Georg Brandl19564802006-05-10 17:13:20 +0000702 print >>self.stdout, '*** Not yet returned!'
Tim Peters2344fae2001-01-15 00:50:52 +0000703 do_rv = do_retval
Guido van Rossum2424f851998-09-11 22:50:09 +0000704
Barry Warsaw210bd202002-11-05 22:40:20 +0000705 def _getval(self, arg):
Tim Peters2344fae2001-01-15 00:50:52 +0000706 try:
Barry Warsaw210bd202002-11-05 22:40:20 +0000707 return eval(arg, self.curframe.f_globals,
708 self.curframe.f_locals)
Tim Peters2344fae2001-01-15 00:50:52 +0000709 except:
710 t, v = sys.exc_info()[:2]
Barry Warsaw210bd202002-11-05 22:40:20 +0000711 if isinstance(t, str):
Tim Peters2344fae2001-01-15 00:50:52 +0000712 exc_type_name = t
713 else: exc_type_name = t.__name__
Georg Brandl19564802006-05-10 17:13:20 +0000714 print >>self.stdout, '***', exc_type_name + ':', repr(v)
Barry Warsaw210bd202002-11-05 22:40:20 +0000715 raise
Guido van Rossum2424f851998-09-11 22:50:09 +0000716
Barry Warsaw210bd202002-11-05 22:40:20 +0000717 def do_p(self, arg):
718 try:
Georg Brandl19564802006-05-10 17:13:20 +0000719 print >>self.stdout, repr(self._getval(arg))
Barry Warsaw210bd202002-11-05 22:40:20 +0000720 except:
721 pass
722
723 def do_pp(self, arg):
724 try:
Georg Brandl19564802006-05-10 17:13:20 +0000725 pprint.pprint(self._getval(arg), self.stdout)
Barry Warsaw210bd202002-11-05 22:40:20 +0000726 except:
727 pass
Guido van Rossum2424f851998-09-11 22:50:09 +0000728
Tim Peters2344fae2001-01-15 00:50:52 +0000729 def do_list(self, arg):
730 self.lastcmd = 'list'
731 last = None
732 if arg:
733 try:
734 x = eval(arg, {}, {})
735 if type(x) == type(()):
736 first, last = x
737 first = int(first)
738 last = int(last)
739 if last < first:
740 # Assume it's a count
741 last = first + last
742 else:
743 first = max(1, int(x) - 5)
744 except:
Georg Brandl19564802006-05-10 17:13:20 +0000745 print >>self.stdout, '*** Error in argument:', repr(arg)
Tim Peters2344fae2001-01-15 00:50:52 +0000746 return
747 elif self.lineno is None:
748 first = max(1, self.curframe.f_lineno - 5)
749 else:
750 first = self.lineno + 1
751 if last is None:
752 last = first + 10
753 filename = self.curframe.f_code.co_filename
754 breaklist = self.get_file_breaks(filename)
755 try:
756 for lineno in range(first, last+1):
757 line = linecache.getline(filename, lineno)
758 if not line:
Georg Brandl19564802006-05-10 17:13:20 +0000759 print >>self.stdout, '[EOF]'
Tim Peters2344fae2001-01-15 00:50:52 +0000760 break
761 else:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000762 s = repr(lineno).rjust(3)
Tim Peters2344fae2001-01-15 00:50:52 +0000763 if len(s) < 4: s = s + ' '
764 if lineno in breaklist: s = s + 'B'
765 else: s = s + ' '
766 if lineno == self.curframe.f_lineno:
767 s = s + '->'
Georg Brandl19564802006-05-10 17:13:20 +0000768 print >>self.stdout, s + '\t' + line,
Tim Peters2344fae2001-01-15 00:50:52 +0000769 self.lineno = lineno
770 except KeyboardInterrupt:
771 pass
772 do_l = do_list
Guido van Rossum2424f851998-09-11 22:50:09 +0000773
Tim Peters2344fae2001-01-15 00:50:52 +0000774 def do_whatis(self, arg):
775 try:
776 value = eval(arg, self.curframe.f_globals,
777 self.curframe.f_locals)
778 except:
779 t, v = sys.exc_info()[:2]
780 if type(t) == type(''):
781 exc_type_name = t
782 else: exc_type_name = t.__name__
Georg Brandl19564802006-05-10 17:13:20 +0000783 print >>self.stdout, '***', exc_type_name + ':', repr(v)
Tim Peters2344fae2001-01-15 00:50:52 +0000784 return
785 code = None
786 # Is it a function?
787 try: code = value.func_code
788 except: pass
789 if code:
Georg Brandl19564802006-05-10 17:13:20 +0000790 print >>self.stdout, 'Function', code.co_name
Tim Peters2344fae2001-01-15 00:50:52 +0000791 return
792 # Is it an instance method?
793 try: code = value.im_func.func_code
794 except: pass
795 if code:
Georg Brandl19564802006-05-10 17:13:20 +0000796 print >>self.stdout, 'Method', code.co_name
Tim Peters2344fae2001-01-15 00:50:52 +0000797 return
798 # None of the above...
Georg Brandl19564802006-05-10 17:13:20 +0000799 print >>self.stdout, type(value)
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000800
Tim Peters2344fae2001-01-15 00:50:52 +0000801 def do_alias(self, arg):
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000802 args = arg.split()
Tim Peters2344fae2001-01-15 00:50:52 +0000803 if len(args) == 0:
804 keys = self.aliases.keys()
805 keys.sort()
806 for alias in keys:
Georg Brandl19564802006-05-10 17:13:20 +0000807 print >>self.stdout, "%s = %s" % (alias, self.aliases[alias])
Tim Peters2344fae2001-01-15 00:50:52 +0000808 return
Guido van Rossum08454592002-07-12 13:10:53 +0000809 if args[0] in self.aliases and len(args) == 1:
Georg Brandl19564802006-05-10 17:13:20 +0000810 print >>self.stdout, "%s = %s" % (args[0], self.aliases[args[0]])
Tim Peters2344fae2001-01-15 00:50:52 +0000811 else:
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000812 self.aliases[args[0]] = ' '.join(args[1:])
Guido van Rossum23efba41992-01-27 16:58:47 +0000813
Tim Peters2344fae2001-01-15 00:50:52 +0000814 def do_unalias(self, arg):
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000815 args = arg.split()
Tim Peters2344fae2001-01-15 00:50:52 +0000816 if len(args) == 0: return
Raymond Hettinger54f02222002-06-01 14:18:47 +0000817 if args[0] in self.aliases:
Tim Peters2344fae2001-01-15 00:50:52 +0000818 del self.aliases[args[0]]
Guido van Rossum00230781993-03-29 11:39:45 +0000819
Martin v. Löwisbd30f522006-04-17 17:08:37 +0000820 #list of all the commands making the program resume execution.
Georg Brandl19564802006-05-10 17:13:20 +0000821 commands_resuming = ['do_continue', 'do_step', 'do_next', 'do_return',
822 'do_quit', 'do_jump']
Martin v. Löwisbd30f522006-04-17 17:08:37 +0000823
Tim Peters2344fae2001-01-15 00:50:52 +0000824 # Print a traceback starting at the top stack frame.
825 # The most recently entered frame is printed last;
826 # this is different from dbx and gdb, but consistent with
827 # the Python interpreter's stack trace.
828 # It is also consistent with the up/down commands (which are
829 # compatible with dbx and gdb: up moves towards 'main()'
830 # and down moves towards the most recent stack frame).
Guido van Rossum2424f851998-09-11 22:50:09 +0000831
Tim Peters2344fae2001-01-15 00:50:52 +0000832 def print_stack_trace(self):
833 try:
834 for frame_lineno in self.stack:
835 self.print_stack_entry(frame_lineno)
836 except KeyboardInterrupt:
837 pass
Guido van Rossum2424f851998-09-11 22:50:09 +0000838
Tim Peters2344fae2001-01-15 00:50:52 +0000839 def print_stack_entry(self, frame_lineno, prompt_prefix=line_prefix):
840 frame, lineno = frame_lineno
841 if frame is self.curframe:
Georg Brandl19564802006-05-10 17:13:20 +0000842 print >>self.stdout, '>',
Tim Peters2344fae2001-01-15 00:50:52 +0000843 else:
Georg Brandl19564802006-05-10 17:13:20 +0000844 print >>self.stdout, ' ',
845 print >>self.stdout, self.format_stack_entry(frame_lineno,
846 prompt_prefix)
Guido van Rossum2424f851998-09-11 22:50:09 +0000847
Guido van Rossum921c8241992-01-10 14:54:42 +0000848
Tim Peters2344fae2001-01-15 00:50:52 +0000849 # Help methods (derived from pdb.doc)
Guido van Rossum921c8241992-01-10 14:54:42 +0000850
Tim Peters2344fae2001-01-15 00:50:52 +0000851 def help_help(self):
852 self.help_h()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000853
Tim Peters2344fae2001-01-15 00:50:52 +0000854 def help_h(self):
Georg Brandl19564802006-05-10 17:13:20 +0000855 print >>self.stdout, """h(elp)
Tim Peters2344fae2001-01-15 00:50:52 +0000856Without argument, print the list of available commands.
857With a command name as argument, print help about that command
858"help pdb" pipes the full documentation file to the $PAGER
859"help exec" gives help on the ! command"""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000860
Tim Peters2344fae2001-01-15 00:50:52 +0000861 def help_where(self):
862 self.help_w()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000863
Tim Peters2344fae2001-01-15 00:50:52 +0000864 def help_w(self):
Georg Brandl19564802006-05-10 17:13:20 +0000865 print >>self.stdout, """w(here)
Tim Peters2344fae2001-01-15 00:50:52 +0000866Print a stack trace, with the most recent frame at the bottom.
867An arrow indicates the "current frame", which determines the
Guido van Rossum6bd68352001-01-20 17:57:37 +0000868context of most commands. 'bt' is an alias for this command."""
869
870 help_bt = help_w
Guido van Rossumb6775db1994-08-01 11:34:53 +0000871
Tim Peters2344fae2001-01-15 00:50:52 +0000872 def help_down(self):
873 self.help_d()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000874
Tim Peters2344fae2001-01-15 00:50:52 +0000875 def help_d(self):
Georg Brandl19564802006-05-10 17:13:20 +0000876 print >>self.stdout, """d(own)
Tim Peters2344fae2001-01-15 00:50:52 +0000877Move the current frame one level down in the stack trace
Johannes Gijsbers34c41202004-08-14 15:19:28 +0000878(to a newer frame)."""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000879
Tim Peters2344fae2001-01-15 00:50:52 +0000880 def help_up(self):
881 self.help_u()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000882
Tim Peters2344fae2001-01-15 00:50:52 +0000883 def help_u(self):
Georg Brandl19564802006-05-10 17:13:20 +0000884 print >>self.stdout, """u(p)
Tim Peters2344fae2001-01-15 00:50:52 +0000885Move the current frame one level up in the stack trace
Johannes Gijsbers34c41202004-08-14 15:19:28 +0000886(to an older frame)."""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000887
Tim Peters2344fae2001-01-15 00:50:52 +0000888 def help_break(self):
889 self.help_b()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000890
Tim Peters2344fae2001-01-15 00:50:52 +0000891 def help_b(self):
Georg Brandl19564802006-05-10 17:13:20 +0000892 print >>self.stdout, """b(reak) ([file:]lineno | function) [, condition]
Tim Peters2344fae2001-01-15 00:50:52 +0000893With a line number argument, set a break there in the current
894file. With a function name, set a break at first executable line
895of that function. Without argument, list all breaks. If a second
896argument is present, it is a string specifying an expression
897which must evaluate to true before the breakpoint is honored.
Guido van Rossumb6775db1994-08-01 11:34:53 +0000898
Tim Peters2344fae2001-01-15 00:50:52 +0000899The line number may be prefixed with a filename and a colon,
900to specify a breakpoint in another file (probably one that
901hasn't been loaded yet). The file is searched for on sys.path;
902the .py suffix may be omitted."""
Guido van Rossumb5699c71998-07-20 23:13:54 +0000903
Tim Peters2344fae2001-01-15 00:50:52 +0000904 def help_clear(self):
905 self.help_cl()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000906
Tim Peters2344fae2001-01-15 00:50:52 +0000907 def help_cl(self):
Georg Brandl19564802006-05-10 17:13:20 +0000908 print >>self.stdout, "cl(ear) filename:lineno"
909 print >>self.stdout, """cl(ear) [bpnumber [bpnumber...]]
Tim Peters2344fae2001-01-15 00:50:52 +0000910With a space separated list of breakpoint numbers, clear
911those breakpoints. Without argument, clear all breaks (but
912first ask confirmation). With a filename:lineno argument,
913clear all breaks at that line in that file.
Guido van Rossumb6775db1994-08-01 11:34:53 +0000914
Tim Peters2344fae2001-01-15 00:50:52 +0000915Note that the argument is different from previous versions of
916the debugger (in python distributions 1.5.1 and before) where
917a linenumber was used instead of either filename:lineno or
918breakpoint numbers."""
Guido van Rossumb5699c71998-07-20 23:13:54 +0000919
Tim Peters2344fae2001-01-15 00:50:52 +0000920 def help_tbreak(self):
Georg Brandl19564802006-05-10 17:13:20 +0000921 print >>self.stdout, """tbreak same arguments as break, but breakpoint is
Tim Peters2344fae2001-01-15 00:50:52 +0000922removed when first hit."""
Guido van Rossum2424f851998-09-11 22:50:09 +0000923
Tim Peters2344fae2001-01-15 00:50:52 +0000924 def help_enable(self):
Georg Brandl19564802006-05-10 17:13:20 +0000925 print >>self.stdout, """enable bpnumber [bpnumber ...]
Tim Peters2344fae2001-01-15 00:50:52 +0000926Enables the breakpoints given as a space separated list of
927bp numbers."""
Guido van Rossum2424f851998-09-11 22:50:09 +0000928
Tim Peters2344fae2001-01-15 00:50:52 +0000929 def help_disable(self):
Georg Brandl19564802006-05-10 17:13:20 +0000930 print >>self.stdout, """disable bpnumber [bpnumber ...]
Tim Peters2344fae2001-01-15 00:50:52 +0000931Disables the breakpoints given as a space separated list of
932bp numbers."""
Guido van Rossum2424f851998-09-11 22:50:09 +0000933
Tim Peters2344fae2001-01-15 00:50:52 +0000934 def help_ignore(self):
Georg Brandl19564802006-05-10 17:13:20 +0000935 print >>self.stdout, """ignore bpnumber count
Tim Peters2344fae2001-01-15 00:50:52 +0000936Sets the ignore count for the given breakpoint number. A breakpoint
937becomes active when the ignore count is zero. When non-zero, the
938count is decremented each time the breakpoint is reached and the
939breakpoint is not disabled and any associated condition evaluates
940to true."""
Guido van Rossum2424f851998-09-11 22:50:09 +0000941
Tim Peters2344fae2001-01-15 00:50:52 +0000942 def help_condition(self):
Georg Brandl19564802006-05-10 17:13:20 +0000943 print >>self.stdout, """condition bpnumber str_condition
Tim Peters2344fae2001-01-15 00:50:52 +0000944str_condition is a string specifying an expression which
945must evaluate to true before the breakpoint is honored.
946If str_condition is absent, any existing condition is removed;
947i.e., the breakpoint is made unconditional."""
Guido van Rossum2424f851998-09-11 22:50:09 +0000948
Tim Peters2344fae2001-01-15 00:50:52 +0000949 def help_step(self):
950 self.help_s()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000951
Tim Peters2344fae2001-01-15 00:50:52 +0000952 def help_s(self):
Georg Brandl19564802006-05-10 17:13:20 +0000953 print >>self.stdout, """s(tep)
Tim Peters2344fae2001-01-15 00:50:52 +0000954Execute the current line, stop at the first possible occasion
955(either in a function that is called or in the current function)."""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000956
Tim Peters2344fae2001-01-15 00:50:52 +0000957 def help_next(self):
958 self.help_n()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000959
Tim Peters2344fae2001-01-15 00:50:52 +0000960 def help_n(self):
Georg Brandl19564802006-05-10 17:13:20 +0000961 print >>self.stdout, """n(ext)
Tim Peters2344fae2001-01-15 00:50:52 +0000962Continue execution until the next line in the current function
963is reached or it returns."""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000964
Tim Peters2344fae2001-01-15 00:50:52 +0000965 def help_return(self):
966 self.help_r()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000967
Tim Peters2344fae2001-01-15 00:50:52 +0000968 def help_r(self):
Georg Brandl19564802006-05-10 17:13:20 +0000969 print >>self.stdout, """r(eturn)
Tim Peters2344fae2001-01-15 00:50:52 +0000970Continue execution until the current function returns."""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000971
Tim Peters2344fae2001-01-15 00:50:52 +0000972 def help_continue(self):
973 self.help_c()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000974
Tim Peters2344fae2001-01-15 00:50:52 +0000975 def help_cont(self):
976 self.help_c()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000977
Tim Peters2344fae2001-01-15 00:50:52 +0000978 def help_c(self):
Georg Brandl19564802006-05-10 17:13:20 +0000979 print >>self.stdout, """c(ont(inue))
Tim Peters2344fae2001-01-15 00:50:52 +0000980Continue execution, only stop when a breakpoint is encountered."""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000981
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000982 def help_jump(self):
983 self.help_j()
984
985 def help_j(self):
Georg Brandl19564802006-05-10 17:13:20 +0000986 print >>self.stdout, """j(ump) lineno
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000987Set the next line that will be executed."""
988
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000989 def help_debug(self):
Georg Brandl19564802006-05-10 17:13:20 +0000990 print >>self.stdout, """debug code
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000991Enter a recursive debugger that steps through the code argument
992(which is an arbitrary expression or statement to be executed
993in the current environment)."""
994
Tim Peters2344fae2001-01-15 00:50:52 +0000995 def help_list(self):
996 self.help_l()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000997
Tim Peters2344fae2001-01-15 00:50:52 +0000998 def help_l(self):
Georg Brandl19564802006-05-10 17:13:20 +0000999 print >>self.stdout, """l(ist) [first [,last]]
Tim Peters2344fae2001-01-15 00:50:52 +00001000List source code for the current file.
1001Without arguments, list 11 lines around the current line
1002or continue the previous listing.
1003With one argument, list 11 lines starting at that line.
1004With two arguments, list the given range;
1005if the second argument is less than the first, it is a count."""
Guido van Rossumb6775db1994-08-01 11:34:53 +00001006
Tim Peters2344fae2001-01-15 00:50:52 +00001007 def help_args(self):
1008 self.help_a()
Guido van Rossumb6775db1994-08-01 11:34:53 +00001009
Tim Peters2344fae2001-01-15 00:50:52 +00001010 def help_a(self):
Georg Brandl19564802006-05-10 17:13:20 +00001011 print >>self.stdout, """a(rgs)
Tim Peters2344fae2001-01-15 00:50:52 +00001012Print the arguments of the current function."""
Guido van Rossumb6775db1994-08-01 11:34:53 +00001013
Tim Peters2344fae2001-01-15 00:50:52 +00001014 def help_p(self):
Georg Brandl19564802006-05-10 17:13:20 +00001015 print >>self.stdout, """p expression
Tim Peters2344fae2001-01-15 00:50:52 +00001016Print the value of the expression."""
Guido van Rossumb6775db1994-08-01 11:34:53 +00001017
Barry Warsaw210bd202002-11-05 22:40:20 +00001018 def help_pp(self):
Georg Brandl19564802006-05-10 17:13:20 +00001019 print >>self.stdout, """pp expression
Barry Warsaw210bd202002-11-05 22:40:20 +00001020Pretty-print the value of the expression."""
1021
Tim Peters2344fae2001-01-15 00:50:52 +00001022 def help_exec(self):
Georg Brandl19564802006-05-10 17:13:20 +00001023 print >>self.stdout, """(!) statement
Tim Peters2344fae2001-01-15 00:50:52 +00001024Execute the (one-line) statement in the context of
1025the current stack frame.
1026The exclamation point can be omitted unless the first word
1027of the statement resembles a debugger command.
1028To assign to a global variable you must always prefix the
1029command with a 'global' command, e.g.:
1030(Pdb) global list_options; list_options = ['-l']
1031(Pdb)"""
Guido van Rossumb6775db1994-08-01 11:34:53 +00001032
Georg Brandl8e84c652007-03-13 21:08:15 +00001033 def help_run(self):
1034 print """run [args...]
1035Restart the debugged python program. If a string is supplied, it is
1036splitted with "shlex" and the result is used as the new sys.argv.
1037History, breakpoints, actions and debugger options are preserved.
1038"restart" is an alias for "run"."""
1039
1040 help_restart = help_run
1041
Tim Peters2344fae2001-01-15 00:50:52 +00001042 def help_quit(self):
1043 self.help_q()
Guido van Rossumb6775db1994-08-01 11:34:53 +00001044
Tim Peters2344fae2001-01-15 00:50:52 +00001045 def help_q(self):
Georg Brandl19564802006-05-10 17:13:20 +00001046 print >>self.stdout, """q(uit) or exit - Quit from the debugger.
Tim Peters2344fae2001-01-15 00:50:52 +00001047The program being executed is aborted."""
Guido van Rossumb6775db1994-08-01 11:34:53 +00001048
Guido van Rossumd1c08f32002-04-15 00:48:24 +00001049 help_exit = help_q
1050
Tim Peters2344fae2001-01-15 00:50:52 +00001051 def help_whatis(self):
Georg Brandl19564802006-05-10 17:13:20 +00001052 print >>self.stdout, """whatis arg
Tim Peters2344fae2001-01-15 00:50:52 +00001053Prints the type of the argument."""
Guido van Rossumb6775db1994-08-01 11:34:53 +00001054
Tim Peters2344fae2001-01-15 00:50:52 +00001055 def help_EOF(self):
Georg Brandl19564802006-05-10 17:13:20 +00001056 print >>self.stdout, """EOF
Tim Peters2344fae2001-01-15 00:50:52 +00001057Handles the receipt of EOF as a command."""
Guido van Rossum2424f851998-09-11 22:50:09 +00001058
Tim Peters2344fae2001-01-15 00:50:52 +00001059 def help_alias(self):
Georg Brandl19564802006-05-10 17:13:20 +00001060 print >>self.stdout, """alias [name [command [parameter parameter ...] ]]
Tim Peters2344fae2001-01-15 00:50:52 +00001061Creates an alias called 'name' the executes 'command'. The command
1062must *not* be enclosed in quotes. Replaceable parameters are
1063indicated by %1, %2, and so on, while %* is replaced by all the
1064parameters. If no command is given, the current alias for name
1065is shown. If no name is given, all aliases are listed.
Guido van Rossum2424f851998-09-11 22:50:09 +00001066
Tim Peters2344fae2001-01-15 00:50:52 +00001067Aliases may be nested and can contain anything that can be
1068legally typed at the pdb prompt. Note! You *can* override
1069internal pdb commands with aliases! Those internal commands
1070are then hidden until the alias is removed. Aliasing is recursively
1071applied to the first word of the command line; all other words
1072in the line are left alone.
Guido van Rossum2424f851998-09-11 22:50:09 +00001073
Tim Peters2344fae2001-01-15 00:50:52 +00001074Some useful aliases (especially when placed in the .pdbrc file) are:
Guido van Rossum2424f851998-09-11 22:50:09 +00001075
Tim Peters2344fae2001-01-15 00:50:52 +00001076#Print instance variables (usage "pi classInst")
1077alias pi for k in %1.__dict__.keys(): print "%1.",k,"=",%1.__dict__[k]
Guido van Rossum2424f851998-09-11 22:50:09 +00001078
Tim Peters2344fae2001-01-15 00:50:52 +00001079#Print instance variables in self
1080alias ps pi self
1081"""
Guido van Rossum2424f851998-09-11 22:50:09 +00001082
Tim Peters2344fae2001-01-15 00:50:52 +00001083 def help_unalias(self):
Georg Brandl19564802006-05-10 17:13:20 +00001084 print >>self.stdout, """unalias name
Tim Peters2344fae2001-01-15 00:50:52 +00001085Deletes the specified alias."""
Guido van Rossum2424f851998-09-11 22:50:09 +00001086
Martin v. Löwisbd30f522006-04-17 17:08:37 +00001087 def help_commands(self):
Georg Brandl19564802006-05-10 17:13:20 +00001088 print >>self.stdout, """commands [bpnumber]
Martin v. Löwisbd30f522006-04-17 17:08:37 +00001089(com) ...
1090(com) end
1091(Pdb)
1092
1093Specify a list of commands for breakpoint number bpnumber. The
1094commands themselves appear on the following lines. Type a line
1095containing just 'end' to terminate the commands.
1096
1097To remove all commands from a breakpoint, type commands and
1098follow it immediately with end; that is, give no commands.
1099
1100With no bpnumber argument, commands refers to the last
1101breakpoint set.
1102
1103You can use breakpoint commands to start your program up again.
1104Simply use the continue command, or step, or any other
1105command that resumes execution.
1106
1107Specifying any command resuming execution (currently continue,
1108step, next, return, jump, quit and their abbreviations) terminates
1109the command list (as if that command was immediately followed by end).
1110This is because any time you resume execution
Martin v. Löwisf62eee12006-04-17 17:37:09 +00001111(even with a simple next or step), you may encounter
Martin v. Löwisbd30f522006-04-17 17:08:37 +00001112another breakpoint--which could have its own command list, leading to
1113ambiguities about which list to execute.
1114
1115 If you use the 'silent' command in the command list, the
1116usual message about stopping at a breakpoint is not printed. This may
1117be desirable for breakpoints that are to print a specific message and
1118then continue. If none of the other commands print anything, you
1119see no sign that the breakpoint was reached.
1120"""
1121
Tim Peters2344fae2001-01-15 00:50:52 +00001122 def help_pdb(self):
1123 help()
Guido van Rossumb6775db1994-08-01 11:34:53 +00001124
Tim Peters2344fae2001-01-15 00:50:52 +00001125 def lookupmodule(self, filename):
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001126 """Helper function for break/clear parsing -- may be overridden.
1127
1128 lookupmodule() translates (possibly incomplete) file or module name
1129 into an absolute file name.
1130 """
1131 if os.path.isabs(filename) and os.path.exists(filename):
Tim Peterse718f612004-10-12 21:51:32 +00001132 return filename
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001133 f = os.path.join(sys.path[0], filename)
1134 if os.path.exists(f) and self.canonic(f) == self.mainpyfile:
1135 return f
Tim Peters2344fae2001-01-15 00:50:52 +00001136 root, ext = os.path.splitext(filename)
1137 if ext == '':
1138 filename = filename + '.py'
1139 if os.path.isabs(filename):
1140 return filename
1141 for dirname in sys.path:
1142 while os.path.islink(dirname):
1143 dirname = os.readlink(dirname)
1144 fullname = os.path.join(dirname, filename)
1145 if os.path.exists(fullname):
1146 return fullname
1147 return None
Guido van Rossumb5699c71998-07-20 23:13:54 +00001148
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001149 def _runscript(self, filename):
Georg Brandlb6ae6aa2007-03-13 21:58:44 +00001150 # The script has to run in __main__ namespace (or imports from
1151 # __main__ will break).
Neal Norwitz0d4c06e2007-04-25 06:30:05 +00001152 #
Georg Brandlb6ae6aa2007-03-13 21:58:44 +00001153 # So we clear up the __main__ and set several special variables
1154 # (this gets rid of pdb's globals and cleans old variables on restarts).
1155 import __main__
1156 __main__.__dict__.clear()
1157 __main__.__dict__.update({"__name__" : "__main__",
1158 "__file__" : filename,
1159 "__builtins__": __builtins__,
1160 })
Neal Norwitz0d4c06e2007-04-25 06:30:05 +00001161
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001162 # When bdb sets tracing, a number of call and line events happens
1163 # BEFORE debugger even reaches user's code (and the exact sequence of
1164 # events depends on python version). So we take special measures to
1165 # avoid stopping before we reach the main script (see user_line and
1166 # user_call for details).
1167 self._wait_for_mainpyfile = 1
1168 self.mainpyfile = self.canonic(filename)
1169 self._user_requested_quit = 0
1170 statement = 'execfile( "%s")' % filename
Neal Norwitz0d4c06e2007-04-25 06:30:05 +00001171 self.run(statement)
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001172
Guido van Rossum35771131992-09-08 11:59:04 +00001173# Simplified interface
1174
Guido van Rossum5e38b6f1995-02-27 13:13:40 +00001175def run(statement, globals=None, locals=None):
Tim Peters2344fae2001-01-15 00:50:52 +00001176 Pdb().run(statement, globals, locals)
Guido van Rossum5e38b6f1995-02-27 13:13:40 +00001177
1178def runeval(expression, globals=None, locals=None):
Tim Peters2344fae2001-01-15 00:50:52 +00001179 return Pdb().runeval(expression, globals, locals)
Guido van Rossum6fe08b01992-01-16 13:50:21 +00001180
1181def runctx(statement, globals, locals):
Tim Peters2344fae2001-01-15 00:50:52 +00001182 # B/W compatibility
1183 run(statement, globals, locals)
Guido van Rossum6fe08b01992-01-16 13:50:21 +00001184
Raymond Hettinger2ef7e6c2004-10-24 00:32:24 +00001185def runcall(*args, **kwds):
1186 return Pdb().runcall(*args, **kwds)
Guido van Rossum4e160981992-09-02 20:43:20 +00001187
Guido van Rossumb6775db1994-08-01 11:34:53 +00001188def set_trace():
Johannes Gijsbers84a6c202004-11-07 11:35:30 +00001189 Pdb().set_trace(sys._getframe().f_back)
Guido van Rossum35771131992-09-08 11:59:04 +00001190
1191# Post-Mortem interface
1192
1193def post_mortem(t):
Tim Peters2344fae2001-01-15 00:50:52 +00001194 p = Pdb()
1195 p.reset()
1196 while t.tb_next is not None:
1197 t = t.tb_next
1198 p.interaction(t.tb_frame, t)
Guido van Rossum35771131992-09-08 11:59:04 +00001199
1200def pm():
Tim Peters2344fae2001-01-15 00:50:52 +00001201 post_mortem(sys.last_traceback)
Guido van Rossum35771131992-09-08 11:59:04 +00001202
1203
1204# Main program for testing
1205
Guido van Rossum23efba41992-01-27 16:58:47 +00001206TESTCMD = 'import x; x.main()'
Guido van Rossum6fe08b01992-01-16 13:50:21 +00001207
Guido van Rossum921c8241992-01-10 14:54:42 +00001208def test():
Tim Peters2344fae2001-01-15 00:50:52 +00001209 run(TESTCMD)
Guido van Rossume61fa0a1993-10-22 13:56:35 +00001210
1211# print help
1212def help():
Tim Peters2344fae2001-01-15 00:50:52 +00001213 for dirname in sys.path:
1214 fullname = os.path.join(dirname, 'pdb.doc')
1215 if os.path.exists(fullname):
1216 sts = os.system('${PAGER-more} '+fullname)
1217 if sts: print '*** Pager exit status:', sts
1218 break
1219 else:
1220 print 'Sorry, can\'t find the help file "pdb.doc"',
1221 print 'along the Python search path'
Guido van Rossumf17361d1996-07-30 16:28:13 +00001222
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001223def main():
Tim Peters2344fae2001-01-15 00:50:52 +00001224 if not sys.argv[1:]:
1225 print "usage: pdb.py scriptfile [arg] ..."
1226 sys.exit(2)
Guido van Rossumf17361d1996-07-30 16:28:13 +00001227
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001228 mainpyfile = sys.argv[1] # Get script filename
1229 if not os.path.exists(mainpyfile):
1230 print 'Error:', mainpyfile, 'does not exist'
Tim Peters2344fae2001-01-15 00:50:52 +00001231 sys.exit(1)
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001232
Tim Peters2344fae2001-01-15 00:50:52 +00001233 del sys.argv[0] # Hide "pdb.py" from argument list
Guido van Rossumec577d51996-09-10 17:39:34 +00001234
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001235 # Replace pdb's dir with script's dir in front of module search path.
1236 sys.path[0] = os.path.dirname(mainpyfile)
Guido van Rossumf17361d1996-07-30 16:28:13 +00001237
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001238 # Note on saving/restoring sys.argv: it's a good idea when sys.argv was
1239 # modified by the script being debugged. It's a bad idea when it was
Georg Brandl8e84c652007-03-13 21:08:15 +00001240 # changed by the user from the command line. There is a "restart" command which
1241 # allows explicit specification of command line arguments.
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001242 pdb = Pdb()
1243 while 1:
1244 try:
1245 pdb._runscript(mainpyfile)
1246 if pdb._user_requested_quit:
1247 break
Tim Peterse718f612004-10-12 21:51:32 +00001248 print "The program finished and will be restarted"
Georg Brandl8e84c652007-03-13 21:08:15 +00001249 except Restart:
1250 print "Restarting", mainpyfile, "with arguments:"
1251 print "\t" + " ".join(sys.argv[1:])
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001252 except SystemExit:
1253 # In most cases SystemExit does not warrant a post-mortem session.
1254 print "The program exited via sys.exit(). Exit status: ",
1255 print sys.exc_info()[1]
1256 except:
1257 traceback.print_exc()
1258 print "Uncaught exception. Entering post mortem debugging"
1259 print "Running 'cont' or 'step' will restart the program"
1260 t = sys.exc_info()[2]
1261 while t.tb_next is not None:
1262 t = t.tb_next
1263 pdb.interaction(t.tb_frame,t)
1264 print "Post mortem debugger finished. The "+mainpyfile+" will be restarted"
1265
1266
1267# When invoked as main program, invoke the debugger on a script
Georg Brandlb6ae6aa2007-03-13 21:58:44 +00001268if __name__ == '__main__':
1269 import pdb
1270 pdb.main()