From dc8e2b30c58982be49c56802c7af57ee4df500c8 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Sat, 4 Nov 2023 14:07:39 -0700 Subject: [PATCH 1/3] Avoid executing code in except block in cmd --- Lib/cmd.py | 5 ++--- Lib/test/test_pdb.py | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/Lib/cmd.py b/Lib/cmd.py index 88ee7d3ddc46944..e933b8dbc1470a8 100644 --- a/Lib/cmd.py +++ b/Lib/cmd.py @@ -210,9 +210,8 @@ def onecmd(self, line): if cmd == '': return self.default(line) else: - try: - func = getattr(self, 'do_' + cmd) - except AttributeError: + func = getattr(self, 'do_' + cmd, None) + if func is None: return self.default(line) return func(arg) diff --git a/Lib/test/test_pdb.py b/Lib/test/test_pdb.py index 5fef8365f597101..1d4d864efc54b5a 100644 --- a/Lib/test/test_pdb.py +++ b/Lib/test/test_pdb.py @@ -2327,6 +2327,31 @@ def test_pdb_issue_gh_108976(): (Pdb) continue """ + +def test_pdb_issue_gh_80731(): + """See GH-80731 + + pdb should correctly print exception info if in an except block. + + >>> with PdbTestInput([ # doctest: +ELLIPSIS + ... 'import sys', + ... 'sys.exc_info()', + ... 'continue' + ... ]): + ... try: + ... raise ValueError('Correct') + ... except ValueError: + ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() + ... pass + > (10)() + -> pass + (Pdb) import sys + (Pdb) sys.exc_info() + (, ValueError('Correct'), ) + (Pdb) continue + """ + + def test_pdb_ambiguous_statements(): """See GH-104301 From ad01f11a178e37ccb7226ae230ce5b04af079cc0 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 4 Nov 2023 21:12:28 +0000 Subject: [PATCH 2/3] =?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/2023-11-04-21-12-27.gh-issue-80731.Wq51xg.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-11-04-21-12-27.gh-issue-80731.Wq51xg.rst diff --git a/Misc/NEWS.d/next/Library/2023-11-04-21-12-27.gh-issue-80731.Wq51xg.rst b/Misc/NEWS.d/next/Library/2023-11-04-21-12-27.gh-issue-80731.Wq51xg.rst new file mode 100644 index 000000000000000..5f957a39b2e34b4 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-11-04-21-12-27.gh-issue-80731.Wq51xg.rst @@ -0,0 +1 @@ +Avoid executing the default function in :class:`cmd.Cmd` in an except block From 23eb43b4129be858da667caad96eb1e16d4ff6d3 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Mon, 6 Nov 2023 13:18:52 -0800 Subject: [PATCH 3/3] Add test in test_cmd as well --- Lib/test/test_cmd.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Lib/test/test_cmd.py b/Lib/test/test_cmd.py index 28f80766677e592..951336fa08542d2 100644 --- a/Lib/test/test_cmd.py +++ b/Lib/test/test_cmd.py @@ -244,6 +244,21 @@ def test_input_reset_at_EOF(self): "(Cmd) *** Unknown syntax: EOF\n")) +class CmdPrintExceptionClass(cmd.Cmd): + """ + GH-80731 + cmd.Cmd should print the correct exception in default() + >>> mycmd = CmdPrintExceptionClass() + >>> try: + ... raise ValueError("test") + ... except ValueError: + ... mycmd.onecmd("not important") + (, ValueError('test')) + """ + + def default(self, line): + print(sys.exc_info()[:2]) + def load_tests(loader, tests, pattern): tests.addTest(doctest.DocTestSuite()) return tests