Skip to content

Commit 58dd262

Browse files
authored
Revert "Handle termination signals"
1 parent 65cdf34 commit 58dd262

File tree

3 files changed

+6
-47
lines changed

3 files changed

+6
-47
lines changed

CHANGELOG.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
## 2.5.0 (TBD)
22
* Breaking Change
33
* `cmd2` 2.5 supports Python 3.8+ (removed support for Python 3.6 and 3.7)
4-
* Bug Fixes
5-
* Fixed issue where persistent history file was not saved upon SIGHUP and SIGTERM signals.
64
* Enhancements
75
* Removed dependency on `attrs` and replaced with [dataclasses](https://docs.python.org/3/library/dataclasses.html)
86
* add `allow_clipboard` initialization parameter and attribute to disable ability to

cmd2/cmd2.py

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2405,13 +2405,13 @@ def get_help_topics(self) -> List[str]:
24052405
return [topic for topic in all_topics if topic not in self.hidden_commands and topic not in self.disabled_commands]
24062406

24072407
# noinspection PyUnusedLocal
2408-
def sigint_handler(self, signum: int, _: Optional[FrameType]) -> None:
2408+
def sigint_handler(self, signum: int, _: FrameType) -> None:
24092409
"""Signal handler for SIGINTs which typically come from Ctrl-C events.
24102410
2411-
If you need custom SIGINT behavior, then override this method.
2411+
If you need custom SIGINT behavior, then override this function.
24122412
24132413
:param signum: signal number
2414-
:param _: the current stack frame or None
2414+
:param _: required param for signal handlers
24152415
"""
24162416
if self._cur_pipe_proc_reader is not None:
24172417
# Pass the SIGINT to the current pipe process
@@ -2427,23 +2427,6 @@ def sigint_handler(self, signum: int, _: Optional[FrameType]) -> None:
24272427
if raise_interrupt:
24282428
self._raise_keyboard_interrupt()
24292429

2430-
def termination_signal_handler(self, signum: int, _: Optional[FrameType]) -> None:
2431-
"""
2432-
Signal handler for SIGHUP and SIGTERM. Only runs on Linux and Mac.
2433-
2434-
SIGHUP - received when terminal window is closed
2435-
SIGTERM - received when this app has been requested to terminate
2436-
2437-
The basic purpose of this method is to call sys.exit() so our exit handler will run
2438-
and save the persistent history file. If you need more complex behavior like killing
2439-
threads and performing cleanup, then override this method.
2440-
2441-
:param signum: signal number
2442-
:param _: the current stack frame or None
2443-
"""
2444-
# POSIX systems add 128 to signal numbers for the exit code
2445-
sys.exit(128 + signum)
2446-
24472430
def _raise_keyboard_interrupt(self) -> None:
24482431
"""Helper function to raise a KeyboardInterrupt"""
24492432
raise KeyboardInterrupt("Got a keyboard interrupt")
@@ -5443,18 +5426,11 @@ def cmdloop(self, intro: Optional[str] = None) -> int: # type: ignore[override]
54435426
if not threading.current_thread() is threading.main_thread():
54445427
raise RuntimeError("cmdloop must be run in the main thread")
54455428

5446-
# Register signal handlers
5429+
# Register a SIGINT signal handler for Ctrl+C
54475430
import signal
54485431

54495432
original_sigint_handler = signal.getsignal(signal.SIGINT)
5450-
signal.signal(signal.SIGINT, self.sigint_handler)
5451-
5452-
if not sys.platform.startswith('win'):
5453-
original_sighup_handler = signal.getsignal(signal.SIGHUP)
5454-
signal.signal(signal.SIGHUP, self.termination_signal_handler)
5455-
5456-
original_sigterm_handler = signal.getsignal(signal.SIGTERM)
5457-
signal.signal(signal.SIGTERM, self.termination_signal_handler)
5433+
signal.signal(signal.SIGINT, self.sigint_handler) # type: ignore
54585434

54595435
# Grab terminal lock before the command line prompt has been drawn by readline
54605436
self.terminal_lock.acquire()
@@ -5488,13 +5464,9 @@ def cmdloop(self, intro: Optional[str] = None) -> int: # type: ignore[override]
54885464
# This will also zero the lock count in case cmdloop() is called again
54895465
self.terminal_lock.release()
54905466

5491-
# Restore original signal handlers
5467+
# Restore the original signal handler
54925468
signal.signal(signal.SIGINT, original_sigint_handler)
54935469

5494-
if not sys.platform.startswith('win'):
5495-
signal.signal(signal.SIGHUP, original_sighup_handler)
5496-
signal.signal(signal.SIGTERM, original_sigterm_handler)
5497-
54985470
return self.exit_code
54995471

55005472
###

tests/test_cmd2.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,17 +1038,6 @@ def test_raise_keyboard_interrupt(base_app):
10381038
assert 'Got a keyboard interrupt' in str(excinfo.value)
10391039

10401040

1041-
@pytest.mark.skipif(sys.platform.startswith('win'), reason="SIGTERM only handeled on Linux/Mac")
1042-
def test_termination_signal_handler(base_app):
1043-
with pytest.raises(SystemExit) as excinfo:
1044-
base_app.termination_signal_handler(signal.SIGHUP, 1)
1045-
assert excinfo.value.code == signal.SIGHUP + 128
1046-
1047-
with pytest.raises(SystemExit) as excinfo:
1048-
base_app.termination_signal_handler(signal.SIGTERM, 1)
1049-
assert excinfo.value.code == signal.SIGTERM + 128
1050-
1051-
10521041
class HookFailureApp(cmd2.Cmd):
10531042
def __init__(self, *args, **kwargs):
10541043
super().__init__(*args, **kwargs)

0 commit comments

Comments
 (0)