diff options
| author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-03-20 16:13:50 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-03-20 16:13:50 -0400 |
| commit | 328b408fa755a17268c33f0fae0301bb85d1f102 (patch) | |
| tree | 2da9801764272b01f80c3b27f1d797ebd15c15a9 /cmd2.py | |
| parent | 627360cfc71995af939569b2aee9dbc8141544d0 (diff) | |
| parent | 0075c6d6b324c934e7cbb694a99e5dc7ab5697a6 (diff) | |
| download | cmd2-git-328b408fa755a17268c33f0fae0301bb85d1f102.tar.gz | |
Merge pull request #323 from python-cmd2/sigint_handler
Added default sigint handler
Diffstat (limited to 'cmd2.py')
| -rwxr-xr-x | cmd2.py | 25 |
1 files changed, 24 insertions, 1 deletions
@@ -36,6 +36,7 @@ import os import platform import re import shlex +import signal import six import sys import tempfile @@ -1051,7 +1052,7 @@ class Cmd(cmd.Cmd): allow_cli_args = True # Should arguments passed on the command-line be processed as commands? allow_redirection = True # Should output redirection and pipes be allowed default_to_shell = False # Attempt to run unrecognized commands as shell commands - quit_on_sigint = True # Quit the loop on interrupt instead of just resetting prompt + quit_on_sigint = False # Quit the loop on interrupt instead of just resetting prompt reserved_words = [] # Attributes which ARE dynamically settable at runtime @@ -1480,6 +1481,28 @@ class Cmd(cmd.Cmd): completions.sort() return completions + # noinspection PyUnusedLocal + def sigint_handler(self, signum, frame): + """Signal handler for SIGINTs which typically come from Ctrl-C events. + + If you need custom SIGINT behavior, then override this function. + + :param signum: int - signal number + :param frame + """ + # Save copy of pipe_proc since it could theoretically change while this is running + pipe_proc = self.pipe_proc + if pipe_proc is not None: + pipe_proc.terminate() + + # Re-raise a KeyboardInterrupt so other parts of the code can catch it + raise KeyboardInterrupt("Got a keyboard interrupt") + + def preloop(self): + """Hook method executed once when the cmdloop() method is called.""" + # Register a default SIGINT signal handler for Ctrl+C + signal.signal(signal.SIGINT, self.sigint_handler) + def precmd(self, statement): """Hook method executed just before the command is processed by ``onecmd()`` and after adding it to the history. |
