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