diff options
Diffstat (limited to 'Lib/_pyrepl/simple_interact.py')
-rw-r--r-- | Lib/_pyrepl/simple_interact.py | 33 |
1 files changed, 29 insertions, 4 deletions
diff --git a/Lib/_pyrepl/simple_interact.py b/Lib/_pyrepl/simple_interact.py index 31b2097..d65b6d0 100644 --- a/Lib/_pyrepl/simple_interact.py +++ b/Lib/_pyrepl/simple_interact.py @@ -30,6 +30,7 @@ import _sitebuiltins import linecache import sys import code +import ast from types import ModuleType from .readline import _get_reader, multiline_input @@ -74,9 +75,36 @@ 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): + super().showsyntaxerror(colorize=self.can_colorize) + def showtraceback(self): super().showtraceback(colorize=self.can_colorize) + def runsource(self, source, filename="<input>", symbol="single"): + try: + tree = ast.parse(source) + except (OverflowError, SyntaxError, ValueError): + self.showsyntaxerror(filename) + return False + if tree.body: + *_, last_stmt = tree.body + for stmt in tree.body: + wrapper = ast.Interactive if stmt is last_stmt else ast.Module + the_symbol = symbol if stmt is last_stmt else "exec" + item = wrapper([stmt]) + try: + code = compile(item, filename, the_symbol) + except (OverflowError, ValueError): + self.showsyntaxerror(filename) + return False + + if code is None: + return True + + self.runcode(code) + return False + def run_multiline_interactive_console( mainmodule: ModuleType | None= None, future_flags: int = 0 @@ -144,10 +172,7 @@ def run_multiline_interactive_console( input_name = f"<python-input-{input_n}>" linecache._register_code(input_name, statement, "<stdin>") # type: ignore[attr-defined] - symbol = "single" if not contains_pasted_code else "exec" - more = console.push(_strip_final_indent(statement), filename=input_name, _symbol=symbol) # type: ignore[call-arg] - if contains_pasted_code and more: - more = console.push(_strip_final_indent(statement), filename=input_name, _symbol="single") # type: ignore[call-arg] + more = console.push(_strip_final_indent(statement), filename=input_name, _symbol="single") # type: ignore[call-arg] assert not more input_n += 1 except KeyboardInterrupt: |