From: Yongtao Huang Date: Tue, 20 Jan 2026 00:11:07 +0000 (+0800) Subject: gh-143874: Use self.message instead of raw print in `_exec_in_closure()` (#143875) X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e66597d6c84a8615340822b8da564e5ca3fba5cd;p=thirdparty%2FPython%2Fcpython.git gh-143874: Use self.message instead of raw print in `_exec_in_closure()` (#143875) --- diff --git a/Lib/pdb.py b/Lib/pdb.py index 0464b288ef82..b5d8f8278274 100644 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -888,7 +888,7 @@ class Pdb(bdb.Bdb, cmd.Cmd): locals.update(pdb_eval["write_back"]) eval_result = pdb_eval["result"] if eval_result is not None: - print(repr(eval_result)) + self.message(repr(eval_result)) return True diff --git a/Lib/test/test_remote_pdb.py b/Lib/test/test_remote_pdb.py index ede99de98197..d26d63faa61d 100644 --- a/Lib/test/test_remote_pdb.py +++ b/Lib/test/test_remote_pdb.py @@ -1441,6 +1441,34 @@ class PdbConnectTestCase(unittest.TestCase): self.assertIn("Function returned: 42", stdout) self.assertEqual(process.returncode, 0) + def test_exec_in_closure_result_uses_pdb_stdout(self): + """ + Expression results executed via _exec_in_closure() should be written + to the debugger output stream (pdb stdout), not to sys.stdout. + """ + self._create_script() + process, client_file = self._connect_and_get_client_file() + + with kill_on_error(process): + self._read_until_prompt(client_file) + + self._send_command(client_file, "(lambda: 123)()") + messages = self._read_until_prompt(client_file) + result_msg = "".join(msg.get("message", "") for msg in messages) + self.assertIn("123", result_msg) + + self._send_command(client_file, "sum(i for i in (1, 2, 3))") + messages = self._read_until_prompt(client_file) + result_msg = "".join(msg.get("message", "") for msg in messages) + self.assertIn("6", result_msg) + + self._send_command(client_file, "c") + stdout, _ = process.communicate(timeout=SHORT_TIMEOUT) + + self.assertNotIn("\n123\n", stdout) + self.assertNotIn("\n6\n", stdout) + self.assertEqual(process.returncode, 0) + def _supports_remote_attaching(): PROCESS_VM_READV_SUPPORTED = False diff --git a/Misc/NEWS.d/next/Library/2026-01-15-16-04-39.gh-issue-143874.1qQgvo.rst b/Misc/NEWS.d/next/Library/2026-01-15-16-04-39.gh-issue-143874.1qQgvo.rst new file mode 100644 index 000000000000..a11cf715b04a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-01-15-16-04-39.gh-issue-143874.1qQgvo.rst @@ -0,0 +1 @@ +Fixed a bug in :mod:`pdb` where expression results were not sent back to remote client.