Skip to content

Commit 79bf87d

Browse files
committed
Removed cmd2.Cmd.quit_on_sigint.
1 parent 37d415b commit 79bf87d

File tree

5 files changed

+10
-59
lines changed

5 files changed

+10
-59
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
and just opens an interactive Python shell.
3131
* Changed default behavior of `runcmds_plus_hooks()` to not stop when Ctrl-C is pressed and instead
3232
run the next command in its list.
33+
* Removed `cmd2.Cmd.quit_on_sigint` flag, which when `True`, quit the application when Ctrl-C was pressed at the prompt.
3334
* Enhancements
3435
* Added support for custom tab completion and up-arrow input history to `cmd2.Cmd2.read_input`.
3536
See [read_input.py](https://github.com/python-cmd2/cmd2/blob/master/examples/read_input.py)

cmd2/cmd2.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,6 @@ def __init__(
293293

294294
# Attributes which should NOT be dynamically settable via the set command at runtime
295295
self.default_to_shell = False # Attempt to run unrecognized commands as shell commands
296-
self.quit_on_sigint = False # Ctrl-C at the prompt will quit the program instead of just resetting prompt
297296
self.allow_redirection = allow_redirection # Security setting to prevent redirection of stdout
298297

299298
# Attributes which ARE dynamically settable via the set command at runtime
@@ -2490,13 +2489,10 @@ def _complete_statement(self, line: str) -> Statement:
24902489
nextline = '\n'
24912490
self.poutput(nextline)
24922491
line = f'{self._multiline_in_progress}{nextline}'
2493-
except KeyboardInterrupt as ex:
2494-
if self.quit_on_sigint:
2495-
raise ex
2496-
else:
2497-
self.poutput('^C')
2498-
statement = self.statement_parser.parse('')
2499-
break
2492+
except KeyboardInterrupt:
2493+
self.poutput('^C')
2494+
statement = self.statement_parser.parse('')
2495+
break
25002496
finally:
25012497
self._at_continuation_prompt = False
25022498

@@ -3067,12 +3063,9 @@ def _cmdloop(self) -> None:
30673063
# Get commands from user
30683064
try:
30693065
line = self._read_command_line(self.prompt)
3070-
except KeyboardInterrupt as ex:
3071-
if self.quit_on_sigint:
3072-
raise ex
3073-
else:
3074-
self.poutput('^C')
3075-
line = ''
3066+
except KeyboardInterrupt:
3067+
self.poutput('^C')
3068+
line = ''
30763069

30773070
# Run the command along with all associated pre and post hooks
30783071
stop = self.onecmd_plus_hooks(line)

docs/features/initialization.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,6 @@ override:
146146
everything available with **self_in_py**)
147147
- **quiet**: if ``True`` then completely suppress nonessential output (Default:
148148
``False``)
149-
- **quit_on_sigint**: if ``True`` Ctrl-C at the prompt will quit the program
150-
instead of just resetting prompt
151149
- **settable**: dictionary that controls which of these instance attributes
152150
are settable at runtime using the *set* command
153151
- **timing**: if ``True`` display execution time for each command (Default:

docs/features/misc.rst

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -99,21 +99,3 @@ method be called.
9999
(Cmd) my dog has fleas
100100
sh: my: not found
101101
*** Unknown syntax: my dog has fleas
102-
103-
104-
Quit on SIGINT
105-
--------------
106-
107-
On many shells, SIGINT (most often triggered by the user pressing Ctrl+C)
108-
while at the prompt only cancels the current line, not the entire command
109-
loop. By default, a ``cmd2`` application matches this behavior. However, if
110-
``quit_on_sigint`` is set to ``True``, the command loop will quit instead.
111-
112-
::
113-
114-
(Cmd) typing a comma^C
115-
(Cmd)
116-
117-
.. warning::
118-
The default SIGINT behavior will only function properly if **cmdloop** is running
119-
in the main thread.

tests/test_cmd2.py

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -962,36 +962,13 @@ def say_app():
962962
return app
963963

964964

965-
def test_interrupt_quit(say_app):
966-
say_app.quit_on_sigint = True
967-
968-
# Mock out the input call so we don't actually wait for a user's response on stdin
969-
m = mock.MagicMock(name='input')
970-
m.side_effect = ['say hello', KeyboardInterrupt(), 'say goodbye', 'eof']
971-
builtins.input = m
972-
973-
try:
974-
say_app.cmdloop()
975-
except KeyboardInterrupt:
976-
pass
977-
978-
# And verify the expected output to stdout
979-
out = say_app.stdout.getvalue()
980-
assert out == 'hello\n'
981-
982-
983-
def test_interrupt_noquit(say_app):
984-
say_app.quit_on_sigint = False
985-
965+
def test_ctrl_c_at_prompt(say_app):
986966
# Mock out the input call so we don't actually wait for a user's response on stdin
987967
m = mock.MagicMock(name='input')
988968
m.side_effect = ['say hello', KeyboardInterrupt(), 'say goodbye', 'eof']
989969
builtins.input = m
990970

991-
try:
992-
say_app.cmdloop()
993-
except KeyboardInterrupt:
994-
pass
971+
say_app.cmdloop()
995972

996973
# And verify the expected output to stdout
997974
out = say_app.stdout.getvalue()

0 commit comments

Comments
 (0)