Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-121584: Toggle helper instead of nesting instances when using key bindings #121668

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions Lib/_pyrepl/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,14 @@ def do(self) -> None:
class help(Command):
def do(self) -> None:
import _sitebuiltins

with self.reader.suspend():
self.reader.msg = _sitebuiltins._Helper()() # type: ignore[assignment, call-arg]
if self.reader.help_mode:
self.reader.help_mode = False
raise KeyboardInterrupt
else:
self.reader.help_mode = True
with self.reader.suspend():
self.reader.msg = _sitebuiltins._Helper()() # type: ignore[assignment, call-arg]
self.reader.help_mode = False


class invalid_key(Command):
Expand Down
3 changes: 2 additions & 1 deletion Lib/_pyrepl/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ class Reader:
dirty: bool = False
finished: bool = False
paste_mode: bool = False
help_mode: bool = False
in_bracketed_paste: bool = False
commands: dict[str, type[Command]] = field(default_factory=make_default_commands)
last_command: type[Command] | None = None
Expand Down Expand Up @@ -665,7 +666,7 @@ def suspend(self) -> SimpleContextManager:
self.restore()
yield
finally:
for arg in ("msg", "ps1", "ps2", "ps3", "ps4", "paste_mode"):
for arg in ("msg", "ps1", "ps2", "ps3", "ps4", "paste_mode", "help_mode"):
setattr(self, arg, prev_state[arg])
self.prepare()

Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_pyrepl/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def prepare_reader(console: Console, **kwargs):
reader = ReadlineAlikeReader(console=console, config=config)
reader.more_lines = partial(more_lines, namespace=None)
reader.paste_mode = True # Avoid extra indents
reader.help_mode = False

def get_prompt(lineno, cursor_on_line) -> str:
return ""
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_pyrepl/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,20 @@ def test_up_arrow_after_ctrl_r(self):
reader, _ = handle_all_events(events)
self.assert_screen_equals(reader, "")

def test_help_toggles_instead_of_nesting(self):

events = [
Event(evt="key", data="f1", raw=bytearray(b"")),
]

no_paste_reader = functools.partial(
prepare_reader,
paste_mode=False,
help_mode=True,
)
reader, _ = handle_all_events(events, prepare_reader=no_paste_reader)
self.assertFalse(reader.help_mode)

def test_newline_within_block_trailing_whitespace(self):
# fmt: off
code = (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Toggle helper instead of nesting instances when using key bindings in the
new REPL.
Loading