Skip to content

Commit af8403a

Browse files
gh-120254: Add a commands argument to pdb.set_trace (#120255)
1 parent fc9e6bf commit af8403a

File tree

5 files changed

+30
-7
lines changed

5 files changed

+30
-7
lines changed

Doc/library/pdb.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,15 @@ slightly different way:
159159
is entered.
160160

161161

162-
.. function:: set_trace(*, header=None)
162+
.. function:: set_trace(*, header=None, commands=None)
163163

164164
Enter the debugger at the calling stack frame. This is useful to hard-code
165165
a breakpoint at a given point in a program, even if the code is not
166166
otherwise being debugged (e.g. when an assertion fails). If given,
167167
*header* is printed to the console just before debugging begins.
168+
The *commands* argument, if given, is a list of commands to execute
169+
when the debugger starts.
170+
168171

169172
.. versionchanged:: 3.7
170173
The keyword-only argument *header*.
@@ -173,6 +176,9 @@ slightly different way:
173176
:func:`set_trace` will enter the debugger immediately, rather than
174177
on the next line of code to be executed.
175178

179+
.. versionadded:: 3.14
180+
The *commands* argument.
181+
176182
.. function:: post_mortem(traceback=None)
177183

178184
Enter post-mortem debugging of the given *traceback* object. If no

Lib/doctest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,11 +389,11 @@ def __init__(self, out):
389389
# still use input() to get user input
390390
self.use_rawinput = 1
391391

392-
def set_trace(self, frame=None):
392+
def set_trace(self, frame=None, *, commands=None):
393393
self.__debugger_used = True
394394
if frame is None:
395395
frame = sys._getframe().f_back
396-
pdb.Pdb.set_trace(self, frame)
396+
pdb.Pdb.set_trace(self, frame, commands=commands)
397397

398398
def set_continue(self):
399399
# Calling set_continue unconditionally would break unit test

Lib/pdb.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,10 +361,14 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None,
361361
self._chained_exceptions = tuple()
362362
self._chained_exception_index = 0
363363

364-
def set_trace(self, frame=None):
364+
def set_trace(self, frame=None, *, commands=None):
365365
Pdb._last_pdb_instance = self
366366
if frame is None:
367367
frame = sys._getframe().f_back
368+
369+
if commands is not None:
370+
self.rcLines.extend(commands)
371+
368372
super().set_trace(frame)
369373

370374
def sigint_handler(self, signum, frame):
@@ -2350,21 +2354,22 @@ def runcall(*args, **kwds):
23502354
"""
23512355
return Pdb().runcall(*args, **kwds)
23522356

2353-
def set_trace(*, header=None):
2357+
def set_trace(*, header=None, commands=None):
23542358
"""Enter the debugger at the calling stack frame.
23552359
23562360
This is useful to hard-code a breakpoint at a given point in a
23572361
program, even if the code is not otherwise being debugged (e.g. when
23582362
an assertion fails). If given, *header* is printed to the console
2359-
just before debugging begins.
2363+
just before debugging begins. *commands* is an optional list of
2364+
pdb commands to run when the debugger starts.
23602365
"""
23612366
if Pdb._last_pdb_instance is not None:
23622367
pdb = Pdb._last_pdb_instance
23632368
else:
23642369
pdb = Pdb()
23652370
if header is not None:
23662371
pdb.message(header)
2367-
pdb.set_trace(sys._getframe().f_back)
2372+
pdb.set_trace(sys._getframe().f_back, commands=commands)
23682373

23692374
# Post-Mortem interface
23702375

Lib/test/test_pdb.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,17 @@ def test_pdb_where_command():
901901
(Pdb) continue
902902
"""
903903

904+
def test_pdb_commands_with_set_trace():
905+
"""Test that commands can be passed to Pdb.set_trace()
906+
907+
>>> def test_function():
908+
... x = 1
909+
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace(commands=['p x', 'c'])
910+
911+
>>> test_function()
912+
1
913+
"""
914+
904915

905916
# skip this test if sys.flags.no_site = True;
906917
# exit() isn't defined unless there's a site module.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added ``commands`` argument to :func:`pdb.set_trace` which allows users to send debugger commands from the source file.

0 commit comments

Comments
 (0)