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 14 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
13 changes: 10 additions & 3 deletions Lib/idlelib/idle_test/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,24 @@ def __eq__(self, other):
('int.reel', AttributeError,
"type object 'int' has no attribute 'reel'. "
"Did you mean: 'real'?\n"),
('raise NameError("123\\n456")', NameError, "123\n456"),
)

@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'))
except exc:
typ, val, tb = sys.exc_info()
actual = run.get_message_lines(typ, val, tb)[0]
if "raise " in code:
actual = run.print_exception(in_test=True)
else:
typ, val, tb = sys.exc_info()
actual = run.get_message_lines(typ, val, tb)[0]
expect = f'{exc.__name__}: {msg}'
self.assertEqual(actual, expect)

Expand Down
19 changes: 15 additions & 4 deletions Lib/idlelib/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,15 +240,19 @@ def get_message_lines(typ, exc, tb):
return traceback.format_exception_only(typ, exc)


def print_exception():
def print_exception(in_test=False): #Only when in test it is True, other it is False
import linecache
linecache.checkcache()
flush_stdout()
efile = sys.stderr
if not in_test:
efile = sys.stderr
else:
efile = io.StringIO() #If in test, anything mustn't be printed to sys.stderr
typ, val, tb = excinfo = sys.exc_info()
sys.last_type, sys.last_value, sys.last_traceback = excinfo
sys.last_exc = val
seen = set()
err = io.StringIO()

def print_exc(typ, exc, tb):
seen.add(id(exc))
Expand All @@ -269,13 +273,20 @@ def print_exc(typ, exc, tb):
print('Traceback (most recent call last):', file=efile)
exclude = ("run.py", "rpc.py", "threading.py", "queue.py",
"debugger_r.py", "bdb.py")
cleanup_traceback(tbe, exclude)
if not in_test: #When in test, the rpc.objecttable has no key 'exec'
cleanup_traceback(tbe, exclude)
traceback.print_list(tbe, file=efile)
lines = get_message_lines(typ, exc, tb)
if ((not isinstance(exc, NameError) and not isinstance(exc, AttributeError))
or "\n" not in str(exc)):
lines = get_message_lines(typ, exc, tb)
else:
lines = [f"{typ.__name__}: {str(exc)}"]
for line in lines:
print(line, end='', file=efile)
print(line, end='', file=err)

print_exc(typ, val, tb)
return err.getvalue()

def cleanup_traceback(tb, exclude):
"Remove excluded traces from beginning/end of tb; get cached lines"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix display of :exc:`NameError` and :exc:`AttributeError` with multi-line message.
Loading