| 1 |
.. _debugger: |
| 2 |
|
| 3 |
:mod:`pdb` --- The Python Debugger |
| 4 |
================================== |
| 5 |
|
| 6 |
.. module:: pdb |
| 7 |
:synopsis: The Python debugger for interactive interpreters. |
| 8 |
|
| 9 |
**Source code:** :source:`Lib/pdb.py` |
| 10 |
|
| 11 |
.. index:: single: debugging |
| 12 |
|
| 13 |
-------------- |
| 14 |
|
| 15 |
The module :mod:`pdb` defines an interactive source code debugger for Python |
| 16 |
programs. It supports setting (conditional) breakpoints and single stepping at |
| 17 |
the source line level, inspection of stack frames, source code listing, and |
| 18 |
evaluation of arbitrary Python code in the context of any stack frame. It also |
| 19 |
supports post-mortem debugging and can be called under program control. |
| 20 |
|
| 21 |
.. index:: |
| 22 |
single: Pdb (class in pdb) |
| 23 |
pair: module; bdb |
| 24 |
pair: module; cmd |
| 25 |
|
| 26 |
The debugger is extensible -- it is actually defined as the class :class:`Pdb`. |
| 27 |
This is currently undocumented but easily understood by reading the source. The |
| 28 |
extension interface uses the modules :mod:`bdb` and :mod:`cmd`. |
| 29 |
|
| 30 |
.. seealso:: |
| 31 |
|
| 32 |
Module :mod:`faulthandler` |
| 33 |
Used to dump Python tracebacks explicitly, on a fault, after a timeout, |
| 34 |
or on a user signal. |
| 35 |
|
| 36 |
Module :mod:`traceback` |
| 37 |
Standard interface to extract, format and print stack traces of Python programs. |
| 38 |
|
| 39 |
The typical usage to break into the debugger is to insert:: |
| 40 |
|
| 41 |
import pdb; pdb.set_trace() |
| 42 |
|
| 43 |
Or:: |
| 44 |
|
| 45 |
breakpoint() |
| 46 |
|
| 47 |
at the location you want to break into the debugger, and then run the program. |
| 48 |
You can then step through the code following this statement, and continue |
| 49 |
running without the debugger using the :pdbcmd:`continue` command. |
| 50 |
|
| 51 |
.. versionchanged:: 3.7 |
| 52 |
The built-in :func:`breakpoint`, when called with defaults, can be used |
| 53 |
instead of ``import pdb; pdb.set_trace()``. |
| 54 |
|
| 55 |
:: |
| 56 |
|
| 57 |
def double(x): |
| 58 |
breakpoint() |
| 59 |
return x * 2 |
| 60 |
val = 3 |
| 61 |
print(f"{val} * 2 is {double(val)}") |
| 62 |
|
| 63 |
The debugger's prompt is ``(Pdb)``, which is the indicator that you are in debug mode:: |
| 64 |
|
| 65 |
> ...(2)double() |
| 66 |
-> breakpoint() |
| 67 |
(Pdb) p x |
| 68 |
3 |
| 69 |
(Pdb) continue |
| 70 |
3 * 2 is 6 |
| 71 |
|
| 72 |
.. versionchanged:: 3.3 |
| 73 |
Tab-completion via the :mod:`readline` module is available for commands and |
| 74 |
command arguments, e.g. the current global and local names are offered as |
| 75 |
arguments of the ``p`` command. |
| 76 |
|
| 77 |
|
| 78 |
.. _pdb-cli: |
| 79 |
|
| 80 |
Command-line interface |
| 81 |
---------------------- |
| 82 |
|
| 83 |
.. program:: pdb |
| 84 |
|
| 85 |
You can also invoke :mod:`pdb` from the command line to debug other scripts. For |
| 86 |
example:: |
| 87 |
|
| 88 |
python -m pdb [-c command] (-m module | pyfile) [args ...] |
| 89 |
|
| 90 |
When invoked as a module, pdb will automatically enter post-mortem debugging if |
| 91 |
the program being debugged exits abnormally. After post-mortem debugging (or |
| 92 |
after normal exit of the program), pdb will restart the program. Automatic |
| 93 |
restarting preserves pdb's state (such as breakpoints) and in most cases is more |
| 94 |
useful than quitting the debugger upon program's exit. |
| 95 |
|
| 96 |
.. option:: -c, --command <command> |
| 97 |
|
| 98 |
To execute commands as if given in a :file:`.pdbrc` file; see |
| 99 |
:ref:`debugger-commands`. |
| 100 |
|
| 101 |
.. versionchanged:: 3.2 |
| 102 |
Added the ``-c`` option. |
| 103 |
|
| 104 |
.. option:: -m <module> |
| 105 |
|
| 106 |
To execute modules similar to the way ``python -m`` does. As with a script, |
| 107 |
the debugger will pause execution just before the first line of the module. |
| 108 |
|
| 109 |
.. versionchanged:: 3.7 |
| 110 |
Added the ``-m`` option. |
| 111 |
|
| 112 |
Typical usage to execute a statement under control of the debugger is:: |
| 113 |
|
| 114 |
>>> import pdb |
| 115 |
>>> def f(x): |
| 116 |
... print(1 / x) |
| 117 |
>>> pdb.run("f(2)") |
| 118 |
> <string>(1)<module>() |
| 119 |
(Pdb) continue |
| 120 |
0.5 |
| 121 |
>>> |
| 122 |
|
| 123 |
The typical usage to inspect a crashed program is:: |
| 124 |
|
| 125 |
>>> import pdb |
| 126 |
>>> def f(x): |
| 127 |
... print(1 / x) |
| 128 |
... |
| 129 |
>>> f(0) |
| 130 |
Traceback (most recent call last): |
| 131 |
File "<stdin>", line 1, in <module> |
| 132 |
File "<stdin>", line 2, in f |
| 133 |
ZeroDivisionError: division by zero |
| 134 |
>>> pdb.pm() |
| 135 |
> <stdin>(2)f() |
| 136 |
(Pdb) p x |
| 137 |
0 |
| 138 |
(Pdb) |
| 139 |
|
| 140 |
.. versionchanged:: 3.13 |
| 141 |
The implementation of :pep:`667` means that name assignments made via ``pdb`` |
| 142 |
will immediately affect the active scope, even when running inside an |
| 143 |
:term:`optimized scope`. |
| 144 |
|
| 145 |
|
| 146 |
The module defines the following functions; each enters the debugger in a |
| 147 |
slightly different way: |
| 148 |
|
| 149 |
.. function:: run(statement, globals=None, locals=None) |
| 150 |
|
| 151 |
Execute the *statement* (given as a string or a code object) under debugger |
| 152 |
control. The debugger prompt appears before any code is executed; you can |
| 153 |
set breakpoints and type :pdbcmd:`continue`, or you can step through the |
| 154 |
statement using :pdbcmd:`step` or :pdbcmd:`next` (all these commands are |
| 155 |
explained below). The optional *globals* and *locals* arguments specify the |
| 156 |
environment in which the code is executed; by default the dictionary of the |
| 157 |
module :mod:`__main__` is used. (See the explanation of the built-in |
| 158 |
:func:`exec` or :func:`eval` functions.) |
| 159 |
|
| 160 |
|
| 161 |
.. function:: runeval(expression, globals=None, locals=None) |
| 162 |
|
| 163 |
Evaluate the *expression* (given as a string or a code object) under debugger |
| 164 |
control. When :func:`runeval` returns, it returns the value of the |
| 165 |
*expression*. Otherwise this function is similar to :func:`run`. |
| 166 |
|
| 167 |
|
| 168 |
.. function:: runcall(function, *args, **kwds) |
| 169 |
|
| 170 |
Call the *function* (a function or method object, not a string) with the |
| 171 |
given arguments. When :func:`runcall` returns, it returns whatever the |
| 172 |
function call returned. The debugger prompt appears as soon as the function |
| 173 |
is entered. |
| 174 |
|
| 175 |
|
| 176 |
.. function:: set_trace(*, header=None) |
| 177 |
|
| 178 |
Enter the debugger at the calling stack frame. This is useful to hard-code |
| 179 |
a breakpoint at a given point in a program, even if the code is not |
| 180 |
otherwise being debugged (e.g. when an assertion fails). If given, |
| 181 |
*header* is printed to the console just before debugging begins. |
| 182 |
|
| 183 |
.. versionchanged:: 3.7 |
| 184 |
The keyword-only argument *header*. |
| 185 |
|
| 186 |
.. versionchanged:: 3.13 |
| 187 |
:func:`set_trace` will enter the debugger immediately, rather than |
| 188 |
on the next line of code to be executed. |
| 189 |
|
| 190 |
.. function:: post_mortem(t=None) |
| 191 |
|
| 192 |
Enter post-mortem debugging of the given exception or |
| 193 |
:ref:`traceback object <traceback-objects>`. If no value is given, it uses |
| 194 |
the exception that is currently being handled, or raises ``ValueError`` if |
| 195 |
there isn’t one. |
| 196 |
|
| 197 |
.. versionchanged:: 3.13 |
| 198 |
Support for exception objects was added. |
| 199 |
|
| 200 |
.. function:: pm() |
| 201 |
|
| 202 |
Enter post-mortem debugging of the exception found in |
| 203 |
:data:`sys.last_exc`. |
| 204 |
|
| 205 |
|
| 206 |
The ``run*`` functions and :func:`set_trace` are aliases for instantiating the |
| 207 |
:class:`Pdb` class and calling the method of the same name. If you want to |
| 208 |
access further features, you have to do this yourself: |
| 209 |
|
| 210 |
.. class:: Pdb(completekey='tab', stdin=None, stdout=None, skip=None, \ |
| 211 |
nosigint=False, readrc=True) |
| 212 |
|
| 213 |
:class:`Pdb` is the debugger class. |
| 214 |
|
| 215 |
The *completekey*, *stdin* and *stdout* arguments are passed to the |
| 216 |
underlying :class:`cmd.Cmd` class; see the description there. |
| 217 |
|
| 218 |
The *skip* argument, if given, must be an iterable of glob-style module name |
| 219 |
patterns. The debugger will not step into frames that originate in a module |
| 220 |
that matches one of these patterns. [1]_ |
| 221 |
|
| 222 |
By default, Pdb sets a handler for the SIGINT signal (which is sent when the |
| 223 |
user presses :kbd:`Ctrl-C` on the console) when you give a :pdbcmd:`continue` command. |
| 224 |
This allows you to break into the debugger again by pressing :kbd:`Ctrl-C`. If you |
| 225 |
want Pdb not to touch the SIGINT handler, set *nosigint* to true. |
| 226 |
|
| 227 |
The *readrc* argument defaults to true and controls whether Pdb will load |
| 228 |
.pdbrc files from the filesystem. |
| 229 |
|
| 230 |
Example call to enable tracing with *skip*:: |
| 231 |
|
| 232 |
import pdb; pdb.Pdb(skip=['django.*']).set_trace() |
| 233 |
|
| 234 |
.. audit-event:: pdb.Pdb "" pdb.Pdb |
| 235 |
|
| 236 |
.. versionchanged:: 3.1 |
| 237 |
Added the *skip* parameter. |
| 238 |
|
| 239 |
.. versionchanged:: 3.2 |
| 240 |
Added the *nosigint* parameter. |
| 241 |
Previously, a SIGINT handler was never set by Pdb. |
| 242 |
|
| 243 |
.. versionchanged:: 3.6 |
| 244 |
The *readrc* argument. |
| 245 |
|
| 246 |
.. method:: run(statement, globals=None, locals=None) |
| 247 |
runeval(expression, globals=None, locals=None) |
| 248 |
runcall(function, *args, **kwds) |
| 249 |
set_trace() |
| 250 |
|
| 251 |
See the documentation for the functions explained above. |
| 252 |
|
| 253 |
|
| 254 |
.. _debugger-commands: |
| 255 |
|
| 256 |
Debugger commands |
| 257 |
----------------- |
| 258 |
|
| 259 |
The commands recognized by the debugger are listed below. Most commands can be |
| 260 |
abbreviated to one or two letters as indicated; e.g. ``h(elp)`` means that |
| 261 |
either ``h`` or ``help`` can be used to enter the help command (but not ``he`` |
| 262 |
or ``hel``, nor ``H`` or ``Help`` or ``HELP``). Arguments to commands must be |
| 263 |
separated by whitespace (spaces or tabs). Optional arguments are enclosed in |
| 264 |
square brackets (``[]``) in the command syntax; the square brackets must not be |
| 265 |
typed. Alternatives in the command syntax are separated by a vertical bar |
| 266 |
(``|``). |
| 267 |
|
| 268 |
Entering a blank line repeats the last command entered. Exception: if the last |
| 269 |
command was a :pdbcmd:`list` command, the next 11 lines are listed. |
| 270 |
|
| 271 |
Commands that the debugger doesn't recognize are assumed to be Python statements |
| 272 |
and are executed in the context of the program being debugged. Python |
| 273 |
statements can also be prefixed with an exclamation point (``!``). This is a |
| 274 |
powerful way to inspect the program being debugged; it is even possible to |
| 275 |
change a variable or call a function. When an exception occurs in such a |
| 276 |
statement, the exception name is printed but the debugger's state is not |
| 277 |
changed. |
| 278 |
|
| 279 |
.. versionchanged:: 3.13 |
| 280 |
Expressions/Statements whose prefix is a pdb command are now correctly |
| 281 |
identified and executed. |
| 282 |
|
| 283 |
The debugger supports :ref:`aliases <debugger-aliases>`. Aliases can have |
| 284 |
parameters which allows one a certain level of adaptability to the context under |
| 285 |
examination. |
| 286 |
|
| 287 |
Multiple commands may be entered on a single line, separated by ``;;``. (A |
| 288 |
single ``;`` is not used as it is the separator for multiple commands in a line |
| 289 |
that is passed to the Python parser.) No intelligence is applied to separating |
| 290 |
the commands; the input is split at the first ``;;`` pair, even if it is in the |
| 291 |
middle of a quoted string. A workaround for strings with double semicolons |
| 292 |
is to use implicit string concatenation ``';'';'`` or ``";"";"``. |
| 293 |
|
| 294 |
To set a temporary global variable, use a *convenience variable*. A *convenience |
| 295 |
variable* is a variable whose name starts with ``$``. For example, ``$foo = 1`` |
| 296 |
sets a global variable ``$foo`` which you can use in the debugger session. The |
| 297 |
*convenience variables* are cleared when the program resumes execution so it's |
| 298 |
less likely to interfere with your program compared to using normal variables |
| 299 |
like ``foo = 1``. |
| 300 |
|
| 301 |
There are three preset *convenience variables*: |
| 302 |
|
| 303 |
* ``$_frame``: the current frame you are debugging |
| 304 |
* ``$_retval``: the return value if the frame is returning |
| 305 |
* ``$_exception``: the exception if the frame is raising an exception |
| 306 |
|
| 307 |
.. versionadded:: 3.12 |
| 308 |
|
| 309 |
Added the *convenience variable* feature. |
| 310 |
|
| 311 |
.. index:: |
| 312 |
pair: .pdbrc; file |
| 313 |
triple: debugger; configuration; file |
| 314 |
|
| 315 |
If a file :file:`.pdbrc` exists in the user's home directory or in the current |
| 316 |
directory, it is read with ``'utf-8'`` encoding and executed as if it had been |
| 317 |
typed at the debugger prompt, with the exception that empty lines and lines |
| 318 |
starting with ``#`` are ignored. This is particularly useful for aliases. If both |
| 319 |
files exist, the one in the home directory is read first and aliases defined there |
| 320 |
can be overridden by the local file. |
| 321 |
|
| 322 |
.. versionchanged:: 3.2 |
| 323 |
:file:`.pdbrc` can now contain commands that continue debugging, such as |
| 324 |
:pdbcmd:`continue` or :pdbcmd:`next`. Previously, these commands had no |
| 325 |
effect. |
| 326 |
|
| 327 |
.. versionchanged:: 3.11 |
| 328 |
:file:`.pdbrc` is now read with ``'utf-8'`` encoding. Previously, it was read |
| 329 |
with the system locale encoding. |
| 330 |
|
| 331 |
|
| 332 |
.. pdbcommand:: h(elp) [command] |
| 333 |
|
| 334 |
Without argument, print the list of available commands. With a *command* as |
| 335 |
argument, print help about that command. ``help pdb`` displays the full |
| 336 |
documentation (the docstring of the :mod:`pdb` module). Since the *command* |
| 337 |
argument must be an identifier, ``help exec`` must be entered to get help on |
| 338 |
the ``!`` command. |
| 339 |
|
| 340 |
.. pdbcommand:: w(here) |
| 341 |
|
| 342 |
Print a stack trace, with the most recent frame at the bottom. An arrow (``>``) |
| 343 |
indicates the current frame, which determines the context of most commands. |
| 344 |
|
| 345 |
.. pdbcommand:: d(own) [count] |
| 346 |
|
| 347 |
Move the current frame *count* (default one) levels down in the stack trace |
| 348 |
(to a newer frame). |
| 349 |
|
| 350 |
.. pdbcommand:: u(p) [count] |
| 351 |
|
| 352 |
Move the current frame *count* (default one) levels up in the stack trace (to |
| 353 |
an older frame). |
| 354 |
|
| 355 |
.. pdbcommand:: b(reak) [([filename:]lineno | function) [, condition]] |
| 356 |
|
| 357 |
With a *lineno* argument, set a break at line *lineno* in the current file. |
| 358 |
The line number may be prefixed with a *filename* and a colon, |
| 359 |
to specify a breakpoint in another file (possibly one that hasn't been loaded |
| 360 |
yet). The file is searched on :data:`sys.path`. Accepatable forms of *filename* |
| 361 |
are ``/abspath/to/file.py``, ``relpath/file.py``, ``module`` and |
| 362 |
``package.module``. |
| 363 |
|
| 364 |
With a *function* argument, set a break at the first executable statement within |
| 365 |
that function. *function* can be any expression that evaluates to a function |
| 366 |
in the current namespace. |
| 367 |
|
| 368 |
If a second argument is present, it is an expression which must evaluate to |
| 369 |
true before the breakpoint is honored. |
| 370 |
|
| 371 |
Without argument, list all breaks, including for each breakpoint, the number |
| 372 |
of times that breakpoint has been hit, the current ignore count, and the |
| 373 |
associated condition if any. |
| 374 |
|
| 375 |
Each breakpoint is assigned a number to which all the other |
| 376 |
breakpoint commands refer. |
| 377 |
|
| 378 |
.. pdbcommand:: tbreak [([filename:]lineno | function) [, condition]] |
| 379 |
|
| 380 |
Temporary breakpoint, which is removed automatically when it is first hit. |
| 381 |
The arguments are the same as for :pdbcmd:`break`. |
| 382 |
|
| 383 |
.. pdbcommand:: cl(ear) [filename:lineno | bpnumber ...] |
| 384 |
|
| 385 |
With a *filename:lineno* argument, clear all the breakpoints at this line. |
| 386 |
With a space separated list of breakpoint numbers, clear those breakpoints. |
| 387 |
Without argument, clear all breaks (but first ask confirmation). |
| 388 |
|
| 389 |
.. pdbcommand:: disable bpnumber [bpnumber ...] |
| 390 |
|
| 391 |
Disable the breakpoints given as a space separated list of breakpoint |
| 392 |
numbers. Disabling a breakpoint means it cannot cause the program to stop |
| 393 |
execution, but unlike clearing a breakpoint, it remains in the list of |
| 394 |
breakpoints and can be (re-)enabled. |
| 395 |
|
| 396 |
.. pdbcommand:: enable bpnumber [bpnumber ...] |
| 397 |
|
| 398 |
Enable the breakpoints specified. |
| 399 |
|
| 400 |
.. pdbcommand:: ignore bpnumber [count] |
| 401 |
|
| 402 |
Set the ignore count for the given breakpoint number. If *count* is omitted, |
| 403 |
the ignore count is set to 0. A breakpoint becomes active when the ignore |
| 404 |
count is zero. When non-zero, the *count* is decremented each time the |
| 405 |
breakpoint is reached and the breakpoint is not disabled and any associated |
| 406 |
condition evaluates to true. |
| 407 |
|
| 408 |
.. pdbcommand:: condition bpnumber [condition] |
| 409 |
|
| 410 |
Set a new *condition* for the breakpoint, an expression which must evaluate |
| 411 |
to true before the breakpoint is honored. If *condition* is absent, any |
| 412 |
existing condition is removed; i.e., the breakpoint is made unconditional. |
| 413 |
|
| 414 |
.. pdbcommand:: commands [bpnumber] |
| 415 |
|
| 416 |
Specify a list of commands for breakpoint number *bpnumber*. The commands |
| 417 |
themselves appear on the following lines. Type a line containing just |
| 418 |
``end`` to terminate the commands. An example:: |
| 419 |
|
| 420 |
(Pdb) commands 1 |
| 421 |
(com) p some_variable |
| 422 |
(com) end |
| 423 |
(Pdb) |
| 424 |
|
| 425 |
To remove all commands from a breakpoint, type ``commands`` and follow it |
| 426 |
immediately with ``end``; that is, give no commands. |
| 427 |
|
| 428 |
With no *bpnumber* argument, ``commands`` refers to the last breakpoint set. |
| 429 |
|
| 430 |
You can use breakpoint commands to start your program up again. Simply use |
| 431 |
the :pdbcmd:`continue` command, or :pdbcmd:`step`, |
| 432 |
or any other command that resumes execution. |
| 433 |
|
| 434 |
Specifying any command resuming execution |
| 435 |
(currently :pdbcmd:`continue`, :pdbcmd:`step`, :pdbcmd:`next`, |
| 436 |
:pdbcmd:`return`, :pdbcmd:`jump`, :pdbcmd:`quit` and their abbreviations) |
| 437 |
terminates the command list (as if |
| 438 |
that command was immediately followed by end). This is because any time you |
| 439 |
resume execution (even with a simple next or step), you may encounter another |
| 440 |
breakpoint—which could have its own command list, leading to ambiguities about |
| 441 |
which list to execute. |
| 442 |
|
| 443 |
If you use the ``silent`` command in the command list, the usual message about |
| 444 |
stopping at a breakpoint is not printed. This may be desirable for breakpoints |
| 445 |
that are to print a specific message and then continue. If none of the other |
| 446 |
commands print anything, you see no sign that the breakpoint was reached. |
| 447 |
|
| 448 |
.. pdbcommand:: s(tep) |
| 449 |
|
| 450 |
Execute the current line, stop at the first possible occasion (either in a |
| 451 |
function that is called or on the next line in the current function). |
| 452 |
|
| 453 |
.. pdbcommand:: n(ext) |
| 454 |
|
| 455 |
Continue execution until the next line in the current function is reached or |
| 456 |
it returns. (The difference between :pdbcmd:`next` and :pdbcmd:`step` is |
| 457 |
that :pdbcmd:`step` stops inside a called function, while :pdbcmd:`next` |
| 458 |
executes called functions at (nearly) full speed, only stopping at the next |
| 459 |
line in the current function.) |
| 460 |
|
| 461 |
.. pdbcommand:: unt(il) [lineno] |
| 462 |
|
| 463 |
Without argument, continue execution until the line with a number greater |
| 464 |
than the current one is reached. |
| 465 |
|
| 466 |
With *lineno*, continue execution until a line with a number greater or |
| 467 |
equal to *lineno* is reached. In both cases, also stop when the current frame |
| 468 |
returns. |
| 469 |
|
| 470 |
.. versionchanged:: 3.2 |
| 471 |
Allow giving an explicit line number. |
| 472 |
|
| 473 |
.. pdbcommand:: r(eturn) |
| 474 |
|
| 475 |
Continue execution until the current function returns. |
| 476 |
|
| 477 |
.. pdbcommand:: c(ont(inue)) |
| 478 |
|
| 479 |
Continue execution, only stop when a breakpoint is encountered. |
| 480 |
|
| 481 |
.. pdbcommand:: j(ump) lineno |
| 482 |
|
| 483 |
Set the next line that will be executed. Only available in the bottom-most |
| 484 |
frame. This lets you jump back and execute code again, or jump forward to |
| 485 |
skip code that you don't want to run. |
| 486 |
|
| 487 |
It should be noted that not all jumps are allowed -- for instance it is not |
| 488 |
possible to jump into the middle of a :keyword:`for` loop or out of a |
| 489 |
:keyword:`finally` clause. |
| 490 |
|
| 491 |
.. pdbcommand:: l(ist) [first[, last]] |
| 492 |
|
| 493 |
List source code for the current file. Without arguments, list 11 lines |
| 494 |
around the current line or continue the previous listing. With ``.`` as |
| 495 |
argument, list 11 lines around the current line. With one argument, |
| 496 |
list 11 lines around at that line. With two arguments, list the given range; |
| 497 |
if the second argument is less than the first, it is interpreted as a count. |
| 498 |
|
| 499 |
The current line in the current frame is indicated by ``->``. If an |
| 500 |
exception is being debugged, the line where the exception was originally |
| 501 |
raised or propagated is indicated by ``>>``, if it differs from the current |
| 502 |
line. |
| 503 |
|
| 504 |
.. versionchanged:: 3.2 |
| 505 |
Added the ``>>`` marker. |
| 506 |
|
| 507 |
.. pdbcommand:: ll | longlist |
| 508 |
|
| 509 |
List all source code for the current function or frame. Interesting lines |
| 510 |
are marked as for :pdbcmd:`list`. |
| 511 |
|
| 512 |
.. versionadded:: 3.2 |
| 513 |
|
| 514 |
.. pdbcommand:: a(rgs) |
| 515 |
|
| 516 |
Print the arguments of the current function and their current values. |
| 517 |
|
| 518 |
.. pdbcommand:: p expression |
| 519 |
|
| 520 |
Evaluate *expression* in the current context and print its value. |
| 521 |
|
| 522 |
.. note:: |
| 523 |
|
| 524 |
``print()`` can also be used, but is not a debugger command --- this executes the |
| 525 |
Python :func:`print` function. |
| 526 |
|
| 527 |
|
| 528 |
.. pdbcommand:: pp expression |
| 529 |
|
| 530 |
Like the :pdbcmd:`p` command, except the value of *expression* is |
| 531 |
pretty-printed using the :mod:`pprint` module. |
| 532 |
|
| 533 |
.. pdbcommand:: whatis expression |
| 534 |
|
| 535 |
Print the type of *expression*. |
| 536 |
|
| 537 |
.. pdbcommand:: source expression |
| 538 |
|
| 539 |
Try to get source code of *expression* and display it. |
| 540 |
|
| 541 |
.. versionadded:: 3.2 |
| 542 |
|
| 543 |
.. pdbcommand:: display [expression] |
| 544 |
|
| 545 |
Display the value of *expression* if it changed, each time execution stops |
| 546 |
in the current frame. |
| 547 |
|
| 548 |
Without *expression*, list all display expressions for the current frame. |
| 549 |
|
| 550 |
.. note:: |
| 551 |
|
| 552 |
Display evaluates *expression* and compares to the result of the previous |
| 553 |
evaluation of *expression*, so when the result is mutable, display may not |
| 554 |
be able to pick up the changes. |
| 555 |
|
| 556 |
Example:: |
| 557 |
|
| 558 |
lst = [] |
| 559 |
breakpoint() |
| 560 |
pass |
| 561 |
lst.append(1) |
| 562 |
print(lst) |
| 563 |
|
| 564 |
Display won't realize ``lst`` has been changed because the result of evaluation |
| 565 |
is modified in place by ``lst.append(1)`` before being compared:: |
| 566 |
|
| 567 |
> example.py(3)<module>() |
| 568 |
-> pass |
| 569 |
(Pdb) display lst |
| 570 |
display lst: [] |
| 571 |
(Pdb) n |
| 572 |
> example.py(4)<module>() |
| 573 |
-> lst.append(1) |
| 574 |
(Pdb) n |
| 575 |
> example.py(5)<module>() |
| 576 |
-> print(lst) |
| 577 |
(Pdb) |
| 578 |
|
| 579 |
You can do some tricks with copy mechanism to make it work:: |
| 580 |
|
| 581 |
> example.py(3)<module>() |
| 582 |
-> pass |
| 583 |
(Pdb) display lst[:] |
| 584 |
display lst[:]: [] |
| 585 |
(Pdb) n |
| 586 |
> example.py(4)<module>() |
| 587 |
-> lst.append(1) |
| 588 |
(Pdb) n |
| 589 |
> example.py(5)<module>() |
| 590 |
-> print(lst) |
| 591 |
display lst[:]: [1] [old: []] |
| 592 |
(Pdb) |
| 593 |
|
| 594 |
.. versionadded:: 3.2 |
| 595 |
|
| 596 |
.. pdbcommand:: undisplay [expression] |
| 597 |
|
| 598 |
Do not display *expression* anymore in the current frame. Without |
| 599 |
*expression*, clear all display expressions for the current frame. |
| 600 |
|
| 601 |
.. versionadded:: 3.2 |
| 602 |
|
| 603 |
.. pdbcommand:: interact |
| 604 |
|
| 605 |
Start an interactive interpreter (using the :mod:`code` module) in a new |
| 606 |
global namespace initialised from the local and global namespaces for the |
| 607 |
current scope. Use ``exit()`` or ``quit()`` to exit the interpreter and |
| 608 |
return to the debugger. |
| 609 |
|
| 610 |
.. note:: |
| 611 |
|
| 612 |
As ``interact`` creates a new dedicated namespace for code execution, |
| 613 |
assignments to variables will not affect the original namespaces. |
| 614 |
However, modifications to any referenced mutable objects will be reflected |
| 615 |
in the original namespaces as usual. |
| 616 |
|
| 617 |
.. versionadded:: 3.2 |
| 618 |
|
| 619 |
.. versionchanged:: 3.13 |
| 620 |
``exit()`` and ``quit()`` can be used to exit the :pdbcmd:`interact` |
| 621 |
command. |
| 622 |
|
| 623 |
.. versionchanged:: 3.13 |
| 624 |
:pdbcmd:`interact` directs its output to the debugger's |
| 625 |
output channel rather than :data:`sys.stderr`. |
| 626 |
|
| 627 |
.. _debugger-aliases: |
| 628 |
|
| 629 |
.. pdbcommand:: alias [name [command]] |
| 630 |
|
| 631 |
Create an alias called *name* that executes *command*. The *command* must |
| 632 |
*not* be enclosed in quotes. Replaceable parameters can be indicated by |
| 633 |
``%1``, ``%2``, ... and ``%9``, while ``%*`` is replaced by all the parameters. |
| 634 |
If *command* is omitted, the current alias for *name* is shown. If no |
| 635 |
arguments are given, all aliases are listed. |
| 636 |
|
| 637 |
Aliases may be nested and can contain anything that can be legally typed at |
| 638 |
the pdb prompt. Note that internal pdb commands *can* be overridden by |
| 639 |
aliases. Such a command is then hidden until the alias is removed. Aliasing |
| 640 |
is recursively applied to the first word of the command line; all other words |
| 641 |
in the line are left alone. |
| 642 |
|
| 643 |
As an example, here are two useful aliases (especially when placed in the |
| 644 |
:file:`.pdbrc` file):: |
| 645 |
|
| 646 |
# Print instance variables (usage "pi classInst") |
| 647 |
alias pi for k in %1.__dict__.keys(): print(f"%1.{k} = {%1.__dict__[k]}") |
| 648 |
# Print instance variables in self |
| 649 |
alias ps pi self |
| 650 |
|
| 651 |
.. pdbcommand:: unalias name |
| 652 |
|
| 653 |
Delete the specified alias *name*. |
| 654 |
|
| 655 |
.. pdbcommand:: ! statement |
| 656 |
|
| 657 |
Execute the (one-line) *statement* in the context of the current stack frame. |
| 658 |
The exclamation point can be omitted unless the first word of the statement |
| 659 |
resembles a debugger command, e.g.: |
| 660 |
|
| 661 |
.. code-block:: none |
| 662 |
|
| 663 |
(Pdb) ! n=42 |
| 664 |
(Pdb) |
| 665 |
|
| 666 |
To set a global variable, you can prefix the assignment command with a |
| 667 |
:keyword:`global` statement on the same line, e.g.: |
| 668 |
|
| 669 |
.. code-block:: none |
| 670 |
|
| 671 |
(Pdb) global list_options; list_options = ['-l'] |
| 672 |
(Pdb) |
| 673 |
|
| 674 |
.. pdbcommand:: run [args ...] |
| 675 |
restart [args ...] |
| 676 |
|
| 677 |
Restart the debugged Python program. If *args* is supplied, it is split |
| 678 |
with :mod:`shlex` and the result is used as the new :data:`sys.argv`. |
| 679 |
History, breakpoints, actions and debugger options are preserved. |
| 680 |
:pdbcmd:`restart` is an alias for :pdbcmd:`run`. |
| 681 |
|
| 682 |
.. pdbcommand:: q(uit) |
| 683 |
|
| 684 |
Quit from the debugger. The program being executed is aborted. |
| 685 |
|
| 686 |
.. pdbcommand:: debug code |
| 687 |
|
| 688 |
Enter a recursive debugger that steps through *code* |
| 689 |
(which is an arbitrary expression or statement to be |
| 690 |
executed in the current environment). |
| 691 |
|
| 692 |
.. pdbcommand:: retval |
| 693 |
|
| 694 |
Print the return value for the last return of the current function. |
| 695 |
|
| 696 |
.. pdbcommand:: exceptions [excnumber] |
| 697 |
|
| 698 |
List or jump between chained exceptions. |
| 699 |
|
| 700 |
When using ``pdb.pm()`` or ``Pdb.post_mortem(...)`` with a chained exception |
| 701 |
instead of a traceback, it allows the user to move between the |
| 702 |
chained exceptions using ``exceptions`` command to list exceptions, and |
| 703 |
``exceptions <number>`` to switch to that exception. |
| 704 |
|
| 705 |
|
| 706 |
Example:: |
| 707 |
|
| 708 |
def out(): |
| 709 |
try: |
| 710 |
middle() |
| 711 |
except Exception as e: |
| 712 |
raise ValueError("reraise middle() error") from e |
| 713 |
|
| 714 |
def middle(): |
| 715 |
try: |
| 716 |
return inner(0) |
| 717 |
except Exception as e: |
| 718 |
raise ValueError("Middle fail") |
| 719 |
|
| 720 |
def inner(x): |
| 721 |
1 / x |
| 722 |
|
| 723 |
out() |
| 724 |
|
| 725 |
calling ``pdb.pm()`` will allow to move between exceptions:: |
| 726 |
|
| 727 |
> example.py(5)out() |
| 728 |
-> raise ValueError("reraise middle() error") from e |
| 729 |
|
| 730 |
(Pdb) exceptions |
| 731 |
0 ZeroDivisionError('division by zero') |
| 732 |
1 ValueError('Middle fail') |
| 733 |
> 2 ValueError('reraise middle() error') |
| 734 |
|
| 735 |
(Pdb) exceptions 0 |
| 736 |
> example.py(16)inner() |
| 737 |
-> 1 / x |
| 738 |
|
| 739 |
(Pdb) up |
| 740 |
> example.py(10)middle() |
| 741 |
-> return inner(0) |
| 742 |
|
| 743 |
.. versionadded:: 3.13 |
| 744 |
|
| 745 |
.. rubric:: Footnotes |
| 746 |
|
| 747 |
.. [1] Whether a frame is considered to originate in a certain module |
| 748 |
is determined by the ``__name__`` in the frame globals. |