From 8ad5baa584b8722e44794c57ce112eb208080710 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Fri, 27 Sep 2024 11:30:18 -0700 Subject: [PATCH 1/4] Do not raise an Exception for exit in inline mode anymore --- Lib/pdb.py | 16 ++++++++++--- Lib/test/test_pdb.py | 56 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 3 deletions(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index 443160eaaae887..b060cf2677b1bb 100644 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -1723,6 +1723,18 @@ def do_quit(self, arg): Quit from the debugger. The program being executed is aborted. """ + if self.mode == 'inline': + while True: + try: + reply = input('Quitting pdb will kill the process. Quit anyway? [y/n] ') + except EOFError: + reply = 'y' + self.message('') + if reply.lower() == 'y' or reply == '': + os._exit(0) + elif reply.lower() == 'n': + return + self._user_requested_quit = True self.set_quit() return 1 @@ -1736,9 +1748,7 @@ def do_EOF(self, arg): Handles the receipt of EOF as a command. """ self.message('') - self._user_requested_quit = True - self.set_quit() - return 1 + return self.do_quit(arg) def do_args(self, arg): """a(rgs) diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index 84c0e1073a1054..276661a66fb940 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -3967,6 +3967,62 @@ def test_checkline_is_not_executable(self): self.assertFalse(db.checkline(os_helper.TESTFN, lineno)) +@support.requires_subprocess() +class PdbTestInline(unittest.TestCase): + @unittest.skipIf(sys.flags.safe_path, + 'PYTHONSAFEPATH changes default sys.path') + def _run_script(self, script, commands, + expected_returncode=0, + extra_env=None): + self.addCleanup(os_helper.rmtree, '__pycache__') + filename = 'main.py' + with open(filename, 'w') as f: + f.write(textwrap.dedent(script)) + self.addCleanup(os_helper.unlink, filename) + + commands = textwrap.dedent(commands) + + cmd = [sys.executable, 'main.py'] + if extra_env is not None: + env = os.environ | extra_env + else: + env = os.environ + with subprocess.Popen( + cmd, + stdout=subprocess.PIPE, + stdin=subprocess.PIPE, + stderr=subprocess.PIPE, + env = {**env, 'PYTHONIOENCODING': 'utf-8'} + ) as proc: + stdout, stderr = proc.communicate(str.encode(commands)) + stdout = bytes.decode(stdout) if isinstance(stdout, bytes) else stdout + stderr = bytes.decode(stderr) if isinstance(stderr, bytes) else stderr + self.assertEqual( + proc.returncode, + expected_returncode, + f"Unexpected return code\nstdout: {stdout}\nstderr: {stderr}" + ) + return stdout, stderr + + def test_quit(self): + script = """ + x = 1 + breakpoint() + """ + + commands = """ + quit + n + p x + 1 + quit + y + """ + + stdout, stderr = self._run_script(script, commands) + self.assertIn("2", stdout) + self.assertIn("Quit anyway", stdout) + + @support.requires_subprocess() class PdbTestReadline(unittest.TestCase): def setUpClass(): From 63a6bf6205d30878133b7897a3a9bec017e8efb9 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Fri, 27 Sep 2024 12:08:06 -0700 Subject: [PATCH 2/4] Use lower + strip --- Lib/pdb.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index b060cf2677b1bb..cf753882cb13ee 100644 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -1727,10 +1727,11 @@ def do_quit(self, arg): while True: try: reply = input('Quitting pdb will kill the process. Quit anyway? [y/n] ') + reply = reply.lower().strip() except EOFError: reply = 'y' self.message('') - if reply.lower() == 'y' or reply == '': + if reply == 'y' or reply == '': os._exit(0) elif reply.lower() == 'n': return From 5747b27504ac7e8b20cd6940d9335609a60e1092 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 27 Sep 2024 19:21:55 +0000 Subject: [PATCH 3/4] =?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/2024-09-27-19-21-53.gh-issue-124703.lYTLEv.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2024-09-27-19-21-53.gh-issue-124703.lYTLEv.rst diff --git a/Misc/NEWS.d/next/Library/2024-09-27-19-21-53.gh-issue-124703.lYTLEv.rst b/Misc/NEWS.d/next/Library/2024-09-27-19-21-53.gh-issue-124703.lYTLEv.rst new file mode 100644 index 00000000000000..e55d3539355d73 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-09-27-19-21-53.gh-issue-124703.lYTLEv.rst @@ -0,0 +1 @@ +Quitting :mod:`pdb` in ``inline`` mode will emit a confirmation prompt and exit gracefully now, instead of printing an exception traceback. From b0968bd75964a311085ea95ae8e4f5637d603d33 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Mon, 14 Oct 2024 23:19:22 -0400 Subject: [PATCH 4/4] Use sys.exit() instead of os._exit() --- Lib/pdb.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/pdb.py b/Lib/pdb.py index cf753882cb13ee..0bdaab75c3456c 100644 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -1732,7 +1732,7 @@ def do_quit(self, arg): reply = 'y' self.message('') if reply == 'y' or reply == '': - os._exit(0) + sys.exit(0) elif reply.lower() == 'n': return