blob: ae2fab6c56f309996591419e1a7d65a6f9fd0440 [file] [log] [blame]
Guido van Rossumf06ee5f1996-11-27 19:52:01 +00001#! /usr/bin/env python
Guido van Rossumf17361d1996-07-30 16:28:13 +00002
Guido van Rossum4b8c6ea2000-02-04 15:39:30 +00003"""A Python debugger."""
Guido van Rossum92df0c61992-01-14 18:30:15 +00004
Guido van Rossum23efba41992-01-27 16:58:47 +00005# (See pdb.doc for documentation.)
Guido van Rossum921c8241992-01-10 14:54:42 +00006
Guido van Rossum921c8241992-01-10 14:54:42 +00007import sys
8import linecache
Guido van Rossum23efba41992-01-27 16:58:47 +00009import cmd
10import bdb
Guido van Rossumef1b41b2002-09-10 21:57:14 +000011from repr import Repr
Guido van Rossumb5699c71998-07-20 23:13:54 +000012import os
Barry Warsaw2bee8fe1999-09-09 16:32:41 +000013import re
Barry Warsaw210bd202002-11-05 22:40:20 +000014import pprint
Barry Warsaw2bee8fe1999-09-09 16:32:41 +000015
Guido van Rossumef1b41b2002-09-10 21:57:14 +000016# Create a custom safe Repr instance and increase its maxstring.
17# The default of 30 truncates error messages too easily.
18_repr = Repr()
19_repr.maxstring = 200
20_saferepr = _repr.repr
21
Skip Montanaro352674d2001-02-07 23:14:30 +000022__all__ = ["run", "pm", "Pdb", "runeval", "runctx", "runcall", "set_trace",
23 "post_mortem", "help"]
24
Barry Warsaw2bee8fe1999-09-09 16:32:41 +000025def find_function(funcname, filename):
Tim Peters2344fae2001-01-15 00:50:52 +000026 cre = re.compile(r'def\s+%s\s*[(]' % funcname)
27 try:
28 fp = open(filename)
29 except IOError:
30 return None
31 # consumer of this info expects the first line to be 1
32 lineno = 1
33 answer = None
34 while 1:
35 line = fp.readline()
36 if line == '':
37 break
38 if cre.match(line):
39 answer = funcname, filename, lineno
40 break
41 lineno = lineno + 1
42 fp.close()
43 return answer
Guido van Rossum921c8241992-01-10 14:54:42 +000044
45
Guido van Rossuma558e371994-11-10 22:27:35 +000046# Interaction prompt line will separate file and call info from code
47# text using value of line_prefix string. A newline and arrow may
48# be to your liking. You can set it once pdb is imported using the
49# command "pdb.line_prefix = '\n% '".
Tim Peters2344fae2001-01-15 00:50:52 +000050# line_prefix = ': ' # Use this to get the old situation back
51line_prefix = '\n-> ' # Probably a better default
Guido van Rossuma558e371994-11-10 22:27:35 +000052
Guido van Rossum23efba41992-01-27 16:58:47 +000053class Pdb(bdb.Bdb, cmd.Cmd):
Guido van Rossum2424f851998-09-11 22:50:09 +000054
Tim Peters2344fae2001-01-15 00:50:52 +000055 def __init__(self):
56 bdb.Bdb.__init__(self)
57 cmd.Cmd.__init__(self)
58 self.prompt = '(Pdb) '
59 self.aliases = {}
60 # Try to load readline if it exists
61 try:
62 import readline
63 except ImportError:
64 pass
Guido van Rossum2424f851998-09-11 22:50:09 +000065
Tim Peters2344fae2001-01-15 00:50:52 +000066 # Read $HOME/.pdbrc and ./.pdbrc
67 self.rcLines = []
Raymond Hettinger54f02222002-06-01 14:18:47 +000068 if 'HOME' in os.environ:
Tim Peters2344fae2001-01-15 00:50:52 +000069 envHome = os.environ['HOME']
70 try:
71 rcFile = open(os.path.join(envHome, ".pdbrc"))
72 except IOError:
73 pass
74 else:
75 for line in rcFile.readlines():
76 self.rcLines.append(line)
77 rcFile.close()
78 try:
79 rcFile = open(".pdbrc")
80 except IOError:
81 pass
82 else:
83 for line in rcFile.readlines():
84 self.rcLines.append(line)
85 rcFile.close()
Guido van Rossum23efba41992-01-27 16:58:47 +000086
Tim Peters2344fae2001-01-15 00:50:52 +000087 def reset(self):
88 bdb.Bdb.reset(self)
89 self.forget()
Guido van Rossum23efba41992-01-27 16:58:47 +000090
Tim Peters2344fae2001-01-15 00:50:52 +000091 def forget(self):
92 self.lineno = None
93 self.stack = []
94 self.curindex = 0
95 self.curframe = None
Guido van Rossum2424f851998-09-11 22:50:09 +000096
Tim Peters2344fae2001-01-15 00:50:52 +000097 def setup(self, f, t):
98 self.forget()
99 self.stack, self.curindex = self.get_stack(f, t)
100 self.curframe = self.stack[self.curindex][0]
101 self.execRcLines()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000102
Tim Peters2344fae2001-01-15 00:50:52 +0000103 # Can be executed earlier than 'setup' if desired
104 def execRcLines(self):
105 if self.rcLines:
106 # Make local copy because of recursion
107 rcLines = self.rcLines
108 # executed only once
109 self.rcLines = []
110 for line in rcLines:
111 line = line[:-1]
Guido van Rossum08454592002-07-12 13:10:53 +0000112 if len(line) > 0 and line[0] != '#':
113 self.onecmd(line)
Guido van Rossum2424f851998-09-11 22:50:09 +0000114
Tim Peters280488b2002-08-23 18:19:30 +0000115 # Override Bdb methods
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000116
117 def user_call(self, frame, argument_list):
118 """This method is called when there is the remote possibility
119 that we ever need to stop in this function."""
Michael W. Hudson01eb85c2003-01-31 17:48:29 +0000120 if self.stop_here(frame):
121 print '--Call--'
122 self.interaction(frame, None)
Guido van Rossum2424f851998-09-11 22:50:09 +0000123
Tim Peters2344fae2001-01-15 00:50:52 +0000124 def user_line(self, frame):
125 """This function is called when we stop or break at this line."""
126 self.interaction(frame, None)
Guido van Rossum9e1ee971997-07-11 13:43:53 +0000127
Tim Peters2344fae2001-01-15 00:50:52 +0000128 def user_return(self, frame, return_value):
129 """This function is called when a return trap is set here."""
130 frame.f_locals['__return__'] = return_value
131 print '--Return--'
132 self.interaction(frame, None)
Guido van Rossum2424f851998-09-11 22:50:09 +0000133
Tim Peters2344fae2001-01-15 00:50:52 +0000134 def user_exception(self, frame, (exc_type, exc_value, exc_traceback)):
135 """This function is called if an exception occurs,
136 but only if we are to stop at or just below this level."""
137 frame.f_locals['__exception__'] = exc_type, exc_value
138 if type(exc_type) == type(''):
139 exc_type_name = exc_type
140 else: exc_type_name = exc_type.__name__
Tim Peters6f8ee592001-02-09 23:28:07 +0000141 print exc_type_name + ':', _saferepr(exc_value)
Tim Peters2344fae2001-01-15 00:50:52 +0000142 self.interaction(frame, exc_traceback)
Guido van Rossum2424f851998-09-11 22:50:09 +0000143
Tim Peters2344fae2001-01-15 00:50:52 +0000144 # General interaction function
145
146 def interaction(self, frame, traceback):
147 self.setup(frame, traceback)
148 self.print_stack_entry(self.stack[self.curindex])
149 self.cmdloop()
150 self.forget()
151
152 def default(self, line):
153 if line[:1] == '!': line = line[1:]
154 locals = self.curframe.f_locals
155 globals = self.curframe.f_globals
156 try:
157 code = compile(line + '\n', '<stdin>', 'single')
158 exec code in globals, locals
159 except:
160 t, v = sys.exc_info()[:2]
161 if type(t) == type(''):
162 exc_type_name = t
163 else: exc_type_name = t.__name__
164 print '***', exc_type_name + ':', v
165
166 def precmd(self, line):
167 """Handle alias expansion and ';;' separator."""
Guido van Rossum08454592002-07-12 13:10:53 +0000168 if not line.strip():
Tim Peters2344fae2001-01-15 00:50:52 +0000169 return line
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000170 args = line.split()
Raymond Hettinger54f02222002-06-01 14:18:47 +0000171 while args[0] in self.aliases:
Tim Peters2344fae2001-01-15 00:50:52 +0000172 line = self.aliases[args[0]]
173 ii = 1
174 for tmpArg in args[1:]:
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000175 line = line.replace("%" + str(ii),
Tim Peters2344fae2001-01-15 00:50:52 +0000176 tmpArg)
177 ii = ii + 1
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000178 line = line.replace("%*", ' '.join(args[1:]))
179 args = line.split()
Tim Peters2344fae2001-01-15 00:50:52 +0000180 # split into ';;' separated commands
181 # unless it's an alias command
182 if args[0] != 'alias':
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000183 marker = line.find(';;')
Tim Peters2344fae2001-01-15 00:50:52 +0000184 if marker >= 0:
185 # queue up everything after marker
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000186 next = line[marker+2:].lstrip()
Tim Peters2344fae2001-01-15 00:50:52 +0000187 self.cmdqueue.append(next)
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000188 line = line[:marker].rstrip()
Tim Peters2344fae2001-01-15 00:50:52 +0000189 return line
190
191 # Command definitions, called by cmdloop()
192 # The argument is the remaining string on the command line
193 # Return true to exit from the command loop
194
195 do_h = cmd.Cmd.do_help
196
Tim Peters2344fae2001-01-15 00:50:52 +0000197 def do_break(self, arg, temporary = 0):
198 # break [ ([filename:]lineno | function) [, "condition"] ]
199 if not arg:
200 if self.breaks: # There's at least one
201 print "Num Type Disp Enb Where"
202 for bp in bdb.Breakpoint.bpbynumber:
203 if bp:
204 bp.bpprint()
205 return
206 # parse arguments; comma has lowest precedence
207 # and cannot occur in filename
208 filename = None
209 lineno = None
210 cond = None
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000211 comma = arg.find(',')
Tim Peters2344fae2001-01-15 00:50:52 +0000212 if comma > 0:
213 # parse stuff after comma: "condition"
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000214 cond = arg[comma+1:].lstrip()
215 arg = arg[:comma].rstrip()
Tim Peters2344fae2001-01-15 00:50:52 +0000216 # parse stuff before comma: [filename:]lineno | function
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000217 colon = arg.rfind(':')
Tim Peters2344fae2001-01-15 00:50:52 +0000218 if colon >= 0:
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000219 filename = arg[:colon].rstrip()
Tim Peters2344fae2001-01-15 00:50:52 +0000220 f = self.lookupmodule(filename)
221 if not f:
222 print '*** ', `filename`,
223 print 'not found from sys.path'
224 return
225 else:
226 filename = f
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000227 arg = arg[colon+1:].lstrip()
Tim Peters2344fae2001-01-15 00:50:52 +0000228 try:
229 lineno = int(arg)
230 except ValueError, msg:
231 print '*** Bad lineno:', arg
232 return
233 else:
234 # no colon; can be lineno or function
235 try:
236 lineno = int(arg)
237 except ValueError:
238 try:
239 func = eval(arg,
240 self.curframe.f_globals,
241 self.curframe.f_locals)
242 except:
243 func = arg
244 try:
245 if hasattr(func, 'im_func'):
246 func = func.im_func
247 code = func.func_code
248 lineno = code.co_firstlineno
249 filename = code.co_filename
250 except:
251 # last thing to try
252 (ok, filename, ln) = self.lineinfo(arg)
253 if not ok:
254 print '*** The specified object',
255 print `arg`,
256 print 'is not a function'
257 print ('or was not found '
258 'along sys.path.')
259 return
260 lineno = int(ln)
261 if not filename:
262 filename = self.defaultFile()
263 # Check for reasonable breakpoint
264 line = self.checkline(filename, lineno)
265 if line:
266 # now set the break point
267 err = self.set_break(filename, line, temporary, cond)
268 if err: print '***', err
269 else:
270 bp = self.get_breaks(filename, line)[-1]
271 print "Breakpoint %d at %s:%d" % (bp.number,
272 bp.file,
273 bp.line)
274
275 # To be overridden in derived debuggers
276 def defaultFile(self):
277 """Produce a reasonable default."""
278 filename = self.curframe.f_code.co_filename
279 if filename == '<string>' and mainpyfile:
280 filename = mainpyfile
281 return filename
282
283 do_b = do_break
284
285 def do_tbreak(self, arg):
286 self.do_break(arg, 1)
287
288 def lineinfo(self, identifier):
289 failed = (None, None, None)
290 # Input is identifier, may be in single quotes
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000291 idstring = identifier.split("'")
Tim Peters2344fae2001-01-15 00:50:52 +0000292 if len(idstring) == 1:
293 # not in single quotes
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000294 id = idstring[0].strip()
Tim Peters2344fae2001-01-15 00:50:52 +0000295 elif len(idstring) == 3:
296 # quoted
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000297 id = idstring[1].strip()
Tim Peters2344fae2001-01-15 00:50:52 +0000298 else:
299 return failed
300 if id == '': return failed
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000301 parts = id.split('.')
Tim Peters2344fae2001-01-15 00:50:52 +0000302 # Protection for derived debuggers
303 if parts[0] == 'self':
304 del parts[0]
305 if len(parts) == 0:
306 return failed
307 # Best first guess at file to look at
308 fname = self.defaultFile()
309 if len(parts) == 1:
310 item = parts[0]
311 else:
312 # More than one part.
313 # First is module, second is method/class
314 f = self.lookupmodule(parts[0])
315 if f:
316 fname = f
317 item = parts[1]
318 answer = find_function(item, fname)
319 return answer or failed
320
321 def checkline(self, filename, lineno):
322 """Return line number of first line at or after input
323 argument such that if the input points to a 'def', the
324 returned line number is the first
325 non-blank/non-comment line to follow. If the input
326 points to a blank or comment line, return 0. At end
327 of file, also return 0."""
328
329 line = linecache.getline(filename, lineno)
330 if not line:
331 print 'End of file'
332 return 0
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000333 line = line.strip()
Tim Peters2344fae2001-01-15 00:50:52 +0000334 # Don't allow setting breakpoint at a blank line
Guido van Rossum08454592002-07-12 13:10:53 +0000335 if (not line or (line[0] == '#') or
336 (line[:3] == '"""') or line[:3] == "'''"):
Tim Peters2344fae2001-01-15 00:50:52 +0000337 print '*** Blank or comment'
338 return 0
339 # When a file is read in and a breakpoint is at
340 # the 'def' statement, the system stops there at
341 # code parse time. We don't want that, so all breakpoints
342 # set at 'def' statements are moved one line onward
343 if line[:3] == 'def':
344 instr = ''
345 brackets = 0
346 while 1:
347 skipone = 0
348 for c in line:
349 if instr:
350 if skipone:
351 skipone = 0
352 elif c == '\\':
353 skipone = 1
354 elif c == instr:
355 instr = ''
356 elif c == '#':
357 break
358 elif c in ('"',"'"):
359 instr = c
360 elif c in ('(','{','['):
361 brackets = brackets + 1
362 elif c in (')','}',']'):
363 brackets = brackets - 1
364 lineno = lineno+1
365 line = linecache.getline(filename, lineno)
366 if not line:
367 print 'end of file'
368 return 0
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000369 line = line.strip()
Tim Peters2344fae2001-01-15 00:50:52 +0000370 if not line: continue # Blank line
371 if brackets <= 0 and line[0] not in ('#','"',"'"):
372 break
373 return lineno
374
375 def do_enable(self, arg):
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000376 args = arg.split()
Tim Peters2344fae2001-01-15 00:50:52 +0000377 for i in args:
Andrew M. Kuchlingb1f8bab2003-05-22 14:46:12 +0000378 try:
379 i = int(i)
380 except ValueError:
381 print 'Breakpoint index %r is not a number' % i
382 continue
383
384 if not (0 <= i < len(bdb.Breakpoint.bpbynumber)):
385 print 'No breakpoint numbered', i
386 continue
387
388 bp = bdb.Breakpoint.bpbynumber[i]
Tim Peters2344fae2001-01-15 00:50:52 +0000389 if bp:
390 bp.enable()
391
392 def do_disable(self, arg):
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000393 args = arg.split()
Tim Peters2344fae2001-01-15 00:50:52 +0000394 for i in args:
Andrew M. Kuchlingb1f8bab2003-05-22 14:46:12 +0000395 try:
396 i = int(i)
397 except ValueError:
398 print 'Breakpoint index %r is not a number' % i
399 continue
400
401 if not (0 <= i < len(bdb.Breakpoint.bpbynumber)):
402 print 'No breakpoint numbered', i
403 continue
404
405 bp = bdb.Breakpoint.bpbynumber[i]
Tim Peters2344fae2001-01-15 00:50:52 +0000406 if bp:
407 bp.disable()
408
409 def do_condition(self, arg):
410 # arg is breakpoint number and condition
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000411 args = arg.split(' ', 1)
412 bpnum = int(args[0].strip())
Tim Peters2344fae2001-01-15 00:50:52 +0000413 try:
414 cond = args[1]
415 except:
416 cond = None
417 bp = bdb.Breakpoint.bpbynumber[bpnum]
418 if bp:
419 bp.cond = cond
420 if not cond:
421 print 'Breakpoint', bpnum,
422 print 'is now unconditional.'
423
424 def do_ignore(self,arg):
425 """arg is bp number followed by ignore count."""
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000426 args = arg.split()
427 bpnum = int(args[0].strip())
Tim Peters2344fae2001-01-15 00:50:52 +0000428 try:
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000429 count = int(args[1].strip())
Tim Peters2344fae2001-01-15 00:50:52 +0000430 except:
431 count = 0
432 bp = bdb.Breakpoint.bpbynumber[bpnum]
433 if bp:
434 bp.ignore = count
Guido van Rossum08454592002-07-12 13:10:53 +0000435 if count > 0:
Tim Peters2344fae2001-01-15 00:50:52 +0000436 reply = 'Will ignore next '
Guido van Rossum08454592002-07-12 13:10:53 +0000437 if count > 1:
Tim Peters2344fae2001-01-15 00:50:52 +0000438 reply = reply + '%d crossings' % count
439 else:
440 reply = reply + '1 crossing'
441 print reply + ' of breakpoint %d.' % bpnum
442 else:
443 print 'Will stop next time breakpoint',
444 print bpnum, 'is reached.'
445
446 def do_clear(self, arg):
447 """Three possibilities, tried in this order:
448 clear -> clear all breaks, ask for confirmation
449 clear file:lineno -> clear all breaks at file:lineno
450 clear bpno bpno ... -> clear breakpoints by number"""
451 if not arg:
452 try:
453 reply = raw_input('Clear all breaks? ')
454 except EOFError:
455 reply = 'no'
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000456 reply = reply.strip().lower()
Tim Peters2344fae2001-01-15 00:50:52 +0000457 if reply in ('y', 'yes'):
458 self.clear_all_breaks()
459 return
460 if ':' in arg:
461 # Make sure it works for "clear C:\foo\bar.py:12"
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000462 i = arg.rfind(':')
Tim Peters2344fae2001-01-15 00:50:52 +0000463 filename = arg[:i]
464 arg = arg[i+1:]
465 try:
466 lineno = int(arg)
467 except:
468 err = "Invalid line number (%s)" % arg
469 else:
470 err = self.clear_break(filename, lineno)
471 if err: print '***', err
472 return
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000473 numberlist = arg.split()
Tim Peters2344fae2001-01-15 00:50:52 +0000474 for i in numberlist:
475 err = self.clear_bpbynumber(i)
476 if err:
477 print '***', err
478 else:
479 print 'Deleted breakpoint %s ' % (i,)
480 do_cl = do_clear # 'c' is already an abbreviation for 'continue'
481
482 def do_where(self, arg):
483 self.print_stack_trace()
484 do_w = do_where
Guido van Rossum6bd68352001-01-20 17:57:37 +0000485 do_bt = do_where
Tim Peters2344fae2001-01-15 00:50:52 +0000486
487 def do_up(self, arg):
488 if self.curindex == 0:
489 print '*** Oldest frame'
490 else:
491 self.curindex = self.curindex - 1
492 self.curframe = self.stack[self.curindex][0]
493 self.print_stack_entry(self.stack[self.curindex])
494 self.lineno = None
495 do_u = do_up
496
497 def do_down(self, arg):
498 if self.curindex + 1 == len(self.stack):
499 print '*** Newest frame'
500 else:
501 self.curindex = self.curindex + 1
502 self.curframe = self.stack[self.curindex][0]
503 self.print_stack_entry(self.stack[self.curindex])
504 self.lineno = None
505 do_d = do_down
506
507 def do_step(self, arg):
508 self.set_step()
509 return 1
510 do_s = do_step
511
512 def do_next(self, arg):
513 self.set_next(self.curframe)
514 return 1
515 do_n = do_next
516
517 def do_return(self, arg):
518 self.set_return(self.curframe)
519 return 1
520 do_r = do_return
521
522 def do_continue(self, arg):
523 self.set_continue()
524 return 1
525 do_c = do_cont = do_continue
526
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000527 def do_jump(self, arg):
528 if self.curindex + 1 != len(self.stack):
529 print "*** You can only jump within the bottom frame"
530 return
531 try:
532 arg = int(arg)
533 except ValueError:
534 print "*** The 'jump' command requires a line number."
535 else:
536 try:
537 # Do the jump, fix up our copy of the stack, and display the
538 # new position
539 self.curframe.f_lineno = arg
540 self.stack[self.curindex] = self.stack[self.curindex][0], arg
541 self.print_stack_entry(self.stack[self.curindex])
542 except ValueError, e:
543 print '*** Jump failed:', e
544 do_j = do_jump
545
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000546 def do_debug(self, arg):
547 sys.settrace(None)
548 globals = self.curframe.f_globals
549 locals = self.curframe.f_locals
Guido van Rossumed538d82003-04-09 19:36:34 +0000550 p = Pdb()
551 p.prompt = "(%s) " % self.prompt.strip()
552 print "ENTERING RECURSIVE DEBUGGER"
553 sys.call_tracing(p.run, (arg, globals, locals))
554 print "LEAVING RECURSIVE DEBUGGER"
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000555 sys.settrace(self.trace_dispatch)
556 self.lastcmd = p.lastcmd
557
Tim Peters2344fae2001-01-15 00:50:52 +0000558 def do_quit(self, arg):
559 self.set_quit()
560 return 1
561 do_q = do_quit
Guido van Rossumd1c08f32002-04-15 00:48:24 +0000562 do_exit = do_quit
Tim Peters2344fae2001-01-15 00:50:52 +0000563
Guido van Rossumeef26072003-01-13 21:13:55 +0000564 def do_EOF(self, arg):
565 print
566 self.set_quit()
567 return 1
568
Tim Peters2344fae2001-01-15 00:50:52 +0000569 def do_args(self, arg):
570 f = self.curframe
571 co = f.f_code
572 dict = f.f_locals
573 n = co.co_argcount
574 if co.co_flags & 4: n = n+1
575 if co.co_flags & 8: n = n+1
576 for i in range(n):
577 name = co.co_varnames[i]
578 print name, '=',
Raymond Hettinger54f02222002-06-01 14:18:47 +0000579 if name in dict: print dict[name]
Tim Peters2344fae2001-01-15 00:50:52 +0000580 else: print "*** undefined ***"
581 do_a = do_args
Guido van Rossum2424f851998-09-11 22:50:09 +0000582
Tim Peters2344fae2001-01-15 00:50:52 +0000583 def do_retval(self, arg):
Raymond Hettinger54f02222002-06-01 14:18:47 +0000584 if '__return__' in self.curframe.f_locals:
Tim Peters2344fae2001-01-15 00:50:52 +0000585 print self.curframe.f_locals['__return__']
586 else:
587 print '*** Not yet returned!'
588 do_rv = do_retval
Guido van Rossum2424f851998-09-11 22:50:09 +0000589
Barry Warsaw210bd202002-11-05 22:40:20 +0000590 def _getval(self, arg):
Tim Peters2344fae2001-01-15 00:50:52 +0000591 try:
Barry Warsaw210bd202002-11-05 22:40:20 +0000592 return eval(arg, self.curframe.f_globals,
593 self.curframe.f_locals)
Tim Peters2344fae2001-01-15 00:50:52 +0000594 except:
595 t, v = sys.exc_info()[:2]
Barry Warsaw210bd202002-11-05 22:40:20 +0000596 if isinstance(t, str):
Tim Peters2344fae2001-01-15 00:50:52 +0000597 exc_type_name = t
598 else: exc_type_name = t.__name__
599 print '***', exc_type_name + ':', `v`
Barry Warsaw210bd202002-11-05 22:40:20 +0000600 raise
Guido van Rossum2424f851998-09-11 22:50:09 +0000601
Barry Warsaw210bd202002-11-05 22:40:20 +0000602 def do_p(self, arg):
603 try:
604 print repr(self._getval(arg))
605 except:
606 pass
607
608 def do_pp(self, arg):
609 try:
610 pprint.pprint(self._getval(arg))
611 except:
612 pass
Guido van Rossum2424f851998-09-11 22:50:09 +0000613
Tim Peters2344fae2001-01-15 00:50:52 +0000614 def do_list(self, arg):
615 self.lastcmd = 'list'
616 last = None
617 if arg:
618 try:
619 x = eval(arg, {}, {})
620 if type(x) == type(()):
621 first, last = x
622 first = int(first)
623 last = int(last)
624 if last < first:
625 # Assume it's a count
626 last = first + last
627 else:
628 first = max(1, int(x) - 5)
629 except:
630 print '*** Error in argument:', `arg`
631 return
632 elif self.lineno is None:
633 first = max(1, self.curframe.f_lineno - 5)
634 else:
635 first = self.lineno + 1
636 if last is None:
637 last = first + 10
638 filename = self.curframe.f_code.co_filename
639 breaklist = self.get_file_breaks(filename)
640 try:
641 for lineno in range(first, last+1):
642 line = linecache.getline(filename, lineno)
643 if not line:
644 print '[EOF]'
645 break
646 else:
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000647 s = `lineno`.rjust(3)
Tim Peters2344fae2001-01-15 00:50:52 +0000648 if len(s) < 4: s = s + ' '
649 if lineno in breaklist: s = s + 'B'
650 else: s = s + ' '
651 if lineno == self.curframe.f_lineno:
652 s = s + '->'
653 print s + '\t' + line,
654 self.lineno = lineno
655 except KeyboardInterrupt:
656 pass
657 do_l = do_list
Guido van Rossum2424f851998-09-11 22:50:09 +0000658
Tim Peters2344fae2001-01-15 00:50:52 +0000659 def do_whatis(self, arg):
660 try:
661 value = eval(arg, self.curframe.f_globals,
662 self.curframe.f_locals)
663 except:
664 t, v = sys.exc_info()[:2]
665 if type(t) == type(''):
666 exc_type_name = t
667 else: exc_type_name = t.__name__
668 print '***', exc_type_name + ':', `v`
669 return
670 code = None
671 # Is it a function?
672 try: code = value.func_code
673 except: pass
674 if code:
675 print 'Function', code.co_name
676 return
677 # Is it an instance method?
678 try: code = value.im_func.func_code
679 except: pass
680 if code:
681 print 'Method', code.co_name
682 return
683 # None of the above...
684 print type(value)
Guido van Rossum8e2ec561993-07-29 09:37:38 +0000685
Tim Peters2344fae2001-01-15 00:50:52 +0000686 def do_alias(self, arg):
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000687 args = arg.split()
Tim Peters2344fae2001-01-15 00:50:52 +0000688 if len(args) == 0:
689 keys = self.aliases.keys()
690 keys.sort()
691 for alias in keys:
692 print "%s = %s" % (alias, self.aliases[alias])
693 return
Guido van Rossum08454592002-07-12 13:10:53 +0000694 if args[0] in self.aliases and len(args) == 1:
Tim Peters2344fae2001-01-15 00:50:52 +0000695 print "%s = %s" % (args[0], self.aliases[args[0]])
696 else:
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000697 self.aliases[args[0]] = ' '.join(args[1:])
Guido van Rossum23efba41992-01-27 16:58:47 +0000698
Tim Peters2344fae2001-01-15 00:50:52 +0000699 def do_unalias(self, arg):
Eric S. Raymond9b93c5f2001-02-09 07:58:53 +0000700 args = arg.split()
Tim Peters2344fae2001-01-15 00:50:52 +0000701 if len(args) == 0: return
Raymond Hettinger54f02222002-06-01 14:18:47 +0000702 if args[0] in self.aliases:
Tim Peters2344fae2001-01-15 00:50:52 +0000703 del self.aliases[args[0]]
Guido van Rossum00230781993-03-29 11:39:45 +0000704
Tim Peters2344fae2001-01-15 00:50:52 +0000705 # Print a traceback starting at the top stack frame.
706 # The most recently entered frame is printed last;
707 # this is different from dbx and gdb, but consistent with
708 # the Python interpreter's stack trace.
709 # It is also consistent with the up/down commands (which are
710 # compatible with dbx and gdb: up moves towards 'main()'
711 # and down moves towards the most recent stack frame).
Guido van Rossum2424f851998-09-11 22:50:09 +0000712
Tim Peters2344fae2001-01-15 00:50:52 +0000713 def print_stack_trace(self):
714 try:
715 for frame_lineno in self.stack:
716 self.print_stack_entry(frame_lineno)
717 except KeyboardInterrupt:
718 pass
Guido van Rossum2424f851998-09-11 22:50:09 +0000719
Tim Peters2344fae2001-01-15 00:50:52 +0000720 def print_stack_entry(self, frame_lineno, prompt_prefix=line_prefix):
721 frame, lineno = frame_lineno
722 if frame is self.curframe:
723 print '>',
724 else:
725 print ' ',
726 print self.format_stack_entry(frame_lineno, prompt_prefix)
Guido van Rossum2424f851998-09-11 22:50:09 +0000727
Guido van Rossum921c8241992-01-10 14:54:42 +0000728
Tim Peters2344fae2001-01-15 00:50:52 +0000729 # Help methods (derived from pdb.doc)
Guido van Rossum921c8241992-01-10 14:54:42 +0000730
Tim Peters2344fae2001-01-15 00:50:52 +0000731 def help_help(self):
732 self.help_h()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000733
Tim Peters2344fae2001-01-15 00:50:52 +0000734 def help_h(self):
735 print """h(elp)
736Without argument, print the list of available commands.
737With a command name as argument, print help about that command
738"help pdb" pipes the full documentation file to the $PAGER
739"help exec" gives help on the ! command"""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000740
Tim Peters2344fae2001-01-15 00:50:52 +0000741 def help_where(self):
742 self.help_w()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000743
Tim Peters2344fae2001-01-15 00:50:52 +0000744 def help_w(self):
745 print """w(here)
746Print a stack trace, with the most recent frame at the bottom.
747An arrow indicates the "current frame", which determines the
Guido van Rossum6bd68352001-01-20 17:57:37 +0000748context of most commands. 'bt' is an alias for this command."""
749
750 help_bt = help_w
Guido van Rossumb6775db1994-08-01 11:34:53 +0000751
Tim Peters2344fae2001-01-15 00:50:52 +0000752 def help_down(self):
753 self.help_d()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000754
Tim Peters2344fae2001-01-15 00:50:52 +0000755 def help_d(self):
756 print """d(own)
757Move the current frame one level down in the stack trace
758(to an older frame)."""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000759
Tim Peters2344fae2001-01-15 00:50:52 +0000760 def help_up(self):
761 self.help_u()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000762
Tim Peters2344fae2001-01-15 00:50:52 +0000763 def help_u(self):
764 print """u(p)
765Move the current frame one level up in the stack trace
766(to a newer frame)."""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000767
Tim Peters2344fae2001-01-15 00:50:52 +0000768 def help_break(self):
769 self.help_b()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000770
Tim Peters2344fae2001-01-15 00:50:52 +0000771 def help_b(self):
772 print """b(reak) ([file:]lineno | function) [, condition]
773With a line number argument, set a break there in the current
774file. With a function name, set a break at first executable line
775of that function. Without argument, list all breaks. If a second
776argument is present, it is a string specifying an expression
777which must evaluate to true before the breakpoint is honored.
Guido van Rossumb6775db1994-08-01 11:34:53 +0000778
Tim Peters2344fae2001-01-15 00:50:52 +0000779The line number may be prefixed with a filename and a colon,
780to specify a breakpoint in another file (probably one that
781hasn't been loaded yet). The file is searched for on sys.path;
782the .py suffix may be omitted."""
Guido van Rossumb5699c71998-07-20 23:13:54 +0000783
Tim Peters2344fae2001-01-15 00:50:52 +0000784 def help_clear(self):
785 self.help_cl()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000786
Tim Peters2344fae2001-01-15 00:50:52 +0000787 def help_cl(self):
788 print "cl(ear) filename:lineno"
789 print """cl(ear) [bpnumber [bpnumber...]]
790With a space separated list of breakpoint numbers, clear
791those breakpoints. Without argument, clear all breaks (but
792first ask confirmation). With a filename:lineno argument,
793clear all breaks at that line in that file.
Guido van Rossumb6775db1994-08-01 11:34:53 +0000794
Tim Peters2344fae2001-01-15 00:50:52 +0000795Note that the argument is different from previous versions of
796the debugger (in python distributions 1.5.1 and before) where
797a linenumber was used instead of either filename:lineno or
798breakpoint numbers."""
Guido van Rossumb5699c71998-07-20 23:13:54 +0000799
Tim Peters2344fae2001-01-15 00:50:52 +0000800 def help_tbreak(self):
801 print """tbreak same arguments as break, but breakpoint is
802removed when first hit."""
Guido van Rossum2424f851998-09-11 22:50:09 +0000803
Tim Peters2344fae2001-01-15 00:50:52 +0000804 def help_enable(self):
805 print """enable bpnumber [bpnumber ...]
806Enables the breakpoints given as a space separated list of
807bp numbers."""
Guido van Rossum2424f851998-09-11 22:50:09 +0000808
Tim Peters2344fae2001-01-15 00:50:52 +0000809 def help_disable(self):
810 print """disable bpnumber [bpnumber ...]
811Disables the breakpoints given as a space separated list of
812bp numbers."""
Guido van Rossum2424f851998-09-11 22:50:09 +0000813
Tim Peters2344fae2001-01-15 00:50:52 +0000814 def help_ignore(self):
815 print """ignore bpnumber count
816Sets the ignore count for the given breakpoint number. A breakpoint
817becomes active when the ignore count is zero. When non-zero, the
818count is decremented each time the breakpoint is reached and the
819breakpoint is not disabled and any associated condition evaluates
820to true."""
Guido van Rossum2424f851998-09-11 22:50:09 +0000821
Tim Peters2344fae2001-01-15 00:50:52 +0000822 def help_condition(self):
823 print """condition bpnumber str_condition
824str_condition is a string specifying an expression which
825must evaluate to true before the breakpoint is honored.
826If str_condition is absent, any existing condition is removed;
827i.e., the breakpoint is made unconditional."""
Guido van Rossum2424f851998-09-11 22:50:09 +0000828
Tim Peters2344fae2001-01-15 00:50:52 +0000829 def help_step(self):
830 self.help_s()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000831
Tim Peters2344fae2001-01-15 00:50:52 +0000832 def help_s(self):
833 print """s(tep)
834Execute the current line, stop at the first possible occasion
835(either in a function that is called or in the current function)."""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000836
Tim Peters2344fae2001-01-15 00:50:52 +0000837 def help_next(self):
838 self.help_n()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000839
Tim Peters2344fae2001-01-15 00:50:52 +0000840 def help_n(self):
841 print """n(ext)
842Continue execution until the next line in the current function
843is reached or it returns."""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000844
Tim Peters2344fae2001-01-15 00:50:52 +0000845 def help_return(self):
846 self.help_r()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000847
Tim Peters2344fae2001-01-15 00:50:52 +0000848 def help_r(self):
849 print """r(eturn)
850Continue execution until the current function returns."""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000851
Tim Peters2344fae2001-01-15 00:50:52 +0000852 def help_continue(self):
853 self.help_c()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000854
Tim Peters2344fae2001-01-15 00:50:52 +0000855 def help_cont(self):
856 self.help_c()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000857
Tim Peters2344fae2001-01-15 00:50:52 +0000858 def help_c(self):
859 print """c(ont(inue))
860Continue execution, only stop when a breakpoint is encountered."""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000861
Michael W. Hudsoncfd38842002-12-17 16:15:34 +0000862 def help_jump(self):
863 self.help_j()
864
865 def help_j(self):
866 print """j(ump) lineno
867Set the next line that will be executed."""
868
Guido van Rossuma12fe4e2003-04-09 19:06:21 +0000869 def help_debug(self):
870 print """debug code
871Enter a recursive debugger that steps through the code argument
872(which is an arbitrary expression or statement to be executed
873in the current environment)."""
874
Tim Peters2344fae2001-01-15 00:50:52 +0000875 def help_list(self):
876 self.help_l()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000877
Tim Peters2344fae2001-01-15 00:50:52 +0000878 def help_l(self):
879 print """l(ist) [first [,last]]
880List source code for the current file.
881Without arguments, list 11 lines around the current line
882or continue the previous listing.
883With one argument, list 11 lines starting at that line.
884With two arguments, list the given range;
885if the second argument is less than the first, it is a count."""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000886
Tim Peters2344fae2001-01-15 00:50:52 +0000887 def help_args(self):
888 self.help_a()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000889
Tim Peters2344fae2001-01-15 00:50:52 +0000890 def help_a(self):
891 print """a(rgs)
892Print the arguments of the current function."""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000893
Tim Peters2344fae2001-01-15 00:50:52 +0000894 def help_p(self):
895 print """p expression
896Print the value of the expression."""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000897
Barry Warsaw210bd202002-11-05 22:40:20 +0000898 def help_pp(self):
899 print """pp expression
900Pretty-print the value of the expression."""
901
Tim Peters2344fae2001-01-15 00:50:52 +0000902 def help_exec(self):
903 print """(!) statement
904Execute the (one-line) statement in the context of
905the current stack frame.
906The exclamation point can be omitted unless the first word
907of the statement resembles a debugger command.
908To assign to a global variable you must always prefix the
909command with a 'global' command, e.g.:
910(Pdb) global list_options; list_options = ['-l']
911(Pdb)"""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000912
Tim Peters2344fae2001-01-15 00:50:52 +0000913 def help_quit(self):
914 self.help_q()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000915
Tim Peters2344fae2001-01-15 00:50:52 +0000916 def help_q(self):
Guido van Rossumd1c08f32002-04-15 00:48:24 +0000917 print """q(uit) or exit - Quit from the debugger.
Tim Peters2344fae2001-01-15 00:50:52 +0000918The program being executed is aborted."""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000919
Guido van Rossumd1c08f32002-04-15 00:48:24 +0000920 help_exit = help_q
921
Tim Peters2344fae2001-01-15 00:50:52 +0000922 def help_whatis(self):
923 print """whatis arg
924Prints the type of the argument."""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000925
Tim Peters2344fae2001-01-15 00:50:52 +0000926 def help_EOF(self):
927 print """EOF
928Handles the receipt of EOF as a command."""
Guido van Rossum2424f851998-09-11 22:50:09 +0000929
Tim Peters2344fae2001-01-15 00:50:52 +0000930 def help_alias(self):
931 print """alias [name [command [parameter parameter ...] ]]
932Creates an alias called 'name' the executes 'command'. The command
933must *not* be enclosed in quotes. Replaceable parameters are
934indicated by %1, %2, and so on, while %* is replaced by all the
935parameters. If no command is given, the current alias for name
936is shown. If no name is given, all aliases are listed.
Guido van Rossum2424f851998-09-11 22:50:09 +0000937
Tim Peters2344fae2001-01-15 00:50:52 +0000938Aliases may be nested and can contain anything that can be
939legally typed at the pdb prompt. Note! You *can* override
940internal pdb commands with aliases! Those internal commands
941are then hidden until the alias is removed. Aliasing is recursively
942applied to the first word of the command line; all other words
943in the line are left alone.
Guido van Rossum2424f851998-09-11 22:50:09 +0000944
Tim Peters2344fae2001-01-15 00:50:52 +0000945Some useful aliases (especially when placed in the .pdbrc file) are:
Guido van Rossum2424f851998-09-11 22:50:09 +0000946
Tim Peters2344fae2001-01-15 00:50:52 +0000947#Print instance variables (usage "pi classInst")
948alias pi for k in %1.__dict__.keys(): print "%1.",k,"=",%1.__dict__[k]
Guido van Rossum2424f851998-09-11 22:50:09 +0000949
Tim Peters2344fae2001-01-15 00:50:52 +0000950#Print instance variables in self
951alias ps pi self
952"""
Guido van Rossum2424f851998-09-11 22:50:09 +0000953
Tim Peters2344fae2001-01-15 00:50:52 +0000954 def help_unalias(self):
955 print """unalias name
956Deletes the specified alias."""
Guido van Rossum2424f851998-09-11 22:50:09 +0000957
Tim Peters2344fae2001-01-15 00:50:52 +0000958 def help_pdb(self):
959 help()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000960
Tim Peters2344fae2001-01-15 00:50:52 +0000961 def lookupmodule(self, filename):
962 """Helper function for break/clear parsing -- may be overridden."""
963 root, ext = os.path.splitext(filename)
964 if ext == '':
965 filename = filename + '.py'
966 if os.path.isabs(filename):
967 return filename
968 for dirname in sys.path:
969 while os.path.islink(dirname):
970 dirname = os.readlink(dirname)
971 fullname = os.path.join(dirname, filename)
972 if os.path.exists(fullname):
973 return fullname
974 return None
Guido van Rossumb5699c71998-07-20 23:13:54 +0000975
Guido van Rossum35771131992-09-08 11:59:04 +0000976# Simplified interface
977
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000978def run(statement, globals=None, locals=None):
Tim Peters2344fae2001-01-15 00:50:52 +0000979 Pdb().run(statement, globals, locals)
Guido van Rossum5e38b6f1995-02-27 13:13:40 +0000980
981def runeval(expression, globals=None, locals=None):
Tim Peters2344fae2001-01-15 00:50:52 +0000982 return Pdb().runeval(expression, globals, locals)
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000983
984def runctx(statement, globals, locals):
Tim Peters2344fae2001-01-15 00:50:52 +0000985 # B/W compatibility
986 run(statement, globals, locals)
Guido van Rossum6fe08b01992-01-16 13:50:21 +0000987
Guido van Rossum4e160981992-09-02 20:43:20 +0000988def runcall(*args):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000989 return Pdb().runcall(*args)
Guido van Rossum4e160981992-09-02 20:43:20 +0000990
Guido van Rossumb6775db1994-08-01 11:34:53 +0000991def set_trace():
Tim Peters2344fae2001-01-15 00:50:52 +0000992 Pdb().set_trace()
Guido van Rossum35771131992-09-08 11:59:04 +0000993
994# Post-Mortem interface
995
996def post_mortem(t):
Tim Peters2344fae2001-01-15 00:50:52 +0000997 p = Pdb()
998 p.reset()
999 while t.tb_next is not None:
1000 t = t.tb_next
1001 p.interaction(t.tb_frame, t)
Guido van Rossum35771131992-09-08 11:59:04 +00001002
1003def pm():
Tim Peters2344fae2001-01-15 00:50:52 +00001004 post_mortem(sys.last_traceback)
Guido van Rossum35771131992-09-08 11:59:04 +00001005
1006
1007# Main program for testing
1008
Guido van Rossum23efba41992-01-27 16:58:47 +00001009TESTCMD = 'import x; x.main()'
Guido van Rossum6fe08b01992-01-16 13:50:21 +00001010
Guido van Rossum921c8241992-01-10 14:54:42 +00001011def test():
Tim Peters2344fae2001-01-15 00:50:52 +00001012 run(TESTCMD)
Guido van Rossume61fa0a1993-10-22 13:56:35 +00001013
1014# print help
1015def help():
Tim Peters2344fae2001-01-15 00:50:52 +00001016 for dirname in sys.path:
1017 fullname = os.path.join(dirname, 'pdb.doc')
1018 if os.path.exists(fullname):
1019 sts = os.system('${PAGER-more} '+fullname)
1020 if sts: print '*** Pager exit status:', sts
1021 break
1022 else:
1023 print 'Sorry, can\'t find the help file "pdb.doc"',
1024 print 'along the Python search path'
Guido van Rossumf17361d1996-07-30 16:28:13 +00001025
Guido van Rossumb5699c71998-07-20 23:13:54 +00001026mainmodule = ''
1027mainpyfile = ''
1028
Guido van Rossumf17361d1996-07-30 16:28:13 +00001029# When invoked as main program, invoke the debugger on a script
1030if __name__=='__main__':
Tim Peters2344fae2001-01-15 00:50:52 +00001031 if not sys.argv[1:]:
1032 print "usage: pdb.py scriptfile [arg] ..."
1033 sys.exit(2)
Guido van Rossumf17361d1996-07-30 16:28:13 +00001034
Tim Peters2344fae2001-01-15 00:50:52 +00001035 mainpyfile = filename = sys.argv[1] # Get script filename
1036 if not os.path.exists(filename):
1037 print 'Error:', `filename`, 'does not exist'
1038 sys.exit(1)
1039 mainmodule = os.path.basename(filename)
1040 del sys.argv[0] # Hide "pdb.py" from argument list
Guido van Rossumec577d51996-09-10 17:39:34 +00001041
Tim Peters2344fae2001-01-15 00:50:52 +00001042 # Insert script directory in front of module search path
1043 sys.path.insert(0, os.path.dirname(filename))
Guido van Rossumf17361d1996-07-30 16:28:13 +00001044
Tim Peters2344fae2001-01-15 00:50:52 +00001045 run('execfile(' + `filename` + ')')