[3.13] gh-118894: Make asyncio REPL use pyrepl (GH-119433) (#119884)

(cherry picked from commit 2237946af0)

Co-authored-by: Łukasz Langa <lukasz@langa.pl>
This commit is contained in:
Miss Islington (bot) 2024-05-31 23:15:44 +02:00 committed by GitHub
parent 67ac19111f
commit a5272e63ef
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 143 additions and 65 deletions

View file

@ -25,14 +25,13 @@ allowing multiline input and multiline history entries.
from __future__ import annotations
import _colorize # type: ignore[import-not-found]
import _sitebuiltins
import linecache
import sys
import code
import ast
from types import ModuleType
from .console import InteractiveColoredConsole
from .readline import _get_reader, multiline_input
_error: tuple[type[Exception], ...] | type[Exception]
@ -74,57 +73,21 @@ REPL_COMMANDS = {
"clear": _clear_screen,
}
class InteractiveColoredConsole(code.InteractiveConsole):
def __init__(
self,
locals: dict[str, object] | None = None,
filename: str = "<console>",
*,
local_exit: bool = False,
) -> None:
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, dont_inherit=True)
except (OverflowError, ValueError, SyntaxError):
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
mainmodule: ModuleType | None = None,
future_flags: int = 0,
console: code.InteractiveConsole | None = None,
) -> None:
import __main__
from .readline import _setup
_setup()
mainmodule = mainmodule or __main__
console = InteractiveColoredConsole(mainmodule.__dict__, filename="<stdin>")
if console is None:
console = InteractiveColoredConsole(
mainmodule.__dict__, filename="<stdin>"
)
if future_flags:
console.compile.compiler.flags |= future_flags