Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 148af38

Browse files
gh-80731: Avoid executing code in except block in cmd (GH-111740)
1 parent afac3c9 commit 148af38

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

Lib/cmd.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,8 @@ def onecmd(self, line):
210210
if cmd == '':
211211
return self.default(line)
212212
else:
213-
try:
214-
func = getattr(self, 'do_' + cmd)
215-
except AttributeError:
213+
func = getattr(self, 'do_' + cmd, None)
214+
if func is None:
216215
return self.default(line)
217216
return func(arg)
218217

Lib/test/test_cmd.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,21 @@ def test_input_reset_at_EOF(self):
244244
"(Cmd) *** Unknown syntax: EOF\n"))
245245

246246

247+
class CmdPrintExceptionClass(cmd.Cmd):
248+
"""
249+
GH-80731
250+
cmd.Cmd should print the correct exception in default()
251+
>>> mycmd = CmdPrintExceptionClass()
252+
>>> try:
253+
... raise ValueError("test")
254+
... except ValueError:
255+
... mycmd.onecmd("not important")
256+
(<class 'ValueError'>, ValueError('test'))
257+
"""
258+
259+
def default(self, line):
260+
print(sys.exc_info()[:2])
261+
247262
def load_tests(loader, tests, pattern):
248263
tests.addTest(doctest.DocTestSuite())
249264
return tests

Lib/test/test_pdb.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2349,6 +2349,31 @@ def test_pdb_issue_gh_108976():
23492349
(Pdb) continue
23502350
"""
23512351

2352+
2353+
def test_pdb_issue_gh_80731():
2354+
"""See GH-80731
2355+
2356+
pdb should correctly print exception info if in an except block.
2357+
2358+
>>> with PdbTestInput([ # doctest: +ELLIPSIS
2359+
... 'import sys',
2360+
... 'sys.exc_info()',
2361+
... 'continue'
2362+
... ]):
2363+
... try:
2364+
... raise ValueError('Correct')
2365+
... except ValueError:
2366+
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace()
2367+
... pass
2368+
> <doctest test.test_pdb.test_pdb_issue_gh_80731[0]>(10)<module>()
2369+
-> pass
2370+
(Pdb) import sys
2371+
(Pdb) sys.exc_info()
2372+
(<class 'ValueError'>, ValueError('Correct'), <traceback object at ...>)
2373+
(Pdb) continue
2374+
"""
2375+
2376+
23522377
def test_pdb_ambiguous_statements():
23532378
"""See GH-104301
23542379
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Avoid executing the default function in :class:`cmd.Cmd` in an except block

0 commit comments

Comments
 (0)