Skip to content
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
6 changes: 1 addition & 5 deletions src/prompt_toolkit/application/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -1055,8 +1055,6 @@ def _breakpointhook(self, *a: object, **kw: object) -> None:
import pdb
from types import FrameType

TraceDispatch = Callable[[FrameType, str, Any], Any]

@contextmanager
def hide_app_from_eventloop_thread() -> Generator[None, None, None]:
"""Stop application if `__breakpointhook__` is called from within
Expand Down Expand Up @@ -1110,9 +1108,7 @@ async def in_loop() -> None:
done.set()

class CustomPdb(pdb.Pdb):
def trace_dispatch(
self, frame: FrameType, event: str, arg: Any
) -> TraceDispatch:
def trace_dispatch(self, frame: FrameType, event: str, arg: Any) -> Any:
if app._loop_thread is None:
return super().trace_dispatch(frame, event, arg)

Expand Down
5 changes: 5 additions & 0 deletions src/prompt_toolkit/shortcuts/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -1527,6 +1527,11 @@ def no(event: E) -> None:
session.default_buffer.text = "n"
event.app.exit(result=False)

@bindings.add("enter")
def _(event: E) -> None:
"Disallow submitting without an answer."
pass

@bindings.add(Keys.Any)
def _(event: E) -> None:
"Disallow inserting other text."
Expand Down
28 changes: 26 additions & 2 deletions tests/test_shortcuts.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
from __future__ import annotations

from prompt_toolkit.shortcuts import print_container
from prompt_toolkit.shortcuts.prompt import _split_multiline_prompt
from functools import partial
from importlib import import_module
from unittest.mock import patch

import pytest

from prompt_toolkit.input import create_pipe_input
from prompt_toolkit.output import DummyOutput
from prompt_toolkit.shortcuts import confirm, print_container
from prompt_toolkit.shortcuts.prompt import PromptSession, _split_multiline_prompt
from prompt_toolkit.widgets import Frame, TextArea


Expand Down Expand Up @@ -66,3 +74,19 @@ def test_print_container(tmpdir):
text = fd.read()
assert "Hello world" in text
assert "Title" in text


@pytest.mark.parametrize(
("answer", "expected"),
[("y", True), ("Y", True), ("n", False), ("N", False)],
)
def test_confirm_ignores_enter_without_answer(answer, expected):
with create_pipe_input() as input:
input.send_text(f"\r{answer}")
session = partial(PromptSession, input=input, output=DummyOutput())
prompt_module = import_module("prompt_toolkit.shortcuts.prompt")

with patch.object(prompt_module, "PromptSession", session):
result = confirm()

assert result is expected
Loading