gh-121790: Fix interactive console initialization (#121793)

Co-authored-by: Łukasz Langa <lukasz@langa.pl>
This commit is contained in:
Milan Oberkirch 2024-07-16 00:24:18 +02:00 committed by GitHub
parent d23be3947c
commit e5c7216f37
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 85 additions and 42 deletions

View file

@ -23,7 +23,7 @@ else:
def interactive_console(mainmodule=None, quiet=False, pythonstartup=False):
if not CAN_USE_PYREPL:
if not os.environ.get('PYTHON_BASIC_REPL', None) and FAIL_REASON:
if not os.getenv('PYTHON_BASIC_REPL') and FAIL_REASON:
from .trace import trace
trace(FAIL_REASON)
print(FAIL_REASON, file=sys.stderr)
@ -51,5 +51,7 @@ def interactive_console(mainmodule=None, quiet=False, pythonstartup=False):
if not hasattr(sys, "ps2"):
sys.ps2 = "... "
from .console import InteractiveColoredConsole
from .simple_interact import run_multiline_interactive_console
run_multiline_interactive_console(namespace)
console = InteractiveColoredConsole(namespace, filename="<stdin>")
run_multiline_interactive_console(console)

View file

@ -58,7 +58,7 @@ from .types import Callback, Completer, KeySpec, CommandName
TYPE_CHECKING = False
if TYPE_CHECKING:
from typing import Any
from typing import Any, Mapping
MoreLinesCallable = Callable[[str], bool]
@ -559,7 +559,7 @@ for _name, _ret in [
# ____________________________________________________________
def _setup(namespace: dict[str, Any]) -> None:
def _setup(namespace: Mapping[str, Any]) -> None:
global raw_input
if raw_input is not None:
return # don't run _setup twice
@ -575,7 +575,9 @@ def _setup(namespace: dict[str, Any]) -> None:
_wrapper.f_in = f_in
_wrapper.f_out = f_out
# set up namespace in rlcompleter
# set up namespace in rlcompleter, which requires it to be a bona fide dict
if not isinstance(namespace, dict):
namespace = dict(namespace)
_wrapper.config.readline_completer = RLCompleter(namespace).complete
# this is not really what readline.c does. Better than nothing I guess

View file

@ -27,12 +27,9 @@ from __future__ import annotations
import _sitebuiltins
import linecache
import builtins
import sys
import code
from types import ModuleType
from .console import InteractiveColoredConsole
from .readline import _get_reader, multiline_input
TYPE_CHECKING = False
@ -82,17 +79,12 @@ REPL_COMMANDS = {
def run_multiline_interactive_console(
namespace: dict[str, Any],
console: code.InteractiveConsole,
*,
future_flags: int = 0,
console: code.InteractiveConsole | None = None,
) -> None:
from .readline import _setup
_setup(namespace)
if console is None:
console = InteractiveColoredConsole(
namespace, filename="<stdin>"
)
_setup(console.locals)
if future_flags:
console.compile.compiler.flags |= future_flags