]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-139940: Handle RuntimeError when attaching to a non-existing process in pdb. ...
authorFrost Ming <me@frostming.com>
Wed, 29 Oct 2025 06:12:12 +0000 (14:12 +0800)
committerGitHub <noreply@github.com>
Wed, 29 Oct 2025 06:12:12 +0000 (23:12 -0700)
Lib/pdb.py
Lib/test/test_remote_pdb.py
Misc/NEWS.d/next/Library/2025-10-11-09-07-06.gh-issue-139940.g54efZ.rst [new file with mode: 0644]

index f695a39332e461772f55cea21866e69d66b77864..4ee12d17a611e6f37d97f3042b931a0eef7fdd04 100644 (file)
@@ -3577,7 +3577,13 @@ def main():
             parser.error("argument -m: not allowed with argument --pid")
         try:
             attach(opts.pid, opts.commands)
-        except PermissionError as e:
+        except RuntimeError:
+            print(
+                f"Cannot attach to pid {opts.pid}, please make sure that the process exists "
+                "and is using the same Python version."
+            )
+            sys.exit(1)
+        except PermissionError:
             exit_with_permission_help_text()
         return
     elif opts.module:
index ec11e41678849bcfd44b25494d4dd272401c9a90..ede99de981971a63fc127193529a3b5a055fa533 100644 (file)
@@ -1590,5 +1590,17 @@ class PdbAttachTestCase(unittest.TestCase):
         self.assertNotIn("while x == 1", output["client"]["stdout"])
         self.assertIn("while x == 1", re.sub("\x1b[^m]*m", "", output["client"]["stdout"]))
 
+    def test_attach_to_non_existent_process(self):
+        with force_color(False):
+            result = subprocess.run([sys.executable, "-m", "pdb", "-p", "999999"], text=True, capture_output=True)
+        self.assertNotEqual(result.returncode, 0)
+        if sys.platform == "darwin":
+            # On MacOS, attaching to a non-existent process gives PermissionError
+            error = "The specified process cannot be attached to due to insufficient permissions"
+        else:
+            error = "Cannot attach to pid 999999, please make sure that the process exists"
+        self.assertIn(error, result.stdout)
+
+
 if __name__ == "__main__":
     unittest.main()
diff --git a/Misc/NEWS.d/next/Library/2025-10-11-09-07-06.gh-issue-139940.g54efZ.rst b/Misc/NEWS.d/next/Library/2025-10-11-09-07-06.gh-issue-139940.g54efZ.rst
new file mode 100644 (file)
index 0000000..2501135
--- /dev/null
@@ -0,0 +1 @@
+Print clearer error message when using ``pdb`` to attach to a non-existing process.