<s> import sys <EOL> from threading import currentThread <EOL> try : <EOL> import signal <EOL> except ImportError : <EOL> signal = None <EOL> if sys . platform . startswith ( '<STR_LIT>' ) : <EOL> from java . lang import IllegalArgumentException <EOL> else : <EOL> IllegalArgumentException = None <EOL> from robot . errors import ExecutionFailed <EOL> from robot . output import LOGGER <EOL> class _StopSignalMonitor ( object ) : <EOL> def __init__ ( self ) : <EOL> self . _signal_count = <NUM_LIT:0> <EOL> self . _running_keyword = False <EOL> def __call__ ( self , signum , frame ) : <EOL> self . _signal_count += <NUM_LIT:1> <EOL> LOGGER . info ( '<STR_LIT>' % signum ) <EOL> if self . _signal_count > <NUM_LIT:1> : <EOL> sys . __stderr__ . write ( '<STR_LIT>' ) <EOL> raise SystemExit ( ) <EOL> sys . __stderr__ . write ( '<STR_LIT>' ) <EOL> if self . _running_keyword and not sys . platform . startswith ( '<STR_LIT>' ) : <EOL> self . _stop_execution_gracefully ( ) <EOL> def _stop_execution_gracefully ( self ) : <EOL> raise ExecutionFailed ( '<STR_LIT>' , exit = True ) <EOL> def start ( self ) : <EOL> if signal : <EOL> for signum in signal . SIGINT , signal . SIGTERM : <EOL> self . _register_signal_handler ( signum ) <EOL> def _register_signal_handler ( self , signum ) : <EOL> try : <EOL> signal . signal ( signum , self ) <EOL> except ( ValueError , IllegalArgumentException ) , err : <EOL> if currentThread ( ) . getName ( ) == '<STR_LIT>' : <EOL> self . _warn_about_registeration_error ( signum , err ) <EOL> def _warn_about_registeration_error ( self , signum , err ) : <EOL> name , ctrlc = { signal . SIGINT : ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> signal . SIGTERM : ( '<STR_LIT>' , '<STR_LIT>' ) } [ signum ] <EOL> LOGGER . warn ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( name , ctrlc , err ) ) <EOL> def start_running_keyword ( self , in_teardown ) : <EOL> self . _running_keyword = True <EOL> if self . _signal_count and not in_teardown : <EOL> self . _stop_execution_gracefully ( ) <EOL> def stop_running_keyword ( self ) : <EOL> self . _running_keyword = False <EOL> STOP_SIGNAL_MONITOR = _StopSignalMonitor ( ) </s>
