mirror of
https://github.com/python/cpython.git
synced 2025-07-29 22:24:49 +00:00
[3.13] gh-118893: Evaluate all statements in the new REPL separately (GH-119318) (#119408)
(cherry picked from commit a3e4fec873
)
Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
This commit is contained in:
parent
eafd633fac
commit
a463cd8e45
5 changed files with 128 additions and 9 deletions
|
@ -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:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue