gh-121804: Always show error location for SyntaxError's in new repl (#121886)

This commit is contained in:
Sergey B Kirpichev 2024-08-19 17:19:23 +03:00 committed by GitHub
parent e077b201f4
commit 354d55eb1f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 36 additions and 10 deletions

View file

@ -161,6 +161,9 @@ class InteractiveColoredConsole(code.InteractiveConsole):
super().__init__(locals=locals, filename=filename, local_exit=local_exit) # type: ignore[call-arg]
self.can_colorize = _colorize.can_colorize()
def showsyntaxerror(self, filename=None, **kwargs):
super().showsyntaxerror(**kwargs)
def _excepthook(self, typ, value, tb):
import traceback
lines = traceback.format_exception(
@ -173,7 +176,7 @@ class InteractiveColoredConsole(code.InteractiveConsole):
try:
tree = ast.parse(source)
except (SyntaxError, OverflowError, ValueError):
self.showsyntaxerror(filename)
self.showsyntaxerror(filename, source=source)
return False
if tree.body:
*_, last_stmt = tree.body
@ -190,10 +193,10 @@ class InteractiveColoredConsole(code.InteractiveConsole):
f"Try the asyncio REPL ({python} -m asyncio) to use"
f" top-level 'await' and run background asyncio tasks."
)
self.showsyntaxerror(filename)
self.showsyntaxerror(filename, source=source)
return False
except (OverflowError, ValueError):
self.showsyntaxerror(filename)
self.showsyntaxerror(filename, source=source)
return False
if code is None: