Skip to content

Commit 4b5ad7e

Browse files
lysnikolaouestyxx
authored andcommitted
pythongh-118908: Fix completions after namespace change in REPL (python#120370)
1 parent 6fa3ebd commit 4b5ad7e

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

Lib/_pyrepl/readline.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@
5555
from collections.abc import Callable, Collection
5656
from .types import Callback, Completer, KeySpec, CommandName
5757

58+
TYPE_CHECKING = False
59+
60+
if TYPE_CHECKING:
61+
from typing import Any
62+
5863

5964
MoreLinesCallable = Callable[[str], bool]
6065

@@ -92,7 +97,7 @@
9297

9398
@dataclass
9499
class ReadlineConfig:
95-
readline_completer: Completer | None = RLCompleter().complete
100+
readline_completer: Completer | None = None
96101
completer_delims: frozenset[str] = frozenset(" \t\n`~!@#$%^&*()-=+[{]}\\|;:'\",<>/?")
97102

98103

@@ -554,7 +559,7 @@ def stub(*args: object, **kwds: object) -> None:
554559
# ____________________________________________________________
555560

556561

557-
def _setup() -> None:
562+
def _setup(namespace: dict[str, Any]) -> None:
558563
global raw_input
559564
if raw_input is not None:
560565
return # don't run _setup twice
@@ -570,9 +575,11 @@ def _setup() -> None:
570575
_wrapper.f_in = f_in
571576
_wrapper.f_out = f_out
572577

578+
# set up namespace in rlcompleter
579+
_wrapper.config.readline_completer = RLCompleter(namespace).complete
580+
573581
# this is not really what readline.c does. Better than nothing I guess
574582
import builtins
575-
576583
raw_input = builtins.input
577584
builtins.input = _wrapper.input
578585

Lib/_pyrepl/simple_interact.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ def run_multiline_interactive_console(
9696
console: code.InteractiveConsole | None = None,
9797
) -> None:
9898
from .readline import _setup
99-
_setup()
100-
10199
namespace = mainmodule.__dict__ if mainmodule else DEFAULT_NAMESPACE
100+
_setup(namespace)
101+
102102
if console is None:
103103
console = InteractiveColoredConsole(
104104
namespace, filename="<stdin>"

0 commit comments

Comments
 (0)