]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-143874: Use self.message instead of raw print in `_exec_in_closure()` ...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 21 Jan 2026 00:35:30 +0000 (01:35 +0100)
committerGitHub <noreply@github.com>
Wed, 21 Jan 2026 00:35:30 +0000 (16:35 -0800)
gh-143874: Use self.message instead of raw print in `_exec_in_closure()`  (GH-143875)
(cherry picked from commit e66597d6c84a8615340822b8da564e5ca3fba5cd)

Co-authored-by: Yongtao Huang <yongtaoh2022@gmail.com>
Lib/pdb.py
Lib/test/test_remote_pdb.py
Misc/NEWS.d/next/Library/2026-01-15-16-04-39.gh-issue-143874.1qQgvo.rst [new file with mode: 0644]

index 04b7a85038a686a17676d5613737ddf509002894..903baeb85c87c02d56efae509dea7608e42dcb7a 100644 (file)
@@ -877,7 +877,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
 
index ec11e41678849bcfd44b25494d4dd272401c9a90..a6130e2ff387bdc337fec54018ae578c1ae3577a 100644 (file)
@@ -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 (file)
index 0000000..a11cf71
--- /dev/null
@@ -0,0 +1 @@
+Fixed a bug in :mod:`pdb` where expression results were not sent back to remote client.