Skip to content

Commit 6cdb03d

Browse files
[3.14] gh-138641: Ignore set_trace() when we are already in pdb (GH-159918) (#159971)
gh-138641: Ignore set_trace() when we are already in pdb (GH-159918) (cherry picked from commit 514cfe7) Co-authored-by: Tian Gao <gaogaotiantian@hotmail.com>
1 parent 86795c8 commit 6cdb03d

3 files changed

Lines changed: 20 additions & 0 deletions

File tree

‎Lib/bdb.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,13 @@ def set_trace(self, frame=None):
610610
611611
If frame is not specified, debugging starts from caller's frame.
612612
"""
613+
614+
if self.enterframe is not None:
615+
# If self.enterframe is not None, it means we are already in
616+
# dispatched functions. Tracing is disabled when calling a
617+
# trace function, so we just skip this set_trace() call.
618+
return
619+
613620
self.stop_trace()
614621
if frame is None:
615622
frame = sys._getframe().f_back

‎Lib/test/test_pdb.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4875,6 +4875,18 @@ def foo():
48754875
stdout, _ = self._run_script(script, commands)
48764876
self.assertIn("42", stdout)
48774877

4878+
def test_nested_set_trace(self):
4879+
# Calling breakpoint() in pdb prompt should just be ignored.
4880+
script = "breakpoint()"
4881+
commands = """
4882+
breakpoint()
4883+
p 40 + 2
4884+
c
4885+
"""
4886+
stdout, stderr = self._run_script(script, commands)
4887+
self.assertIn('42', stdout)
4888+
self.assertEqual(stderr, '')
4889+
48784890
def test_readline_not_imported(self):
48794891
"""GH-138860
48804892
Directly or indirectly importing readline might deadlock a subprocess
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Ignore :func:`pdb.set_trace` when called directly at the :mod:`pdb` prompt.

0 commit comments

Comments
 (0)