Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions Doc/library/pdb.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,15 @@ slightly different way:
is entered.


.. function:: set_trace()
.. function:: set_trace(*, header=None)

Enter the debugger at the calling stack frame. This is useful to hard-code a
breakpoint at a given point in a program, even if the code is not otherwise
being debugged (e.g. when an assertion fails).
Enter the debugger at the calling stack frame. This is useful to hard-code
a breakpoint at a given point in a program, even if the code is not
otherwise being debugged (e.g. when an assertion fails). If given,
``header`` is printed to the console just before debugging begins.

.. versionadded:: 3.7

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I never remember if versionadded in a function will cause sphinx to report that the whole function is new, or will display the text that you wrote.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review! It displays the text; from a local build:

New in version 3.7: The keyword-only argument header.

The keyword-only argument ``header``.


.. function:: post_mortem(traceback=None)
Expand Down
7 changes: 7 additions & 0 deletions Doc/whatsnew/3.7.rst
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,13 @@ New function :func:`os.register_at_fork` allows registering Python callbacks
to be executed on a process fork. (Contributed by Antoine Pitrou in
:issue:`16500`.)

pdb
---

:func:`~pdb.set_trace` now takes an optional ``header`` keyword-only
argument. If given, this is printed to the console just before debugging
begins.

string
------

Expand Down
7 changes: 5 additions & 2 deletions Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1581,8 +1581,11 @@ def runctx(statement, globals, locals):
def runcall(*args, **kwds):
return Pdb().runcall(*args, **kwds)

def set_trace():
Pdb().set_trace(sys._getframe().f_back)
def set_trace(*, header=None):
pdb = Pdb()
if header is not None:
pdb.message(header)
pdb.set_trace(sys._getframe().f_back)

# Post-Mortem interface

Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
import subprocess
import textwrap

from contextlib import ExitStack
from io import StringIO
from test import support
# This little helper class is essential for testing pdb under doctest.
from test.test_doctest import _FakeInput
from unittest.mock import patch


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

def test_header(self):
stdout = StringIO()
header = 'Nobody expects... blah, blah, blah'
with ExitStack() as resources:
resources.enter_context(patch('sys.stdout', stdout))
resources.enter_context(patch.object(pdb.Pdb, 'set_trace'))
pdb.set_trace(header=header)
self.assertEqual(stdout.getvalue(), header + '\n')

def tearDown(self):
support.unlink(support.TESTFN)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
``pdb.set_trace()`` now takes an optional keyword-only argument ``header``.
If given, this is printed to the console just before debugging begins.