Skip to content

Commit 1fbb212

Browse files
committed
pdb: fix expression result output stream in _exec_in_closure()
Fixes an issue where results of expressions executed via `_exec_in_closure()` were written to `sys.stdout` instead of the debugger output stream. Adds a regression test to ensure expression results are consistently emitted via pdb.stdout. Signed-off-by: Yongtao Huang <yongtaoh2022@gmail.com>
1 parent bdba5f0 commit 1fbb212

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

Lib/pdb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ def _exec_in_closure(self, source, globals, locals):
888888
locals.update(pdb_eval["write_back"])
889889
eval_result = pdb_eval["result"]
890890
if eval_result is not None:
891-
print(repr(eval_result))
891+
self.message(repr(eval_result))
892892

893893
return True
894894

Lib/test/test_pdb.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3666,6 +3666,28 @@ def test_print_stack_entry_uses_dynamic_line_prefix(self):
36663666
# Check if the custom prefix appeared in the output
36673667
self.assertIn('CUSTOM_PREFIX> ', stdout.getvalue())
36683668

3669+
def test_exec_in_closure_result_uses_pdb_stdout(self):
3670+
"""
3671+
Expression results executed via _exec_in_closure() should be written
3672+
to the debugger output stream (pdb stdout), not to sys.stdout.
3673+
"""
3674+
pdb_out = io.StringIO()
3675+
sys_out = io.StringIO()
3676+
3677+
p = pdb.Pdb(nosigint=True, readrc=False, stdout=pdb_out)
3678+
3679+
with redirect_stdout(sys_out):
3680+
self.assertTrue(p._exec_in_closure('(lambda: 123)()', {}, {}))
3681+
self.assertTrue(p._exec_in_closure('sum(i for i in (1, 2, 3))', {}, {}))
3682+
3683+
pdb_lines = [line.strip() for line in pdb_out.getvalue().splitlines()]
3684+
sys_lines = [line.strip() for line in sys_out.getvalue().splitlines()]
3685+
3686+
self.assertIn('123', pdb_lines)
3687+
self.assertIn('6', pdb_lines)
3688+
self.assertNotIn('123', sys_lines)
3689+
self.assertNotIn('6', sys_lines)
3690+
36693691
def test_find_function_found_with_encoding_cookie(self):
36703692
self._assert_find_function(
36713693
"""\

0 commit comments

Comments
 (0)