@@ -854,7 +854,7 @@ def _register_subcommands(self, cmdset: Union[CommandSet, 'Cmd']) -> None:
854
854
)
855
855
856
856
# iterate through all matching methods
857
- for method_name , method in methods :
857
+ for _ , method in methods :
858
858
subcommand_name : str = getattr (method , constants .SUBCMD_ATTR_NAME )
859
859
full_command_name : str = getattr (method , constants .SUBCMD_ATTR_COMMAND )
860
860
subcmd_parser_builder = getattr (method , constants .CMD_ATTR_ARGPARSER )
@@ -963,7 +963,7 @@ def _unregister_subcommands(self, cmdset: Union[CommandSet, 'Cmd']) -> None:
963
963
)
964
964
965
965
# iterate through all matching methods
966
- for method_name , method in methods :
966
+ for _ , method in methods :
967
967
subcommand_name = getattr (method , constants .SUBCMD_ATTR_NAME )
968
968
command_name = getattr (method , constants .SUBCMD_ATTR_COMMAND )
969
969
@@ -2430,29 +2430,22 @@ def sigint_handler(self, signum: int, _: FrameType) -> None:
2430
2430
if raise_interrupt :
2431
2431
self ._raise_keyboard_interrupt ()
2432
2432
2433
- def sigterm_handler (self , signum : int , _ : FrameType ) -> None : # pragma: no cover
2433
+ def termination_signal_handler (self , signum : int , _ : FrameType ) -> None :
2434
2434
"""
2435
- Signal handler for SIGTERMs which are sent to politely ask this app to terminate .
2435
+ Signal handler for SIGHUP and SIGTERM. Only runs on Linux and Mac .
2436
2436
2437
- If you need custom SIGTERM behavior, then override this method.
2437
+ SIGHUP - received when terminal window is closed.
2438
+ SIGTERM - received when this process politely asked to terminate.
2438
2439
2439
- :param signum: signal number
2440
- :param _: required param for signal handlers
2441
- """
2442
- # Gracefully exit so the persistent history file will be written.
2443
- sys .exit (self .exit_code )
2444
-
2445
- def sighup_handler (self , signum : int , _ : FrameType ) -> None : # pragma: no cover
2446
- """
2447
- Signal handler for SIGHUPs which are sent when the terminal is closed.
2448
-
2449
- If you need custom SIGHUP behavior, then override this method.
2440
+ The basic purpose of this method is to call sys.exit() so our atexit handler will run
2441
+ and save the persistent history file. If you need more complex behavior like killing
2442
+ threads and performing cleanup, then override this method.
2450
2443
2451
2444
:param signum: signal number
2452
2445
:param _: required param for signal handlers
2453
2446
"""
2454
- # Gracefully exit so the persistent history file will be written.
2455
- sys .exit (self . exit_code )
2447
+ # POSIX systems add 128 to signal numbers for the exit code
2448
+ sys .exit (128 + signum )
2456
2449
2457
2450
def _raise_keyboard_interrupt (self ) -> None :
2458
2451
"""Helper function to raise a KeyboardInterrupt"""
@@ -5458,17 +5451,18 @@ def cmdloop(self, intro: Optional[str] = None) -> int: # type: ignore[override]
5458
5451
if not threading .current_thread () is threading .main_thread ():
5459
5452
raise RuntimeError ("cmdloop must be run in the main thread" )
5460
5453
5461
- # Register a SIGINT signal handler for Ctrl+C
5454
+ # Register signal handlers
5462
5455
import signal
5463
5456
5464
5457
original_sigint_handler = signal .getsignal (signal .SIGINT )
5465
5458
signal .signal (signal .SIGINT , self .sigint_handler ) # type: ignore
5466
5459
5467
- original_sigterm_handler = signal .getsignal (signal .SIGTERM )
5468
- signal .signal (signal .SIGTERM , self .sigterm_handler ) # type: ignore
5460
+ if not sys .platform .startswith ('win' ):
5461
+ original_sighup_handler = signal .getsignal (signal .SIGHUP )
5462
+ signal .signal (signal .SIGHUP , self .termination_signal_handler ) # type: ignore
5469
5463
5470
- original_sighup_handler = signal .getsignal (signal .SIGHUP )
5471
- signal .signal (signal .SIGHUP , self .sighup_handler ) # type: ignore
5464
+ original_sigterm_handler = signal .getsignal (signal .SIGTERM )
5465
+ signal .signal (signal .SIGTERM , self .termination_signal_handler ) # type: ignore
5472
5466
5473
5467
# Grab terminal lock before the command line prompt has been drawn by readline
5474
5468
self .terminal_lock .acquire ()
@@ -5502,10 +5496,12 @@ def cmdloop(self, intro: Optional[str] = None) -> int: # type: ignore[override]
5502
5496
# This will also zero the lock count in case cmdloop() is called again
5503
5497
self .terminal_lock .release ()
5504
5498
5505
- # Restore the original signal handlers
5499
+ # Restore original signal handlers
5506
5500
signal .signal (signal .SIGINT , original_sigint_handler )
5507
- signal .signal (signal .SIGTERM , original_sigterm_handler )
5508
- signal .signal (signal .SIGHUP , original_sighup_handler )
5501
+
5502
+ if not sys .platform .startswith ('win' ):
5503
+ signal .signal (signal .SIGHUP , original_sighup_handler )
5504
+ signal .signal (signal .SIGTERM , original_sigterm_handler )
5509
5505
5510
5506
return self .exit_code
5511
5507
0 commit comments