|
36 | 36 | import platform |
37 | 37 | import re |
38 | 38 | import shlex |
| 39 | +import signal |
39 | 40 | import six |
40 | 41 | import sys |
41 | 42 | import tempfile |
@@ -1051,7 +1052,7 @@ class Cmd(cmd.Cmd): |
1051 | 1052 | allow_cli_args = True # Should arguments passed on the command-line be processed as commands? |
1052 | 1053 | allow_redirection = True # Should output redirection and pipes be allowed |
1053 | 1054 | default_to_shell = False # Attempt to run unrecognized commands as shell commands |
1054 | | - quit_on_sigint = True # Quit the loop on interrupt instead of just resetting prompt |
| 1055 | + quit_on_sigint = False # Quit the loop on interrupt instead of just resetting prompt |
1055 | 1056 | reserved_words = [] |
1056 | 1057 |
|
1057 | 1058 | # Attributes which ARE dynamically settable at runtime |
@@ -1480,6 +1481,28 @@ def complete_help(self, text, line, begidx, endidx): |
1480 | 1481 | completions.sort() |
1481 | 1482 | return completions |
1482 | 1483 |
|
| 1484 | + # noinspection PyUnusedLocal |
| 1485 | + def sigint_handler(self, signum, frame): |
| 1486 | + """Signal handler for SIGINTs which typically come from Ctrl-C events. |
| 1487 | +
|
| 1488 | + If you need custom SIGINT behavior, then override this function. |
| 1489 | +
|
| 1490 | + :param signum: int - signal number |
| 1491 | + :param frame |
| 1492 | + """ |
| 1493 | + # Save copy of pipe_proc since it could theoretically change while this is running |
| 1494 | + pipe_proc = self.pipe_proc |
| 1495 | + if pipe_proc is not None: |
| 1496 | + pipe_proc.terminate() |
| 1497 | + |
| 1498 | + # Re-raise a KeyboardInterrupt so other parts of the code can catch it |
| 1499 | + raise KeyboardInterrupt("Got a keyboard interrupt") |
| 1500 | + |
| 1501 | + def preloop(self): |
| 1502 | + """Hook method executed once when the cmdloop() method is called.""" |
| 1503 | + # Register a default SIGINT signal handler for Ctrl+C |
| 1504 | + signal.signal(signal.SIGINT, self.sigint_handler) |
| 1505 | + |
1483 | 1506 | def precmd(self, statement): |
1484 | 1507 | """Hook method executed just before the command is processed by ``onecmd()`` and after adding it to the history. |
1485 | 1508 |
|
|
0 commit comments