Skip to content

Commit 80826ab

Browse files
Move commands argument to set_trace
1 parent 0a7a52f commit 80826ab

File tree

4 files changed

+23
-19
lines changed

4 files changed

+23
-19
lines changed

Doc/library/pdb.rst

Lines changed: 8 additions & 5 deletions
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
@@ -192,7 +198,7 @@ The ``run*`` functions and :func:`set_trace` are aliases for instantiating the
192198
access further features, you have to do this yourself:
193199

194200
.. class:: Pdb(completekey='tab', stdin=None, stdout=None, skip=None, \
195-
nosigint=False, readrc=True, commands=None)
201+
nosigint=False, readrc=True)
196202

197203
:class:`Pdb` is the debugger class.
198204

@@ -211,9 +217,6 @@ access further features, you have to do this yourself:
211217
The *readrc* argument defaults to true and controls whether Pdb will load
212218
.pdbrc files from the filesystem.
213219

214-
The *commands* argument, if given, would be a list of commands to execute
215-
when the debugger starts. It has similar effects to the :file:`.pdbrc` file.
216-
217220
Example call to enable tracing with *skip*::
218221

219222
import pdb; pdb.Pdb(skip=['django.*']).set_trace()

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: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ class Pdb(bdb.Bdb, cmd.Cmd):
309309
_last_pdb_instance = None
310310

311311
def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None,
312-
nosigint=False, readrc=True, commands=None):
312+
nosigint=False, readrc=True):
313313
bdb.Bdb.__init__(self, skip=skip)
314314
cmd.Cmd.__init__(self, completekey, stdin, stdout)
315315
sys.audit("pdb.Pdb")
@@ -348,9 +348,6 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None,
348348
except OSError:
349349
pass
350350

351-
if commands is not None:
352-
self.rcLines.extend(commands)
353-
354351
self.commands = {} # associates a command list to breakpoint numbers
355352
self.commands_doprompt = {} # for each bp num, tells if the prompt
356353
# must be disp. after execing the cmd list
@@ -364,10 +361,14 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None,
364361
self._chained_exceptions = tuple()
365362
self._chained_exception_index = 0
366363

367-
def set_trace(self, frame=None):
364+
def set_trace(self, frame=None, *, commands=None):
368365
Pdb._last_pdb_instance = self
369366
if frame is None:
370367
frame = sys._getframe().f_back
368+
369+
if commands is not None:
370+
self.rcLines.extend(commands)
371+
371372
super().set_trace(frame)
372373

373374
def sigint_handler(self, signum, frame):
@@ -2353,21 +2354,22 @@ def runcall(*args, **kwds):
23532354
"""
23542355
return Pdb().runcall(*args, **kwds)
23552356

2356-
def set_trace(*, header=None):
2357+
def set_trace(*, header=None, commands=None):
23572358
"""Enter the debugger at the calling stack frame.
23582359
23592360
This is useful to hard-code a breakpoint at a given point in a
23602361
program, even if the code is not otherwise being debugged (e.g. when
23612362
an assertion fails). If given, *header* is printed to the console
2362-
just before debugging begins.
2363+
just before debugging begins. *commands* is an optional list of
2364+
pdb commands to run when the debugger starts.
23632365
"""
23642366
if Pdb._last_pdb_instance is not None:
23652367
pdb = Pdb._last_pdb_instance
23662368
else:
23672369
pdb = Pdb()
23682370
if header is not None:
23692371
pdb.message(header)
2370-
pdb.set_trace(sys._getframe().f_back)
2372+
pdb.set_trace(sys._getframe().f_back, commands=commands)
23712373

23722374
# Post-Mortem interface
23732375

Lib/test/test_pdb.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -901,13 +901,12 @@ def test_pdb_where_command():
901901
(Pdb) continue
902902
"""
903903

904-
905-
def test_pdb_commands_at_init():
906-
"""Test that commands can be passed to the constructor
904+
def test_pdb_commands_with_set_trace():
905+
"""Test that commands can be passed to Pdb.set_trace()
907906
908907
>>> def test_function():
909908
... x = 1
910-
... import pdb; pdb.Pdb(nosigint=True, readrc=False, commands=['p x', 'c']).set_trace()
909+
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace(commands=['p x', 'c'])
911910
912911
>>> test_function()
913912
1

0 commit comments

Comments
 (0)