Skip to content

Commit 058bc18

Browse files
authored
gh-139940: Handle RuntimeError when attaching to a non-existing process in pdb. (#139941)
1 parent c41f84f commit 058bc18

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

Lib/pdb.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3577,7 +3577,13 @@ def main():
35773577
parser.error("argument -m: not allowed with argument --pid")
35783578
try:
35793579
attach(opts.pid, opts.commands)
3580-
except PermissionError as e:
3580+
except RuntimeError:
3581+
print(
3582+
f"Cannot attach to pid {opts.pid}, please make sure that the process exists "
3583+
"and is using the same Python version."
3584+
)
3585+
sys.exit(1)
3586+
except PermissionError:
35813587
exit_with_permission_help_text()
35823588
return
35833589
elif opts.module:

Lib/test/test_remote_pdb.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,5 +1590,17 @@ def test_attach_to_process_with_colors(self):
15901590
self.assertNotIn("while x == 1", output["client"]["stdout"])
15911591
self.assertIn("while x == 1", re.sub("\x1b[^m]*m", "", output["client"]["stdout"]))
15921592

1593+
def test_attach_to_non_existent_process(self):
1594+
with force_color(False):
1595+
result = subprocess.run([sys.executable, "-m", "pdb", "-p", "999999"], text=True, capture_output=True)
1596+
self.assertNotEqual(result.returncode, 0)
1597+
if sys.platform == "darwin":
1598+
# On MacOS, attaching to a non-existent process gives PermissionError
1599+
error = "The specified process cannot be attached to due to insufficient permissions"
1600+
else:
1601+
error = "Cannot attach to pid 999999, please make sure that the process exists"
1602+
self.assertIn(error, result.stdout)
1603+
1604+
15931605
if __name__ == "__main__":
15941606
unittest.main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Print clearer error message when using ``pdb`` to attach to a non-existing process.

0 commit comments

Comments
 (0)