blob: 4eba7bbdd81c6cbc37ab5cbc026e7e9c7925a63f [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
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
Neal Norwitzce96f692006-03-17 06:49:51 +000025def raw_input(prompt):
26 sys.stdout.write(prompt)
27 sys.stdout.flush()
28 return sys.stdin.readline()
29
Barry Warsaw2bee8fe1999-09-09 16:32:41 +000030def find_function(funcname, filename):
Thomas Wouters89f507f2006-12-13 04:49:30 +000031 cre = re.compile(r'def\s+%s\s*[(]' % re.escape(funcname))
Tim Peters2344fae2001-01-15 00:50:52 +000032 try:
33 fp = open(filename)
34 except IOError:
35 return None
36 # consumer of this info expects the first line to be 1
37 lineno = 1
38 answer = None
39 while 1:
40 line = fp.readline()
41 if line == '':
42 break
43 if cre.match(line):
44 answer = funcname, filename, lineno
45 break
46 lineno = lineno + 1
47 fp.close()
48 return answer
Guido van Rossum921c8241992-01-10 14:54:42 +000049
50
Guido van Rossuma558e371994-11-10 22:27:35 +000051# Interaction prompt line will separate file and call info from code
52# text using value of line_prefix string. A newline and arrow may
53# be to your liking. You can set it once pdb is imported using the
54# command "pdb.line_prefix = '\n% '".
Tim Peters2344fae2001-01-15 00:50:52 +000055# line_prefix = ': ' # Use this to get the old situation back
56line_prefix = '\n-> ' # Probably a better default
Guido van Rossuma558e371994-11-10 22:27:35 +000057
Guido van Rossum23efba41992-01-27 16:58:47 +000058class Pdb(bdb.Bdb, cmd.Cmd):
Guido van Rossum2424f851998-09-11 22:50:09 +000059
Thomas Wouters477c8d52006-05-27 19:21:47 +000060 def __init__(self, completekey='tab', stdin=None, stdout=None):
Tim Peters2344fae2001-01-15 00:50:52 +000061 bdb.Bdb.__init__(self)
Thomas Wouters477c8d52006-05-27 19:21:47 +000062 cmd.Cmd.__init__(self, completekey, stdin, stdout)
63 if stdout:
64 self.use_rawinput = 0
Tim Peters2344fae2001-01-15 00:50:52 +000065 self.prompt = '(Pdb) '
66 self.aliases = {}
Johannes Gijsbers25b38c82004-10-12 18:12:09 +000067 self.mainpyfile = ''
68 self._wait_for_mainpyfile = 0
Tim Peters2344fae2001-01-15 00:50:52 +000069 # Try to load readline if it exists
70 try:
71 import readline
72 except ImportError:
73 pass
Guido van Rossum2424f851998-09-11 22:50:09 +000074
Tim Peters2344fae2001-01-15 00:50:52 +000075 # Read $HOME/.pdbrc and ./.pdbrc
76 self.rcLines = []
Raymond Hettinger54f02222002-06-01 14:18:47 +000077 if 'HOME' in os.environ:
Tim Peters2344fae2001-01-15 00:50:52 +000078 envHome = os.environ['HOME']
79 try:
80 rcFile = open(os.path.join(envHome, ".pdbrc"))
81 except IOError:
82 pass
83 else:
84 for line in rcFile.readlines():
85 self.rcLines.append(line)
86 rcFile.close()
87 try:
88 rcFile = open(".pdbrc")
89 except IOError:
90 pass
91 else:
92 for line in rcFile.readlines():
93 self.rcLines.append(line)
94 rcFile.close()
Guido van Rossum23efba41992-01-27 16:58:47 +000095
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000096 self.commands = {} # associates a command list to breakpoint numbers
97 self.commands_doprompt = {} # for each bp num, tells if the prompt must be disp. after execing the cmd list
98 self.commands_silent = {} # for each bp num, tells if the stack trace must be disp. after execing the cmd list
99 self.commands_defining = False # True while in the process of defining a command list
100 self.commands_bnum = None # The breakpoint number for which we are defining a list
101
Tim Peters2344fae2001-01-15 00:50:52 +0000102 def reset(self):
103 bdb.Bdb.reset(self)
104 self.forget()
Guido van Rossum23efba41992-01-27 16:58:47 +0000105
Tim Peters2344fae2001-01-15 00:50:52 +0000106 def forget(self):
107 self.lineno = None
108 self.stack = []
109 self.curindex = 0
110 self.curframe = None
Guido van Rossum2424f851998-09-11 22:50:09 +0000111
Tim Peters2344fae2001-01-15 00:50:52 +0000112 def setup(self, f, t):
113 self.forget()
114 self.stack, self.curindex = self.get_stack(f, t)
115 self.curframe = self.stack[self.curindex][0]
116 self.execRcLines()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000117
Tim Peters2344fae2001-01-15 00:50:52 +0000118 # Can be executed earlier than 'setup' if desired
119 def execRcLines(self):
120 if self.rcLines:
121 # Make local copy because of recursion
122 rcLines = self.rcLines
123 # executed only once
124 self.rcLines = []
125 for line in rcLines:
126 line = line[:-1]
Guido van Rossum08454592002-07-12 13:10:53 +0000127 if len(line) > 0 and line[0] != '#':
128 self.onecmd(line)
Guido van Rossum2424f851998-09-11 22:50:09 +0000129
Tim Peters280488b2002-08-23 18:19:30 +0000130 # Override Bdb methods
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000131
132 def user_call(self, frame, argument_list):
133 """This method is called when there is the remote possibility
134 that we ever need to stop in this function."""
Johannes Gijsbers25b38c82004-10-12 18:12:09 +0000135 if self._wait_for_mainpyfile:
136 return
Michael W. Hudson01eb85c2003-01-31 17:48:29 +0000137 if self.stop_here(frame):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000138 print('--Call--', file=self.stdout)
Michael W. Hudson01eb85c2003-01-31 17:48:29 +0000139 self.interaction(frame, None)
Guido van Rossum2424f851998-09-11 22:50:09 +0000140
Tim Peters2344fae2001-01-15 00:50:52 +0000141 def user_line(self, frame):
142 """This function is called when we stop or break at this line."""
Johannes Gijsbers25b38c82004-10-12 18:12:09 +0000143 if self._wait_for_mainpyfile:
144 if (self.mainpyfile != self.canonic(frame.f_code.co_filename)
145 or frame.f_lineno<= 0):
146 return
147 self._wait_for_mainpyfile = 0
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000148 if self.bp_commands(frame):
149 self.interaction(frame, None)
150
151 def bp_commands(self,frame):
152 """ Call every command that was set for the current active breakpoint (if there is one)
153 Returns True if the normal interaction function must be called, False otherwise """
154 #self.currentbp is set in bdb.py in bdb.break_here if a breakpoint was hit
155 if getattr(self,"currentbp",False) and self.currentbp in self.commands:
156 currentbp = self.currentbp
157 self.currentbp = 0
158 lastcmd_back = self.lastcmd
159 self.setup(frame, None)
160 for line in self.commands[currentbp]:
161 self.onecmd(line)
162 self.lastcmd = lastcmd_back
163 if not self.commands_silent[currentbp]:
164 self.print_stack_entry(self.stack[self.curindex])
165 if self.commands_doprompt[currentbp]:
166 self.cmdloop()
167 self.forget()
168 return
169 return 1
Guido van Rossum9e1ee971997-07-11 13:43:53 +0000170
Tim Peters2344fae2001-01-15 00:50:52 +0000171 def user_return(self, frame, return_value):
172 """This function is called when a return trap is set here."""
173 frame.f_locals['__return__'] = return_value
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000174 print('--Return--', file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000175 self.interaction(frame, None)
Guido van Rossum2424f851998-09-11 22:50:09 +0000176
Tim Peters2344fae2001-01-15 00:50:52 +0000177 def user_exception(self, frame, (exc_type, exc_value, exc_traceback)):
178 """This function is called if an exception occurs,
179 but only if we are to stop at or just below this level."""
180 frame.f_locals['__exception__'] = exc_type, exc_value
181 if type(exc_type) == type(''):
182 exc_type_name = exc_type
183 else: exc_type_name = exc_type.__name__
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000184 print(exc_type_name + ':', _saferepr(exc_value), file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000185 self.interaction(frame, exc_traceback)
Guido van Rossum2424f851998-09-11 22:50:09 +0000186
Tim Peters2344fae2001-01-15 00:50:52 +0000187 # General interaction function
188
189 def interaction(self, frame, traceback):
190 self.setup(frame, traceback)
191 self.print_stack_entry(self.stack[self.curindex])
192 self.cmdloop()
193 self.forget()
194
195 def default(self, line):
196 if line[:1] == '!': line = line[1:]
197 locals = self.curframe.f_locals
198 globals = self.curframe.f_globals
199 try:
200 code = compile(line + '\n', '<stdin>', 'single')
Georg Brandl7cae87c2006-09-06 06:51:57 +0000201 exec(code, globals, locals)
Tim Peters2344fae2001-01-15 00:50:52 +0000202 except:
203 t, v = sys.exc_info()[:2]
204 if type(t) == type(''):
205 exc_type_name = t
206 else: exc_type_name = t.__name__
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000207 print('***', exc_type_name + ':', v, file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000208
209 def precmd(self, line):
210 """Handle alias expansion and ';;' separator."""
Guido van Rossum08454592002-07-12 13:10:53 +0000211 if not line.strip():
Tim Peters2344fae2001-01-15 00:50:52 +0000212 return line
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000213 args = line.split()
Raymond Hettinger54f02222002-06-01 14:18:47 +0000214 while args[0] in self.aliases:
Tim Peters2344fae2001-01-15 00:50:52 +0000215 line = self.aliases[args[0]]
216 ii = 1
217 for tmpArg in args[1:]:
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000218 line = line.replace("%" + str(ii),
Tim Peters2344fae2001-01-15 00:50:52 +0000219 tmpArg)
220 ii = ii + 1
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000221 line = line.replace("%*", ' '.join(args[1:]))
222 args = line.split()
Tim Peters2344fae2001-01-15 00:50:52 +0000223 # split into ';;' separated commands
224 # unless it's an alias command
225 if args[0] != 'alias':
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000226 marker = line.find(';;')
Tim Peters2344fae2001-01-15 00:50:52 +0000227 if marker >= 0:
228 # queue up everything after marker
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000229 next = line[marker+2:].lstrip()
Tim Peters2344fae2001-01-15 00:50:52 +0000230 self.cmdqueue.append(next)
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000231 line = line[:marker].rstrip()
Tim Peters2344fae2001-01-15 00:50:52 +0000232 return line
233
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000234 def onecmd(self, line):
235 """Interpret the argument as though it had been typed in response
236 to the prompt.
237
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000238 Checks whether this line is typed at the normal prompt or in
239 a breakpoint command list definition.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000240 """
241 if not self.commands_defining:
242 return cmd.Cmd.onecmd(self, line)
243 else:
244 return self.handle_command_def(line)
245
246 def handle_command_def(self,line):
247 """ Handles one command line during command list definition. """
248 cmd, arg, line = self.parseline(line)
249 if cmd == 'silent':
250 self.commands_silent[self.commands_bnum] = True
251 return # continue to handle other cmd def in the cmd list
252 elif cmd == 'end':
253 self.cmdqueue = []
254 return 1 # end of cmd list
255 cmdlist = self.commands[self.commands_bnum]
256 if (arg):
257 cmdlist.append(cmd+' '+arg)
258 else:
259 cmdlist.append(cmd)
260 # Determine if we must stop
261 try:
262 func = getattr(self, 'do_' + cmd)
263 except AttributeError:
264 func = self.default
Neal Norwitz221085d2007-02-25 20:55:47 +0000265 if func.__name__ in self.commands_resuming : # one of the resuming commands.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000266 self.commands_doprompt[self.commands_bnum] = False
267 self.cmdqueue = []
268 return 1
269 return
270
Tim Peters2344fae2001-01-15 00:50:52 +0000271 # Command definitions, called by cmdloop()
272 # The argument is the remaining string on the command line
273 # Return true to exit from the command loop
274
275 do_h = cmd.Cmd.do_help
276
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000277 def do_commands(self, arg):
278 """Defines a list of commands associated to a breakpoint
279 Those commands will be executed whenever the breakpoint causes the program to stop execution."""
280 if not arg:
281 bnum = len(bdb.Breakpoint.bpbynumber)-1
282 else:
283 try:
284 bnum = int(arg)
285 except:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000286 print("Usage : commands [bnum]\n ...\n end", file=self.stdout)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000287 return
288 self.commands_bnum = bnum
289 self.commands[bnum] = []
290 self.commands_doprompt[bnum] = True
291 self.commands_silent[bnum] = False
292 prompt_back = self.prompt
293 self.prompt = '(com) '
294 self.commands_defining = True
295 self.cmdloop()
296 self.commands_defining = False
297 self.prompt = prompt_back
298
Tim Peters2344fae2001-01-15 00:50:52 +0000299 def do_break(self, arg, temporary = 0):
300 # break [ ([filename:]lineno | function) [, "condition"] ]
301 if not arg:
302 if self.breaks: # There's at least one
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000303 print("Num Type Disp Enb Where", file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000304 for bp in bdb.Breakpoint.bpbynumber:
305 if bp:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000306 bp.bpprint(self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000307 return
308 # parse arguments; comma has lowest precedence
309 # and cannot occur in filename
310 filename = None
311 lineno = None
312 cond = None
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000313 comma = arg.find(',')
Tim Peters2344fae2001-01-15 00:50:52 +0000314 if comma > 0:
315 # parse stuff after comma: "condition"
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000316 cond = arg[comma+1:].lstrip()
317 arg = arg[:comma].rstrip()
Tim Peters2344fae2001-01-15 00:50:52 +0000318 # parse stuff before comma: [filename:]lineno | function
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000319 colon = arg.rfind(':')
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000320 funcname = None
Tim Peters2344fae2001-01-15 00:50:52 +0000321 if colon >= 0:
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000322 filename = arg[:colon].rstrip()
Tim Peters2344fae2001-01-15 00:50:52 +0000323 f = self.lookupmodule(filename)
324 if not f:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000325 print('*** ', repr(filename), end=' ', file=self.stdout)
326 print('not found from sys.path', file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000327 return
328 else:
329 filename = f
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000330 arg = arg[colon+1:].lstrip()
Tim Peters2344fae2001-01-15 00:50:52 +0000331 try:
332 lineno = int(arg)
Guido van Rossumb940e112007-01-10 16:19:56 +0000333 except ValueError as msg:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000334 print('*** Bad lineno:', arg, file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000335 return
336 else:
337 # no colon; can be lineno or function
338 try:
339 lineno = int(arg)
340 except ValueError:
341 try:
342 func = eval(arg,
343 self.curframe.f_globals,
344 self.curframe.f_locals)
345 except:
346 func = arg
347 try:
348 if hasattr(func, 'im_func'):
349 func = func.im_func
Neal Norwitz221085d2007-02-25 20:55:47 +0000350 code = func.__code__
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000351 #use co_name to identify the bkpt (function names
352 #could be aliased, but co_name is invariant)
353 funcname = code.co_name
Tim Peters2344fae2001-01-15 00:50:52 +0000354 lineno = code.co_firstlineno
355 filename = code.co_filename
356 except:
357 # last thing to try
358 (ok, filename, ln) = self.lineinfo(arg)
359 if not ok:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000360 print('*** The specified object', end=' ', file=self.stdout)
361 print(repr(arg), end=' ', file=self.stdout)
362 print('is not a function', file=self.stdout)
363 print('or was not found along sys.path.', file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000364 return
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000365 funcname = ok # ok contains a function name
Tim Peters2344fae2001-01-15 00:50:52 +0000366 lineno = int(ln)
367 if not filename:
368 filename = self.defaultFile()
369 # Check for reasonable breakpoint
370 line = self.checkline(filename, lineno)
371 if line:
372 # now set the break point
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000373 err = self.set_break(filename, line, temporary, cond, funcname)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000374 if err: print('***', err, file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000375 else:
376 bp = self.get_breaks(filename, line)[-1]
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000377 print("Breakpoint %d at %s:%d" % (bp.number,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000378 bp.file,
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000379 bp.line), file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000380
381 # To be overridden in derived debuggers
382 def defaultFile(self):
383 """Produce a reasonable default."""
384 filename = self.curframe.f_code.co_filename
Johannes Gijsbers25b38c82004-10-12 18:12:09 +0000385 if filename == '<string>' and self.mainpyfile:
386 filename = self.mainpyfile
Tim Peters2344fae2001-01-15 00:50:52 +0000387 return filename
388
389 do_b = do_break
390
391 def do_tbreak(self, arg):
392 self.do_break(arg, 1)
393
394 def lineinfo(self, identifier):
395 failed = (None, None, None)
396 # Input is identifier, may be in single quotes
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000397 idstring = identifier.split("'")
Tim Peters2344fae2001-01-15 00:50:52 +0000398 if len(idstring) == 1:
399 # not in single quotes
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000400 id = idstring[0].strip()
Tim Peters2344fae2001-01-15 00:50:52 +0000401 elif len(idstring) == 3:
402 # quoted
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000403 id = idstring[1].strip()
Tim Peters2344fae2001-01-15 00:50:52 +0000404 else:
405 return failed
406 if id == '': return failed
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000407 parts = id.split('.')
Tim Peters2344fae2001-01-15 00:50:52 +0000408 # Protection for derived debuggers
409 if parts[0] == 'self':
410 del parts[0]
411 if len(parts) == 0:
412 return failed
413 # Best first guess at file to look at
414 fname = self.defaultFile()
415 if len(parts) == 1:
416 item = parts[0]
417 else:
418 # More than one part.
419 # First is module, second is method/class
420 f = self.lookupmodule(parts[0])
421 if f:
422 fname = f
423 item = parts[1]
424 answer = find_function(item, fname)
425 return answer or failed
426
427 def checkline(self, filename, lineno):
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000428 """Check whether specified line seems to be executable.
Tim Peters2344fae2001-01-15 00:50:52 +0000429
Johannes Gijsbers4a9faa12004-08-30 13:29:44 +0000430 Return `lineno` if it is, 0 if not (e.g. a docstring, comment, blank
431 line or EOF). Warning: testing is not comprehensive.
432 """
Tim Peters2344fae2001-01-15 00:50:52 +0000433 line = linecache.getline(filename, lineno)
434 if not line:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000435 print('End of file', file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000436 return 0
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000437 line = line.strip()
Tim Peters2344fae2001-01-15 00:50:52 +0000438 # Don't allow setting breakpoint at a blank line
Guido van Rossum08454592002-07-12 13:10:53 +0000439 if (not line or (line[0] == '#') or
440 (line[:3] == '"""') or line[:3] == "'''"):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000441 print('*** Blank or comment', file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000442 return 0
Tim Peters2344fae2001-01-15 00:50:52 +0000443 return lineno
444
445 def do_enable(self, arg):
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000446 args = arg.split()
Tim Peters2344fae2001-01-15 00:50:52 +0000447 for i in args:
Andrew M. Kuchlingb1f8bab2003-05-22 14:46:12 +0000448 try:
449 i = int(i)
450 except ValueError:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000451 print('Breakpoint index %r is not a number' % i, file=self.stdout)
Andrew M. Kuchlingb1f8bab2003-05-22 14:46:12 +0000452 continue
453
454 if not (0 <= i < len(bdb.Breakpoint.bpbynumber)):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000455 print('No breakpoint numbered', i, file=self.stdout)
Andrew M. Kuchlingb1f8bab2003-05-22 14:46:12 +0000456 continue
457
458 bp = bdb.Breakpoint.bpbynumber[i]
Tim Peters2344fae2001-01-15 00:50:52 +0000459 if bp:
460 bp.enable()
461
462 def do_disable(self, arg):
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000463 args = arg.split()
Tim Peters2344fae2001-01-15 00:50:52 +0000464 for i in args:
Andrew M. Kuchlingb1f8bab2003-05-22 14:46:12 +0000465 try:
466 i = int(i)
467 except ValueError:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000468 print('Breakpoint index %r is not a number' % i, file=self.stdout)
Andrew M. Kuchlingb1f8bab2003-05-22 14:46:12 +0000469 continue
Tim Petersf545baa2003-06-15 23:26:30 +0000470
Andrew M. Kuchlingb1f8bab2003-05-22 14:46:12 +0000471 if not (0 <= i < len(bdb.Breakpoint.bpbynumber)):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000472 print('No breakpoint numbered', i, file=self.stdout)
Andrew M. Kuchlingb1f8bab2003-05-22 14:46:12 +0000473 continue
474
475 bp = bdb.Breakpoint.bpbynumber[i]
Tim Peters2344fae2001-01-15 00:50:52 +0000476 if bp:
477 bp.disable()
478
479 def do_condition(self, arg):
480 # arg is breakpoint number and condition
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000481 args = arg.split(' ', 1)
Thomas Woutersb2137042007-02-01 18:02:27 +0000482 try:
483 bpnum = int(args[0].strip())
484 except ValueError:
485 # something went wrong
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000486 print('Breakpoint index %r is not a number' % args[0], file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000487 try:
488 cond = args[1]
489 except:
490 cond = None
491 bp = bdb.Breakpoint.bpbynumber[bpnum]
492 if bp:
493 bp.cond = cond
494 if not cond:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000495 print('Breakpoint', bpnum, end=' ', file=self.stdout)
496 print('is now unconditional.', file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000497
498 def do_ignore(self,arg):
499 """arg is bp number followed by ignore count."""
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000500 args = arg.split()
Thomas Woutersb2137042007-02-01 18:02:27 +0000501 try:
502 bpnum = int(args[0].strip())
503 except ValueError:
504 # something went wrong
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000505 print('Breakpoint index %r is not a number' % args[0], file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000506 try:
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000507 count = int(args[1].strip())
Tim Peters2344fae2001-01-15 00:50:52 +0000508 except:
509 count = 0
510 bp = bdb.Breakpoint.bpbynumber[bpnum]
511 if bp:
512 bp.ignore = count
Guido van Rossum08454592002-07-12 13:10:53 +0000513 if count > 0:
Tim Peters2344fae2001-01-15 00:50:52 +0000514 reply = 'Will ignore next '
Guido van Rossum08454592002-07-12 13:10:53 +0000515 if count > 1:
Tim Peters2344fae2001-01-15 00:50:52 +0000516 reply = reply + '%d crossings' % count
517 else:
518 reply = reply + '1 crossing'
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000519 print(reply + ' of breakpoint %d.' % bpnum, file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000520 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000521 print('Will stop next time breakpoint', end=' ', file=self.stdout)
522 print(bpnum, 'is reached.', file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000523
524 def do_clear(self, arg):
525 """Three possibilities, tried in this order:
526 clear -> clear all breaks, ask for confirmation
527 clear file:lineno -> clear all breaks at file:lineno
528 clear bpno bpno ... -> clear breakpoints by number"""
529 if not arg:
530 try:
531 reply = raw_input('Clear all breaks? ')
532 except EOFError:
533 reply = 'no'
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000534 reply = reply.strip().lower()
Tim Peters2344fae2001-01-15 00:50:52 +0000535 if reply in ('y', 'yes'):
536 self.clear_all_breaks()
537 return
538 if ':' in arg:
539 # Make sure it works for "clear C:\foo\bar.py:12"
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000540 i = arg.rfind(':')
Tim Peters2344fae2001-01-15 00:50:52 +0000541 filename = arg[:i]
542 arg = arg[i+1:]
543 try:
544 lineno = int(arg)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000545 except ValueError:
Tim Peters2344fae2001-01-15 00:50:52 +0000546 err = "Invalid line number (%s)" % arg
547 else:
548 err = self.clear_break(filename, lineno)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000549 if err: print('***', err, file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000550 return
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000551 numberlist = arg.split()
Tim Peters2344fae2001-01-15 00:50:52 +0000552 for i in numberlist:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000553 try:
554 i = int(i)
555 except ValueError:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000556 print('Breakpoint index %r is not a number' % i, file=self.stdout)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000557 continue
558
Georg Brandl6d2b3462005-08-24 07:36:17 +0000559 if not (0 <= i < len(bdb.Breakpoint.bpbynumber)):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000560 print('No breakpoint numbered', i, file=self.stdout)
Georg Brandl6d2b3462005-08-24 07:36:17 +0000561 continue
Tim Peters2344fae2001-01-15 00:50:52 +0000562 err = self.clear_bpbynumber(i)
563 if err:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000564 print('***', err, file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000565 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000566 print('Deleted breakpoint', i, file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000567 do_cl = do_clear # 'c' is already an abbreviation for 'continue'
568
569 def do_where(self, arg):
570 self.print_stack_trace()
571 do_w = do_where
Guido van Rossum6bd68352001-01-20 17:57:37 +0000572 do_bt = do_where
Tim Peters2344fae2001-01-15 00:50:52 +0000573
574 def do_up(self, arg):
575 if self.curindex == 0:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000576 print('*** Oldest frame', file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000577 else:
578 self.curindex = self.curindex - 1
579 self.curframe = self.stack[self.curindex][0]
580 self.print_stack_entry(self.stack[self.curindex])
581 self.lineno = None
582 do_u = do_up
583
584 def do_down(self, arg):
585 if self.curindex + 1 == len(self.stack):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000586 print('*** Newest frame', file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000587 else:
588 self.curindex = self.curindex + 1
589 self.curframe = self.stack[self.curindex][0]
590 self.print_stack_entry(self.stack[self.curindex])
591 self.lineno = None
592 do_d = do_down
593
594 def do_step(self, arg):
595 self.set_step()
596 return 1
597 do_s = do_step
598
599 def do_next(self, arg):
600 self.set_next(self.curframe)
601 return 1
602 do_n = do_next
603
604 def do_return(self, arg):
605 self.set_return(self.curframe)
606 return 1
607 do_r = do_return
608
609 def do_continue(self, arg):
610 self.set_continue()
611 return 1
612 do_c = do_cont = do_continue
613
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000614 def do_jump(self, arg):
615 if self.curindex + 1 != len(self.stack):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000616 print("*** You can only jump within the bottom frame", file=self.stdout)
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000617 return
618 try:
619 arg = int(arg)
620 except ValueError:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000621 print("*** The 'jump' command requires a line number.", file=self.stdout)
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000622 else:
623 try:
624 # Do the jump, fix up our copy of the stack, and display the
625 # new position
626 self.curframe.f_lineno = arg
627 self.stack[self.curindex] = self.stack[self.curindex][0], arg
628 self.print_stack_entry(self.stack[self.curindex])
Guido van Rossumb940e112007-01-10 16:19:56 +0000629 except ValueError as e:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000630 print('*** Jump failed:', e, file=self.stdout)
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000631 do_j = do_jump
632
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000633 def do_debug(self, arg):
634 sys.settrace(None)
635 globals = self.curframe.f_globals
636 locals = self.curframe.f_locals
Guido van Rossumed538d82003-04-09 19:36:34 +0000637 p = Pdb()
638 p.prompt = "(%s) " % self.prompt.strip()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000639 print("ENTERING RECURSIVE DEBUGGER", file=self.stdout)
Guido van Rossumed538d82003-04-09 19:36:34 +0000640 sys.call_tracing(p.run, (arg, globals, locals))
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000641 print("LEAVING RECURSIVE DEBUGGER", file=self.stdout)
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000642 sys.settrace(self.trace_dispatch)
643 self.lastcmd = p.lastcmd
644
Tim Peters2344fae2001-01-15 00:50:52 +0000645 def do_quit(self, arg):
Johannes Gijsbers25b38c82004-10-12 18:12:09 +0000646 self._user_requested_quit = 1
Tim Peters2344fae2001-01-15 00:50:52 +0000647 self.set_quit()
648 return 1
Johannes Gijsbers25b38c82004-10-12 18:12:09 +0000649
Tim Peters2344fae2001-01-15 00:50:52 +0000650 do_q = do_quit
Guido van Rossumd1c08f32002-04-15 00:48:24 +0000651 do_exit = do_quit
Tim Peters2344fae2001-01-15 00:50:52 +0000652
Guido van Rossumeef26072003-01-13 21:13:55 +0000653 def do_EOF(self, arg):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000654 print(file=self.stdout)
Johannes Gijsbers25b38c82004-10-12 18:12:09 +0000655 self._user_requested_quit = 1
Guido van Rossumeef26072003-01-13 21:13:55 +0000656 self.set_quit()
657 return 1
658
Tim Peters2344fae2001-01-15 00:50:52 +0000659 def do_args(self, arg):
660 f = self.curframe
661 co = f.f_code
662 dict = f.f_locals
663 n = co.co_argcount
664 if co.co_flags & 4: n = n+1
665 if co.co_flags & 8: n = n+1
666 for i in range(n):
667 name = co.co_varnames[i]
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000668 print(name, '=', end=' ', file=self.stdout)
669 if name in dict: print(dict[name], file=self.stdout)
670 else: print("*** undefined ***", file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000671 do_a = do_args
Guido van Rossum2424f851998-09-11 22:50:09 +0000672
Tim Peters2344fae2001-01-15 00:50:52 +0000673 def do_retval(self, arg):
Raymond Hettinger54f02222002-06-01 14:18:47 +0000674 if '__return__' in self.curframe.f_locals:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000675 print(self.curframe.f_locals['__return__'], file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000676 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000677 print('*** Not yet returned!', file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000678 do_rv = do_retval
Guido van Rossum2424f851998-09-11 22:50:09 +0000679
Barry Warsaw210bd202002-11-05 22:40:20 +0000680 def _getval(self, arg):
Tim Peters2344fae2001-01-15 00:50:52 +0000681 try:
Barry Warsaw210bd202002-11-05 22:40:20 +0000682 return eval(arg, self.curframe.f_globals,
683 self.curframe.f_locals)
Tim Peters2344fae2001-01-15 00:50:52 +0000684 except:
685 t, v = sys.exc_info()[:2]
Barry Warsaw210bd202002-11-05 22:40:20 +0000686 if isinstance(t, str):
Tim Peters2344fae2001-01-15 00:50:52 +0000687 exc_type_name = t
688 else: exc_type_name = t.__name__
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000689 print('***', exc_type_name + ':', repr(v), file=self.stdout)
Barry Warsaw210bd202002-11-05 22:40:20 +0000690 raise
Guido van Rossum2424f851998-09-11 22:50:09 +0000691
Barry Warsaw210bd202002-11-05 22:40:20 +0000692 def do_p(self, arg):
693 try:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000694 print(repr(self._getval(arg)), file=self.stdout)
Barry Warsaw210bd202002-11-05 22:40:20 +0000695 except:
696 pass
697
698 def do_pp(self, arg):
699 try:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000700 pprint.pprint(self._getval(arg), self.stdout)
Barry Warsaw210bd202002-11-05 22:40:20 +0000701 except:
702 pass
Guido van Rossum2424f851998-09-11 22:50:09 +0000703
Tim Peters2344fae2001-01-15 00:50:52 +0000704 def do_list(self, arg):
705 self.lastcmd = 'list'
706 last = None
707 if arg:
708 try:
709 x = eval(arg, {}, {})
710 if type(x) == type(()):
711 first, last = x
712 first = int(first)
713 last = int(last)
714 if last < first:
715 # Assume it's a count
716 last = first + last
717 else:
718 first = max(1, int(x) - 5)
719 except:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000720 print('*** Error in argument:', repr(arg), file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000721 return
722 elif self.lineno is None:
723 first = max(1, self.curframe.f_lineno - 5)
724 else:
725 first = self.lineno + 1
726 if last is None:
727 last = first + 10
728 filename = self.curframe.f_code.co_filename
729 breaklist = self.get_file_breaks(filename)
730 try:
731 for lineno in range(first, last+1):
732 line = linecache.getline(filename, lineno)
733 if not line:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000734 print('[EOF]', file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000735 break
736 else:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000737 s = repr(lineno).rjust(3)
Tim Peters2344fae2001-01-15 00:50:52 +0000738 if len(s) < 4: s = s + ' '
739 if lineno in breaklist: s = s + 'B'
740 else: s = s + ' '
741 if lineno == self.curframe.f_lineno:
742 s = s + '->'
Guido van Rossumceae3752007-02-09 22:16:54 +0000743 print(s + '\t' + line, end='', file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000744 self.lineno = lineno
745 except KeyboardInterrupt:
746 pass
747 do_l = do_list
Guido van Rossum2424f851998-09-11 22:50:09 +0000748
Tim Peters2344fae2001-01-15 00:50:52 +0000749 def do_whatis(self, arg):
750 try:
751 value = eval(arg, self.curframe.f_globals,
752 self.curframe.f_locals)
753 except:
754 t, v = sys.exc_info()[:2]
755 if type(t) == type(''):
756 exc_type_name = t
757 else: exc_type_name = t.__name__
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000758 print('***', exc_type_name + ':', repr(v), file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000759 return
760 code = None
761 # Is it a function?
Neal Norwitz221085d2007-02-25 20:55:47 +0000762 try: code = value.__code__
Tim Peters2344fae2001-01-15 00:50:52 +0000763 except: pass
764 if code:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000765 print('Function', code.co_name, file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000766 return
767 # Is it an instance method?
Neal Norwitz221085d2007-02-25 20:55:47 +0000768 try: code = value.im_func.__code__
Tim Peters2344fae2001-01-15 00:50:52 +0000769 except: pass
770 if code:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000771 print('Method', code.co_name, file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000772 return
773 # None of the above...
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000774 print(type(value), file=self.stdout)
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000775
Tim Peters2344fae2001-01-15 00:50:52 +0000776 def do_alias(self, arg):
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000777 args = arg.split()
Tim Peters2344fae2001-01-15 00:50:52 +0000778 if len(args) == 0:
779 keys = self.aliases.keys()
780 keys.sort()
781 for alias in keys:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000782 print("%s = %s" % (alias, self.aliases[alias]), file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000783 return
Guido van Rossum08454592002-07-12 13:10:53 +0000784 if args[0] in self.aliases and len(args) == 1:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000785 print("%s = %s" % (args[0], self.aliases[args[0]]), file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000786 else:
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000787 self.aliases[args[0]] = ' '.join(args[1:])
Guido van Rossum23efba41992-01-27 16:58:47 +0000788
Tim Peters2344fae2001-01-15 00:50:52 +0000789 def do_unalias(self, arg):
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000790 args = arg.split()
Tim Peters2344fae2001-01-15 00:50:52 +0000791 if len(args) == 0: return
Raymond Hettinger54f02222002-06-01 14:18:47 +0000792 if args[0] in self.aliases:
Tim Peters2344fae2001-01-15 00:50:52 +0000793 del self.aliases[args[0]]
Guido van Rossum00230781993-03-29 11:39:45 +0000794
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000795 #list of all the commands making the program resume execution.
Thomas Wouters477c8d52006-05-27 19:21:47 +0000796 commands_resuming = ['do_continue', 'do_step', 'do_next', 'do_return',
797 'do_quit', 'do_jump']
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000798
Tim Peters2344fae2001-01-15 00:50:52 +0000799 # Print a traceback starting at the top stack frame.
800 # The most recently entered frame is printed last;
801 # this is different from dbx and gdb, but consistent with
802 # the Python interpreter's stack trace.
803 # It is also consistent with the up/down commands (which are
804 # compatible with dbx and gdb: up moves towards 'main()'
805 # and down moves towards the most recent stack frame).
Guido van Rossum2424f851998-09-11 22:50:09 +0000806
Tim Peters2344fae2001-01-15 00:50:52 +0000807 def print_stack_trace(self):
808 try:
809 for frame_lineno in self.stack:
810 self.print_stack_entry(frame_lineno)
811 except KeyboardInterrupt:
812 pass
Guido van Rossum2424f851998-09-11 22:50:09 +0000813
Tim Peters2344fae2001-01-15 00:50:52 +0000814 def print_stack_entry(self, frame_lineno, prompt_prefix=line_prefix):
815 frame, lineno = frame_lineno
816 if frame is self.curframe:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000817 print('>', end=' ', file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000818 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000819 print(' ', end=' ', file=self.stdout)
820 print(self.format_stack_entry(frame_lineno,
821 prompt_prefix), file=self.stdout)
Guido van Rossum2424f851998-09-11 22:50:09 +0000822
Guido van Rossum921c8241992-01-10 14:54:42 +0000823
Tim Peters2344fae2001-01-15 00:50:52 +0000824 # Help methods (derived from pdb.doc)
Guido van Rossum921c8241992-01-10 14:54:42 +0000825
Tim Peters2344fae2001-01-15 00:50:52 +0000826 def help_help(self):
827 self.help_h()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000828
Tim Peters2344fae2001-01-15 00:50:52 +0000829 def help_h(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000830 print("""h(elp)
Tim Peters2344fae2001-01-15 00:50:52 +0000831Without argument, print the list of available commands.
832With a command name as argument, print help about that command
833"help pdb" pipes the full documentation file to the $PAGER
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000834"help exec" gives help on the ! command""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000835
Tim Peters2344fae2001-01-15 00:50:52 +0000836 def help_where(self):
837 self.help_w()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000838
Tim Peters2344fae2001-01-15 00:50:52 +0000839 def help_w(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000840 print("""w(here)
Tim Peters2344fae2001-01-15 00:50:52 +0000841Print a stack trace, with the most recent frame at the bottom.
842An arrow indicates the "current frame", which determines the
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000843context of most commands. 'bt' is an alias for this command.""", file=self.stdout)
Guido van Rossum6bd68352001-01-20 17:57:37 +0000844
845 help_bt = help_w
Guido van Rossumb6775db1994-08-01 11:34:53 +0000846
Tim Peters2344fae2001-01-15 00:50:52 +0000847 def help_down(self):
848 self.help_d()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000849
Tim Peters2344fae2001-01-15 00:50:52 +0000850 def help_d(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000851 print("""d(own)
Tim Peters2344fae2001-01-15 00:50:52 +0000852Move the current frame one level down in the stack trace
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000853(to a newer frame).""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000854
Tim Peters2344fae2001-01-15 00:50:52 +0000855 def help_up(self):
856 self.help_u()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000857
Tim Peters2344fae2001-01-15 00:50:52 +0000858 def help_u(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000859 print("""u(p)
Tim Peters2344fae2001-01-15 00:50:52 +0000860Move the current frame one level up in the stack trace
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000861(to an older frame).""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000862
Tim Peters2344fae2001-01-15 00:50:52 +0000863 def help_break(self):
864 self.help_b()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000865
Tim Peters2344fae2001-01-15 00:50:52 +0000866 def help_b(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000867 print("""b(reak) ([file:]lineno | function) [, condition]
Tim Peters2344fae2001-01-15 00:50:52 +0000868With a line number argument, set a break there in the current
869file. With a function name, set a break at first executable line
870of that function. Without argument, list all breaks. If a second
871argument is present, it is a string specifying an expression
872which must evaluate to true before the breakpoint is honored.
Guido van Rossumb6775db1994-08-01 11:34:53 +0000873
Tim Peters2344fae2001-01-15 00:50:52 +0000874The line number may be prefixed with a filename and a colon,
875to specify a breakpoint in another file (probably one that
876hasn't been loaded yet). The file is searched for on sys.path;
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000877the .py suffix may be omitted.""", file=self.stdout)
Guido van Rossumb5699c71998-07-20 23:13:54 +0000878
Tim Peters2344fae2001-01-15 00:50:52 +0000879 def help_clear(self):
880 self.help_cl()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000881
Tim Peters2344fae2001-01-15 00:50:52 +0000882 def help_cl(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000883 print("cl(ear) filename:lineno", file=self.stdout)
884 print("""cl(ear) [bpnumber [bpnumber...]]
Tim Peters2344fae2001-01-15 00:50:52 +0000885With a space separated list of breakpoint numbers, clear
886those breakpoints. Without argument, clear all breaks (but
887first ask confirmation). With a filename:lineno argument,
888clear all breaks at that line in that file.
Guido van Rossumb6775db1994-08-01 11:34:53 +0000889
Tim Peters2344fae2001-01-15 00:50:52 +0000890Note that the argument is different from previous versions of
891the debugger (in python distributions 1.5.1 and before) where
892a linenumber was used instead of either filename:lineno or
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000893breakpoint numbers.""", file=self.stdout)
Guido van Rossumb5699c71998-07-20 23:13:54 +0000894
Tim Peters2344fae2001-01-15 00:50:52 +0000895 def help_tbreak(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000896 print("""tbreak same arguments as break, but breakpoint is
897removed when first hit.""", file=self.stdout)
Guido van Rossum2424f851998-09-11 22:50:09 +0000898
Tim Peters2344fae2001-01-15 00:50:52 +0000899 def help_enable(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000900 print("""enable bpnumber [bpnumber ...]
Tim Peters2344fae2001-01-15 00:50:52 +0000901Enables the breakpoints given as a space separated list of
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000902bp numbers.""", file=self.stdout)
Guido van Rossum2424f851998-09-11 22:50:09 +0000903
Tim Peters2344fae2001-01-15 00:50:52 +0000904 def help_disable(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000905 print("""disable bpnumber [bpnumber ...]
Tim Peters2344fae2001-01-15 00:50:52 +0000906Disables the breakpoints given as a space separated list of
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000907bp numbers.""", file=self.stdout)
Guido van Rossum2424f851998-09-11 22:50:09 +0000908
Tim Peters2344fae2001-01-15 00:50:52 +0000909 def help_ignore(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000910 print("""ignore bpnumber count
Tim Peters2344fae2001-01-15 00:50:52 +0000911Sets the ignore count for the given breakpoint number. A breakpoint
912becomes active when the ignore count is zero. When non-zero, the
913count is decremented each time the breakpoint is reached and the
914breakpoint is not disabled and any associated condition evaluates
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000915to true.""", file=self.stdout)
Guido van Rossum2424f851998-09-11 22:50:09 +0000916
Tim Peters2344fae2001-01-15 00:50:52 +0000917 def help_condition(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000918 print("""condition bpnumber str_condition
Tim Peters2344fae2001-01-15 00:50:52 +0000919str_condition is a string specifying an expression which
920must evaluate to true before the breakpoint is honored.
921If str_condition is absent, any existing condition is removed;
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000922i.e., the breakpoint is made unconditional.""", file=self.stdout)
Guido van Rossum2424f851998-09-11 22:50:09 +0000923
Tim Peters2344fae2001-01-15 00:50:52 +0000924 def help_step(self):
925 self.help_s()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000926
Tim Peters2344fae2001-01-15 00:50:52 +0000927 def help_s(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000928 print("""s(tep)
Tim Peters2344fae2001-01-15 00:50:52 +0000929Execute the current line, stop at the first possible occasion
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000930(either in a function that is called or in the current function).""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000931
Tim Peters2344fae2001-01-15 00:50:52 +0000932 def help_next(self):
933 self.help_n()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000934
Tim Peters2344fae2001-01-15 00:50:52 +0000935 def help_n(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000936 print("""n(ext)
Tim Peters2344fae2001-01-15 00:50:52 +0000937Continue execution until the next line in the current function
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000938is reached or it returns.""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000939
Tim Peters2344fae2001-01-15 00:50:52 +0000940 def help_return(self):
941 self.help_r()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000942
Tim Peters2344fae2001-01-15 00:50:52 +0000943 def help_r(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000944 print("""r(eturn)
945Continue execution until the current function returns.""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000946
Tim Peters2344fae2001-01-15 00:50:52 +0000947 def help_continue(self):
948 self.help_c()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000949
Tim Peters2344fae2001-01-15 00:50:52 +0000950 def help_cont(self):
951 self.help_c()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000952
Tim Peters2344fae2001-01-15 00:50:52 +0000953 def help_c(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000954 print("""c(ont(inue))
955Continue execution, only stop when a breakpoint is encountered.""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000956
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000957 def help_jump(self):
958 self.help_j()
959
960 def help_j(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000961 print("""j(ump) lineno
962Set the next line that will be executed.""", file=self.stdout)
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000963
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000964 def help_debug(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000965 print("""debug code
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000966Enter a recursive debugger that steps through the code argument
967(which is an arbitrary expression or statement to be executed
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000968in the current environment).""", file=self.stdout)
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000969
Tim Peters2344fae2001-01-15 00:50:52 +0000970 def help_list(self):
971 self.help_l()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000972
Tim Peters2344fae2001-01-15 00:50:52 +0000973 def help_l(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000974 print("""l(ist) [first [,last]]
Tim Peters2344fae2001-01-15 00:50:52 +0000975List source code for the current file.
976Without arguments, list 11 lines around the current line
977or continue the previous listing.
978With one argument, list 11 lines starting at that line.
979With two arguments, list the given range;
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000980if the second argument is less than the first, it is a count.""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000981
Tim Peters2344fae2001-01-15 00:50:52 +0000982 def help_args(self):
983 self.help_a()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000984
Tim Peters2344fae2001-01-15 00:50:52 +0000985 def help_a(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000986 print("""a(rgs)
987Print the arguments of the current function.""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000988
Tim Peters2344fae2001-01-15 00:50:52 +0000989 def help_p(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000990 print("""p expression
991Print the value of the expression.""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000992
Barry Warsaw210bd202002-11-05 22:40:20 +0000993 def help_pp(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000994 print("""pp expression
995Pretty-print the value of the expression.""", file=self.stdout)
Barry Warsaw210bd202002-11-05 22:40:20 +0000996
Tim Peters2344fae2001-01-15 00:50:52 +0000997 def help_exec(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000998 print("""(!) statement
Tim Peters2344fae2001-01-15 00:50:52 +0000999Execute the (one-line) statement in the context of
1000the current stack frame.
1001The exclamation point can be omitted unless the first word
1002of the statement resembles a debugger command.
1003To assign to a global variable you must always prefix the
1004command with a 'global' command, e.g.:
1005(Pdb) global list_options; list_options = ['-l']
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001006(Pdb)""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001007
Tim Peters2344fae2001-01-15 00:50:52 +00001008 def help_quit(self):
1009 self.help_q()
Guido van Rossumb6775db1994-08-01 11:34:53 +00001010
Tim Peters2344fae2001-01-15 00:50:52 +00001011 def help_q(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001012 print("""q(uit) or exit - Quit from the debugger.
1013The program being executed is aborted.""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001014
Guido van Rossumd1c08f32002-04-15 00:48:24 +00001015 help_exit = help_q
1016
Tim Peters2344fae2001-01-15 00:50:52 +00001017 def help_whatis(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001018 print("""whatis arg
1019Prints the type of the argument.""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001020
Tim Peters2344fae2001-01-15 00:50:52 +00001021 def help_EOF(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001022 print("""EOF
1023Handles the receipt of EOF as a command.""", file=self.stdout)
Guido van Rossum2424f851998-09-11 22:50:09 +00001024
Tim Peters2344fae2001-01-15 00:50:52 +00001025 def help_alias(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001026 print("""alias [name [command [parameter parameter ...] ]]
Tim Peters2344fae2001-01-15 00:50:52 +00001027Creates an alias called 'name' the executes 'command'. The command
1028must *not* be enclosed in quotes. Replaceable parameters are
1029indicated by %1, %2, and so on, while %* is replaced by all the
1030parameters. If no command is given, the current alias for name
1031is shown. If no name is given, all aliases are listed.
Guido van Rossum2424f851998-09-11 22:50:09 +00001032
Tim Peters2344fae2001-01-15 00:50:52 +00001033Aliases may be nested and can contain anything that can be
1034legally typed at the pdb prompt. Note! You *can* override
1035internal pdb commands with aliases! Those internal commands
1036are then hidden until the alias is removed. Aliasing is recursively
1037applied to the first word of the command line; all other words
1038in the line are left alone.
Guido van Rossum2424f851998-09-11 22:50:09 +00001039
Tim Peters2344fae2001-01-15 00:50:52 +00001040Some useful aliases (especially when placed in the .pdbrc file) are:
Guido van Rossum2424f851998-09-11 22:50:09 +00001041
Tim Peters2344fae2001-01-15 00:50:52 +00001042#Print instance variables (usage "pi classInst")
1043alias pi for k in %1.__dict__.keys(): print "%1.",k,"=",%1.__dict__[k]
Guido van Rossum2424f851998-09-11 22:50:09 +00001044
Tim Peters2344fae2001-01-15 00:50:52 +00001045#Print instance variables in self
1046alias ps pi self
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001047""", file=self.stdout)
Guido van Rossum2424f851998-09-11 22:50:09 +00001048
Tim Peters2344fae2001-01-15 00:50:52 +00001049 def help_unalias(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001050 print("""unalias name
1051Deletes the specified alias.""", file=self.stdout)
Guido van Rossum2424f851998-09-11 22:50:09 +00001052
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001053 def help_commands(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001054 print("""commands [bpnumber]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001055(com) ...
1056(com) end
1057(Pdb)
1058
1059Specify a list of commands for breakpoint number bpnumber. The
1060commands themselves appear on the following lines. Type a line
1061containing just 'end' to terminate the commands.
1062
1063To remove all commands from a breakpoint, type commands and
1064follow it immediately with end; that is, give no commands.
1065
1066With no bpnumber argument, commands refers to the last
1067breakpoint set.
1068
1069You can use breakpoint commands to start your program up again.
1070Simply use the continue command, or step, or any other
1071command that resumes execution.
1072
1073Specifying any command resuming execution (currently continue,
1074step, next, return, jump, quit and their abbreviations) terminates
1075the command list (as if that command was immediately followed by end).
1076This is because any time you resume execution
1077(even with a simple next or step), you may encounter
1078another breakpoint--which could have its own command list, leading to
1079ambiguities about which list to execute.
1080
1081 If you use the 'silent' command in the command list, the
1082usual message about stopping at a breakpoint is not printed. This may
1083be desirable for breakpoints that are to print a specific message and
1084then continue. If none of the other commands print anything, you
1085see no sign that the breakpoint was reached.
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001086""", file=self.stdout)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001087
Tim Peters2344fae2001-01-15 00:50:52 +00001088 def help_pdb(self):
1089 help()
Guido van Rossumb6775db1994-08-01 11:34:53 +00001090
Tim Peters2344fae2001-01-15 00:50:52 +00001091 def lookupmodule(self, filename):
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001092 """Helper function for break/clear parsing -- may be overridden.
1093
1094 lookupmodule() translates (possibly incomplete) file or module name
1095 into an absolute file name.
1096 """
1097 if os.path.isabs(filename) and os.path.exists(filename):
Tim Peterse718f612004-10-12 21:51:32 +00001098 return filename
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001099 f = os.path.join(sys.path[0], filename)
1100 if os.path.exists(f) and self.canonic(f) == self.mainpyfile:
1101 return f
Tim Peters2344fae2001-01-15 00:50:52 +00001102 root, ext = os.path.splitext(filename)
1103 if ext == '':
1104 filename = filename + '.py'
1105 if os.path.isabs(filename):
1106 return filename
1107 for dirname in sys.path:
1108 while os.path.islink(dirname):
1109 dirname = os.readlink(dirname)
1110 fullname = os.path.join(dirname, filename)
1111 if os.path.exists(fullname):
1112 return fullname
1113 return None
Guido van Rossumb5699c71998-07-20 23:13:54 +00001114
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001115 def _runscript(self, filename):
1116 # Start with fresh empty copy of globals and locals and tell the script
1117 # that it's being run as __main__ to avoid scripts being able to access
1118 # the pdb.py namespace.
Tim Peterse718f612004-10-12 21:51:32 +00001119 globals_ = {"__name__" : "__main__"}
1120 locals_ = globals_
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001121
1122 # When bdb sets tracing, a number of call and line events happens
1123 # BEFORE debugger even reaches user's code (and the exact sequence of
1124 # events depends on python version). So we take special measures to
1125 # avoid stopping before we reach the main script (see user_line and
1126 # user_call for details).
1127 self._wait_for_mainpyfile = 1
1128 self.mainpyfile = self.canonic(filename)
1129 self._user_requested_quit = 0
1130 statement = 'execfile( "%s")' % filename
1131 self.run(statement, globals=globals_, locals=locals_)
1132
Guido van Rossum35771131992-09-08 11:59:04 +00001133# Simplified interface
1134
Guido van Rossum5e38b6f1995-02-27 13:13:40 +00001135def run(statement, globals=None, locals=None):
Tim Peters2344fae2001-01-15 00:50:52 +00001136 Pdb().run(statement, globals, locals)
Guido van Rossum5e38b6f1995-02-27 13:13:40 +00001137
1138def runeval(expression, globals=None, locals=None):
Tim Peters2344fae2001-01-15 00:50:52 +00001139 return Pdb().runeval(expression, globals, locals)
Guido van Rossum6fe08b01992-01-16 13:50:21 +00001140
1141def runctx(statement, globals, locals):
Tim Peters2344fae2001-01-15 00:50:52 +00001142 # B/W compatibility
1143 run(statement, globals, locals)
Guido van Rossum6fe08b01992-01-16 13:50:21 +00001144
Raymond Hettinger2ef7e6c2004-10-24 00:32:24 +00001145def runcall(*args, **kwds):
1146 return Pdb().runcall(*args, **kwds)
Guido van Rossum4e160981992-09-02 20:43:20 +00001147
Guido van Rossumb6775db1994-08-01 11:34:53 +00001148def set_trace():
Johannes Gijsbers84a6c202004-11-07 11:35:30 +00001149 Pdb().set_trace(sys._getframe().f_back)
Guido van Rossum35771131992-09-08 11:59:04 +00001150
1151# Post-Mortem interface
1152
1153def post_mortem(t):
Tim Peters2344fae2001-01-15 00:50:52 +00001154 p = Pdb()
1155 p.reset()
1156 while t.tb_next is not None:
1157 t = t.tb_next
1158 p.interaction(t.tb_frame, t)
Guido van Rossum35771131992-09-08 11:59:04 +00001159
1160def pm():
Tim Peters2344fae2001-01-15 00:50:52 +00001161 post_mortem(sys.last_traceback)
Guido van Rossum35771131992-09-08 11:59:04 +00001162
1163
1164# Main program for testing
1165
Guido van Rossum23efba41992-01-27 16:58:47 +00001166TESTCMD = 'import x; x.main()'
Guido van Rossum6fe08b01992-01-16 13:50:21 +00001167
Guido van Rossum921c8241992-01-10 14:54:42 +00001168def test():
Tim Peters2344fae2001-01-15 00:50:52 +00001169 run(TESTCMD)
Guido van Rossume61fa0a1993-10-22 13:56:35 +00001170
1171# print help
1172def help():
Tim Peters2344fae2001-01-15 00:50:52 +00001173 for dirname in sys.path:
1174 fullname = os.path.join(dirname, 'pdb.doc')
1175 if os.path.exists(fullname):
1176 sts = os.system('${PAGER-more} '+fullname)
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001177 if sts: print('*** Pager exit status:', sts)
Tim Peters2344fae2001-01-15 00:50:52 +00001178 break
1179 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001180 print('Sorry, can\'t find the help file "pdb.doc"', end=' ')
1181 print('along the Python search path')
Guido van Rossumf17361d1996-07-30 16:28:13 +00001182
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001183def main():
Tim Peters2344fae2001-01-15 00:50:52 +00001184 if not sys.argv[1:]:
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001185 print("usage: pdb.py scriptfile [arg] ...")
Tim Peters2344fae2001-01-15 00:50:52 +00001186 sys.exit(2)
Guido van Rossumf17361d1996-07-30 16:28:13 +00001187
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001188 mainpyfile = sys.argv[1] # Get script filename
1189 if not os.path.exists(mainpyfile):
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001190 print('Error:', mainpyfile, 'does not exist')
Tim Peters2344fae2001-01-15 00:50:52 +00001191 sys.exit(1)
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001192
Tim Peters2344fae2001-01-15 00:50:52 +00001193 del sys.argv[0] # Hide "pdb.py" from argument list
Guido van Rossumec577d51996-09-10 17:39:34 +00001194
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001195 # Replace pdb's dir with script's dir in front of module search path.
1196 sys.path[0] = os.path.dirname(mainpyfile)
Guido van Rossumf17361d1996-07-30 16:28:13 +00001197
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001198 # Note on saving/restoring sys.argv: it's a good idea when sys.argv was
1199 # modified by the script being debugged. It's a bad idea when it was
1200 # changed by the user from the command line. The best approach would be to
1201 # have a "restart" command which would allow explicit specification of
1202 # command line arguments.
1203 pdb = Pdb()
1204 while 1:
1205 try:
1206 pdb._runscript(mainpyfile)
1207 if pdb._user_requested_quit:
1208 break
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001209 print("The program finished and will be restarted")
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001210 except SystemExit:
1211 # In most cases SystemExit does not warrant a post-mortem session.
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001212 print("The program exited via sys.exit(). Exit status: ", end=' ')
1213 print(sys.exc_info()[1])
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001214 except:
1215 traceback.print_exc()
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001216 print("Uncaught exception. Entering post mortem debugging")
1217 print("Running 'cont' or 'step' will restart the program")
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001218 t = sys.exc_info()[2]
1219 while t.tb_next is not None:
1220 t = t.tb_next
1221 pdb.interaction(t.tb_frame,t)
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001222 print("Post mortem debugger finished. The "+mainpyfile+" will be restarted")
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001223
1224
1225# When invoked as main program, invoke the debugger on a script
1226if __name__=='__main__':
1227 main()