mirror of
https://github.com/python/cpython.git
synced 2025-08-01 07:33:08 +00:00
[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:
parent
67ac19111f
commit
a5272e63ef
7 changed files with 143 additions and 65 deletions
|
@ -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
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue