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(
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
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:
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:
except:
self.showtraceback()
- def showsyntaxerror(self, filename=None):
+ def showsyntaxerror(self, filename=None, **kwargs):
"""Display the syntax error that just occurred.
This doesn't display a stack trace because there isn't one.
else:
# Stuff in the right filename
value = SyntaxError(msg, (filename, lineno, offset, line))
- self._showtraceback(typ, value, None)
+ source = kwargs.pop('source', "")
+ self._showtraceback(typ, value, None, source)
finally:
typ = value = tb = None
"""
try:
typ, value, tb = sys.exc_info()
- self._showtraceback(typ, value, tb.tb_next)
+ self._showtraceback(typ, value, tb.tb_next, "")
finally:
typ = value = tb = None
- def _showtraceback(self, typ, value, tb):
+ def _showtraceback(self, typ, value, tb, source):
sys.last_type = typ
sys.last_traceback = tb
- sys.last_exc = sys.last_value = value = value.with_traceback(tb)
+ value = value.with_traceback(tb)
+ # Set the line of text that the exception refers to
+ lines = source.splitlines()
+ if (source and typ is SyntaxError
+ and not value.text and len(lines) >= value.lineno):
+ value.text = lines[value.lineno - 1]
+ sys.last_exc = sys.last_value = value
if sys.excepthook is sys.__excepthook__:
self._excepthook(typ, value, tb)
else:
self.assertFalse(result)
self.assertIn('SyntaxError', f.getvalue())
+ @force_not_colorized
+ def test_runsource_show_syntax_error_location(self):
+ console = InteractiveColoredConsole()
+ source = "def f(x, x): ..."
+ f = io.StringIO()
+ with contextlib.redirect_stderr(f):
+ result = console.runsource(source)
+ self.assertFalse(result)
+ r = """
+ def f(x, x): ...
+ ^
+SyntaxError: duplicate argument 'x' in function definition"""
+ self.assertIn(r, f.getvalue())
+
def test_runsource_shows_syntax_error_for_failed_compilation(self):
console = InteractiveColoredConsole()
source = "print('Hello, world!'"