Skip to content

Commit c23ef5c

Browse files
serhiy-storchakaLocked-chess-officialterryjreedy
authored
[3.13] gh-135511: Fixed truncated exception message for Name- and AttributeError (GH-135526) (#158276)
Only the last line of multiline error messages was displayed. --------- (cherry picked from commit 421c411) Co-authored-by: Locked-chess-official <13140752715@163.com> Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
1 parent ff03e45 commit c23ef5c

3 files changed

Lines changed: 13 additions & 14 deletions

File tree

‎Lib/idlelib/idle_test/test_run.py‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,15 @@ def __eq__(self, other):
4949
('int.reel', AttributeError,
5050
"type object 'int' has no attribute 'reel'. "
5151
"Did you mean: 'real'?\n"),
52+
(r'raise NameError("123\n456")', NameError, "123\n456\n"),
5253
)
5354

5455
@force_not_colorized
5556
def test_get_message(self):
5657
for code, exc, msg in self.data:
5758
with self.subTest(code=code):
5859
try:
59-
eval(compile(code, '', 'eval'))
60+
exec(compile(code, '', 'exec'))
6061
except exc:
6162
typ, val, tb = sys.exc_info()
6263
actual = run.get_message_lines(typ, val, tb)[0]
@@ -68,15 +69,20 @@ def test_get_message(self):
6869
new_callable=lambda: (lambda t, e: None))
6970
def test_get_multiple_message(self, mock):
7071
d = self.data
71-
data2 = ((d[0], d[1]), (d[1], d[2]), (d[2], d[0]))
72+
data2 = ((d[0], d[1]),
73+
(d[1], d[2]),
74+
(d[2], d[3]),
75+
(d[3], d[0]),
76+
(d[1], d[3]),
77+
(d[0], d[2]))
7278
subtests = 0
7379
for (code1, exc1, msg1), (code2, exc2, msg2) in data2:
7480
with self.subTest(codes=(code1,code2)):
7581
try:
76-
eval(compile(code1, '', 'eval'))
82+
exec(compile(code1, '', 'exec'))
7783
except exc1:
7884
try:
79-
eval(compile(code2, '', 'eval'))
85+
exec(compile(code2, '', 'exec'))
8086
except exc2:
8187
with captured_stderr() as output:
8288
run.print_exception()

‎Lib/idlelib/run.py‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
f'''{sys.executable} -c "__import__('idlelib.run').run.main()"'''
55
'.run' is needed because __import__ returns idlelib, not idlelib.run.
66
"""
7-
import contextlib
87
import functools
98
import io
109
import linecache
@@ -244,15 +243,8 @@ def show_socket_error(err, address):
244243

245244

246245
def get_message_lines(typ, exc, tb):
247-
"Return line composing the exception message."
248-
if typ in (AttributeError, NameError):
249-
# 3.10+ hints are not directly accessible from python (#44026).
250-
err = io.StringIO()
251-
with contextlib.redirect_stderr(err):
252-
sys.__excepthook__(typ, exc, tb)
253-
return [err.getvalue().split("\n")[-2] + "\n"]
254-
else:
255-
return traceback.format_exception_only(typ, exc)
246+
"Return lines of the exception message, with any suggestion."
247+
return list(traceback.TracebackException(typ, exc, tb).format_exception_only())
256248

257249

258250
def print_exception():
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix display of :exc:`NameError` and :exc:`AttributeError` with multi-line message.

0 commit comments

Comments
 (0)