| Guido van Rossum | f06ee5f | 1996-11-27 19:52:01 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python | 
| Guido van Rossum | f17361d | 1996-07-30 16:28:13 +0000 | [diff] [blame] | 2 |  | 
| Guido van Rossum | 4b8c6ea | 2000-02-04 15:39:30 +0000 | [diff] [blame] | 3 | """A Python debugger.""" | 
| Guido van Rossum | 92df0c6 | 1992-01-14 18:30:15 +0000 | [diff] [blame] | 4 |  | 
| Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 5 | # (See pdb.doc for documentation.) | 
| Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 6 |  | 
| Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 7 | import sys | 
 | 8 | import linecache | 
| Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 9 | import cmd | 
 | 10 | import bdb | 
| Brett Cannon | 2ee0e8e | 2008-05-23 05:03:59 +0000 | [diff] [blame] | 11 | from repr import Repr | 
| Guido van Rossum | b5699c7 | 1998-07-20 23:13:54 +0000 | [diff] [blame] | 12 | import os | 
| Barry Warsaw | 2bee8fe | 1999-09-09 16:32:41 +0000 | [diff] [blame] | 13 | import re | 
| Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 14 | import pprint | 
| Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 15 | import traceback | 
| Georg Brandl | 8e84c65 | 2007-03-13 21:08:15 +0000 | [diff] [blame] | 16 |  | 
 | 17 |  | 
 | 18 | class Restart(Exception): | 
 | 19 |     """Causes a debugger to be restarted for the debugged python program.""" | 
 | 20 |     pass | 
 | 21 |  | 
| Guido van Rossum | ef1b41b | 2002-09-10 21:57:14 +0000 | [diff] [blame] | 22 | # 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 Montanaro | 352674d | 2001-02-07 23:14:30 +0000 | [diff] [blame] | 28 | __all__ = ["run", "pm", "Pdb", "runeval", "runctx", "runcall", "set_trace", | 
 | 29 |            "post_mortem", "help"] | 
 | 30 |  | 
| Barry Warsaw | 2bee8fe | 1999-09-09 16:32:41 +0000 | [diff] [blame] | 31 | def find_function(funcname, filename): | 
| Andrew M. Kuchling | e672825 | 2006-09-05 13:19:18 +0000 | [diff] [blame] | 32 |     cre = re.compile(r'def\s+%s\s*[(]' % re.escape(funcname)) | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 33 |     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 Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 50 |  | 
 | 51 |  | 
| Guido van Rossum | a558e37 | 1994-11-10 22:27:35 +0000 | [diff] [blame] | 52 | # 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 Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 56 | # line_prefix = ': '    # Use this to get the old situation back | 
 | 57 | line_prefix = '\n-> '   # Probably a better default | 
| Guido van Rossum | a558e37 | 1994-11-10 22:27:35 +0000 | [diff] [blame] | 58 |  | 
| Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 59 | class Pdb(bdb.Bdb, cmd.Cmd): | 
| Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 60 |  | 
| Georg Brandl | 4d4313d | 2009-05-05 08:54:11 +0000 | [diff] [blame] | 61 |     def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None): | 
 | 62 |         bdb.Bdb.__init__(self, skip=skip) | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 63 |         cmd.Cmd.__init__(self, completekey, stdin, stdout) | 
 | 64 |         if stdout: | 
 | 65 |             self.use_rawinput = 0 | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 66 |         self.prompt = '(Pdb) ' | 
 | 67 |         self.aliases = {} | 
| Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 68 |         self.mainpyfile = '' | 
 | 69 |         self._wait_for_mainpyfile = 0 | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 70 |         # Try to load readline if it exists | 
 | 71 |         try: | 
 | 72 |             import readline | 
 | 73 |         except ImportError: | 
 | 74 |             pass | 
| Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 75 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 76 |         # Read $HOME/.pdbrc and ./.pdbrc | 
 | 77 |         self.rcLines = [] | 
| Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 78 |         if 'HOME' in os.environ: | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 79 |             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 Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 96 |  | 
| Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 97 |         self.commands = {} # associates a command list to breakpoint numbers | 
| Georg Brandl | e361bcb | 2009-04-01 23:32:17 +0000 | [diff] [blame] | 98 |         self.commands_doprompt = {} # for each bp num, tells if the prompt | 
 | 99 |                                     # must be disp. after execing the cmd list | 
 | 100 |         self.commands_silent = {} # for each bp num, tells if the stack trace | 
 | 101 |                                   # must be disp. after execing the cmd list | 
 | 102 |         self.commands_defining = False # True while in the process of defining | 
 | 103 |                                        # a command list | 
 | 104 |         self.commands_bnum = None # The breakpoint number for which we are | 
 | 105 |                                   # defining a list | 
| Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 106 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 107 |     def reset(self): | 
 | 108 |         bdb.Bdb.reset(self) | 
 | 109 |         self.forget() | 
| Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 110 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 111 |     def forget(self): | 
 | 112 |         self.lineno = None | 
 | 113 |         self.stack = [] | 
 | 114 |         self.curindex = 0 | 
 | 115 |         self.curframe = None | 
| Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 116 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 117 |     def setup(self, f, t): | 
 | 118 |         self.forget() | 
 | 119 |         self.stack, self.curindex = self.get_stack(f, t) | 
 | 120 |         self.curframe = self.stack[self.curindex][0] | 
| Georg Brandl | e361bcb | 2009-04-01 23:32:17 +0000 | [diff] [blame] | 121 |         # The f_locals dictionary is updated from the actual frame | 
 | 122 |         # locals whenever the .f_locals accessor is called, so we | 
 | 123 |         # cache it here to ensure that modifications are not overwritten. | 
 | 124 |         self.curframe_locals = self.curframe.f_locals | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 125 |         self.execRcLines() | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 126 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 127 |     # Can be executed earlier than 'setup' if desired | 
 | 128 |     def execRcLines(self): | 
 | 129 |         if self.rcLines: | 
 | 130 |             # Make local copy because of recursion | 
 | 131 |             rcLines = self.rcLines | 
 | 132 |             # executed only once | 
 | 133 |             self.rcLines = [] | 
 | 134 |             for line in rcLines: | 
 | 135 |                 line = line[:-1] | 
| Guido van Rossum | 0845459 | 2002-07-12 13:10:53 +0000 | [diff] [blame] | 136 |                 if len(line) > 0 and line[0] != '#': | 
 | 137 |                     self.onecmd(line) | 
| Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 138 |  | 
| Tim Peters | 280488b | 2002-08-23 18:19:30 +0000 | [diff] [blame] | 139 |     # Override Bdb methods | 
| Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 140 |  | 
 | 141 |     def user_call(self, frame, argument_list): | 
 | 142 |         """This method is called when there is the remote possibility | 
 | 143 |         that we ever need to stop in this function.""" | 
| Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 144 |         if self._wait_for_mainpyfile: | 
 | 145 |             return | 
| Michael W. Hudson | 01eb85c | 2003-01-31 17:48:29 +0000 | [diff] [blame] | 146 |         if self.stop_here(frame): | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 147 |             print >>self.stdout, '--Call--' | 
| Michael W. Hudson | 01eb85c | 2003-01-31 17:48:29 +0000 | [diff] [blame] | 148 |             self.interaction(frame, None) | 
| Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 149 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 150 |     def user_line(self, frame): | 
 | 151 |         """This function is called when we stop or break at this line.""" | 
| Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 152 |         if self._wait_for_mainpyfile: | 
 | 153 |             if (self.mainpyfile != self.canonic(frame.f_code.co_filename) | 
 | 154 |                 or frame.f_lineno<= 0): | 
 | 155 |                 return | 
 | 156 |             self._wait_for_mainpyfile = 0 | 
| Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 157 |         if self.bp_commands(frame): | 
 | 158 |             self.interaction(frame, None) | 
| Martin v. Löwis | 1a00e18 | 2006-04-17 19:18:18 +0000 | [diff] [blame] | 159 |  | 
| Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 160 |     def bp_commands(self,frame): | 
| Georg Brandl | 5815220 | 2009-05-05 09:06:02 +0000 | [diff] [blame] | 161 |         """Call every command that was set for the current active breakpoint | 
 | 162 |         (if there is one). | 
 | 163 |  | 
 | 164 |         Returns True if the normal interaction function must be called, | 
 | 165 |         False otherwise.""" | 
 | 166 |         # self.currentbp is set in bdb in Bdb.break_here if a breakpoint was hit | 
 | 167 |         if getattr(self, "currentbp", False) and \ | 
 | 168 |                self.currentbp in self.commands: | 
| Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 169 |             currentbp = self.currentbp | 
 | 170 |             self.currentbp = 0 | 
 | 171 |             lastcmd_back = self.lastcmd | 
 | 172 |             self.setup(frame, None) | 
 | 173 |             for line in self.commands[currentbp]: | 
 | 174 |                 self.onecmd(line) | 
 | 175 |             self.lastcmd = lastcmd_back | 
 | 176 |             if not self.commands_silent[currentbp]: | 
 | 177 |                 self.print_stack_entry(self.stack[self.curindex]) | 
 | 178 |             if self.commands_doprompt[currentbp]: | 
| Gregory P. Smith | 860852f | 2010-05-09 01:20:20 +0000 | [diff] [blame] | 179 |                 self.cmdloop() | 
| Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 180 |             self.forget() | 
| Martin v. Löwis | 1a00e18 | 2006-04-17 19:18:18 +0000 | [diff] [blame] | 181 |             return | 
| Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 182 |         return 1 | 
| Guido van Rossum | 9e1ee97 | 1997-07-11 13:43:53 +0000 | [diff] [blame] | 183 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 184 |     def user_return(self, frame, return_value): | 
 | 185 |         """This function is called when a return trap is set here.""" | 
| Georg Brandl | 5077599 | 2010-08-01 19:33:15 +0000 | [diff] [blame] | 186 |         if self._wait_for_mainpyfile: | 
 | 187 |             return | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 188 |         frame.f_locals['__return__'] = return_value | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 189 |         print >>self.stdout, '--Return--' | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 190 |         self.interaction(frame, None) | 
| Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 191 |  | 
| Brett Cannon | c6a30ec | 2008-08-01 01:36:47 +0000 | [diff] [blame] | 192 |     def user_exception(self, frame, exc_info): | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 193 |         """This function is called if an exception occurs, | 
 | 194 |         but only if we are to stop at or just below this level.""" | 
| Georg Brandl | 5077599 | 2010-08-01 19:33:15 +0000 | [diff] [blame] | 195 |         if self._wait_for_mainpyfile: | 
 | 196 |             return | 
 | 197 |         exc_type, exc_value, exc_traceback = exc_info | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 198 |         frame.f_locals['__exception__'] = exc_type, exc_value | 
 | 199 |         if type(exc_type) == type(''): | 
 | 200 |             exc_type_name = exc_type | 
 | 201 |         else: exc_type_name = exc_type.__name__ | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 202 |         print >>self.stdout, exc_type_name + ':', _saferepr(exc_value) | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 203 |         self.interaction(frame, exc_traceback) | 
| Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 204 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 205 |     # General interaction function | 
 | 206 |  | 
 | 207 |     def interaction(self, frame, traceback): | 
 | 208 |         self.setup(frame, traceback) | 
 | 209 |         self.print_stack_entry(self.stack[self.curindex]) | 
| Gregory P. Smith | 860852f | 2010-05-09 01:20:20 +0000 | [diff] [blame] | 210 |         self.cmdloop() | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 211 |         self.forget() | 
 | 212 |  | 
| Georg Brandl | 58b8b95 | 2009-04-01 21:54:21 +0000 | [diff] [blame] | 213 |     def displayhook(self, obj): | 
 | 214 |         """Custom displayhook for the exec in default(), which prevents | 
 | 215 |         assignment of the _ variable in the builtins. | 
 | 216 |         """ | 
| Georg Brandl | 69dfe8d | 2009-09-16 16:36:39 +0000 | [diff] [blame] | 217 |         # reproduce the behavior of the standard displayhook, not printing None | 
 | 218 |         if obj is not None: | 
 | 219 |             print repr(obj) | 
| Georg Brandl | 58b8b95 | 2009-04-01 21:54:21 +0000 | [diff] [blame] | 220 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 221 |     def default(self, line): | 
 | 222 |         if line[:1] == '!': line = line[1:] | 
| Georg Brandl | e361bcb | 2009-04-01 23:32:17 +0000 | [diff] [blame] | 223 |         locals = self.curframe_locals | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 224 |         globals = self.curframe.f_globals | 
 | 225 |         try: | 
 | 226 |             code = compile(line + '\n', '<stdin>', 'single') | 
| Amaury Forgeot d'Arc | ff0f267 | 2008-01-15 21:25:11 +0000 | [diff] [blame] | 227 |             save_stdout = sys.stdout | 
 | 228 |             save_stdin = sys.stdin | 
| Georg Brandl | 58b8b95 | 2009-04-01 21:54:21 +0000 | [diff] [blame] | 229 |             save_displayhook = sys.displayhook | 
| Guido van Rossum | cad3724 | 2008-01-15 17:59:29 +0000 | [diff] [blame] | 230 |             try: | 
 | 231 |                 sys.stdin = self.stdin | 
 | 232 |                 sys.stdout = self.stdout | 
| Georg Brandl | 58b8b95 | 2009-04-01 21:54:21 +0000 | [diff] [blame] | 233 |                 sys.displayhook = self.displayhook | 
| Guido van Rossum | cad3724 | 2008-01-15 17:59:29 +0000 | [diff] [blame] | 234 |                 exec code in globals, locals | 
 | 235 |             finally: | 
 | 236 |                 sys.stdout = save_stdout | 
 | 237 |                 sys.stdin = save_stdin | 
| Georg Brandl | 58b8b95 | 2009-04-01 21:54:21 +0000 | [diff] [blame] | 238 |                 sys.displayhook = save_displayhook | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 239 |         except: | 
 | 240 |             t, v = sys.exc_info()[:2] | 
 | 241 |             if type(t) == type(''): | 
 | 242 |                 exc_type_name = t | 
 | 243 |             else: exc_type_name = t.__name__ | 
| Georg Brandl | 3e67c5c | 2010-07-30 14:14:42 +0000 | [diff] [blame] | 244 |             print >>self.stdout, '***', exc_type_name + ':', v | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 245 |  | 
 | 246 |     def precmd(self, line): | 
 | 247 |         """Handle alias expansion and ';;' separator.""" | 
| Guido van Rossum | 0845459 | 2002-07-12 13:10:53 +0000 | [diff] [blame] | 248 |         if not line.strip(): | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 249 |             return line | 
| Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 250 |         args = line.split() | 
| Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 251 |         while args[0] in self.aliases: | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 252 |             line = self.aliases[args[0]] | 
 | 253 |             ii = 1 | 
 | 254 |             for tmpArg in args[1:]: | 
| Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 255 |                 line = line.replace("%" + str(ii), | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 256 |                                       tmpArg) | 
 | 257 |                 ii = ii + 1 | 
| Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 258 |             line = line.replace("%*", ' '.join(args[1:])) | 
 | 259 |             args = line.split() | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 260 |         # split into ';;' separated commands | 
 | 261 |         # unless it's an alias command | 
 | 262 |         if args[0] != 'alias': | 
| Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 263 |             marker = line.find(';;') | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 264 |             if marker >= 0: | 
 | 265 |                 # queue up everything after marker | 
| Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 266 |                 next = line[marker+2:].lstrip() | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 267 |                 self.cmdqueue.append(next) | 
| Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 268 |                 line = line[:marker].rstrip() | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 269 |         return line | 
 | 270 |  | 
| Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 271 |     def onecmd(self, line): | 
 | 272 |         """Interpret the argument as though it had been typed in response | 
| Martin v. Löwis | 1a00e18 | 2006-04-17 19:18:18 +0000 | [diff] [blame] | 273 |         to the prompt. | 
 | 274 |  | 
| Andrew M. Kuchling | 9aed98f | 2006-07-27 12:18:20 +0000 | [diff] [blame] | 275 |         Checks whether this line is typed at the normal prompt or in | 
| Tim Peters | daea035 | 2006-07-27 15:11:00 +0000 | [diff] [blame] | 276 |         a breakpoint command list definition. | 
| Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 277 |         """ | 
 | 278 |         if not self.commands_defining: | 
 | 279 |             return cmd.Cmd.onecmd(self, line) | 
 | 280 |         else: | 
 | 281 |             return self.handle_command_def(line) | 
 | 282 |  | 
| Martin v. Löwis | 1a00e18 | 2006-04-17 19:18:18 +0000 | [diff] [blame] | 283 |     def handle_command_def(self,line): | 
| Georg Brandl | 5077599 | 2010-08-01 19:33:15 +0000 | [diff] [blame] | 284 |         """Handles one command line during command list definition.""" | 
| Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 285 |         cmd, arg, line = self.parseline(line) | 
| Georg Brandl | 5077599 | 2010-08-01 19:33:15 +0000 | [diff] [blame] | 286 |         if not cmd: | 
 | 287 |             return | 
| Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 288 |         if cmd == 'silent': | 
 | 289 |             self.commands_silent[self.commands_bnum] = True | 
 | 290 |             return # continue to handle other cmd def in the cmd list | 
 | 291 |         elif cmd == 'end': | 
 | 292 |             self.cmdqueue = [] | 
 | 293 |             return 1 # end of cmd list | 
 | 294 |         cmdlist = self.commands[self.commands_bnum] | 
| Georg Brandl | 5077599 | 2010-08-01 19:33:15 +0000 | [diff] [blame] | 295 |         if arg: | 
| Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 296 |             cmdlist.append(cmd+' '+arg) | 
 | 297 |         else: | 
 | 298 |             cmdlist.append(cmd) | 
 | 299 |         # Determine if we must stop | 
 | 300 |         try: | 
 | 301 |             func = getattr(self, 'do_' + cmd) | 
 | 302 |         except AttributeError: | 
 | 303 |             func = self.default | 
| Georg Brandl | 5815220 | 2009-05-05 09:06:02 +0000 | [diff] [blame] | 304 |         # one of the resuming commands | 
 | 305 |         if func.func_name in self.commands_resuming: | 
| Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 306 |             self.commands_doprompt[self.commands_bnum] = False | 
 | 307 |             self.cmdqueue = [] | 
 | 308 |             return 1 | 
| Martin v. Löwis | 1a00e18 | 2006-04-17 19:18:18 +0000 | [diff] [blame] | 309 |         return | 
| Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 310 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 311 |     # Command definitions, called by cmdloop() | 
 | 312 |     # The argument is the remaining string on the command line | 
 | 313 |     # Return true to exit from the command loop | 
 | 314 |  | 
 | 315 |     do_h = cmd.Cmd.do_help | 
 | 316 |  | 
| Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 317 |     def do_commands(self, arg): | 
| Georg Brandl | 5815220 | 2009-05-05 09:06:02 +0000 | [diff] [blame] | 318 |         """Defines a list of commands associated to a breakpoint. | 
 | 319 |  | 
 | 320 |         Those commands will be executed whenever the breakpoint causes | 
 | 321 |         the program to stop execution.""" | 
| Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 322 |         if not arg: | 
 | 323 |             bnum = len(bdb.Breakpoint.bpbynumber)-1 | 
 | 324 |         else: | 
 | 325 |             try: | 
 | 326 |                 bnum = int(arg) | 
 | 327 |             except: | 
| Georg Brandl | 5815220 | 2009-05-05 09:06:02 +0000 | [diff] [blame] | 328 |                 print >>self.stdout, "Usage : commands [bnum]\n        ..." \ | 
 | 329 |                                      "\n        end" | 
| Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 330 |                 return | 
 | 331 |         self.commands_bnum = bnum | 
 | 332 |         self.commands[bnum] = [] | 
 | 333 |         self.commands_doprompt[bnum] = True | 
 | 334 |         self.commands_silent[bnum] = False | 
 | 335 |         prompt_back = self.prompt | 
 | 336 |         self.prompt = '(com) ' | 
 | 337 |         self.commands_defining = True | 
| Georg Brandl | 5077599 | 2010-08-01 19:33:15 +0000 | [diff] [blame] | 338 |         try: | 
 | 339 |             self.cmdloop() | 
 | 340 |         finally: | 
 | 341 |             self.commands_defining = False | 
 | 342 |             self.prompt = prompt_back | 
| Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 343 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 344 |     def do_break(self, arg, temporary = 0): | 
 | 345 |         # break [ ([filename:]lineno | function) [, "condition"] ] | 
 | 346 |         if not arg: | 
 | 347 |             if self.breaks:  # There's at least one | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 348 |                 print >>self.stdout, "Num Type         Disp Enb   Where" | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 349 |                 for bp in bdb.Breakpoint.bpbynumber: | 
 | 350 |                     if bp: | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 351 |                         bp.bpprint(self.stdout) | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 352 |             return | 
 | 353 |         # parse arguments; comma has lowest precedence | 
 | 354 |         # and cannot occur in filename | 
 | 355 |         filename = None | 
 | 356 |         lineno = None | 
 | 357 |         cond = None | 
| Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 358 |         comma = arg.find(',') | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 359 |         if comma > 0: | 
 | 360 |             # parse stuff after comma: "condition" | 
| Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 361 |             cond = arg[comma+1:].lstrip() | 
 | 362 |             arg = arg[:comma].rstrip() | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 363 |         # parse stuff before comma: [filename:]lineno | function | 
| Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 364 |         colon = arg.rfind(':') | 
| Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 365 |         funcname = None | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 366 |         if colon >= 0: | 
| Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 367 |             filename = arg[:colon].rstrip() | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 368 |             f = self.lookupmodule(filename) | 
 | 369 |             if not f: | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 370 |                 print >>self.stdout, '*** ', repr(filename), | 
 | 371 |                 print >>self.stdout, 'not found from sys.path' | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 372 |                 return | 
 | 373 |             else: | 
 | 374 |                 filename = f | 
| Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 375 |             arg = arg[colon+1:].lstrip() | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 376 |             try: | 
 | 377 |                 lineno = int(arg) | 
 | 378 |             except ValueError, msg: | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 379 |                 print >>self.stdout, '*** Bad lineno:', arg | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 380 |                 return | 
 | 381 |         else: | 
 | 382 |             # no colon; can be lineno or function | 
 | 383 |             try: | 
 | 384 |                 lineno = int(arg) | 
 | 385 |             except ValueError: | 
 | 386 |                 try: | 
 | 387 |                     func = eval(arg, | 
 | 388 |                                 self.curframe.f_globals, | 
| Georg Brandl | e361bcb | 2009-04-01 23:32:17 +0000 | [diff] [blame] | 389 |                                 self.curframe_locals) | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 390 |                 except: | 
 | 391 |                     func = arg | 
 | 392 |                 try: | 
 | 393 |                     if hasattr(func, 'im_func'): | 
 | 394 |                         func = func.im_func | 
 | 395 |                     code = func.func_code | 
| Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 396 |                     #use co_name to identify the bkpt (function names | 
 | 397 |                     #could be aliased, but co_name is invariant) | 
 | 398 |                     funcname = code.co_name | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 399 |                     lineno = code.co_firstlineno | 
 | 400 |                     filename = code.co_filename | 
 | 401 |                 except: | 
 | 402 |                     # last thing to try | 
 | 403 |                     (ok, filename, ln) = self.lineinfo(arg) | 
 | 404 |                     if not ok: | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 405 |                         print >>self.stdout, '*** The specified object', | 
 | 406 |                         print >>self.stdout, repr(arg), | 
 | 407 |                         print >>self.stdout, 'is not a function' | 
 | 408 |                         print >>self.stdout, 'or was not found along sys.path.' | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 409 |                         return | 
| Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 410 |                     funcname = ok # ok contains a function name | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 411 |                     lineno = int(ln) | 
 | 412 |         if not filename: | 
 | 413 |             filename = self.defaultFile() | 
 | 414 |         # Check for reasonable breakpoint | 
 | 415 |         line = self.checkline(filename, lineno) | 
 | 416 |         if line: | 
 | 417 |             # now set the break point | 
| Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 418 |             err = self.set_break(filename, line, temporary, cond, funcname) | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 419 |             if err: print >>self.stdout, '***', err | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 420 |             else: | 
 | 421 |                 bp = self.get_breaks(filename, line)[-1] | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 422 |                 print >>self.stdout, "Breakpoint %d at %s:%d" % (bp.number, | 
 | 423 |                                                                  bp.file, | 
 | 424 |                                                                  bp.line) | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 425 |  | 
 | 426 |     # To be overridden in derived debuggers | 
 | 427 |     def defaultFile(self): | 
 | 428 |         """Produce a reasonable default.""" | 
 | 429 |         filename = self.curframe.f_code.co_filename | 
| Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 430 |         if filename == '<string>' and self.mainpyfile: | 
 | 431 |             filename = self.mainpyfile | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 432 |         return filename | 
 | 433 |  | 
 | 434 |     do_b = do_break | 
 | 435 |  | 
 | 436 |     def do_tbreak(self, arg): | 
 | 437 |         self.do_break(arg, 1) | 
 | 438 |  | 
 | 439 |     def lineinfo(self, identifier): | 
 | 440 |         failed = (None, None, None) | 
 | 441 |         # Input is identifier, may be in single quotes | 
| Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 442 |         idstring = identifier.split("'") | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 443 |         if len(idstring) == 1: | 
 | 444 |             # not in single quotes | 
| Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 445 |             id = idstring[0].strip() | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 446 |         elif len(idstring) == 3: | 
 | 447 |             # quoted | 
| Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 448 |             id = idstring[1].strip() | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 449 |         else: | 
 | 450 |             return failed | 
 | 451 |         if id == '': return failed | 
| Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 452 |         parts = id.split('.') | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 453 |         # Protection for derived debuggers | 
 | 454 |         if parts[0] == 'self': | 
 | 455 |             del parts[0] | 
 | 456 |             if len(parts) == 0: | 
 | 457 |                 return failed | 
 | 458 |         # Best first guess at file to look at | 
 | 459 |         fname = self.defaultFile() | 
 | 460 |         if len(parts) == 1: | 
 | 461 |             item = parts[0] | 
 | 462 |         else: | 
 | 463 |             # More than one part. | 
 | 464 |             # First is module, second is method/class | 
 | 465 |             f = self.lookupmodule(parts[0]) | 
 | 466 |             if f: | 
 | 467 |                 fname = f | 
 | 468 |             item = parts[1] | 
 | 469 |         answer = find_function(item, fname) | 
 | 470 |         return answer or failed | 
 | 471 |  | 
 | 472 |     def checkline(self, filename, lineno): | 
| Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 473 |         """Check whether specified line seems to be executable. | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 474 |  | 
| Johannes Gijsbers | 4a9faa1 | 2004-08-30 13:29:44 +0000 | [diff] [blame] | 475 |         Return `lineno` if it is, 0 if not (e.g. a docstring, comment, blank | 
 | 476 |         line or EOF). Warning: testing is not comprehensive. | 
 | 477 |         """ | 
| Georg Brandl | 5077599 | 2010-08-01 19:33:15 +0000 | [diff] [blame] | 478 |         # this method should be callable before starting debugging, so default | 
 | 479 |         # to "no globals" if there is no current frame | 
 | 480 |         globs = self.curframe.f_globals if hasattr(self, 'curframe') else None | 
 | 481 |         line = linecache.getline(filename, lineno, globs) | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 482 |         if not line: | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 483 |             print >>self.stdout, 'End of file' | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 484 |             return 0 | 
| Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 485 |         line = line.strip() | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 486 |         # Don't allow setting breakpoint at a blank line | 
| Guido van Rossum | 0845459 | 2002-07-12 13:10:53 +0000 | [diff] [blame] | 487 |         if (not line or (line[0] == '#') or | 
 | 488 |              (line[:3] == '"""') or line[:3] == "'''"): | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 489 |             print >>self.stdout, '*** Blank or comment' | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 490 |             return 0 | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 491 |         return lineno | 
 | 492 |  | 
 | 493 |     def do_enable(self, arg): | 
| Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 494 |         args = arg.split() | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 495 |         for i in args: | 
| Andrew M. Kuchling | b1f8bab | 2003-05-22 14:46:12 +0000 | [diff] [blame] | 496 |             try: | 
 | 497 |                 i = int(i) | 
 | 498 |             except ValueError: | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 499 |                 print >>self.stdout, 'Breakpoint index %r is not a number' % i | 
| Andrew M. Kuchling | b1f8bab | 2003-05-22 14:46:12 +0000 | [diff] [blame] | 500 |                 continue | 
 | 501 |  | 
 | 502 |             if not (0 <= i < len(bdb.Breakpoint.bpbynumber)): | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 503 |                 print >>self.stdout, 'No breakpoint numbered', i | 
| Andrew M. Kuchling | b1f8bab | 2003-05-22 14:46:12 +0000 | [diff] [blame] | 504 |                 continue | 
 | 505 |  | 
 | 506 |             bp = bdb.Breakpoint.bpbynumber[i] | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 507 |             if bp: | 
 | 508 |                 bp.enable() | 
 | 509 |  | 
 | 510 |     def do_disable(self, arg): | 
| Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 511 |         args = arg.split() | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 512 |         for i in args: | 
| Andrew M. Kuchling | b1f8bab | 2003-05-22 14:46:12 +0000 | [diff] [blame] | 513 |             try: | 
 | 514 |                 i = int(i) | 
 | 515 |             except ValueError: | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 516 |                 print >>self.stdout, 'Breakpoint index %r is not a number' % i | 
| Andrew M. Kuchling | b1f8bab | 2003-05-22 14:46:12 +0000 | [diff] [blame] | 517 |                 continue | 
| Tim Peters | f545baa | 2003-06-15 23:26:30 +0000 | [diff] [blame] | 518 |  | 
| Andrew M. Kuchling | b1f8bab | 2003-05-22 14:46:12 +0000 | [diff] [blame] | 519 |             if not (0 <= i < len(bdb.Breakpoint.bpbynumber)): | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 520 |                 print >>self.stdout, 'No breakpoint numbered', i | 
| Andrew M. Kuchling | b1f8bab | 2003-05-22 14:46:12 +0000 | [diff] [blame] | 521 |                 continue | 
 | 522 |  | 
 | 523 |             bp = bdb.Breakpoint.bpbynumber[i] | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 524 |             if bp: | 
 | 525 |                 bp.disable() | 
 | 526 |  | 
 | 527 |     def do_condition(self, arg): | 
 | 528 |         # arg is breakpoint number and condition | 
| Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 529 |         args = arg.split(' ', 1) | 
| Georg Brandl | e498083 | 2007-01-22 21:23:41 +0000 | [diff] [blame] | 530 |         try: | 
 | 531 |             bpnum = int(args[0].strip()) | 
 | 532 |         except ValueError: | 
 | 533 |             # something went wrong | 
 | 534 |             print >>self.stdout, \ | 
 | 535 |                 'Breakpoint index %r is not a number' % args[0] | 
| Georg Brandl | b278318 | 2007-03-11 08:28:46 +0000 | [diff] [blame] | 536 |             return | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 537 |         try: | 
 | 538 |             cond = args[1] | 
 | 539 |         except: | 
 | 540 |             cond = None | 
| Collin Winter | 2faa9e1 | 2007-03-11 16:00:20 +0000 | [diff] [blame] | 541 |         try: | 
 | 542 |             bp = bdb.Breakpoint.bpbynumber[bpnum] | 
 | 543 |         except IndexError: | 
 | 544 |             print >>self.stdout, 'Breakpoint index %r is not valid' % args[0] | 
 | 545 |             return | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 546 |         if bp: | 
 | 547 |             bp.cond = cond | 
 | 548 |             if not cond: | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 549 |                 print >>self.stdout, 'Breakpoint', bpnum, | 
 | 550 |                 print >>self.stdout, 'is now unconditional.' | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 551 |  | 
 | 552 |     def do_ignore(self,arg): | 
 | 553 |         """arg is bp number followed by ignore count.""" | 
| Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 554 |         args = arg.split() | 
| Georg Brandl | e498083 | 2007-01-22 21:23:41 +0000 | [diff] [blame] | 555 |         try: | 
 | 556 |             bpnum = int(args[0].strip()) | 
 | 557 |         except ValueError: | 
 | 558 |             # something went wrong | 
 | 559 |             print >>self.stdout, \ | 
 | 560 |                 'Breakpoint index %r is not a number' % args[0] | 
| Georg Brandl | b278318 | 2007-03-11 08:28:46 +0000 | [diff] [blame] | 561 |             return | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 562 |         try: | 
| Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 563 |             count = int(args[1].strip()) | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 564 |         except: | 
 | 565 |             count = 0 | 
| Collin Winter | 2faa9e1 | 2007-03-11 16:00:20 +0000 | [diff] [blame] | 566 |         try: | 
 | 567 |             bp = bdb.Breakpoint.bpbynumber[bpnum] | 
 | 568 |         except IndexError: | 
 | 569 |             print >>self.stdout, 'Breakpoint index %r is not valid' % args[0] | 
 | 570 |             return | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 571 |         if bp: | 
 | 572 |             bp.ignore = count | 
| Guido van Rossum | 0845459 | 2002-07-12 13:10:53 +0000 | [diff] [blame] | 573 |             if count > 0: | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 574 |                 reply = 'Will ignore next ' | 
| Guido van Rossum | 0845459 | 2002-07-12 13:10:53 +0000 | [diff] [blame] | 575 |                 if count > 1: | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 576 |                     reply = reply + '%d crossings' % count | 
 | 577 |                 else: | 
 | 578 |                     reply = reply + '1 crossing' | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 579 |                 print >>self.stdout, reply + ' of breakpoint %d.' % bpnum | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 580 |             else: | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 581 |                 print >>self.stdout, 'Will stop next time breakpoint', | 
 | 582 |                 print >>self.stdout, bpnum, 'is reached.' | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 583 |  | 
 | 584 |     def do_clear(self, arg): | 
 | 585 |         """Three possibilities, tried in this order: | 
 | 586 |         clear -> clear all breaks, ask for confirmation | 
 | 587 |         clear file:lineno -> clear all breaks at file:lineno | 
 | 588 |         clear bpno bpno ... -> clear breakpoints by number""" | 
 | 589 |         if not arg: | 
 | 590 |             try: | 
 | 591 |                 reply = raw_input('Clear all breaks? ') | 
 | 592 |             except EOFError: | 
 | 593 |                 reply = 'no' | 
| Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 594 |             reply = reply.strip().lower() | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 595 |             if reply in ('y', 'yes'): | 
 | 596 |                 self.clear_all_breaks() | 
 | 597 |             return | 
 | 598 |         if ':' in arg: | 
 | 599 |             # Make sure it works for "clear C:\foo\bar.py:12" | 
| Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 600 |             i = arg.rfind(':') | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 601 |             filename = arg[:i] | 
 | 602 |             arg = arg[i+1:] | 
 | 603 |             try: | 
 | 604 |                 lineno = int(arg) | 
| Georg Brandl | 23d9d45 | 2006-05-03 18:12:33 +0000 | [diff] [blame] | 605 |             except ValueError: | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 606 |                 err = "Invalid line number (%s)" % arg | 
 | 607 |             else: | 
 | 608 |                 err = self.clear_break(filename, lineno) | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 609 |             if err: print >>self.stdout, '***', err | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 610 |             return | 
| Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 611 |         numberlist = arg.split() | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 612 |         for i in numberlist: | 
| Georg Brandl | 23d9d45 | 2006-05-03 18:12:33 +0000 | [diff] [blame] | 613 |             try: | 
 | 614 |                 i = int(i) | 
 | 615 |             except ValueError: | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 616 |                 print >>self.stdout, 'Breakpoint index %r is not a number' % i | 
| Georg Brandl | 23d9d45 | 2006-05-03 18:12:33 +0000 | [diff] [blame] | 617 |                 continue | 
 | 618 |  | 
| Georg Brandl | 6d2b346 | 2005-08-24 07:36:17 +0000 | [diff] [blame] | 619 |             if not (0 <= i < len(bdb.Breakpoint.bpbynumber)): | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 620 |                 print >>self.stdout, 'No breakpoint numbered', i | 
| Georg Brandl | 6d2b346 | 2005-08-24 07:36:17 +0000 | [diff] [blame] | 621 |                 continue | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 622 |             err = self.clear_bpbynumber(i) | 
 | 623 |             if err: | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 624 |                 print >>self.stdout, '***', err | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 625 |             else: | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 626 |                 print >>self.stdout, 'Deleted breakpoint', i | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 627 |     do_cl = do_clear # 'c' is already an abbreviation for 'continue' | 
 | 628 |  | 
 | 629 |     def do_where(self, arg): | 
 | 630 |         self.print_stack_trace() | 
 | 631 |     do_w = do_where | 
| Guido van Rossum | 6bd6835 | 2001-01-20 17:57:37 +0000 | [diff] [blame] | 632 |     do_bt = do_where | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 633 |  | 
 | 634 |     def do_up(self, arg): | 
 | 635 |         if self.curindex == 0: | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 636 |             print >>self.stdout, '*** Oldest frame' | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 637 |         else: | 
 | 638 |             self.curindex = self.curindex - 1 | 
 | 639 |             self.curframe = self.stack[self.curindex][0] | 
| Georg Brandl | 569fc96 | 2009-04-02 02:00:01 +0000 | [diff] [blame] | 640 |             self.curframe_locals = self.curframe.f_locals | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 641 |             self.print_stack_entry(self.stack[self.curindex]) | 
 | 642 |             self.lineno = None | 
 | 643 |     do_u = do_up | 
 | 644 |  | 
 | 645 |     def do_down(self, arg): | 
 | 646 |         if self.curindex + 1 == len(self.stack): | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 647 |             print >>self.stdout, '*** Newest frame' | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 648 |         else: | 
 | 649 |             self.curindex = self.curindex + 1 | 
 | 650 |             self.curframe = self.stack[self.curindex][0] | 
| Georg Brandl | 569fc96 | 2009-04-02 02:00:01 +0000 | [diff] [blame] | 651 |             self.curframe_locals = self.curframe.f_locals | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 652 |             self.print_stack_entry(self.stack[self.curindex]) | 
 | 653 |             self.lineno = None | 
 | 654 |     do_d = do_down | 
 | 655 |  | 
| Benjamin Peterson | 9835394 | 2008-05-11 14:13:25 +0000 | [diff] [blame] | 656 |     def do_until(self, arg): | 
 | 657 |         self.set_until(self.curframe) | 
 | 658 |         return 1 | 
 | 659 |     do_unt = do_until | 
 | 660 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 661 |     def do_step(self, arg): | 
 | 662 |         self.set_step() | 
 | 663 |         return 1 | 
 | 664 |     do_s = do_step | 
 | 665 |  | 
 | 666 |     def do_next(self, arg): | 
 | 667 |         self.set_next(self.curframe) | 
 | 668 |         return 1 | 
 | 669 |     do_n = do_next | 
 | 670 |  | 
| Georg Brandl | 8e84c65 | 2007-03-13 21:08:15 +0000 | [diff] [blame] | 671 |     def do_run(self, arg): | 
| Georg Brandl | 5815220 | 2009-05-05 09:06:02 +0000 | [diff] [blame] | 672 |         """Restart program by raising an exception to be caught in the main | 
 | 673 |         debugger loop.  If arguments were given, set them in sys.argv.""" | 
| Georg Brandl | 8e84c65 | 2007-03-13 21:08:15 +0000 | [diff] [blame] | 674 |         if arg: | 
 | 675 |             import shlex | 
 | 676 |             argv0 = sys.argv[0:1] | 
 | 677 |             sys.argv = shlex.split(arg) | 
 | 678 |             sys.argv[:0] = argv0 | 
 | 679 |         raise Restart | 
 | 680 |  | 
 | 681 |     do_restart = do_run | 
 | 682 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 683 |     def do_return(self, arg): | 
 | 684 |         self.set_return(self.curframe) | 
 | 685 |         return 1 | 
 | 686 |     do_r = do_return | 
 | 687 |  | 
 | 688 |     def do_continue(self, arg): | 
 | 689 |         self.set_continue() | 
 | 690 |         return 1 | 
 | 691 |     do_c = do_cont = do_continue | 
 | 692 |  | 
| Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 693 |     def do_jump(self, arg): | 
 | 694 |         if self.curindex + 1 != len(self.stack): | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 695 |             print >>self.stdout, "*** You can only jump within the bottom frame" | 
| Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 696 |             return | 
 | 697 |         try: | 
 | 698 |             arg = int(arg) | 
 | 699 |         except ValueError: | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 700 |             print >>self.stdout, "*** The 'jump' command requires a line number." | 
| Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 701 |         else: | 
 | 702 |             try: | 
 | 703 |                 # Do the jump, fix up our copy of the stack, and display the | 
 | 704 |                 # new position | 
 | 705 |                 self.curframe.f_lineno = arg | 
 | 706 |                 self.stack[self.curindex] = self.stack[self.curindex][0], arg | 
 | 707 |                 self.print_stack_entry(self.stack[self.curindex]) | 
 | 708 |             except ValueError, e: | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 709 |                 print >>self.stdout, '*** Jump failed:', e | 
| Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 710 |     do_j = do_jump | 
 | 711 |  | 
| Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 712 |     def do_debug(self, arg): | 
 | 713 |         sys.settrace(None) | 
 | 714 |         globals = self.curframe.f_globals | 
| Georg Brandl | e361bcb | 2009-04-01 23:32:17 +0000 | [diff] [blame] | 715 |         locals = self.curframe_locals | 
| Guido van Rossum | cad3724 | 2008-01-15 17:59:29 +0000 | [diff] [blame] | 716 |         p = Pdb(self.completekey, self.stdin, self.stdout) | 
| Guido van Rossum | ed538d8 | 2003-04-09 19:36:34 +0000 | [diff] [blame] | 717 |         p.prompt = "(%s) " % self.prompt.strip() | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 718 |         print >>self.stdout, "ENTERING RECURSIVE DEBUGGER" | 
| Guido van Rossum | ed538d8 | 2003-04-09 19:36:34 +0000 | [diff] [blame] | 719 |         sys.call_tracing(p.run, (arg, globals, locals)) | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 720 |         print >>self.stdout, "LEAVING RECURSIVE DEBUGGER" | 
| Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 721 |         sys.settrace(self.trace_dispatch) | 
 | 722 |         self.lastcmd = p.lastcmd | 
 | 723 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 724 |     def do_quit(self, arg): | 
| Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 725 |         self._user_requested_quit = 1 | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 726 |         self.set_quit() | 
 | 727 |         return 1 | 
| Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 728 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 729 |     do_q = do_quit | 
| Guido van Rossum | d1c08f3 | 2002-04-15 00:48:24 +0000 | [diff] [blame] | 730 |     do_exit = do_quit | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 731 |  | 
| Guido van Rossum | eef2607 | 2003-01-13 21:13:55 +0000 | [diff] [blame] | 732 |     def do_EOF(self, arg): | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 733 |         print >>self.stdout | 
| Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 734 |         self._user_requested_quit = 1 | 
| Guido van Rossum | eef2607 | 2003-01-13 21:13:55 +0000 | [diff] [blame] | 735 |         self.set_quit() | 
 | 736 |         return 1 | 
 | 737 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 738 |     def do_args(self, arg): | 
| Georg Brandl | e361bcb | 2009-04-01 23:32:17 +0000 | [diff] [blame] | 739 |         co = self.curframe.f_code | 
 | 740 |         dict = self.curframe_locals | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 741 |         n = co.co_argcount | 
 | 742 |         if co.co_flags & 4: n = n+1 | 
 | 743 |         if co.co_flags & 8: n = n+1 | 
 | 744 |         for i in range(n): | 
 | 745 |             name = co.co_varnames[i] | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 746 |             print >>self.stdout, name, '=', | 
 | 747 |             if name in dict: print >>self.stdout, dict[name] | 
 | 748 |             else: print >>self.stdout, "*** undefined ***" | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 749 |     do_a = do_args | 
| Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 750 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 751 |     def do_retval(self, arg): | 
| Georg Brandl | e361bcb | 2009-04-01 23:32:17 +0000 | [diff] [blame] | 752 |         if '__return__' in self.curframe_locals: | 
 | 753 |             print >>self.stdout, self.curframe_locals['__return__'] | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 754 |         else: | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 755 |             print >>self.stdout, '*** Not yet returned!' | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 756 |     do_rv = do_retval | 
| Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 757 |  | 
| Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 758 |     def _getval(self, arg): | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 759 |         try: | 
| Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 760 |             return eval(arg, self.curframe.f_globals, | 
| Georg Brandl | e361bcb | 2009-04-01 23:32:17 +0000 | [diff] [blame] | 761 |                         self.curframe_locals) | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 762 |         except: | 
 | 763 |             t, v = sys.exc_info()[:2] | 
| Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 764 |             if isinstance(t, str): | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 765 |                 exc_type_name = t | 
 | 766 |             else: exc_type_name = t.__name__ | 
| Georg Brandl | 3e67c5c | 2010-07-30 14:14:42 +0000 | [diff] [blame] | 767 |             print >>self.stdout, '***', exc_type_name + ':', repr(v) | 
| Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 768 |             raise | 
| Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 769 |  | 
| Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 770 |     def do_p(self, arg): | 
 | 771 |         try: | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 772 |             print >>self.stdout, repr(self._getval(arg)) | 
| Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 773 |         except: | 
 | 774 |             pass | 
 | 775 |  | 
 | 776 |     def do_pp(self, arg): | 
 | 777 |         try: | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 778 |             pprint.pprint(self._getval(arg), self.stdout) | 
| Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 779 |         except: | 
 | 780 |             pass | 
| Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 781 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 782 |     def do_list(self, arg): | 
 | 783 |         self.lastcmd = 'list' | 
 | 784 |         last = None | 
 | 785 |         if arg: | 
 | 786 |             try: | 
 | 787 |                 x = eval(arg, {}, {}) | 
 | 788 |                 if type(x) == type(()): | 
 | 789 |                     first, last = x | 
 | 790 |                     first = int(first) | 
 | 791 |                     last = int(last) | 
 | 792 |                     if last < first: | 
 | 793 |                         # Assume it's a count | 
 | 794 |                         last = first + last | 
 | 795 |                 else: | 
 | 796 |                     first = max(1, int(x) - 5) | 
 | 797 |             except: | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 798 |                 print >>self.stdout, '*** Error in argument:', repr(arg) | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 799 |                 return | 
 | 800 |         elif self.lineno is None: | 
 | 801 |             first = max(1, self.curframe.f_lineno - 5) | 
 | 802 |         else: | 
 | 803 |             first = self.lineno + 1 | 
 | 804 |         if last is None: | 
 | 805 |             last = first + 10 | 
 | 806 |         filename = self.curframe.f_code.co_filename | 
 | 807 |         breaklist = self.get_file_breaks(filename) | 
 | 808 |         try: | 
 | 809 |             for lineno in range(first, last+1): | 
| Georg Brandl | 5815220 | 2009-05-05 09:06:02 +0000 | [diff] [blame] | 810 |                 line = linecache.getline(filename, lineno, | 
 | 811 |                                          self.curframe.f_globals) | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 812 |                 if not line: | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 813 |                     print >>self.stdout, '[EOF]' | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 814 |                     break | 
 | 815 |                 else: | 
| Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 816 |                     s = repr(lineno).rjust(3) | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 817 |                     if len(s) < 4: s = s + ' ' | 
 | 818 |                     if lineno in breaklist: s = s + 'B' | 
 | 819 |                     else: s = s + ' ' | 
 | 820 |                     if lineno == self.curframe.f_lineno: | 
 | 821 |                         s = s + '->' | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 822 |                     print >>self.stdout, s + '\t' + line, | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 823 |                     self.lineno = lineno | 
 | 824 |         except KeyboardInterrupt: | 
 | 825 |             pass | 
 | 826 |     do_l = do_list | 
| Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 827 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 828 |     def do_whatis(self, arg): | 
 | 829 |         try: | 
 | 830 |             value = eval(arg, self.curframe.f_globals, | 
| Georg Brandl | e361bcb | 2009-04-01 23:32:17 +0000 | [diff] [blame] | 831 |                             self.curframe_locals) | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 832 |         except: | 
 | 833 |             t, v = sys.exc_info()[:2] | 
 | 834 |             if type(t) == type(''): | 
 | 835 |                 exc_type_name = t | 
 | 836 |             else: exc_type_name = t.__name__ | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 837 |             print >>self.stdout, '***', exc_type_name + ':', repr(v) | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 838 |             return | 
 | 839 |         code = None | 
 | 840 |         # Is it a function? | 
 | 841 |         try: code = value.func_code | 
 | 842 |         except: pass | 
 | 843 |         if code: | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 844 |             print >>self.stdout, 'Function', code.co_name | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 845 |             return | 
 | 846 |         # Is it an instance method? | 
 | 847 |         try: code = value.im_func.func_code | 
 | 848 |         except: pass | 
 | 849 |         if code: | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 850 |             print >>self.stdout, 'Method', code.co_name | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 851 |             return | 
 | 852 |         # None of the above... | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 853 |         print >>self.stdout, type(value) | 
| Guido van Rossum | 8e2ec56 | 1993-07-29 09:37:38 +0000 | [diff] [blame] | 854 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 855 |     def do_alias(self, arg): | 
| Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 856 |         args = arg.split() | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 857 |         if len(args) == 0: | 
 | 858 |             keys = self.aliases.keys() | 
 | 859 |             keys.sort() | 
 | 860 |             for alias in keys: | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 861 |                 print >>self.stdout, "%s = %s" % (alias, self.aliases[alias]) | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 862 |             return | 
| Guido van Rossum | 0845459 | 2002-07-12 13:10:53 +0000 | [diff] [blame] | 863 |         if args[0] in self.aliases and len(args) == 1: | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 864 |             print >>self.stdout, "%s = %s" % (args[0], self.aliases[args[0]]) | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 865 |         else: | 
| Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 866 |             self.aliases[args[0]] = ' '.join(args[1:]) | 
| Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 867 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 868 |     def do_unalias(self, arg): | 
| Eric S. Raymond | 9b93c5f | 2001-02-09 07:58:53 +0000 | [diff] [blame] | 869 |         args = arg.split() | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 870 |         if len(args) == 0: return | 
| Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 871 |         if args[0] in self.aliases: | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 872 |             del self.aliases[args[0]] | 
| Guido van Rossum | 0023078 | 1993-03-29 11:39:45 +0000 | [diff] [blame] | 873 |  | 
| Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 874 |     #list of all the commands making the program resume execution. | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 875 |     commands_resuming = ['do_continue', 'do_step', 'do_next', 'do_return', | 
 | 876 |                          'do_quit', 'do_jump'] | 
| Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 877 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 878 |     # Print a traceback starting at the top stack frame. | 
 | 879 |     # The most recently entered frame is printed last; | 
 | 880 |     # this is different from dbx and gdb, but consistent with | 
 | 881 |     # the Python interpreter's stack trace. | 
 | 882 |     # It is also consistent with the up/down commands (which are | 
 | 883 |     # compatible with dbx and gdb: up moves towards 'main()' | 
 | 884 |     # and down moves towards the most recent stack frame). | 
| Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 885 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 886 |     def print_stack_trace(self): | 
 | 887 |         try: | 
 | 888 |             for frame_lineno in self.stack: | 
 | 889 |                 self.print_stack_entry(frame_lineno) | 
 | 890 |         except KeyboardInterrupt: | 
 | 891 |             pass | 
| Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 892 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 893 |     def print_stack_entry(self, frame_lineno, prompt_prefix=line_prefix): | 
 | 894 |         frame, lineno = frame_lineno | 
 | 895 |         if frame is self.curframe: | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 896 |             print >>self.stdout, '>', | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 897 |         else: | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 898 |             print >>self.stdout, ' ', | 
 | 899 |         print >>self.stdout, self.format_stack_entry(frame_lineno, | 
 | 900 |                                                      prompt_prefix) | 
| Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 901 |  | 
| Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 902 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 903 |     # Help methods (derived from pdb.doc) | 
| Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 904 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 905 |     def help_help(self): | 
 | 906 |         self.help_h() | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 907 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 908 |     def help_h(self): | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 909 |         print >>self.stdout, """h(elp) | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 910 | Without argument, print the list of available commands. | 
 | 911 | With a command name as argument, print help about that command | 
 | 912 | "help pdb" pipes the full documentation file to the $PAGER | 
 | 913 | "help exec" gives help on the ! command""" | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 914 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 915 |     def help_where(self): | 
 | 916 |         self.help_w() | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 917 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 918 |     def help_w(self): | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 919 |         print >>self.stdout, """w(here) | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 920 | Print a stack trace, with the most recent frame at the bottom. | 
 | 921 | An arrow indicates the "current frame", which determines the | 
| Guido van Rossum | 6bd6835 | 2001-01-20 17:57:37 +0000 | [diff] [blame] | 922 | context of most commands.  'bt' is an alias for this command.""" | 
 | 923 |  | 
 | 924 |     help_bt = help_w | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 925 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 926 |     def help_down(self): | 
 | 927 |         self.help_d() | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 928 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 929 |     def help_d(self): | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 930 |         print >>self.stdout, """d(own) | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 931 | Move the current frame one level down in the stack trace | 
| Johannes Gijsbers | 34c4120 | 2004-08-14 15:19:28 +0000 | [diff] [blame] | 932 | (to a newer frame).""" | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 933 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 934 |     def help_up(self): | 
 | 935 |         self.help_u() | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 936 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 937 |     def help_u(self): | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 938 |         print >>self.stdout, """u(p) | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 939 | Move the current frame one level up in the stack trace | 
| Johannes Gijsbers | 34c4120 | 2004-08-14 15:19:28 +0000 | [diff] [blame] | 940 | (to an older frame).""" | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 941 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 942 |     def help_break(self): | 
 | 943 |         self.help_b() | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 944 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 945 |     def help_b(self): | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 946 |         print >>self.stdout, """b(reak) ([file:]lineno | function) [, condition] | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 947 | With a line number argument, set a break there in the current | 
 | 948 | file.  With a function name, set a break at first executable line | 
 | 949 | of that function.  Without argument, list all breaks.  If a second | 
 | 950 | argument is present, it is a string specifying an expression | 
 | 951 | which must evaluate to true before the breakpoint is honored. | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 952 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 953 | The line number may be prefixed with a filename and a colon, | 
 | 954 | to specify a breakpoint in another file (probably one that | 
 | 955 | hasn't been loaded yet).  The file is searched for on sys.path; | 
 | 956 | the .py suffix may be omitted.""" | 
| Guido van Rossum | b5699c7 | 1998-07-20 23:13:54 +0000 | [diff] [blame] | 957 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 958 |     def help_clear(self): | 
 | 959 |         self.help_cl() | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 960 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 961 |     def help_cl(self): | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 962 |         print >>self.stdout, "cl(ear) filename:lineno" | 
 | 963 |         print >>self.stdout, """cl(ear) [bpnumber [bpnumber...]] | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 964 | With a space separated list of breakpoint numbers, clear | 
 | 965 | those breakpoints.  Without argument, clear all breaks (but | 
 | 966 | first ask confirmation).  With a filename:lineno argument, | 
 | 967 | clear all breaks at that line in that file. | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 968 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 969 | Note that the argument is different from previous versions of | 
 | 970 | the debugger (in python distributions 1.5.1 and before) where | 
 | 971 | a linenumber was used instead of either filename:lineno or | 
 | 972 | breakpoint numbers.""" | 
| Guido van Rossum | b5699c7 | 1998-07-20 23:13:54 +0000 | [diff] [blame] | 973 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 974 |     def help_tbreak(self): | 
| Georg Brandl | 5815220 | 2009-05-05 09:06:02 +0000 | [diff] [blame] | 975 |         print >>self.stdout, """tbreak  same arguments as break, but breakpoint | 
 | 976 | is removed when first hit.""" | 
| Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 977 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 978 |     def help_enable(self): | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 979 |         print >>self.stdout, """enable bpnumber [bpnumber ...] | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 980 | Enables the breakpoints given as a space separated list of | 
 | 981 | bp numbers.""" | 
| Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 982 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 983 |     def help_disable(self): | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 984 |         print >>self.stdout, """disable bpnumber [bpnumber ...] | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 985 | Disables the breakpoints given as a space separated list of | 
 | 986 | bp numbers.""" | 
| Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 987 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 988 |     def help_ignore(self): | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 989 |         print >>self.stdout, """ignore bpnumber count | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 990 | Sets the ignore count for the given breakpoint number.  A breakpoint | 
 | 991 | becomes active when the ignore count is zero.  When non-zero, the | 
 | 992 | count is decremented each time the breakpoint is reached and the | 
 | 993 | breakpoint is not disabled and any associated condition evaluates | 
 | 994 | to true.""" | 
| Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 995 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 996 |     def help_condition(self): | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 997 |         print >>self.stdout, """condition bpnumber str_condition | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 998 | str_condition is a string specifying an expression which | 
 | 999 | must evaluate to true before the breakpoint is honored. | 
 | 1000 | If str_condition is absent, any existing condition is removed; | 
 | 1001 | i.e., the breakpoint is made unconditional.""" | 
| Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 1002 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1003 |     def help_step(self): | 
 | 1004 |         self.help_s() | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1005 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1006 |     def help_s(self): | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1007 |         print >>self.stdout, """s(tep) | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1008 | Execute the current line, stop at the first possible occasion | 
 | 1009 | (either in a function that is called or in the current function).""" | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1010 |  | 
| Benjamin Peterson | 9835394 | 2008-05-11 14:13:25 +0000 | [diff] [blame] | 1011 |     def help_until(self): | 
 | 1012 |         self.help_unt() | 
 | 1013 |  | 
 | 1014 |     def help_unt(self): | 
 | 1015 |         print """unt(il) | 
 | 1016 | Continue execution until the line with a number greater than the current | 
 | 1017 | one is reached or until the current frame returns""" | 
 | 1018 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1019 |     def help_next(self): | 
 | 1020 |         self.help_n() | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1021 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1022 |     def help_n(self): | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1023 |         print >>self.stdout, """n(ext) | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1024 | Continue execution until the next line in the current function | 
 | 1025 | is reached or it returns.""" | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1026 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1027 |     def help_return(self): | 
 | 1028 |         self.help_r() | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1029 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1030 |     def help_r(self): | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1031 |         print >>self.stdout, """r(eturn) | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1032 | Continue execution until the current function returns.""" | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1033 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1034 |     def help_continue(self): | 
 | 1035 |         self.help_c() | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1036 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1037 |     def help_cont(self): | 
 | 1038 |         self.help_c() | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1039 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1040 |     def help_c(self): | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1041 |         print >>self.stdout, """c(ont(inue)) | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1042 | Continue execution, only stop when a breakpoint is encountered.""" | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1043 |  | 
| Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 1044 |     def help_jump(self): | 
 | 1045 |         self.help_j() | 
 | 1046 |  | 
 | 1047 |     def help_j(self): | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1048 |         print >>self.stdout, """j(ump) lineno | 
| Michael W. Hudson | cfd3884 | 2002-12-17 16:15:34 +0000 | [diff] [blame] | 1049 | Set the next line that will be executed.""" | 
 | 1050 |  | 
| Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 1051 |     def help_debug(self): | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1052 |         print >>self.stdout, """debug code | 
| Guido van Rossum | a12fe4e | 2003-04-09 19:06:21 +0000 | [diff] [blame] | 1053 | Enter a recursive debugger that steps through the code argument | 
 | 1054 | (which is an arbitrary expression or statement to be executed | 
 | 1055 | in the current environment).""" | 
 | 1056 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1057 |     def help_list(self): | 
 | 1058 |         self.help_l() | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1059 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1060 |     def help_l(self): | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1061 |         print >>self.stdout, """l(ist) [first [,last]] | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1062 | List source code for the current file. | 
 | 1063 | Without arguments, list 11 lines around the current line | 
 | 1064 | or continue the previous listing. | 
 | 1065 | With one argument, list 11 lines starting at that line. | 
 | 1066 | With two arguments, list the given range; | 
 | 1067 | if the second argument is less than the first, it is a count.""" | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1068 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1069 |     def help_args(self): | 
 | 1070 |         self.help_a() | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1071 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1072 |     def help_a(self): | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1073 |         print >>self.stdout, """a(rgs) | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1074 | Print the arguments of the current function.""" | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1075 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1076 |     def help_p(self): | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1077 |         print >>self.stdout, """p expression | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1078 | Print the value of the expression.""" | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1079 |  | 
| Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 1080 |     def help_pp(self): | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1081 |         print >>self.stdout, """pp expression | 
| Barry Warsaw | 210bd20 | 2002-11-05 22:40:20 +0000 | [diff] [blame] | 1082 | Pretty-print the value of the expression.""" | 
 | 1083 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1084 |     def help_exec(self): | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1085 |         print >>self.stdout, """(!) statement | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1086 | Execute the (one-line) statement in the context of | 
 | 1087 | the current stack frame. | 
 | 1088 | The exclamation point can be omitted unless the first word | 
 | 1089 | of the statement resembles a debugger command. | 
 | 1090 | To assign to a global variable you must always prefix the | 
 | 1091 | command with a 'global' command, e.g.: | 
 | 1092 | (Pdb) global list_options; list_options = ['-l'] | 
 | 1093 | (Pdb)""" | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1094 |  | 
| Georg Brandl | 8e84c65 | 2007-03-13 21:08:15 +0000 | [diff] [blame] | 1095 |     def help_run(self): | 
 | 1096 |         print """run [args...] | 
 | 1097 | Restart the debugged python program. If a string is supplied, it is | 
 | 1098 | splitted with "shlex" and the result is used as the new sys.argv. | 
 | 1099 | History, breakpoints, actions and debugger options are preserved. | 
 | 1100 | "restart" is an alias for "run".""" | 
 | 1101 |  | 
 | 1102 |     help_restart = help_run | 
 | 1103 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1104 |     def help_quit(self): | 
 | 1105 |         self.help_q() | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1106 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1107 |     def help_q(self): | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1108 |         print >>self.stdout, """q(uit) or exit - Quit from the debugger. | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1109 | The program being executed is aborted.""" | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1110 |  | 
| Guido van Rossum | d1c08f3 | 2002-04-15 00:48:24 +0000 | [diff] [blame] | 1111 |     help_exit = help_q | 
 | 1112 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1113 |     def help_whatis(self): | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1114 |         print >>self.stdout, """whatis arg | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1115 | Prints the type of the argument.""" | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1116 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1117 |     def help_EOF(self): | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1118 |         print >>self.stdout, """EOF | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1119 | Handles the receipt of EOF as a command.""" | 
| Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 1120 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1121 |     def help_alias(self): | 
| Georg Brandl | 5815220 | 2009-05-05 09:06:02 +0000 | [diff] [blame] | 1122 |         print >>self.stdout, """alias [name [command [parameter parameter ...]]] | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1123 | Creates an alias called 'name' the executes 'command'.  The command | 
 | 1124 | must *not* be enclosed in quotes.  Replaceable parameters are | 
 | 1125 | indicated by %1, %2, and so on, while %* is replaced by all the | 
 | 1126 | parameters.  If no command is given, the current alias for name | 
 | 1127 | is shown. If no name is given, all aliases are listed. | 
| Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 1128 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1129 | Aliases may be nested and can contain anything that can be | 
 | 1130 | legally typed at the pdb prompt.  Note!  You *can* override | 
 | 1131 | internal pdb commands with aliases!  Those internal commands | 
 | 1132 | are then hidden until the alias is removed.  Aliasing is recursively | 
 | 1133 | applied to the first word of the command line; all other words | 
 | 1134 | in the line are left alone. | 
| Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 1135 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1136 | Some useful aliases (especially when placed in the .pdbrc file) are: | 
| Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 1137 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1138 | #Print instance variables (usage "pi classInst") | 
 | 1139 | alias pi for k in %1.__dict__.keys(): print "%1.",k,"=",%1.__dict__[k] | 
| Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 1140 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1141 | #Print instance variables in self | 
 | 1142 | alias ps pi self | 
 | 1143 | """ | 
| Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 1144 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1145 |     def help_unalias(self): | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1146 |         print >>self.stdout, """unalias name | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1147 | Deletes the specified alias.""" | 
| Guido van Rossum | 2424f85 | 1998-09-11 22:50:09 +0000 | [diff] [blame] | 1148 |  | 
| Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 1149 |     def help_commands(self): | 
| Georg Brandl | 1956480 | 2006-05-10 17:13:20 +0000 | [diff] [blame] | 1150 |         print >>self.stdout, """commands [bpnumber] | 
| Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 1151 | (com) ... | 
 | 1152 | (com) end | 
 | 1153 | (Pdb) | 
 | 1154 |  | 
 | 1155 | Specify a list of commands for breakpoint number bpnumber.  The | 
 | 1156 | commands themselves appear on the following lines.  Type a line | 
 | 1157 | containing just 'end' to terminate the commands. | 
 | 1158 |  | 
 | 1159 | To remove all commands from a breakpoint, type commands and | 
 | 1160 | follow it immediately with  end; that is, give no commands. | 
 | 1161 |  | 
 | 1162 | With no bpnumber argument, commands refers to the last | 
 | 1163 | breakpoint set. | 
 | 1164 |  | 
 | 1165 | You can use breakpoint commands to start your program up again. | 
 | 1166 | Simply use the continue command, or step, or any other | 
 | 1167 | command that resumes execution. | 
 | 1168 |  | 
 | 1169 | Specifying any command resuming execution (currently continue, | 
 | 1170 | step, next, return, jump, quit and their abbreviations) terminates | 
 | 1171 | the command list (as if that command was immediately followed by end). | 
 | 1172 | This is because any time you resume execution | 
| Martin v. Löwis | f62eee1 | 2006-04-17 17:37:09 +0000 | [diff] [blame] | 1173 | (even with a simple next or step), you may encounter | 
| Martin v. Löwis | bd30f52 | 2006-04-17 17:08:37 +0000 | [diff] [blame] | 1174 | another breakpoint--which could have its own command list, leading to | 
 | 1175 | ambiguities about which list to execute. | 
 | 1176 |  | 
 | 1177 |    If you use the 'silent' command in the command list, the | 
 | 1178 | usual message about stopping at a breakpoint is not printed.  This may | 
 | 1179 | be desirable for breakpoints that are to print a specific message and | 
 | 1180 | then continue.  If none of the other commands print anything, you | 
 | 1181 | see no sign that the breakpoint was reached. | 
 | 1182 | """ | 
 | 1183 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1184 |     def help_pdb(self): | 
 | 1185 |         help() | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1186 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1187 |     def lookupmodule(self, filename): | 
| Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1188 |         """Helper function for break/clear parsing -- may be overridden. | 
 | 1189 |  | 
 | 1190 |         lookupmodule() translates (possibly incomplete) file or module name | 
 | 1191 |         into an absolute file name. | 
 | 1192 |         """ | 
 | 1193 |         if os.path.isabs(filename) and  os.path.exists(filename): | 
| Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 1194 |             return filename | 
| Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1195 |         f = os.path.join(sys.path[0], filename) | 
 | 1196 |         if  os.path.exists(f) and self.canonic(f) == self.mainpyfile: | 
 | 1197 |             return f | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1198 |         root, ext = os.path.splitext(filename) | 
 | 1199 |         if ext == '': | 
 | 1200 |             filename = filename + '.py' | 
 | 1201 |         if os.path.isabs(filename): | 
 | 1202 |             return filename | 
 | 1203 |         for dirname in sys.path: | 
 | 1204 |             while os.path.islink(dirname): | 
 | 1205 |                 dirname = os.readlink(dirname) | 
 | 1206 |             fullname = os.path.join(dirname, filename) | 
 | 1207 |             if os.path.exists(fullname): | 
 | 1208 |                 return fullname | 
 | 1209 |         return None | 
| Guido van Rossum | b5699c7 | 1998-07-20 23:13:54 +0000 | [diff] [blame] | 1210 |  | 
| Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1211 |     def _runscript(self, filename): | 
| Georg Brandl | b6ae6aa | 2007-03-13 21:58:44 +0000 | [diff] [blame] | 1212 |         # The script has to run in __main__ namespace (or imports from | 
 | 1213 |         # __main__ will break). | 
| Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 1214 |         # | 
| Georg Brandl | b6ae6aa | 2007-03-13 21:58:44 +0000 | [diff] [blame] | 1215 |         # So we clear up the __main__ and set several special variables | 
 | 1216 |         # (this gets rid of pdb's globals and cleans old variables on restarts). | 
 | 1217 |         import __main__ | 
 | 1218 |         __main__.__dict__.clear() | 
 | 1219 |         __main__.__dict__.update({"__name__"    : "__main__", | 
 | 1220 |                                   "__file__"    : filename, | 
 | 1221 |                                   "__builtins__": __builtins__, | 
 | 1222 |                                  }) | 
| Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 1223 |  | 
| Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1224 |         # When bdb sets tracing, a number of call and line events happens | 
 | 1225 |         # BEFORE debugger even reaches user's code (and the exact sequence of | 
 | 1226 |         # events depends on python version). So we take special measures to | 
 | 1227 |         # avoid stopping before we reach the main script (see user_line and | 
 | 1228 |         # user_call for details). | 
 | 1229 |         self._wait_for_mainpyfile = 1 | 
 | 1230 |         self.mainpyfile = self.canonic(filename) | 
 | 1231 |         self._user_requested_quit = 0 | 
 | 1232 |         statement = 'execfile( "%s")' % filename | 
| Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 1233 |         self.run(statement) | 
| Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1234 |  | 
| Guido van Rossum | 3577113 | 1992-09-08 11:59:04 +0000 | [diff] [blame] | 1235 | # Simplified interface | 
 | 1236 |  | 
| Guido van Rossum | 5e38b6f | 1995-02-27 13:13:40 +0000 | [diff] [blame] | 1237 | def run(statement, globals=None, locals=None): | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1238 |     Pdb().run(statement, globals, locals) | 
| Guido van Rossum | 5e38b6f | 1995-02-27 13:13:40 +0000 | [diff] [blame] | 1239 |  | 
 | 1240 | def runeval(expression, globals=None, locals=None): | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1241 |     return Pdb().runeval(expression, globals, locals) | 
| Guido van Rossum | 6fe08b0 | 1992-01-16 13:50:21 +0000 | [diff] [blame] | 1242 |  | 
 | 1243 | def runctx(statement, globals, locals): | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1244 |     # B/W compatibility | 
 | 1245 |     run(statement, globals, locals) | 
| Guido van Rossum | 6fe08b0 | 1992-01-16 13:50:21 +0000 | [diff] [blame] | 1246 |  | 
| Raymond Hettinger | 2ef7e6c | 2004-10-24 00:32:24 +0000 | [diff] [blame] | 1247 | def runcall(*args, **kwds): | 
 | 1248 |     return Pdb().runcall(*args, **kwds) | 
| Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 1249 |  | 
| Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1250 | def set_trace(): | 
| Johannes Gijsbers | 84a6c20 | 2004-11-07 11:35:30 +0000 | [diff] [blame] | 1251 |     Pdb().set_trace(sys._getframe().f_back) | 
| Guido van Rossum | 3577113 | 1992-09-08 11:59:04 +0000 | [diff] [blame] | 1252 |  | 
 | 1253 | # Post-Mortem interface | 
 | 1254 |  | 
| Facundo Batista | c54aec1 | 2008-03-08 16:50:27 +0000 | [diff] [blame] | 1255 | def post_mortem(t=None): | 
 | 1256 |     # handling the default | 
 | 1257 |     if t is None: | 
 | 1258 |         # sys.exc_info() returns (type, value, traceback) if an exception is | 
 | 1259 |         # being handled, otherwise it returns None | 
 | 1260 |         t = sys.exc_info()[2] | 
 | 1261 |         if t is None: | 
 | 1262 |             raise ValueError("A valid traceback must be passed if no " | 
 | 1263 |                                                "exception is being handled") | 
 | 1264 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1265 |     p = Pdb() | 
 | 1266 |     p.reset() | 
| Benjamin Peterson | c18574c | 2008-10-22 21:16:34 +0000 | [diff] [blame] | 1267 |     p.interaction(None, t) | 
| Guido van Rossum | 3577113 | 1992-09-08 11:59:04 +0000 | [diff] [blame] | 1268 |  | 
 | 1269 | def pm(): | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1270 |     post_mortem(sys.last_traceback) | 
| Guido van Rossum | 3577113 | 1992-09-08 11:59:04 +0000 | [diff] [blame] | 1271 |  | 
 | 1272 |  | 
 | 1273 | # Main program for testing | 
 | 1274 |  | 
| Guido van Rossum | 23efba4 | 1992-01-27 16:58:47 +0000 | [diff] [blame] | 1275 | TESTCMD = 'import x; x.main()' | 
| Guido van Rossum | 6fe08b0 | 1992-01-16 13:50:21 +0000 | [diff] [blame] | 1276 |  | 
| Guido van Rossum | 921c824 | 1992-01-10 14:54:42 +0000 | [diff] [blame] | 1277 | def test(): | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1278 |     run(TESTCMD) | 
| Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 1279 |  | 
 | 1280 | # print help | 
 | 1281 | def help(): | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1282 |     for dirname in sys.path: | 
 | 1283 |         fullname = os.path.join(dirname, 'pdb.doc') | 
 | 1284 |         if os.path.exists(fullname): | 
 | 1285 |             sts = os.system('${PAGER-more} '+fullname) | 
 | 1286 |             if sts: print '*** Pager exit status:', sts | 
 | 1287 |             break | 
 | 1288 |     else: | 
 | 1289 |         print 'Sorry, can\'t find the help file "pdb.doc"', | 
 | 1290 |         print 'along the Python search path' | 
| Guido van Rossum | f17361d | 1996-07-30 16:28:13 +0000 | [diff] [blame] | 1291 |  | 
| Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1292 | def main(): | 
| Benjamin Peterson | 13be2cf | 2008-03-26 11:57:47 +0000 | [diff] [blame] | 1293 |     if not sys.argv[1:] or sys.argv[1] in ("--help", "-h"): | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1294 |         print "usage: pdb.py scriptfile [arg] ..." | 
 | 1295 |         sys.exit(2) | 
| Guido van Rossum | f17361d | 1996-07-30 16:28:13 +0000 | [diff] [blame] | 1296 |  | 
| Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1297 |     mainpyfile =  sys.argv[1]     # Get script filename | 
 | 1298 |     if not os.path.exists(mainpyfile): | 
 | 1299 |         print 'Error:', mainpyfile, 'does not exist' | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1300 |         sys.exit(1) | 
| Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1301 |  | 
| Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 1302 |     del sys.argv[0]         # Hide "pdb.py" from argument list | 
| Guido van Rossum | ec577d5 | 1996-09-10 17:39:34 +0000 | [diff] [blame] | 1303 |  | 
| Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1304 |     # Replace pdb's dir with script's dir in front of module search path. | 
 | 1305 |     sys.path[0] = os.path.dirname(mainpyfile) | 
| Guido van Rossum | f17361d | 1996-07-30 16:28:13 +0000 | [diff] [blame] | 1306 |  | 
| Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1307 |     # Note on saving/restoring sys.argv: it's a good idea when sys.argv was | 
 | 1308 |     # modified by the script being debugged. It's a bad idea when it was | 
| Georg Brandl | 5815220 | 2009-05-05 09:06:02 +0000 | [diff] [blame] | 1309 |     # changed by the user from the command line. There is a "restart" command | 
 | 1310 |     # which allows explicit specification of command line arguments. | 
| Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1311 |     pdb = Pdb() | 
| Georg Brandl | 5077599 | 2010-08-01 19:33:15 +0000 | [diff] [blame] | 1312 |     while True: | 
| Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1313 |         try: | 
 | 1314 |             pdb._runscript(mainpyfile) | 
 | 1315 |             if pdb._user_requested_quit: | 
 | 1316 |                 break | 
| Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 1317 |             print "The program finished and will be restarted" | 
| Georg Brandl | 8e84c65 | 2007-03-13 21:08:15 +0000 | [diff] [blame] | 1318 |         except Restart: | 
 | 1319 |             print "Restarting", mainpyfile, "with arguments:" | 
 | 1320 |             print "\t" + " ".join(sys.argv[1:]) | 
| Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1321 |         except SystemExit: | 
 | 1322 |             # In most cases SystemExit does not warrant a post-mortem session. | 
 | 1323 |             print "The program exited via sys.exit(). Exit status: ", | 
 | 1324 |             print sys.exc_info()[1] | 
 | 1325 |         except: | 
 | 1326 |             traceback.print_exc() | 
 | 1327 |             print "Uncaught exception. Entering post mortem debugging" | 
 | 1328 |             print "Running 'cont' or 'step' will restart the program" | 
 | 1329 |             t = sys.exc_info()[2] | 
| Benjamin Peterson | c18574c | 2008-10-22 21:16:34 +0000 | [diff] [blame] | 1330 |             pdb.interaction(None, t) | 
| Georg Brandl | 5815220 | 2009-05-05 09:06:02 +0000 | [diff] [blame] | 1331 |             print "Post mortem debugger finished. The " + mainpyfile + \ | 
 | 1332 |                   " will be restarted" | 
| Johannes Gijsbers | 25b38c8 | 2004-10-12 18:12:09 +0000 | [diff] [blame] | 1333 |  | 
 | 1334 |  | 
 | 1335 | # When invoked as main program, invoke the debugger on a script | 
| Georg Brandl | b6ae6aa | 2007-03-13 21:58:44 +0000 | [diff] [blame] | 1336 | if __name__ == '__main__': | 
 | 1337 |     import pdb | 
 | 1338 |     pdb.main() |