From bbb539e0794fa66249f00cd8b7c1be2631092389 Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Sat, 11 Oct 2025 09:02:22 +0000 Subject: [PATCH 1/7] Handle RuntimeError when attaching to a process in pdb.py Signed-off-by: Frost Ming --- Lib/pdb.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Lib/pdb.py b/Lib/pdb.py index f695a39332e461..f5d01e1e362343 100644 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -3577,6 +3577,11 @@ def main(): parser.error("argument -m: not allowed with argument --pid") try: attach(opts.pid, opts.commands) + except RuntimeError as e: + while e.__context__ is not None: + e = e.__context__ + print(f"Error attaching to process: {e}") + sys.exit(1) except PermissionError as e: exit_with_permission_help_text() return From bd05b89c799bfb9672907b48a054f15f0078caa1 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 11 Oct 2025 09:07:13 +0000 Subject: [PATCH 2/7] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2025-10-11-09-07-06.gh-issue-139940.g54efZ.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2025-10-11-09-07-06.gh-issue-139940.g54efZ.rst 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 index 00000000000000..55b691a42693d9 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-10-11-09-07-06.gh-issue-139940.g54efZ.rst @@ -0,0 +1 @@ +Print clearer error message when using pdb to attaching to a non-existing process on Linux. From c50b200efb4bf3ca338673490e70c60c99967b4b Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Wed, 15 Oct 2025 08:34:34 +0800 Subject: [PATCH 3/7] Refactor exception variable naming in pdb.py for clarity Signed-off-by: Frost Ming --- Lib/pdb.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index f5d01e1e362343..d910268e654c07 100644 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -3577,12 +3577,12 @@ def main(): parser.error("argument -m: not allowed with argument --pid") try: attach(opts.pid, opts.commands) - except RuntimeError as e: - while e.__context__ is not None: - e = e.__context__ - print(f"Error attaching to process: {e}") + except RuntimeError as exc: + while exc.__context__ is not None: + exc = exc.__context__ + print(f"Error attaching to process: {exc}") sys.exit(1) - except PermissionError as e: + except PermissionError: exit_with_permission_help_text() return elif opts.module: From 3170ece9157eb670fd48bbc9826f6aeaa59de25e Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Fri, 24 Oct 2025 12:15:27 +0800 Subject: [PATCH 4/7] fix: print a user-friendly error message when unable to attach to process Signed-off-by: Frost Ming --- Lib/pdb.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index d910268e654c07..4ee12d17a611e6 100644 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -3577,10 +3577,11 @@ def main(): parser.error("argument -m: not allowed with argument --pid") try: attach(opts.pid, opts.commands) - except RuntimeError as exc: - while exc.__context__ is not None: - exc = exc.__context__ - print(f"Error attaching to process: {exc}") + 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() From 6ce5940615dd02d2c7331cb86db0a712767413f4 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Fri, 24 Oct 2025 00:03:29 -0700 Subject: [PATCH 5/7] Clarify error message for pdb process attachment --- .../next/Library/2025-10-11-09-07-06.gh-issue-139940.g54efZ.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 55b691a42693d9..2501135e657e7d 100644 --- 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 @@ -1 +1 @@ -Print clearer error message when using pdb to attaching to a non-existing process on Linux. +Print clearer error message when using ``pdb`` to attach to a non-existing process. From de4a9627a3e16accd4727e8f9d42992cbbd42bfc Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Fri, 24 Oct 2025 15:22:00 +0800 Subject: [PATCH 6/7] fix: add a test case Signed-off-by: Frost Ming --- Lib/test/test_remote_pdb.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Lib/test/test_remote_pdb.py b/Lib/test/test_remote_pdb.py index ec11e41678849b..fac49c816edfa7 100644 --- a/Lib/test/test_remote_pdb.py +++ b/Lib/test/test_remote_pdb.py @@ -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) + error = ( + "The specified process cannot be attached to due to insufficient permissions." + if sys.platform == "darwin" else + "Cannot attach to pid 999999, please make sure that the process exists" + ) + self.assertIn(error, result.stdout) + + if __name__ == "__main__": unittest.main() From 9f51d1c5cee76e15c2f862c92e1137a349cb27af Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Mon, 27 Oct 2025 08:16:28 +0800 Subject: [PATCH 7/7] refactor: reorganize imports and improve error message handling in remote pdb tests Signed-off-by: Frost Ming --- Lib/test/test_remote_pdb.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_remote_pdb.py b/Lib/test/test_remote_pdb.py index fac49c816edfa7..ede99de981971a 100644 --- a/Lib/test/test_remote_pdb.py +++ b/Lib/test/test_remote_pdb.py @@ -1594,11 +1594,11 @@ 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) - error = ( - "The specified process cannot be attached to due to insufficient permissions." - if sys.platform == "darwin" else - "Cannot attach to pid 999999, please make sure that the process exists" - ) + 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)