Skip to content

gh-135511 Fixed NameError and AttributeError lack of the message in IDLE #135526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions Lib/idlelib/idle_test/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,18 @@ def __eq__(self, other):
('int.reel', AttributeError,
"type object 'int' has no attribute 'reel'. "
"Did you mean: 'real'?\n"),
(r'raise NameError("123\n456")', NameError, "123\n456\n"),
)

@force_not_colorized
def test_get_message(self):
for code, exc, msg in self.data:
with self.subTest(code=code):
try:
eval(compile(code, '', 'eval'))
if "raise" not in code:
eval(compile(code, '', 'eval'))
else:
exec(compile(code, '', 'exec')) # code r"raise NameError("123\n456")" cannot run in "eval" mode: SyntaxError
except exc:
typ, val, tb = sys.exc_info()
actual = run.get_message_lines(typ, val, tb)[0]
Expand All @@ -64,15 +68,26 @@ def test_get_message(self):
new_callable=lambda: (lambda t, e: None))
def test_get_multiple_message(self, mock):
d = self.data
data2 = ((d[0], d[1]), (d[1], d[2]), (d[2], d[0]))
data2 = ((d[0], d[1]),
(d[1], d[2]),
(d[2], d[3]),
(d[3], d[0]),
(d[1], d[3]),
(d[0], d[2]))
subtests = 0
for (code1, exc1, msg1), (code2, exc2, msg2) in data2:
with self.subTest(codes=(code1,code2)):
try:
eval(compile(code1, '', 'eval'))
if "raise" not in code1:
eval(compile(code1, '', 'eval'))
else:
exec(compile(code1, '', 'exec'))
except exc1:
try:
eval(compile(code2, '', 'eval'))
if "raise" not in code2:
eval(compile(code2, '', 'eval'))
else:
exec(compile(code2, '', 'exec'))
except exc2:
with captured_stderr() as output:
run.print_exception()
Expand Down
10 changes: 9 additions & 1 deletion Lib/idlelib/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,15 @@ def get_message_lines(typ, exc, tb):
err = io.StringIO()
with contextlib.redirect_stderr(err):
sys.__excepthook__(typ, exc, tb)
return [err.getvalue().split("\n")[-2] + "\n"]
err_list = err.getvalue().split("\n")[1:]

for i in range(len(err_list)):
if err_list[i].startswith(" "):
continue
else:
err_list = err_list[i:-1]
break
return ["\n".join(err_list) + "\n"]
else:
return traceback.format_exception_only(typ, exc)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix the missing error message in "NameError" and "AttributeError" in IDLE
when "\n" in message
Loading