Index: pdb.doc =================================================================== --- pdb.doc (revision 41867) +++ pdb.doc (working copy) @@ -131,6 +131,12 @@ r(eturn) Continue execution until the current function returns. +ru(n) [args...] + Restart the debugged python program. If a string is supplied that + becomes the new command arguments. + History, breakpoints, actions and debugger options are preserved. + "restart" is an alias for "run". + c(ont(inue)) Continue execution, only stop when a breakpoint is encountered. Index: pdb.py =================================================================== --- pdb.py (revision 41867) +++ pdb.py (working copy) @@ -13,6 +13,12 @@ import re import pprint import traceback + + +class Restart(Exception): + """Causes a debugger to be restarted for the debugged python program.""" + pass + # Create a custom safe Repr instance and increase its maxstring. # The default of 30 truncates error messages too easily. _repr = Repr() @@ -495,6 +500,17 @@ return 1 do_n = do_next + def do_run(self, arg): + """Restart program by raising an exception to be caught in the main debugger + loop. If arguments were given, set them in sys.argv.""" + if arg: + argv_start = sys.argv[0:1] + sys.argv = arg.split(" ") + sys.argv[:0] = argv_start + raise Restart + + do_R = do_ru = do_restart = do_run + def do_return(self, arg): self.set_return(self.curframe) return 1 @@ -894,6 +910,14 @@ (Pdb) global list_options; list_options = ['-l'] (Pdb)""" + def help_run(self): + print """ru(n) [args...] +Restart the debugged python program. If a string is supplied that +becomes the new command arguments. +History, breakpoints, actions and debugger options are preserved.""" + + help_R = help_restart = help_run + def help_quit(self): self.help_q() @@ -1051,9 +1075,8 @@ # Note on saving/restoring sys.argv: it's a good idea when sys.argv was # modified by the script being debugged. It's a bad idea when it was - # changed by the user from the command line. The best approach would be to - # have a "restart" command which would allow explicit specification of - # command line arguments. + # changed by the user from the command line. There is a "restart" command which + # allows explicit specification of command line arguments. pdb = Pdb() while 1: try: @@ -1061,6 +1084,9 @@ if pdb._user_requested_quit: break print "The program finished and will be restarted" + except Restart: + print "Restarting "+mainpyfile+" with arguments:\n\t" \ + +" ".join(sys.argv[1:]) except SystemExit: # In most cases SystemExit does not warrant a post-mortem session. print "The program exited via sys.exit(). Exit status: ",