]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-118908: Fix completions after namespace change in REPL (#120370)
authorLysandros Nikolaou <lisandrosnik@gmail.com>
Wed, 12 Jun 2024 08:21:53 +0000 (10:21 +0200)
committerGitHub <noreply@github.com>
Wed, 12 Jun 2024 08:21:53 +0000 (10:21 +0200)
Lib/_pyrepl/readline.py
Lib/_pyrepl/simple_interact.py

index b10d0c66e4f813d545e05e29f1597821b6d87482..28f592d80b1b03ee6b682a60cacf73f97dc86edc 100644 (file)
@@ -55,6 +55,11 @@ Command = commands.Command
 from collections.abc import Callable, Collection
 from .types import Callback, Completer, KeySpec, CommandName
 
+TYPE_CHECKING = False
+
+if TYPE_CHECKING:
+    from typing import Any
+
 
 MoreLinesCallable = Callable[[str], bool]
 
@@ -92,7 +97,7 @@ __all__ = [
 
 @dataclass
 class ReadlineConfig:
-    readline_completer: Completer | None = RLCompleter().complete
+    readline_completer: Completer | None = None
     completer_delims: frozenset[str] = frozenset(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?")
 
 
@@ -554,7 +559,7 @@ for _name, _ret in [
 # ____________________________________________________________
 
 
-def _setup() -> None:
+def _setup(namespace: dict[str, Any]) -> None:
     global raw_input
     if raw_input is not None:
         return  # don't run _setup twice
@@ -570,9 +575,11 @@ def _setup() -> None:
     _wrapper.f_in = f_in
     _wrapper.f_out = f_out
 
+    # set up namespace in rlcompleter
+    _wrapper.config.readline_completer = RLCompleter(namespace).complete
+
     # this is not really what readline.c does.  Better than nothing I guess
     import builtins
-
     raw_input = builtins.input
     builtins.input = _wrapper.input
 
index 620f87b4867073bab1566361daa2df664248faca..2de3b38c37a9da634b364f45e7eaa6ed5f67bbf7 100644 (file)
@@ -96,9 +96,9 @@ def run_multiline_interactive_console(
     console: code.InteractiveConsole | None = None,
 ) -> None:
     from .readline import _setup
-    _setup()
-
     namespace = mainmodule.__dict__ if mainmodule else DEFAULT_NAMESPACE
+    _setup(namespace)
+
     if console is None:
         console = InteractiveColoredConsole(
             namespace, filename="<stdin>"