summaryrefslogtreecommitdiffstats
path: root/Lib/_pyrepl/simple_interact.py
diff options
context:
space:
mode:
authorƁukasz Langa <lukasz@langa.pl>2024-05-31 20:26:02 (GMT)
committerGitHub <noreply@github.com>2024-05-31 20:26:02 (GMT)
commit2237946af0981c46dc7d3886477e425ccfb37f28 (patch)
tree55317ee5d19a7bcba247052de2d0de660ebcb724 /Lib/_pyrepl/simple_interact.py
parentf9d47fed9fbbe9313404838050f6dfe1c7fe6340 (diff)
downloadcpython-2237946af0981c46dc7d3886477e425ccfb37f28.zip
cpython-2237946af0981c46dc7d3886477e425ccfb37f28.tar.gz
cpython-2237946af0981c46dc7d3886477e425ccfb37f28.tar.bz2
gh-118894: Make asyncio REPL use pyrepl (GH-119433)
Diffstat (limited to 'Lib/_pyrepl/simple_interact.py')
-rw-r--r--Lib/_pyrepl/simple_interact.py53
1 files changed, 8 insertions, 45 deletions
diff --git a/Lib/_pyrepl/simple_interact.py b/Lib/_pyrepl/simple_interact.py
index c624f6e..256bbc7 100644
--- a/Lib/_pyrepl/simple_interact.py
+++ b/Lib/_pyrepl/simple_interact.py
@@ -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