diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-10-21 17:21:56 -0400 |
---|---|---|
committer | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-10-21 17:21:56 -0400 |
commit | 8ae418d33320135a4eea3c864065aa6868497eeb (patch) | |
tree | 0f63a9be2b8e55a2a9db7a29ef1fd85a6ad819ea /cmd2/cmd2.py | |
parent | 7b2603e550deeb98399473f2c17535ac740b2c1a (diff) | |
download | cmd2-git-8ae418d33320135a4eea3c864065aa6868497eeb.tar.gz |
cmdloop now checks to see if it is running in the main thread before attempting to register a signal handler for SIGINT
Diffstat (limited to 'cmd2/cmd2.py')
-rw-r--r-- | cmd2/cmd2.py | 15 |
1 files changed, 9 insertions, 6 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index e08db8c5..b9e6aa99 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -3588,10 +3588,12 @@ a..b, a:b, a:, ..b items by indices (inclusive) if callargs: self.cmdqueue.extend(callargs) - # Register a SIGINT signal handler for Ctrl+C - import signal - original_sigint_handler = signal.getsignal(signal.SIGINT) - signal.signal(signal.SIGINT, self.sigint_handler) + # Only the main thread is allowed to set a new signal handler in Python, so only attempt if that is the case + if threading.current_thread() is threading.main_thread(): + # Register a SIGINT signal handler for Ctrl+C + import signal + original_sigint_handler = signal.getsignal(signal.SIGINT) + signal.signal(signal.SIGINT, self.sigint_handler) # Grab terminal lock before the prompt has been drawn by readline self.terminal_lock.acquire() @@ -3625,8 +3627,9 @@ a..b, a:b, a:, ..b items by indices (inclusive) # This will also zero the lock count in case cmdloop() is called again self.terminal_lock.release() - # Restore the original signal handler - signal.signal(signal.SIGINT, original_sigint_handler) + if threading.current_thread() is threading.main_thread(): + # Restore the original signal handler + signal.signal(signal.SIGINT, original_sigint_handler) if self.exit_code is not None: sys.exit(self.exit_code) |