]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-133490: Add color support to remote PDB (#133491)
authorMatt Wozniski <mwozniski@bloomberg.net>
Tue, 6 May 2025 05:28:16 +0000 (01:28 -0400)
committerGitHub <noreply@github.com>
Tue, 6 May 2025 05:28:16 +0000 (01:28 -0400)
Lib/pdb.py
Lib/test/test_remote_pdb.py
Misc/NEWS.d/next/Library/2025-05-06-00-10-10.gh-issue-133490.Ubrppz.rst [new file with mode: 0644]

index 225bbb9c5e592bdedd600c19efeab89241ae896b..4efda171b7a8136f70b275b704b1420439057052 100644 (file)
@@ -2677,6 +2677,7 @@ class _PdbServer(Pdb):
         sockfile,
         signal_server=None,
         owns_sockfile=True,
+        colorize=False,
         **kwargs,
     ):
         self._owns_sockfile = owns_sockfile
@@ -2687,7 +2688,10 @@ class _PdbServer(Pdb):
         if signal_server:
             # Only started by the top level _PdbServer, not recursive ones.
             self._start_signal_listener(signal_server)
+        # Override the `colorize` attribute set by the parent constructor,
+        # because it checks the server's stdout, rather than the client's.
         super().__init__(colorize=False, **kwargs)
+        self.colorize = colorize
 
     @staticmethod
     def protocol_version():
@@ -2975,7 +2979,11 @@ class _PdbServer(Pdb):
 
     @typing.override
     def _create_recursive_debugger(self):
-        return _PdbServer(self._sockfile, owns_sockfile=False)
+        return _PdbServer(
+            self._sockfile,
+            owns_sockfile=False,
+            colorize=self.colorize,
+        )
 
     @typing.override
     def _prompt_for_confirmation(self, prompt, default):
@@ -3336,7 +3344,16 @@ class _PdbClient:
             return None
 
 
-def _connect(*, host, port, frame, commands, version, signal_raising_thread):
+def _connect(
+    *,
+    host,
+    port,
+    frame,
+    commands,
+    version,
+    signal_raising_thread,
+    colorize,
+):
     with closing(socket.create_connection((host, port))) as conn:
         sockfile = conn.makefile("rwb")
 
@@ -3347,7 +3364,11 @@ def _connect(*, host, port, frame, commands, version, signal_raising_thread):
     else:
         signal_server = None
 
-    remote_pdb = _PdbServer(sockfile, signal_server=signal_server)
+    remote_pdb = _PdbServer(
+        sockfile,
+        signal_server=signal_server,
+        colorize=colorize,
+    )
     weakref.finalize(remote_pdb, sockfile.close)
 
     if Pdb._last_pdb_instance is not None:
@@ -3379,6 +3400,7 @@ def attach(pid, commands=()):
         )
 
         use_signal_thread = sys.platform == "win32"
+        colorize = _colorize.can_colorize()
 
         connect_script.write(
             textwrap.dedent(
@@ -3391,6 +3413,7 @@ def attach(pid, commands=()):
                     commands={json.dumps("\n".join(commands))},
                     version={_PdbServer.protocol_version()},
                     signal_raising_thread={use_signal_thread!r},
+                    colorize={colorize!r},
                 )
                 """
             )
index 9c794991dd5ed9b7e8722ad1848c6348d17ef170..7c77bedb766c53e457cc94be4fe13faee4ca7e9a 100644 (file)
@@ -1040,6 +1040,7 @@ class PdbConnectTestCase(unittest.TestCase):
                             commands="",
                             version=pdb._PdbServer.protocol_version(),
                             signal_raising_thread=False,
+                            colorize=False,
                         )
                         return x  # This line won't be reached in debugging
 
@@ -1210,6 +1211,7 @@ class PdbConnectTestCase(unittest.TestCase):
                     commands="",
                     version=pdb._PdbServer.protocol_version(),
                     signal_raising_thread=True,
+                    colorize=False,
                 )
                 print("Connected to debugger")
                 iterations = 50
@@ -1301,6 +1303,7 @@ class PdbConnectTestCase(unittest.TestCase):
                     commands="",
                     version=fake_version,
                     signal_raising_thread=False,
+                    colorize=False,
                 )
 
                 # This should print if the debugger detaches correctly
diff --git a/Misc/NEWS.d/next/Library/2025-05-06-00-10-10.gh-issue-133490.Ubrppz.rst b/Misc/NEWS.d/next/Library/2025-05-06-00-10-10.gh-issue-133490.Ubrppz.rst
new file mode 100644 (file)
index 0000000..beaa8db
--- /dev/null
@@ -0,0 +1 @@
+Add color support to PDB in remote mode.