Skip to content

Commit 15f3aec

Browse files
Allow passing exception classes for KeyboardInterrupt and EOFError in PromptSession.
1 parent a04c37d commit 15f3aec

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

Diff for: src/prompt_toolkit/shortcuts/prompt.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ class PromptSession(Generic[_T]):
324324
:param input: `Input` object. (Note that the preferred way to change the
325325
input/output is by creating an `AppSession`.)
326326
:param output: `Output` object.
327+
:param interrupt_exception: The exception type that will be raised when
328+
there is a keyboard interrupt (control-c keypress).
329+
:param eof_exception: The exception type that will be raised when there is
330+
an end-of-file/exit event (control-d keypress).
327331
"""
328332

329333
_fields = (
@@ -410,6 +414,8 @@ def __init__(
410414
refresh_interval: float = 0,
411415
input: Input | None = None,
412416
output: Output | None = None,
417+
interrupt_exception: type[BaseException] = KeyboardInterrupt,
418+
eof_exception: type[BaseException] = EOFError,
413419
) -> None:
414420
history = history or InMemoryHistory()
415421
clipboard = clipboard or InMemoryClipboard()
@@ -459,6 +465,8 @@ def __init__(
459465
self.reserve_space_for_menu = reserve_space_for_menu
460466
self.tempfile_suffix = tempfile_suffix
461467
self.tempfile = tempfile
468+
self.interrupt_exception = interrupt_exception
469+
self.eof_exception = eof_exception
462470

463471
# Create buffers, layout and Application.
464472
self.history = history
@@ -811,7 +819,7 @@ def _complete_like_readline(event: E) -> None:
811819
@handle("<sigint>")
812820
def _keyboard_interrupt(event: E) -> None:
813821
"Abort when Control-C has been pressed."
814-
event.app.exit(exception=KeyboardInterrupt, style="class:aborting")
822+
event.app.exit(exception=self.interrupt_exception(), style="class:aborting")
815823

816824
@Condition
817825
def ctrl_d_condition() -> bool:
@@ -826,7 +834,7 @@ def ctrl_d_condition() -> bool:
826834
@handle("c-d", filter=ctrl_d_condition & default_focused)
827835
def _eof(event: E) -> None:
828836
"Exit when Control-D has been pressed."
829-
event.app.exit(exception=EOFError, style="class:exiting")
837+
event.app.exit(exception=self.eof_exception(), style="class:exiting")
830838

831839
suspend_supported = Condition(suspend_to_background_supported)
832840

0 commit comments

Comments
 (0)