mirror of
https://github.com/python/cpython.git
synced 2025-10-10 00:43:41 +00:00
gh-80731: Avoid executing code in except block in cmd (GH-111740)
This commit is contained in:
parent
afac3c9b7e
commit
148af38cd0
4 changed files with 43 additions and 3 deletions
|
@ -210,9 +210,8 @@ class Cmd:
|
||||||
if cmd == '':
|
if cmd == '':
|
||||||
return self.default(line)
|
return self.default(line)
|
||||||
else:
|
else:
|
||||||
try:
|
func = getattr(self, 'do_' + cmd, None)
|
||||||
func = getattr(self, 'do_' + cmd)
|
if func is None:
|
||||||
except AttributeError:
|
|
||||||
return self.default(line)
|
return self.default(line)
|
||||||
return func(arg)
|
return func(arg)
|
||||||
|
|
||||||
|
|
|
@ -244,6 +244,21 @@ class TestAlternateInput(unittest.TestCase):
|
||||||
"(Cmd) *** Unknown syntax: EOF\n"))
|
"(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")
|
||||||
|
(<class 'ValueError'>, ValueError('test'))
|
||||||
|
"""
|
||||||
|
|
||||||
|
def default(self, line):
|
||||||
|
print(sys.exc_info()[:2])
|
||||||
|
|
||||||
def load_tests(loader, tests, pattern):
|
def load_tests(loader, tests, pattern):
|
||||||
tests.addTest(doctest.DocTestSuite())
|
tests.addTest(doctest.DocTestSuite())
|
||||||
return tests
|
return tests
|
||||||
|
|
|
@ -2349,6 +2349,31 @@ def test_pdb_issue_gh_108976():
|
||||||
(Pdb) continue
|
(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
|
||||||
|
> <doctest test.test_pdb.test_pdb_issue_gh_80731[0]>(10)<module>()
|
||||||
|
-> pass
|
||||||
|
(Pdb) import sys
|
||||||
|
(Pdb) sys.exc_info()
|
||||||
|
(<class 'ValueError'>, ValueError('Correct'), <traceback object at ...>)
|
||||||
|
(Pdb) continue
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
def test_pdb_ambiguous_statements():
|
def test_pdb_ambiguous_statements():
|
||||||
"""See GH-104301
|
"""See GH-104301
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Avoid executing the default function in :class:`cmd.Cmd` in an except block
|
Loading…
Add table
Add a link
Reference in a new issue