[3.13] gh-121804: Always show error location for SyntaxError's in new repl (GH-121886) (#123148)

(cherry picked from commit 354d55eb1f)
This commit is contained in:
Sergey B Kirpichev 2024-08-19 22:01:58 +03:00 committed by GitHub
parent 21399a0963
commit c8f4069ab1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 29 additions and 7 deletions

View file

@ -64,7 +64,7 @@ class InteractiveInterpreter:
code = self.compile(source, filename, symbol)
except (OverflowError, SyntaxError, ValueError):
# Case 1
self.showsyntaxerror(filename)
self.showsyntaxerror(filename, source=source)
return False
if code is None:
@ -123,6 +123,12 @@ class InteractiveInterpreter:
# Stuff in the right filename
value = SyntaxError(msg, (filename, lineno, offset, line))
sys.last_exc = sys.last_value = value
# Set the line of text that the exception refers to
source = kwargs.pop('source', '')
lines = source.splitlines()
if (source and type is SyntaxError
and not value.text and len(lines) >= value.lineno):
value.text = lines[value.lineno - 1]
if sys.excepthook is sys.__excepthook__:
lines = traceback.format_exception_only(type, value, colorize=colorize)
self.write(''.join(lines))