← Back to DAGDA | Support | Source: https://github.com/python/cpython/commit/35425d638c0eeb8377620e016f47df3ae08d7061
Skip to content

Commit 35425d6

Browse files
authored
bpo-31389 Add an optional header argument to pdb.set_trace() (#3438)
* Give pdb.set_trace() an optional `header` argument * What's new. * Give pdb.set_trace() an optional `header` argument * What's new.
1 parent b1558a0 commit 35425d6

5 files changed

Lines changed: 34 additions & 6 deletions

File tree

‎Doc/library/pdb.rst‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,15 @@ slightly different way:
118118
is entered.
119119

120120

121-
.. function:: set_trace()
121+
.. function:: set_trace(*, header=None)
122122

123-
Enter the debugger at the calling stack frame. This is useful to hard-code a
124-
breakpoint at a given point in a program, even if the code is not otherwise
125-
being debugged (e.g. when an assertion fails).
123+
Enter the debugger at the calling stack frame. This is useful to hard-code
124+
a breakpoint at a given point in a program, even if the code is not
125+
otherwise being debugged (e.g. when an assertion fails). If given,
126+
``header`` is printed to the console just before debugging begins.
127+
128+
.. versionadded:: 3.7
129+
The keyword-only argument ``header``.
126130

127131

128132
.. function:: post_mortem(traceback=None)

‎Doc/whatsnew/3.7.rst‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,13 @@ New function :func:`os.register_at_fork` allows registering Python callbacks
232232
to be executed on a process fork. (Contributed by Antoine Pitrou in
233233
:issue:`16500`.)
234234

235+
pdb
236+
---
237+
238+
:func:`~pdb.set_trace` now takes an optional ``header`` keyword-only
239+
argument. If given, this is printed to the console just before debugging
240+
begins.
241+
235242
string
236243
------
237244

‎Lib/pdb.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,8 +1581,11 @@ def runctx(statement, globals, locals):
15811581
def runcall(*args, **kwds):
15821582
return Pdb().runcall(*args, **kwds)
15831583

1584-
def set_trace():
1585-
Pdb().set_trace(sys._getframe().f_back)
1584+
def set_trace(*, header=None):
1585+
pdb = Pdb()
1586+
if header is not None:
1587+
pdb.message(header)
1588+
pdb.set_trace(sys._getframe().f_back)
15861589

15871590
# Post-Mortem interface
15881591

‎Lib/test/test_pdb.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
import subprocess
1010
import textwrap
1111

12+
from contextlib import ExitStack
13+
from io import StringIO
1214
from test import support
1315
# This little helper class is essential for testing pdb under doctest.
1416
from test.test_doctest import _FakeInput
17+
from unittest.mock import patch
1518

1619

1720
class PdbTestInput(object):
@@ -1107,6 +1110,15 @@ def test_readrc_kwarg(self):
11071110
if save_home is not None:
11081111
os.environ['HOME'] = save_home
11091112

1113+
def test_header(self):
1114+
stdout = StringIO()
1115+
header = 'Nobody expects... blah, blah, blah'
1116+
with ExitStack() as resources:
1117+
resources.enter_context(patch('sys.stdout', stdout))
1118+
resources.enter_context(patch.object(pdb.Pdb, 'set_trace'))
1119+
pdb.set_trace(header=header)
1120+
self.assertEqual(stdout.getvalue(), header + '\n')
1121+
11101122
def tearDown(self):
11111123
support.unlink(support.TESTFN)
11121124

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
``pdb.set_trace()`` now takes an optional keyword-only argument ``header``.
2+
If given, this is printed to the console just before debugging begins.

0 commit comments

Comments
 (0)