Skip to content

Commit 7905b0d

Browse files
committed
fix: add unittest for it
Signed-off-by: yihong0618 <zouzou0208@gmail.com>
1 parent 506bcf6 commit 7905b0d

2 files changed

Lines changed: 22 additions & 9 deletions

File tree

Lib/pdb.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2647,12 +2647,12 @@ def set_trace(*, header=None, commands=None):
26472647
just before debugging begins. *commands* is an optional list of
26482648
pdb commands to run when the debugger starts.
26492649
"""
2650-
# Check if we're already in a pdb session by examining the call stack
2650+
# gh-138641: Check if we're already in a pdb session.
26512651
frame = sys._getframe()
26522652
while frame:
2653-
if frame.f_code.co_name == 'interaction' and frame.f_code.co_filename.endswith('pdb.py'):
2654-
# We're already in a pdb session, just print a message and return
2655-
print("*** Nested breakpoint calls are not supported. "
2653+
if (frame.f_code.co_name == 'interaction'
2654+
and frame.f_code.co_filename.endswith('pdb.py')):
2655+
print("Nested breakpoint calls are not supported. "
26562656
"Already running in the debugger.", file=sys.stderr)
26572657
return
26582658
frame = frame.f_back
@@ -2672,12 +2672,13 @@ async def set_trace_async(*, header=None, commands=None):
26722672
if they enter the debugger with this function. Otherwise it's the same
26732673
as set_trace().
26742674
"""
2675-
# Check if we're already in a pdb session by examining the call stack
2675+
# gh-138641: Check if we're already in a pdb session.
26762676
frame = sys._getframe()
26772677
while frame:
2678-
if frame.f_code.co_name == 'interaction' and frame.f_code.co_filename.endswith('pdb.py'):
2678+
if (frame.f_code.co_name == 'interaction' and
2679+
frame.f_code.co_filename.endswith('pdb.py')):
26792680
# We're already in a pdb session, just print a message and return
2680-
print("*** Nested breakpoint calls are not supported. "
2681+
print("Nested breakpoint calls are not supported. "
26812682
"Already running in the debugger.", file=sys.stderr)
26822683
return
26832684
frame = frame.f_back

Lib/test/test_pdb.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
import zipfile
1818

1919
from asyncio.events import _set_event_loop_policy
20-
from contextlib import ExitStack, redirect_stdout
20+
from contextlib import ExitStack, redirect_stdout, redirect_stderr
2121
from io import StringIO
2222
from test import support
2323
from test.support import has_socket_support, os_helper
2424
from test.support.import_helper import import_module
2525
from test.support.pty_helper import run_pty, FakeInput
2626
from test.support.script_helper import kill_python
27-
from unittest.mock import patch
27+
from unittest.mock import Mock, patch
2828

2929
SKIP_CORO_TESTS = False
3030

@@ -4539,6 +4539,18 @@ def bar():
45394539
]))
45404540
self.assertIn('break in bar', stdout)
45414541

4542+
def test_nested_breakpoint_calls(self):
4543+
# gh-138641 pdb.set_trace() called when already in the debugger
4544+
mock_frame = Mock()
4545+
mock_frame.f_code.co_name = 'interaction'
4546+
mock_frame.f_code.co_filename = '/path/to/pdb.py'
4547+
mock_frame.f_back = None
4548+
captured_stderr = io.StringIO()
4549+
with redirect_stderr(captured_stderr):
4550+
with patch('sys._getframe', return_value=mock_frame):
4551+
pdb.set_trace()
4552+
stderr_content = captured_stderr.getvalue()
4553+
self.assertIn("Nested breakpoint calls are not supported", stderr_content)
45424554

45434555
class ChecklineTests(unittest.TestCase):
45444556
def setUp(self):

0 commit comments

Comments
 (0)