From 5264b687df7433b8cff244c0ddb20a2efa1013ad Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Fri, 30 Jul 2021 16:55:13 +0200 Subject: [PATCH] ENH Improve PyodideConsole error messages (#1759) when code causes a lexer error --- src/py/_pyodide/console.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/py/_pyodide/console.py b/src/py/_pyodide/console.py index 962aa1eb3..1ec34e970 100644 --- a/src/py/_pyodide/console.py +++ b/src/py/_pyodide/console.py @@ -12,6 +12,7 @@ from contextlib import _RedirectStream # type: ignore import rlcompleter import platform import sys +from tokenize import TokenError import traceback from typing import Literal from typing import ( @@ -97,8 +98,13 @@ class _Compile(Compile): def __call__(self, source, filename, symbol) -> CodeRunner: # type: ignore return_mode = self.return_mode - if self.quiet_trailing_semicolon and should_quiet(source): - return_mode = None + try: + if self.quiet_trailing_semicolon and should_quiet(source): + return_mode = None + except (TokenError, SyntaxError): + # Invalid code, let the Python parser throw the error later. + pass + code_runner = CodeRunner( source, mode=symbol,