Skip to content
Merged
8 changes: 7 additions & 1 deletion Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_remote_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1590,5 +1590,17 @@ def test_attach_to_process_with_colors(self):
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()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Print clearer error message when using ``pdb`` to attach to a non-existing process.
Loading