Skip to content

Commit 3cc5750

Browse files
authored
gh-142834: pdb commands command should use last available breakpoint (#142835)
1 parent 5989095 commit 3cc5750

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

Doc/library/pdb.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,8 @@ can be overridden by the local file.
520520
To remove all commands from a breakpoint, type ``commands`` and follow it
521521
immediately with ``end``; that is, give no commands.
522522

523-
With no *bpnumber* argument, ``commands`` refers to the last breakpoint set.
523+
With no *bpnumber* argument, ``commands`` refers to the most recently set
524+
breakpoint that still exists.
524525

525526
You can use breakpoint commands to start your program up again. Simply use
526527
the :pdbcmd:`continue` command, or :pdbcmd:`step`,

Lib/pdb.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1315,7 +1315,14 @@ def do_commands(self, arg):
13151315
reached.
13161316
"""
13171317
if not arg:
1318-
bnum = len(bdb.Breakpoint.bpbynumber) - 1
1318+
for bp in reversed(bdb.Breakpoint.bpbynumber):
1319+
if bp is None:
1320+
continue
1321+
bnum = bp.number
1322+
break
1323+
else:
1324+
self.error('cannot set commands: no existing breakpoint')
1325+
return
13191326
else:
13201327
try:
13211328
bnum = int(arg)

Lib/test/test_pdb.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3478,6 +3478,49 @@ def test_pdb_issue_gh_65052():
34783478
(Pdb) continue
34793479
"""
34803480

3481+
def test_pdb_commands_last_breakpoint():
3482+
"""See GH-142834
3483+
3484+
>>> def test_function():
3485+
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
3486+
... foo = 1
3487+
... bar = 2
3488+
3489+
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE
3490+
... 'break 4',
3491+
... 'break 3',
3492+
... 'clear 2',
3493+
... 'commands',
3494+
... 'p "success"',
3495+
... 'end',
3496+
... 'continue',
3497+
... 'clear 1',
3498+
... 'commands',
3499+
... 'continue',
3500+
... ]):
3501+
... test_function()
3502+
> <doctest test.test_pdb.test_pdb_commands_last_breakpoint[0]>(2)test_function()
3503+
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
3504+
(Pdb) break 4
3505+
Breakpoint 1 at <doctest test.test_pdb.test_pdb_commands_last_breakpoint[0]>:4
3506+
(Pdb) break 3
3507+
Breakpoint 2 at <doctest test.test_pdb.test_pdb_commands_last_breakpoint[0]>:3
3508+
(Pdb) clear 2
3509+
Deleted breakpoint 2 at <doctest test.test_pdb.test_pdb_commands_last_breakpoint[0]>:3
3510+
(Pdb) commands
3511+
(com) p "success"
3512+
(com) end
3513+
(Pdb) continue
3514+
'success'
3515+
> <doctest test.test_pdb.test_pdb_commands_last_breakpoint[0]>(4)test_function()
3516+
-> bar = 2
3517+
(Pdb) clear 1
3518+
Deleted breakpoint 1 at <doctest test.test_pdb.test_pdb_commands_last_breakpoint[0]>:4
3519+
(Pdb) commands
3520+
*** cannot set commands: no existing breakpoint
3521+
(Pdb) continue
3522+
"""
3523+
34813524

34823525
@support.force_not_colorized_test_class
34833526
@support.requires_subprocess()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Change the :mod:`pdb` ``commands`` command to use the last available breakpoint instead of failing when the most recently created breakpoint was deleted.

0 commit comments

Comments
 (0)