]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Patch #1393667: pdb now has a "run" command which restarts the debugged
authorGeorg Brandl <georg@python.org>
Tue, 13 Mar 2007 21:08:15 +0000 (21:08 +0000)
committerGeorg Brandl <georg@python.org>
Tue, 13 Mar 2007 21:08:15 +0000 (21:08 +0000)
Python program, optionally with different arguments.

Doc/lib/libpdb.tex
Lib/pdb.doc
Lib/pdb.py
Misc/NEWS

index b252aeb42f1561e7f942897f3977297f28e12e26..1edae643a82f5300c3a8fa5d9c28e82cbc7159e4 100644 (file)
@@ -378,6 +378,14 @@ command with a \samp{global} command on the same line, e.g.:
 (Pdb)
 \end{verbatim}
 
+\item[run \optional{\var{args} ...}]
+Restart the debugged python program. If an argument is supplied, it is
+splitted with "shlex" and the result is used as the new sys.argv.
+History, breakpoints, actions and debugger options are preserved.
+"restart" is an alias for "run".
+
+\versionadded{2.6}
+
 \item[q(uit)]
 
 Quit from the debugger.
index 81df3237f69d802926aef60fe7fac1ab6a36a8aa..c5139548b98af60230f1db345000fed81d72b6e2 100644 (file)
@@ -131,6 +131,12 @@ n(ext)
 r(eturn)
         Continue execution until the current function returns.
 
+run [args...]
+        Restart the debugged python program. If a string is supplied it is
+        splitted with "shlex", and the result is used as the new sys.argv.
+       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 2ec736b2627e750825a64c3fd3ef69c9027d398f..5778c05d94b0760ffc1ad9a2654f7aa368a16451 100755 (executable)
@@ -13,6 +13,12 @@ import os
 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()
@@ -608,6 +614,18 @@ class Pdb(bdb.Bdb, cmd.Cmd):
         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:
+            import shlex
+            argv0 = sys.argv[0:1]
+            sys.argv = shlex.split(arg)
+            sys.argv[:0] = argv0
+        raise Restart
+
+    do_restart = do_run
+
     def do_return(self, arg):
         self.set_return(self.curframe)
         return 1
@@ -1012,6 +1030,15 @@ command with a 'global' command, e.g.:
 (Pdb) global list_options; list_options = ['-l']
 (Pdb)"""
 
+    def help_run(self):
+        print """run [args...]
+Restart the debugged python program. If a string is supplied, it is
+splitted with "shlex" and the result is used as the new sys.argv.
+History, breakpoints, actions and debugger options are preserved.
+"restart" is an alias for "run"."""
+
+    help_restart = help_run
+
     def help_quit(self):
         self.help_q()
 
@@ -1204,9 +1231,8 @@ def main():
 
     # 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:
@@ -1214,6 +1240,9 @@ def main():
             if pdb._user_requested_quit:
                 break
             print "The program finished and will be restarted"
+        except Restart:
+            print "Restarting", mainpyfile, "with arguments:"
+            print "\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: ",
index 27669f838e99bd142b66dcdb30e610b431a61f9e..cc3d198c1ae6966355bb220de40eb153e6118512 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -170,6 +170,9 @@ Core and builtins
 Library
 -------
 
+- Patch #1393667: pdb now has a "run" command which restarts the debugged
+  Python program, optionally with different arguments.
+
 - Patch #1649190: Adding support for _Bool to ctypes as c_bool.
 
 - Patch #1530482: add pydoc.render_doc() which returns the documentation