Skip to content

Commit

Permalink
pythonGH-111744: Make breakpoint() enter the debugger immediately (py…
Browse files Browse the repository at this point in the history
  • Loading branch information
gaogaotiantian authored May 5, 2024
1 parent 1511bc9 commit 5a0022a
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 68 deletions.
4 changes: 4 additions & 0 deletions Doc/library/bdb.rst
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@ The :mod:`bdb` module also defines two classes:
Start debugging from *frame*. If *frame* is not specified, debugging
starts from caller's frame.

.. versionchanged:: 3.13
:func:`set_trace` will enter the debugger immediately, rather than
on the next line of code to be executed.

.. method:: set_continue()

Stop only at breakpoints or when finished. If there are no breakpoints,
Expand Down
7 changes: 5 additions & 2 deletions Doc/library/pdb.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ running without the debugger using the :pdbcmd:`continue` command.

The debugger's prompt is ``(Pdb)``, which is the indicator that you are in debug mode::

> ...(3)double()
-> return x * 2
> ...(2)double()
-> breakpoint()
(Pdb) p x
3
(Pdb) continue
Expand Down Expand Up @@ -164,6 +164,9 @@ slightly different way:
.. versionchanged:: 3.7
The keyword-only argument *header*.

.. versionchanged:: 3.13
:func:`set_trace` will enter the debugger immediately, rather than
on the next line of code to be executed.

.. function:: post_mortem(traceback=None)

Expand Down
6 changes: 6 additions & 0 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,12 @@ pdb
* :mod:`zipapp` is supported as a debugging target.
(Contributed by Tian Gao in :gh:`118501`.)

* ``breakpoint()`` and ``pdb.set_trace()`` now enter the debugger immediately
rather than on the next line of code to be executed. This change prevents the
debugger from breaking outside of the context when ``breakpoint()`` is positioned
at the end of the context.
(Contributed by Tian Gao in :gh:`118579`.)

queue
-----

Expand Down
2 changes: 1 addition & 1 deletion Lib/bdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ def set_trace(self, frame=None):
# We need f_trace_lines == True for the debugger to work
frame.f_trace_lines = True
frame = frame.f_back
self.set_step()
self.set_stepinstr()
sys.settrace(self.trace_dispatch)

def set_continue(self):
Expand Down
2 changes: 2 additions & 0 deletions Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,8 @@ def user_line(self, frame):
if self.bp_commands(frame):
self.interaction(frame, None)

user_opcode = user_line

def bp_commands(self, frame):
"""Call every command that was set for the current active breakpoint
(if there is one).
Expand Down
14 changes: 10 additions & 4 deletions Lib/test/test_doctest/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2056,8 +2056,7 @@ def test_pdb_set_trace():
>>> try: runner.run(test)
... finally: sys.stdin = real_stdin
--Return--
> <doctest foo-bar@baz[2]>(1)<module>()->None
> <doctest foo-bar@baz[2]>(1)<module>()
-> import pdb; pdb.set_trace()
(Pdb) print(x)
42
Expand Down Expand Up @@ -2087,8 +2086,7 @@ def test_pdb_set_trace():
... runner.run(test)
... finally:
... sys.stdin = real_stdin
--Return--
> <doctest test.test_doctest.test_doctest.test_pdb_set_trace[9]>(3)calls_set_trace()->None
> <doctest test.test_doctest.test_doctest.test_pdb_set_trace[9]>(3)calls_set_trace()
-> import pdb; pdb.set_trace()
(Pdb) print(y)
2
Expand All @@ -2114,6 +2112,7 @@ def test_pdb_set_trace():
>>> test = parser.get_doctest(doc, globals(), "foo-bar@baz", "[email protected]", 0)
>>> real_stdin = sys.stdin
>>> sys.stdin = FakeInput([
... 'step', # return event of g
... 'list', # list source from example 2
... 'next', # return from g()
... 'list', # list source from example 1
Expand All @@ -2124,6 +2123,9 @@ def test_pdb_set_trace():
>>> try: runner.run(test)
... finally: sys.stdin = real_stdin
... # doctest: +NORMALIZE_WHITESPACE
> <doctest foo-bar@baz[1]>(3)g()
-> import pdb; pdb.set_trace()
(Pdb) step
--Return--
> <doctest foo-bar@baz[1]>(3)g()->None
-> import pdb; pdb.set_trace()
Expand Down Expand Up @@ -2188,6 +2190,7 @@ def test_pdb_set_trace_nested():
>>> test = parser.get_doctest(doc, globals(), "foo-bar@baz", "[email protected]", 0)
>>> real_stdin = sys.stdin
>>> sys.stdin = FakeInput([
... 'step',
... 'print(y)', # print data defined in the function
... 'step', 'step', 'step', 'step', 'step', 'step', 'print(z)',
... 'up', 'print(x)',
Expand All @@ -2201,6 +2204,9 @@ def test_pdb_set_trace_nested():
... finally:
... sys.stdin = real_stdin
... # doctest: +REPORT_NDIFF
> <doctest test.test_doctest.test_doctest.test_pdb_set_trace_nested[0]>(4)calls_set_trace()
-> import pdb; pdb.set_trace()
(Pdb) step
> <doctest test.test_doctest.test_doctest.test_pdb_set_trace_nested[0]>(5)calls_set_trace()
-> self.f1()
(Pdb) print(y)
Expand Down
Loading

0 comments on commit 5a0022a

Please sign in to comment.