Skip to content

Commit 449bf2a

Browse files
gh-103237: Polish pdb docs (#103238)
1 parent 2f41a00 commit 449bf2a

File tree

1 file changed

+100
-38
lines changed

1 file changed

+100
-38
lines changed

Doc/library/pdb.rst

+100-38
Original file line numberDiff line numberDiff line change
@@ -36,73 +36,91 @@ extension interface uses the modules :mod:`bdb` and :mod:`cmd`.
3636
Module :mod:`traceback`
3737
Standard interface to extract, format and print stack traces of Python programs.
3838

39-
The debugger's prompt is ``(Pdb)``. Typical usage to run a program under control
40-
of the debugger is::
39+
The typical usage to break into the debugger is to insert::
4140

42-
>>> import pdb
43-
>>> import mymodule
44-
>>> pdb.run('mymodule.test()')
45-
> <string>(0)?()
46-
(Pdb) continue
47-
> <string>(1)?()
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+
.. versionadded:: 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+
> ...(3)double()
66+
-> return x * 2
67+
(Pdb) p x
68+
3
4869
(Pdb) continue
49-
NameError: 'spam'
50-
> <string>(1)?()
51-
(Pdb)
70+
3 * 2 is 6
5271

5372
.. versionchanged:: 3.3
5473
Tab-completion via the :mod:`readline` module is available for commands and
5574
command arguments, e.g. the current global and local names are offered as
5675
arguments of the ``p`` command.
5776

58-
:file:`pdb.py` can also be invoked as a script to debug other scripts. For
77+
78+
You can also invoke :mod:`pdb` from the command line to debug other scripts. For
5979
example::
6080

6181
python -m pdb myscript.py
6282

63-
When invoked as a script, pdb will automatically enter post-mortem debugging if
83+
When invoked as a module, pdb will automatically enter post-mortem debugging if
6484
the program being debugged exits abnormally. After post-mortem debugging (or
6585
after normal exit of the program), pdb will restart the program. Automatic
6686
restarting preserves pdb's state (such as breakpoints) and in most cases is more
6787
useful than quitting the debugger upon program's exit.
6888

6989
.. versionadded:: 3.2
70-
:file:`pdb.py` now accepts a ``-c`` option that executes commands as if given
90+
``-c`` option is introduced to execute commands as if given
7191
in a :file:`.pdbrc` file, see :ref:`debugger-commands`.
7292

7393
.. versionadded:: 3.7
74-
:file:`pdb.py` now accepts a ``-m`` option that execute modules similar to the way
94+
``-m`` option is introduced to execute modules similar to the way
7595
``python -m`` does. As with a script, the debugger will pause execution just
7696
before the first line of the module.
7797

98+
Typical usage to execute a statement under control of the debugger is::
7899

79-
The typical usage to break into the debugger is to insert::
80-
81-
import pdb; pdb.set_trace()
82-
83-
at the location you want to break into the debugger, and then run the program.
84-
You can then step through the code following this statement, and continue
85-
running without the debugger using the :pdbcmd:`continue` command.
86-
87-
.. versionadded:: 3.7
88-
The built-in :func:`breakpoint()`, when called with defaults, can be used
89-
instead of ``import pdb; pdb.set_trace()``.
100+
>>> import pdb
101+
>>> def f(x):
102+
... print(1 / x)
103+
>>> pdb.run("f(2)")
104+
> <string>(1)<module>()
105+
(Pdb) continue
106+
0.5
107+
>>>
90108

91109
The typical usage to inspect a crashed program is::
92110

93111
>>> import pdb
94-
>>> import mymodule
95-
>>> mymodule.test()
112+
>>> def f(x):
113+
... print(1 / x)
114+
...
115+
>>> f(0)
96116
Traceback (most recent call last):
97117
File "<stdin>", line 1, in <module>
98-
File "./mymodule.py", line 4, in test
99-
test2()
100-
File "./mymodule.py", line 3, in test2
101-
print(spam)
102-
NameError: spam
118+
File "<stdin>", line 2, in f
119+
ZeroDivisionError: division by zero
103120
>>> pdb.pm()
104-
> ./mymodule.py(3)test2()
105-
-> print(spam)
121+
> <stdin>(2)f()
122+
(Pdb) p x
123+
0
106124
(Pdb)
107125

108126

@@ -275,7 +293,7 @@ can be overridden by the local file.
275293

276294
.. pdbcommand:: w(here)
277295

278-
Print a stack trace, with the most recent frame at the bottom. An arrow
296+
Print a stack trace, with the most recent frame at the bottom. An arrow (``>``)
279297
indicates the current frame, which determines the context of most commands.
280298

281299
.. pdbcommand:: d(own) [count]
@@ -442,7 +460,7 @@ can be overridden by the local file.
442460

443461
.. pdbcommand:: a(rgs)
444462

445-
Print the argument list of the current function.
463+
Print the arguments of the current function and their current values.
446464

447465
.. pdbcommand:: p expression
448466

@@ -476,6 +494,50 @@ can be overridden by the local file.
476494

477495
Without *expression*, list all display expressions for the current frame.
478496

497+
.. note::
498+
499+
Display evaluates *expression* and compares to the result of the previous
500+
evaluation of *expression*, so when the result is mutable, display may not
501+
be able to pick up the changes.
502+
503+
Example::
504+
505+
lst = []
506+
breakpoint()
507+
pass
508+
lst.append(1)
509+
print(lst)
510+
511+
Display won't realize ``lst`` has been changed because the result of evaluation
512+
is modified in place by ``lst.append(1)`` before being compared::
513+
514+
> example.py(3)<module>()
515+
-> pass
516+
(Pdb) display lst
517+
display lst: []
518+
(Pdb) n
519+
> example.py(4)<module>()
520+
-> lst.append(1)
521+
(Pdb) n
522+
> example.py(5)<module>()
523+
-> print(lst)
524+
(Pdb)
525+
526+
You can do some tricks with copy mechanism to make it work::
527+
528+
> example.py(3)<module>()
529+
-> pass
530+
(Pdb) display lst[:]
531+
display lst[:]: []
532+
(Pdb) n
533+
> example.py(4)<module>()
534+
-> lst.append(1)
535+
(Pdb) n
536+
> example.py(5)<module>()
537+
-> print(lst)
538+
display lst[:]: [1] [old: []]
539+
(Pdb)
540+
479541
.. versionadded:: 3.2
480542

481543
.. pdbcommand:: undisplay [expression]
@@ -552,7 +614,7 @@ can be overridden by the local file.
552614

553615
.. pdbcommand:: retval
554616

555-
Print the return value for the last return of a function.
617+
Print the return value for the last return of the current function.
556618

557619
.. rubric:: Footnotes
558620

0 commit comments

Comments
 (0)