]> granicus.if.org Git - python/commitdiff
Part of #7245: when KeyboardInterrupt is raised while defining commands, restore...
authorGeorg Brandl <georg@python.org>
Fri, 30 Jul 2010 22:20:16 +0000 (22:20 +0000)
committerGeorg Brandl <georg@python.org>
Fri, 30 Jul 2010 22:20:16 +0000 (22:20 +0000)
Lib/pdb.py
Misc/NEWS

index 83e1197b7c9305c627b85b5cfba9b3f835f63857..3362ea1e7044b9e89804c89d0844f6bb183df742 100755 (executable)
@@ -371,7 +371,7 @@ class Pdb(bdb.Bdb, cmd.Cmd):
         else:
             return self.handle_command_def(line)
 
-    def handle_command_def(self,line):
+    def handle_command_def(self, line):
         """Handles one command line during command list definition."""
         cmd, arg, line = self.parseline(line)
         if not cmd:
@@ -457,14 +457,33 @@ class Pdb(bdb.Bdb, cmd.Cmd):
                 self.error("Usage: commands [bnum]\n        ...\n        end")
                 return
         self.commands_bnum = bnum
+        # Save old definitions for the case of a keyboard interrupt.
+        if bnum in self.commands:
+            old_command_defs = (self.commands[bnum],
+                                self.commands_doprompt[bnum],
+                                self.commands_silent[bnum])
+        else:
+            old_command_defs = None
         self.commands[bnum] = []
         self.commands_doprompt[bnum] = True
         self.commands_silent[bnum] = False
+
         prompt_back = self.prompt
         self.prompt = '(com) '
         self.commands_defining = True
         try:
             self.cmdloop()
+        except KeyboardInterrupt:
+            # Restore old definitions.
+            if old_command_defs:
+                self.commands[bnum] = old_command_defs[0]
+                self.commands_doprompt[bnum] = old_command_defs[1]
+                self.commands_silent[bnum] = old_command_defs[2]
+            else:
+                del self.commands[bnum]
+                del self.commands_doprompt[bnum]
+                del self.commands_silent[bnum]
+            self.error('command definition aborted, old commands restored')
         finally:
             self.commands_defining = False
             self.prompt = prompt_back
index 2bfebd01ece3db7672198312fd8aed8f17b1903d..b7a5eda0cbb087e9d3a81c7ad967592abc8bcd75 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -475,6 +475,9 @@ C-API
 Library
 -------
 
+- In pdb, when Ctrl-C is entered while defining commands for a
+  breakpoint, the old commands are restored.
+
 - For traceback debugging, the pdb listing now also shows the locations
   where the exception was originally (re)raised, if it differs from the
   last line executed (e.g. in case of finally clauses).