@@ -36,73 +36,91 @@ extension interface uses the modules :mod:`bdb` and :mod:`cmd`.
36
36
Module :mod: `traceback `
37
37
Standard interface to extract, format and print stack traces of Python programs.
38
38
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::
41
40
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
48
69
(Pdb) continue
49
- NameError: 'spam'
50
- > <string>(1)?()
51
- (Pdb)
70
+ 3 * 2 is 6
52
71
53
72
.. versionchanged :: 3.3
54
73
Tab-completion via the :mod: `readline ` module is available for commands and
55
74
command arguments, e.g. the current global and local names are offered as
56
75
arguments of the ``p `` command.
57
76
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
59
79
example::
60
80
61
81
python -m pdb myscript.py
62
82
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
64
84
the program being debugged exits abnormally. After post-mortem debugging (or
65
85
after normal exit of the program), pdb will restart the program. Automatic
66
86
restarting preserves pdb's state (such as breakpoints) and in most cases is more
67
87
useful than quitting the debugger upon program's exit.
68
88
69
89
.. 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
71
91
in a :file: `.pdbrc ` file, see :ref: `debugger-commands `.
72
92
73
93
.. 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
75
95
``python -m `` does. As with a script, the debugger will pause execution just
76
96
before the first line of the module.
77
97
98
+ Typical usage to execute a statement under control of the debugger is::
78
99
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
+ >>>
90
108
91
109
The typical usage to inspect a crashed program is::
92
110
93
111
>>> import pdb
94
- >>> import mymodule
95
- >>> mymodule.test()
112
+ >>> def f(x):
113
+ ... print(1 / x)
114
+ ...
115
+ >>> f(0)
96
116
Traceback (most recent call last):
97
117
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
103
120
>>> pdb.pm()
104
- > ./mymodule.py(3)test2()
105
- -> print(spam)
121
+ > <stdin>(2)f()
122
+ (Pdb) p x
123
+ 0
106
124
(Pdb)
107
125
108
126
@@ -275,7 +293,7 @@ can be overridden by the local file.
275
293
276
294
.. pdbcommand :: w(here)
277
295
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 (`` > ``)
279
297
indicates the current frame, which determines the context of most commands.
280
298
281
299
.. pdbcommand :: d(own) [count]
@@ -442,7 +460,7 @@ can be overridden by the local file.
442
460
443
461
.. pdbcommand :: a(rgs)
444
462
445
- Print the argument list of the current function.
463
+ Print the arguments of the current function and their current values .
446
464
447
465
.. pdbcommand :: p expression
448
466
@@ -476,6 +494,50 @@ can be overridden by the local file.
476
494
477
495
Without *expression *, list all display expressions for the current frame.
478
496
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
+
479
541
.. versionadded :: 3.2
480
542
481
543
.. pdbcommand :: undisplay [expression]
@@ -552,7 +614,7 @@ can be overridden by the local file.
552
614
553
615
.. pdbcommand :: retval
554
616
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.
556
618
557
619
.. rubric :: Footnotes
558
620
0 commit comments