summaryrefslogtreecommitdiffstats
path: root/Lib/_pyrepl
diff options
context:
space:
mode:
authorMilan Oberkirch <milan.oberkirch@geops.com>2024-07-15 22:24:18 (GMT)
committerGitHub <noreply@github.com>2024-07-15 22:24:18 (GMT)
commite5c7216f376a06d2c931daf999e2980e494e747e (patch)
tree6df64e6652cbdaa4e2f6832666f566e75f6a8bf0 /Lib/_pyrepl
parentd23be3947ced081914f4458c84f729c9c37f0219 (diff)
downloadcpython-e5c7216f376a06d2c931daf999e2980e494e747e.zip
cpython-e5c7216f376a06d2c931daf999e2980e494e747e.tar.gz
cpython-e5c7216f376a06d2c931daf999e2980e494e747e.tar.bz2
gh-121790: Fix interactive console initialization (#121793)
Co-authored-by: Ɓukasz Langa <lukasz@langa.pl>
Diffstat (limited to 'Lib/_pyrepl')
-rw-r--r--Lib/_pyrepl/main.py6
-rw-r--r--Lib/_pyrepl/readline.py8
-rw-r--r--Lib/_pyrepl/simple_interact.py14
3 files changed, 12 insertions, 16 deletions
diff --git a/Lib/_pyrepl/main.py b/Lib/_pyrepl/main.py
index 946bf33..8d6e07d 100644
--- a/Lib/_pyrepl/main.py
+++ b/Lib/_pyrepl/main.py
@@ -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)
diff --git a/Lib/_pyrepl/readline.py b/Lib/_pyrepl/readline.py
index 28f592d..3d94f91 100644
--- a/Lib/_pyrepl/readline.py
+++ b/Lib/_pyrepl/readline.py
@@ -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
diff --git a/Lib/_pyrepl/simple_interact.py b/Lib/_pyrepl/simple_interact.py
index bc16c1f..5af0798 100644
--- a/Lib/_pyrepl/simple_interact.py
+++ b/Lib/_pyrepl/simple_interact.py
@@ -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