blob: bd51c31e0520480342ebf85719056e8d2b6f7e44 [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 Rossumd8faa362007-04-27 19:54:29 +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):
Thomas Wouters89f507f2006-12-13 04:49:30 +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
Thomas Wouters477c8d52006-05-27 19:21:47 +000061 def __init__(self, completekey='tab', stdin=None, stdout=None):
Tim Peters2344fae2001-01-15 00:50:52 +000062 bdb.Bdb.__init__(self)
Thomas Wouters477c8d52006-05-27 19:21:47 +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
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000139 print('--Call--', file=self.stdout)
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
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000149 if self.bp_commands(frame):
150 self.interaction(frame, None)
151
152 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()
169 return
170 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
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000175 print('--Return--', file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000176 self.interaction(frame, None)
Guido van Rossum2424f851998-09-11 22:50:09 +0000177
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000178 def user_exception(self, frame, exc_info):
Tim Peters2344fae2001-01-15 00:50:52 +0000179 """This function is called if an exception occurs,
180 but only if we are to stop at or just below this level."""
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000181 exc_type, exc_value, exc_traceback = exc_info
Tim Peters2344fae2001-01-15 00:50:52 +0000182 frame.f_locals['__exception__'] = exc_type, exc_value
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000183 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:
Christian Heimesff737952007-11-27 10:40:20 +0000348 if hasattr(func, '__func__'):
349 func = func.__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)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000487 return
Tim Peters2344fae2001-01-15 00:50:52 +0000488 try:
489 cond = args[1]
490 except:
491 cond = None
Guido van Rossumd8faa362007-04-27 19:54:29 +0000492 try:
493 bp = bdb.Breakpoint.bpbynumber[bpnum]
494 except IndexError:
495 print >>self.stdout, 'Breakpoint index %r is not valid' % args[0]
496 return
Tim Peters2344fae2001-01-15 00:50:52 +0000497 if bp:
498 bp.cond = cond
499 if not cond:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000500 print('Breakpoint', bpnum, end=' ', file=self.stdout)
501 print('is now unconditional.', file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000502
503 def do_ignore(self,arg):
504 """arg is bp number followed by ignore count."""
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000505 args = arg.split()
Thomas Woutersb2137042007-02-01 18:02:27 +0000506 try:
507 bpnum = int(args[0].strip())
508 except ValueError:
509 # something went wrong
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000510 print('Breakpoint index %r is not a number' % args[0], file=self.stdout)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000511 return
Tim Peters2344fae2001-01-15 00:50:52 +0000512 try:
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000513 count = int(args[1].strip())
Tim Peters2344fae2001-01-15 00:50:52 +0000514 except:
515 count = 0
Guido van Rossumd8faa362007-04-27 19:54:29 +0000516 try:
517 bp = bdb.Breakpoint.bpbynumber[bpnum]
518 except IndexError:
519 print >>self.stdout, 'Breakpoint index %r is not valid' % args[0]
520 return
Tim Peters2344fae2001-01-15 00:50:52 +0000521 if bp:
522 bp.ignore = count
Guido van Rossum08454592002-07-12 13:10:53 +0000523 if count > 0:
Tim Peters2344fae2001-01-15 00:50:52 +0000524 reply = 'Will ignore next '
Guido van Rossum08454592002-07-12 13:10:53 +0000525 if count > 1:
Tim Peters2344fae2001-01-15 00:50:52 +0000526 reply = reply + '%d crossings' % count
527 else:
528 reply = reply + '1 crossing'
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000529 print(reply + ' of breakpoint %d.' % bpnum, file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000530 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000531 print('Will stop next time breakpoint', end=' ', file=self.stdout)
532 print(bpnum, 'is reached.', file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000533
534 def do_clear(self, arg):
535 """Three possibilities, tried in this order:
536 clear -> clear all breaks, ask for confirmation
537 clear file:lineno -> clear all breaks at file:lineno
538 clear bpno bpno ... -> clear breakpoints by number"""
539 if not arg:
540 try:
Guido van Rossumc5b6ab02007-05-27 09:19:52 +0000541 reply = input('Clear all breaks? ')
Tim Peters2344fae2001-01-15 00:50:52 +0000542 except EOFError:
543 reply = 'no'
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000544 reply = reply.strip().lower()
Tim Peters2344fae2001-01-15 00:50:52 +0000545 if reply in ('y', 'yes'):
546 self.clear_all_breaks()
547 return
548 if ':' in arg:
549 # Make sure it works for "clear C:\foo\bar.py:12"
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000550 i = arg.rfind(':')
Tim Peters2344fae2001-01-15 00:50:52 +0000551 filename = arg[:i]
552 arg = arg[i+1:]
553 try:
554 lineno = int(arg)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000555 except ValueError:
Tim Peters2344fae2001-01-15 00:50:52 +0000556 err = "Invalid line number (%s)" % arg
557 else:
558 err = self.clear_break(filename, lineno)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000559 if err: print('***', err, file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000560 return
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000561 numberlist = arg.split()
Tim Peters2344fae2001-01-15 00:50:52 +0000562 for i in numberlist:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000563 try:
564 i = int(i)
565 except ValueError:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000566 print('Breakpoint index %r is not a number' % i, file=self.stdout)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000567 continue
568
Georg Brandl6d2b3462005-08-24 07:36:17 +0000569 if not (0 <= i < len(bdb.Breakpoint.bpbynumber)):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000570 print('No breakpoint numbered', i, file=self.stdout)
Georg Brandl6d2b3462005-08-24 07:36:17 +0000571 continue
Tim Peters2344fae2001-01-15 00:50:52 +0000572 err = self.clear_bpbynumber(i)
573 if err:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000574 print('***', err, file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000575 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000576 print('Deleted breakpoint', i, file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000577 do_cl = do_clear # 'c' is already an abbreviation for 'continue'
578
579 def do_where(self, arg):
580 self.print_stack_trace()
581 do_w = do_where
Guido van Rossum6bd68352001-01-20 17:57:37 +0000582 do_bt = do_where
Tim Peters2344fae2001-01-15 00:50:52 +0000583
584 def do_up(self, arg):
585 if self.curindex == 0:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000586 print('*** Oldest 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_u = do_up
593
594 def do_down(self, arg):
595 if self.curindex + 1 == len(self.stack):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000596 print('*** Newest frame', file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000597 else:
598 self.curindex = self.curindex + 1
599 self.curframe = self.stack[self.curindex][0]
600 self.print_stack_entry(self.stack[self.curindex])
601 self.lineno = None
602 do_d = do_down
603
604 def do_step(self, arg):
605 self.set_step()
606 return 1
607 do_s = do_step
608
609 def do_next(self, arg):
610 self.set_next(self.curframe)
611 return 1
612 do_n = do_next
613
Guido van Rossumd8faa362007-04-27 19:54:29 +0000614 def do_run(self, arg):
615 """Restart program by raising an exception to be caught in the main debugger
616 loop. If arguments were given, set them in sys.argv."""
617 if arg:
618 import shlex
619 argv0 = sys.argv[0:1]
620 sys.argv = shlex.split(arg)
621 sys.argv[:0] = argv0
622 raise Restart
623
624 do_restart = do_run
625
Tim Peters2344fae2001-01-15 00:50:52 +0000626 def do_return(self, arg):
627 self.set_return(self.curframe)
628 return 1
629 do_r = do_return
630
631 def do_continue(self, arg):
632 self.set_continue()
633 return 1
634 do_c = do_cont = do_continue
635
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000636 def do_jump(self, arg):
637 if self.curindex + 1 != len(self.stack):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000638 print("*** You can only jump within the bottom frame", file=self.stdout)
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000639 return
640 try:
641 arg = int(arg)
642 except ValueError:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000643 print("*** The 'jump' command requires a line number.", file=self.stdout)
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000644 else:
645 try:
646 # Do the jump, fix up our copy of the stack, and display the
647 # new position
648 self.curframe.f_lineno = arg
649 self.stack[self.curindex] = self.stack[self.curindex][0], arg
650 self.print_stack_entry(self.stack[self.curindex])
Guido van Rossumb940e112007-01-10 16:19:56 +0000651 except ValueError as e:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000652 print('*** Jump failed:', e, file=self.stdout)
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000653 do_j = do_jump
654
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000655 def do_debug(self, arg):
656 sys.settrace(None)
657 globals = self.curframe.f_globals
658 locals = self.curframe.f_locals
Guido van Rossumed538d82003-04-09 19:36:34 +0000659 p = Pdb()
660 p.prompt = "(%s) " % self.prompt.strip()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000661 print("ENTERING RECURSIVE DEBUGGER", file=self.stdout)
Guido van Rossumed538d82003-04-09 19:36:34 +0000662 sys.call_tracing(p.run, (arg, globals, locals))
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000663 print("LEAVING RECURSIVE DEBUGGER", file=self.stdout)
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000664 sys.settrace(self.trace_dispatch)
665 self.lastcmd = p.lastcmd
666
Tim Peters2344fae2001-01-15 00:50:52 +0000667 def do_quit(self, arg):
Johannes Gijsbers25b38c82004-10-12 18:12:09 +0000668 self._user_requested_quit = 1
Tim Peters2344fae2001-01-15 00:50:52 +0000669 self.set_quit()
670 return 1
Johannes Gijsbers25b38c82004-10-12 18:12:09 +0000671
Tim Peters2344fae2001-01-15 00:50:52 +0000672 do_q = do_quit
Guido van Rossumd1c08f32002-04-15 00:48:24 +0000673 do_exit = do_quit
Tim Peters2344fae2001-01-15 00:50:52 +0000674
Guido van Rossumeef26072003-01-13 21:13:55 +0000675 def do_EOF(self, arg):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000676 print(file=self.stdout)
Johannes Gijsbers25b38c82004-10-12 18:12:09 +0000677 self._user_requested_quit = 1
Guido van Rossumeef26072003-01-13 21:13:55 +0000678 self.set_quit()
679 return 1
680
Tim Peters2344fae2001-01-15 00:50:52 +0000681 def do_args(self, arg):
682 f = self.curframe
683 co = f.f_code
684 dict = f.f_locals
685 n = co.co_argcount
686 if co.co_flags & 4: n = n+1
687 if co.co_flags & 8: n = n+1
688 for i in range(n):
689 name = co.co_varnames[i]
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000690 print(name, '=', end=' ', file=self.stdout)
691 if name in dict: print(dict[name], file=self.stdout)
692 else: print("*** undefined ***", file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000693 do_a = do_args
Guido van Rossum2424f851998-09-11 22:50:09 +0000694
Tim Peters2344fae2001-01-15 00:50:52 +0000695 def do_retval(self, arg):
Raymond Hettinger54f02222002-06-01 14:18:47 +0000696 if '__return__' in self.curframe.f_locals:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000697 print(self.curframe.f_locals['__return__'], file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000698 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000699 print('*** Not yet returned!', file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000700 do_rv = do_retval
Guido van Rossum2424f851998-09-11 22:50:09 +0000701
Barry Warsaw210bd202002-11-05 22:40:20 +0000702 def _getval(self, arg):
Tim Peters2344fae2001-01-15 00:50:52 +0000703 try:
Barry Warsaw210bd202002-11-05 22:40:20 +0000704 return eval(arg, self.curframe.f_globals,
705 self.curframe.f_locals)
Tim Peters2344fae2001-01-15 00:50:52 +0000706 except:
707 t, v = sys.exc_info()[:2]
Barry Warsaw210bd202002-11-05 22:40:20 +0000708 if isinstance(t, str):
Tim Peters2344fae2001-01-15 00:50:52 +0000709 exc_type_name = t
710 else: exc_type_name = t.__name__
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000711 print('***', exc_type_name + ':', repr(v), file=self.stdout)
Barry Warsaw210bd202002-11-05 22:40:20 +0000712 raise
Guido van Rossum2424f851998-09-11 22:50:09 +0000713
Barry Warsaw210bd202002-11-05 22:40:20 +0000714 def do_p(self, arg):
715 try:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000716 print(repr(self._getval(arg)), file=self.stdout)
Barry Warsaw210bd202002-11-05 22:40:20 +0000717 except:
718 pass
Georg Brandlc9879242007-09-04 07:07:56 +0000719 # make "print" an alias of "p" since print isn't a Python statement anymore
720 do_print = do_p
Barry Warsaw210bd202002-11-05 22:40:20 +0000721
722 def do_pp(self, arg):
723 try:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000724 pprint.pprint(self._getval(arg), self.stdout)
Barry Warsaw210bd202002-11-05 22:40:20 +0000725 except:
726 pass
Guido van Rossum2424f851998-09-11 22:50:09 +0000727
Tim Peters2344fae2001-01-15 00:50:52 +0000728 def do_list(self, arg):
729 self.lastcmd = 'list'
730 last = None
731 if arg:
732 try:
733 x = eval(arg, {}, {})
734 if type(x) == type(()):
735 first, last = x
736 first = int(first)
737 last = int(last)
738 if last < first:
739 # Assume it's a count
740 last = first + last
741 else:
742 first = max(1, int(x) - 5)
743 except:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000744 print('*** Error in argument:', repr(arg), file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000745 return
746 elif self.lineno is None:
747 first = max(1, self.curframe.f_lineno - 5)
748 else:
749 first = self.lineno + 1
750 if last is None:
751 last = first + 10
752 filename = self.curframe.f_code.co_filename
753 breaklist = self.get_file_breaks(filename)
754 try:
755 for lineno in range(first, last+1):
756 line = linecache.getline(filename, lineno)
757 if not line:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000758 print('[EOF]', file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000759 break
760 else:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000761 s = repr(lineno).rjust(3)
Tim Peters2344fae2001-01-15 00:50:52 +0000762 if len(s) < 4: s = s + ' '
763 if lineno in breaklist: s = s + 'B'
764 else: s = s + ' '
765 if lineno == self.curframe.f_lineno:
766 s = s + '->'
Guido van Rossumceae3752007-02-09 22:16:54 +0000767 print(s + '\t' + line, end='', file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000768 self.lineno = lineno
769 except KeyboardInterrupt:
770 pass
771 do_l = do_list
Guido van Rossum2424f851998-09-11 22:50:09 +0000772
Tim Peters2344fae2001-01-15 00:50:52 +0000773 def do_whatis(self, arg):
774 try:
775 value = eval(arg, self.curframe.f_globals,
776 self.curframe.f_locals)
777 except:
778 t, v = sys.exc_info()[:2]
779 if type(t) == type(''):
780 exc_type_name = t
781 else: exc_type_name = t.__name__
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000782 print('***', exc_type_name + ':', repr(v), file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000783 return
784 code = None
785 # Is it a function?
Neal Norwitz221085d2007-02-25 20:55:47 +0000786 try: code = value.__code__
Tim Peters2344fae2001-01-15 00:50:52 +0000787 except: pass
788 if code:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000789 print('Function', code.co_name, file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000790 return
791 # Is it an instance method?
Christian Heimesff737952007-11-27 10:40:20 +0000792 try: code = value.__func__.__code__
Tim Peters2344fae2001-01-15 00:50:52 +0000793 except: pass
794 if code:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000795 print('Method', code.co_name, file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000796 return
797 # None of the above...
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000798 print(type(value), file=self.stdout)
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000799
Tim Peters2344fae2001-01-15 00:50:52 +0000800 def do_alias(self, arg):
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000801 args = arg.split()
Tim Peters2344fae2001-01-15 00:50:52 +0000802 if len(args) == 0:
803 keys = self.aliases.keys()
804 keys.sort()
805 for alias in keys:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000806 print("%s = %s" % (alias, self.aliases[alias]), file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000807 return
Guido van Rossum08454592002-07-12 13:10:53 +0000808 if args[0] in self.aliases and len(args) == 1:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000809 print("%s = %s" % (args[0], self.aliases[args[0]]), file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000810 else:
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000811 self.aliases[args[0]] = ' '.join(args[1:])
Guido van Rossum23efba41992-01-27 16:58:47 +0000812
Tim Peters2344fae2001-01-15 00:50:52 +0000813 def do_unalias(self, arg):
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000814 args = arg.split()
Tim Peters2344fae2001-01-15 00:50:52 +0000815 if len(args) == 0: return
Raymond Hettinger54f02222002-06-01 14:18:47 +0000816 if args[0] in self.aliases:
Tim Peters2344fae2001-01-15 00:50:52 +0000817 del self.aliases[args[0]]
Guido van Rossum00230781993-03-29 11:39:45 +0000818
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000819 #list of all the commands making the program resume execution.
Thomas Wouters477c8d52006-05-27 19:21:47 +0000820 commands_resuming = ['do_continue', 'do_step', 'do_next', 'do_return',
821 'do_quit', 'do_jump']
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000822
Tim Peters2344fae2001-01-15 00:50:52 +0000823 # Print a traceback starting at the top stack frame.
824 # The most recently entered frame is printed last;
825 # this is different from dbx and gdb, but consistent with
826 # the Python interpreter's stack trace.
827 # It is also consistent with the up/down commands (which are
828 # compatible with dbx and gdb: up moves towards 'main()'
829 # and down moves towards the most recent stack frame).
Guido van Rossum2424f851998-09-11 22:50:09 +0000830
Tim Peters2344fae2001-01-15 00:50:52 +0000831 def print_stack_trace(self):
832 try:
833 for frame_lineno in self.stack:
834 self.print_stack_entry(frame_lineno)
835 except KeyboardInterrupt:
836 pass
Guido van Rossum2424f851998-09-11 22:50:09 +0000837
Tim Peters2344fae2001-01-15 00:50:52 +0000838 def print_stack_entry(self, frame_lineno, prompt_prefix=line_prefix):
839 frame, lineno = frame_lineno
840 if frame is self.curframe:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000841 print('>', end=' ', file=self.stdout)
Tim Peters2344fae2001-01-15 00:50:52 +0000842 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000843 print(' ', end=' ', file=self.stdout)
844 print(self.format_stack_entry(frame_lineno,
845 prompt_prefix), file=self.stdout)
Guido van Rossum2424f851998-09-11 22:50:09 +0000846
Guido van Rossum921c8241992-01-10 14:54:42 +0000847
Tim Peters2344fae2001-01-15 00:50:52 +0000848 # Help methods (derived from pdb.doc)
Guido van Rossum921c8241992-01-10 14:54:42 +0000849
Tim Peters2344fae2001-01-15 00:50:52 +0000850 def help_help(self):
851 self.help_h()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000852
Tim Peters2344fae2001-01-15 00:50:52 +0000853 def help_h(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000854 print("""h(elp)
Tim Peters2344fae2001-01-15 00:50:52 +0000855Without argument, print the list of available commands.
856With a command name as argument, print help about that command
857"help pdb" pipes the full documentation file to the $PAGER
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000858"help exec" gives help on the ! command""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000859
Tim Peters2344fae2001-01-15 00:50:52 +0000860 def help_where(self):
861 self.help_w()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000862
Tim Peters2344fae2001-01-15 00:50:52 +0000863 def help_w(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000864 print("""w(here)
Tim Peters2344fae2001-01-15 00:50:52 +0000865Print a stack trace, with the most recent frame at the bottom.
866An arrow indicates the "current frame", which determines the
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000867context of most commands. 'bt' is an alias for this command.""", file=self.stdout)
Guido van Rossum6bd68352001-01-20 17:57:37 +0000868
869 help_bt = help_w
Guido van Rossumb6775db1994-08-01 11:34:53 +0000870
Tim Peters2344fae2001-01-15 00:50:52 +0000871 def help_down(self):
872 self.help_d()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000873
Tim Peters2344fae2001-01-15 00:50:52 +0000874 def help_d(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000875 print("""d(own)
Tim Peters2344fae2001-01-15 00:50:52 +0000876Move the current frame one level down in the stack trace
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000877(to a newer frame).""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000878
Tim Peters2344fae2001-01-15 00:50:52 +0000879 def help_up(self):
880 self.help_u()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000881
Tim Peters2344fae2001-01-15 00:50:52 +0000882 def help_u(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000883 print("""u(p)
Tim Peters2344fae2001-01-15 00:50:52 +0000884Move the current frame one level up in the stack trace
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000885(to an older frame).""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000886
Tim Peters2344fae2001-01-15 00:50:52 +0000887 def help_break(self):
888 self.help_b()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000889
Tim Peters2344fae2001-01-15 00:50:52 +0000890 def help_b(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000891 print("""b(reak) ([file:]lineno | function) [, condition]
Tim Peters2344fae2001-01-15 00:50:52 +0000892With a line number argument, set a break there in the current
893file. With a function name, set a break at first executable line
894of that function. Without argument, list all breaks. If a second
895argument is present, it is a string specifying an expression
896which must evaluate to true before the breakpoint is honored.
Guido van Rossumb6775db1994-08-01 11:34:53 +0000897
Tim Peters2344fae2001-01-15 00:50:52 +0000898The line number may be prefixed with a filename and a colon,
899to specify a breakpoint in another file (probably one that
900hasn't been loaded yet). The file is searched for on sys.path;
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000901the .py suffix may be omitted.""", file=self.stdout)
Guido van Rossumb5699c71998-07-20 23:13:54 +0000902
Tim Peters2344fae2001-01-15 00:50:52 +0000903 def help_clear(self):
904 self.help_cl()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000905
Tim Peters2344fae2001-01-15 00:50:52 +0000906 def help_cl(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000907 print("cl(ear) filename:lineno", file=self.stdout)
908 print("""cl(ear) [bpnumber [bpnumber...]]
Tim Peters2344fae2001-01-15 00:50:52 +0000909With a space separated list of breakpoint numbers, clear
910those breakpoints. Without argument, clear all breaks (but
911first ask confirmation). With a filename:lineno argument,
Georg Brandld348b252008-01-05 20:00:55 +0000912clear all breaks at that line in that file.""", file=self.stdout)
Guido van Rossumb5699c71998-07-20 23:13:54 +0000913
Tim Peters2344fae2001-01-15 00:50:52 +0000914 def help_tbreak(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000915 print("""tbreak same arguments as break, but breakpoint is
916removed when first hit.""", file=self.stdout)
Guido van Rossum2424f851998-09-11 22:50:09 +0000917
Tim Peters2344fae2001-01-15 00:50:52 +0000918 def help_enable(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000919 print("""enable bpnumber [bpnumber ...]
Tim Peters2344fae2001-01-15 00:50:52 +0000920Enables the breakpoints given as a space separated list of
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000921bp numbers.""", file=self.stdout)
Guido van Rossum2424f851998-09-11 22:50:09 +0000922
Tim Peters2344fae2001-01-15 00:50:52 +0000923 def help_disable(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000924 print("""disable bpnumber [bpnumber ...]
Tim Peters2344fae2001-01-15 00:50:52 +0000925Disables the breakpoints given as a space separated list of
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000926bp numbers.""", file=self.stdout)
Guido van Rossum2424f851998-09-11 22:50:09 +0000927
Tim Peters2344fae2001-01-15 00:50:52 +0000928 def help_ignore(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000929 print("""ignore bpnumber count
Tim Peters2344fae2001-01-15 00:50:52 +0000930Sets the ignore count for the given breakpoint number. A breakpoint
931becomes active when the ignore count is zero. When non-zero, the
932count is decremented each time the breakpoint is reached and the
933breakpoint is not disabled and any associated condition evaluates
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000934to true.""", file=self.stdout)
Guido van Rossum2424f851998-09-11 22:50:09 +0000935
Tim Peters2344fae2001-01-15 00:50:52 +0000936 def help_condition(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000937 print("""condition bpnumber str_condition
Tim Peters2344fae2001-01-15 00:50:52 +0000938str_condition is a string specifying an expression which
939must evaluate to true before the breakpoint is honored.
940If str_condition is absent, any existing condition is removed;
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000941i.e., the breakpoint is made unconditional.""", file=self.stdout)
Guido van Rossum2424f851998-09-11 22:50:09 +0000942
Tim Peters2344fae2001-01-15 00:50:52 +0000943 def help_step(self):
944 self.help_s()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000945
Tim Peters2344fae2001-01-15 00:50:52 +0000946 def help_s(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000947 print("""s(tep)
Tim Peters2344fae2001-01-15 00:50:52 +0000948Execute the current line, stop at the first possible occasion
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000949(either in a function that is called or in the current function).""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000950
Tim Peters2344fae2001-01-15 00:50:52 +0000951 def help_next(self):
952 self.help_n()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000953
Tim Peters2344fae2001-01-15 00:50:52 +0000954 def help_n(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000955 print("""n(ext)
Tim Peters2344fae2001-01-15 00:50:52 +0000956Continue execution until the next line in the current function
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000957is reached or it returns.""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000958
Tim Peters2344fae2001-01-15 00:50:52 +0000959 def help_return(self):
960 self.help_r()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000961
Tim Peters2344fae2001-01-15 00:50:52 +0000962 def help_r(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000963 print("""r(eturn)
964Continue execution until the current function returns.""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000965
Tim Peters2344fae2001-01-15 00:50:52 +0000966 def help_continue(self):
967 self.help_c()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000968
Tim Peters2344fae2001-01-15 00:50:52 +0000969 def help_cont(self):
970 self.help_c()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000971
Tim Peters2344fae2001-01-15 00:50:52 +0000972 def help_c(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000973 print("""c(ont(inue))
974Continue execution, only stop when a breakpoint is encountered.""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000975
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000976 def help_jump(self):
977 self.help_j()
978
979 def help_j(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000980 print("""j(ump) lineno
981Set the next line that will be executed.""", file=self.stdout)
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000982
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000983 def help_debug(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000984 print("""debug code
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000985Enter a recursive debugger that steps through the code argument
986(which is an arbitrary expression or statement to be executed
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000987in the current environment).""", file=self.stdout)
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000988
Tim Peters2344fae2001-01-15 00:50:52 +0000989 def help_list(self):
990 self.help_l()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000991
Tim Peters2344fae2001-01-15 00:50:52 +0000992 def help_l(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000993 print("""l(ist) [first [,last]]
Tim Peters2344fae2001-01-15 00:50:52 +0000994List source code for the current file.
995Without arguments, list 11 lines around the current line
996or continue the previous listing.
997With one argument, list 11 lines starting at that line.
998With two arguments, list the given range;
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000999if the second argument is less than the first, it is a count.""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001000
Tim Peters2344fae2001-01-15 00:50:52 +00001001 def help_args(self):
1002 self.help_a()
Guido van Rossumb6775db1994-08-01 11:34:53 +00001003
Tim Peters2344fae2001-01-15 00:50:52 +00001004 def help_a(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001005 print("""a(rgs)
1006Print the arguments of the current function.""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001007
Tim Peters2344fae2001-01-15 00:50:52 +00001008 def help_p(self):
Georg Brandlc9879242007-09-04 07:07:56 +00001009 print("""p(rint) expression
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001010Print the value of the expression.""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001011
Barry Warsaw210bd202002-11-05 22:40:20 +00001012 def help_pp(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001013 print("""pp expression
1014Pretty-print the value of the expression.""", file=self.stdout)
Barry Warsaw210bd202002-11-05 22:40:20 +00001015
Tim Peters2344fae2001-01-15 00:50:52 +00001016 def help_exec(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001017 print("""(!) statement
Tim Peters2344fae2001-01-15 00:50:52 +00001018Execute the (one-line) statement in the context of
1019the current stack frame.
1020The exclamation point can be omitted unless the first word
1021of the statement resembles a debugger command.
1022To assign to a global variable you must always prefix the
1023command with a 'global' command, e.g.:
1024(Pdb) global list_options; list_options = ['-l']
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001025(Pdb)""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001026
Guido van Rossumd8faa362007-04-27 19:54:29 +00001027 def help_run(self):
1028 print("""run [args...]
1029Restart the debugged python program. If a string is supplied, it is
1030splitted with "shlex" and the result is used as the new sys.argv.
1031History, breakpoints, actions and debugger options are preserved.
1032"restart" is an alias for "run".""")
1033
1034 help_restart = help_run
1035
Tim Peters2344fae2001-01-15 00:50:52 +00001036 def help_quit(self):
1037 self.help_q()
Guido van Rossumb6775db1994-08-01 11:34:53 +00001038
Tim Peters2344fae2001-01-15 00:50:52 +00001039 def help_q(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001040 print("""q(uit) or exit - Quit from the debugger.
1041The program being executed is aborted.""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001042
Guido van Rossumd1c08f32002-04-15 00:48:24 +00001043 help_exit = help_q
1044
Tim Peters2344fae2001-01-15 00:50:52 +00001045 def help_whatis(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001046 print("""whatis arg
1047Prints the type of the argument.""", file=self.stdout)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001048
Tim Peters2344fae2001-01-15 00:50:52 +00001049 def help_EOF(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001050 print("""EOF
1051Handles the receipt of EOF as a command.""", file=self.stdout)
Guido van Rossum2424f851998-09-11 22:50:09 +00001052
Tim Peters2344fae2001-01-15 00:50:52 +00001053 def help_alias(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001054 print("""alias [name [command [parameter parameter ...] ]]
Tim Peters2344fae2001-01-15 00:50:52 +00001055Creates an alias called 'name' the executes 'command'. The command
1056must *not* be enclosed in quotes. Replaceable parameters are
1057indicated by %1, %2, and so on, while %* is replaced by all the
1058parameters. If no command is given, the current alias for name
1059is shown. If no name is given, all aliases are listed.
Guido van Rossum2424f851998-09-11 22:50:09 +00001060
Tim Peters2344fae2001-01-15 00:50:52 +00001061Aliases may be nested and can contain anything that can be
1062legally typed at the pdb prompt. Note! You *can* override
1063internal pdb commands with aliases! Those internal commands
1064are then hidden until the alias is removed. Aliasing is recursively
1065applied to the first word of the command line; all other words
1066in the line are left alone.
Guido van Rossum2424f851998-09-11 22:50:09 +00001067
Tim Peters2344fae2001-01-15 00:50:52 +00001068Some useful aliases (especially when placed in the .pdbrc file) are:
Guido van Rossum2424f851998-09-11 22:50:09 +00001069
Tim Peters2344fae2001-01-15 00:50:52 +00001070#Print instance variables (usage "pi classInst")
1071alias pi for k in %1.__dict__.keys(): print "%1.",k,"=",%1.__dict__[k]
Guido van Rossum2424f851998-09-11 22:50:09 +00001072
Tim Peters2344fae2001-01-15 00:50:52 +00001073#Print instance variables in self
1074alias ps pi self
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001075""", file=self.stdout)
Guido van Rossum2424f851998-09-11 22:50:09 +00001076
Tim Peters2344fae2001-01-15 00:50:52 +00001077 def help_unalias(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001078 print("""unalias name
1079Deletes the specified alias.""", file=self.stdout)
Guido van Rossum2424f851998-09-11 22:50:09 +00001080
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001081 def help_commands(self):
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001082 print("""commands [bpnumber]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001083(com) ...
1084(com) end
1085(Pdb)
1086
1087Specify a list of commands for breakpoint number bpnumber. The
1088commands themselves appear on the following lines. Type a line
1089containing just 'end' to terminate the commands.
1090
1091To remove all commands from a breakpoint, type commands and
1092follow it immediately with end; that is, give no commands.
1093
1094With no bpnumber argument, commands refers to the last
1095breakpoint set.
1096
1097You can use breakpoint commands to start your program up again.
1098Simply use the continue command, or step, or any other
1099command that resumes execution.
1100
1101Specifying any command resuming execution (currently continue,
1102step, next, return, jump, quit and their abbreviations) terminates
1103the command list (as if that command was immediately followed by end).
1104This is because any time you resume execution
1105(even with a simple next or step), you may encounter
1106another breakpoint--which could have its own command list, leading to
1107ambiguities about which list to execute.
1108
1109 If you use the 'silent' command in the command list, the
1110usual message about stopping at a breakpoint is not printed. This may
1111be desirable for breakpoints that are to print a specific message and
1112then continue. If none of the other commands print anything, you
1113see no sign that the breakpoint was reached.
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001114""", file=self.stdout)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001115
Tim Peters2344fae2001-01-15 00:50:52 +00001116 def help_pdb(self):
1117 help()
Guido van Rossumb6775db1994-08-01 11:34:53 +00001118
Tim Peters2344fae2001-01-15 00:50:52 +00001119 def lookupmodule(self, filename):
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001120 """Helper function for break/clear parsing -- may be overridden.
1121
1122 lookupmodule() translates (possibly incomplete) file or module name
1123 into an absolute file name.
1124 """
1125 if os.path.isabs(filename) and os.path.exists(filename):
Tim Peterse718f612004-10-12 21:51:32 +00001126 return filename
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001127 f = os.path.join(sys.path[0], filename)
1128 if os.path.exists(f) and self.canonic(f) == self.mainpyfile:
1129 return f
Tim Peters2344fae2001-01-15 00:50:52 +00001130 root, ext = os.path.splitext(filename)
1131 if ext == '':
1132 filename = filename + '.py'
1133 if os.path.isabs(filename):
1134 return filename
1135 for dirname in sys.path:
1136 while os.path.islink(dirname):
1137 dirname = os.readlink(dirname)
1138 fullname = os.path.join(dirname, filename)
1139 if os.path.exists(fullname):
1140 return fullname
1141 return None
Guido van Rossumb5699c71998-07-20 23:13:54 +00001142
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001143 def _runscript(self, filename):
Guido van Rossumd8faa362007-04-27 19:54:29 +00001144 # The script has to run in __main__ namespace (or imports from
1145 # __main__ will break).
1146 #
1147 # So we clear up the __main__ and set several special variables
1148 # (this gets rid of pdb's globals and cleans old variables on restarts).
1149 import __main__
1150 __main__.__dict__.clear()
1151 __main__.__dict__.update({"__name__" : "__main__",
1152 "__file__" : filename,
1153 "__builtins__": __builtins__,
1154 })
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001155
1156 # When bdb sets tracing, a number of call and line events happens
1157 # BEFORE debugger even reaches user's code (and the exact sequence of
1158 # events depends on python version). So we take special measures to
1159 # avoid stopping before we reach the main script (see user_line and
1160 # user_call for details).
1161 self._wait_for_mainpyfile = 1
1162 self.mainpyfile = self.canonic(filename)
1163 self._user_requested_quit = 0
Georg Brandl7dd803a2007-09-12 19:44:18 +00001164 with open(filename) as fp:
1165 statement = fp.read()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001166 self.run(statement)
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001167
Guido van Rossum35771131992-09-08 11:59:04 +00001168# Simplified interface
1169
Guido van Rossum5e38b6f1995-02-27 13:13:40 +00001170def run(statement, globals=None, locals=None):
Tim Peters2344fae2001-01-15 00:50:52 +00001171 Pdb().run(statement, globals, locals)
Guido van Rossum5e38b6f1995-02-27 13:13:40 +00001172
1173def runeval(expression, globals=None, locals=None):
Tim Peters2344fae2001-01-15 00:50:52 +00001174 return Pdb().runeval(expression, globals, locals)
Guido van Rossum6fe08b01992-01-16 13:50:21 +00001175
1176def runctx(statement, globals, locals):
Tim Peters2344fae2001-01-15 00:50:52 +00001177 # B/W compatibility
1178 run(statement, globals, locals)
Guido van Rossum6fe08b01992-01-16 13:50:21 +00001179
Raymond Hettinger2ef7e6c2004-10-24 00:32:24 +00001180def runcall(*args, **kwds):
1181 return Pdb().runcall(*args, **kwds)
Guido van Rossum4e160981992-09-02 20:43:20 +00001182
Guido van Rossumb6775db1994-08-01 11:34:53 +00001183def set_trace():
Johannes Gijsbers84a6c202004-11-07 11:35:30 +00001184 Pdb().set_trace(sys._getframe().f_back)
Guido van Rossum35771131992-09-08 11:59:04 +00001185
1186# Post-Mortem interface
1187
1188def post_mortem(t):
Tim Peters2344fae2001-01-15 00:50:52 +00001189 p = Pdb()
1190 p.reset()
1191 while t.tb_next is not None:
1192 t = t.tb_next
1193 p.interaction(t.tb_frame, t)
Guido van Rossum35771131992-09-08 11:59:04 +00001194
1195def pm():
Tim Peters2344fae2001-01-15 00:50:52 +00001196 post_mortem(sys.last_traceback)
Guido van Rossum35771131992-09-08 11:59:04 +00001197
1198
1199# Main program for testing
1200
Guido van Rossum23efba41992-01-27 16:58:47 +00001201TESTCMD = 'import x; x.main()'
Guido van Rossum6fe08b01992-01-16 13:50:21 +00001202
Guido van Rossum921c8241992-01-10 14:54:42 +00001203def test():
Tim Peters2344fae2001-01-15 00:50:52 +00001204 run(TESTCMD)
Guido van Rossume61fa0a1993-10-22 13:56:35 +00001205
1206# print help
1207def help():
Tim Peters2344fae2001-01-15 00:50:52 +00001208 for dirname in sys.path:
1209 fullname = os.path.join(dirname, 'pdb.doc')
1210 if os.path.exists(fullname):
1211 sts = os.system('${PAGER-more} '+fullname)
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001212 if sts: print('*** Pager exit status:', sts)
Tim Peters2344fae2001-01-15 00:50:52 +00001213 break
1214 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001215 print('Sorry, can\'t find the help file "pdb.doc"', end=' ')
1216 print('along the Python search path')
Guido van Rossumf17361d1996-07-30 16:28:13 +00001217
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001218def main():
Tim Peters2344fae2001-01-15 00:50:52 +00001219 if not sys.argv[1:]:
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001220 print("usage: pdb.py scriptfile [arg] ...")
Tim Peters2344fae2001-01-15 00:50:52 +00001221 sys.exit(2)
Guido van Rossumf17361d1996-07-30 16:28:13 +00001222
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001223 mainpyfile = sys.argv[1] # Get script filename
1224 if not os.path.exists(mainpyfile):
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001225 print('Error:', mainpyfile, 'does not exist')
Tim Peters2344fae2001-01-15 00:50:52 +00001226 sys.exit(1)
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001227
Tim Peters2344fae2001-01-15 00:50:52 +00001228 del sys.argv[0] # Hide "pdb.py" from argument list
Guido van Rossumec577d51996-09-10 17:39:34 +00001229
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001230 # Replace pdb's dir with script's dir in front of module search path.
1231 sys.path[0] = os.path.dirname(mainpyfile)
Guido van Rossumf17361d1996-07-30 16:28:13 +00001232
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001233 # Note on saving/restoring sys.argv: it's a good idea when sys.argv was
1234 # modified by the script being debugged. It's a bad idea when it was
Guido van Rossumd8faa362007-04-27 19:54:29 +00001235 # changed by the user from the command line. There is a "restart" command which
1236 # allows explicit specification of command line arguments.
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001237 pdb = Pdb()
1238 while 1:
1239 try:
1240 pdb._runscript(mainpyfile)
1241 if pdb._user_requested_quit:
1242 break
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001243 print("The program finished and will be restarted")
Guido van Rossumd8faa362007-04-27 19:54:29 +00001244 except Restart:
1245 print("Restarting", mainpyfile, "with arguments:")
1246 print("\t" + " ".join(sys.argv[1:]))
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001247 except SystemExit:
1248 # In most cases SystemExit does not warrant a post-mortem session.
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001249 print("The program exited via sys.exit(). Exit status: ", end=' ')
1250 print(sys.exc_info()[1])
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001251 except:
1252 traceback.print_exc()
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001253 print("Uncaught exception. Entering post mortem debugging")
1254 print("Running 'cont' or 'step' will restart the program")
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001255 t = sys.exc_info()[2]
1256 while t.tb_next is not None:
1257 t = t.tb_next
1258 pdb.interaction(t.tb_frame,t)
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001259 print("Post mortem debugger finished. The "+mainpyfile+" will be restarted")
Johannes Gijsbers25b38c82004-10-12 18:12:09 +00001260
1261
1262# When invoked as main program, invoke the debugger on a script
Guido van Rossumd8faa362007-04-27 19:54:29 +00001263if __name__ == '__main__':
1264 import pdb
1265 pdb.main()