<s> import sys , signal , commands <EOL> DEFAULT_TIMEOUT = <NUM_LIT> <EOL> class TimeoutException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class call_with_timeout : <EOL> def __init__ ( self , function , timeout = DEFAULT_TIMEOUT ) : <EOL> self . timeout = timeout <EOL> self . function = function <EOL> def handler ( self , signum , frame ) : <EOL> raise TimeoutException ( ) <EOL> def __call__ ( self , * args ) : <EOL> old = signal . signal ( signal . SIGALRM , self . handler ) <EOL> signal . setitimer ( signal . ITIMER_REAL , self . timeout ) <EOL> try : <EOL> result = self . function ( * args ) <EOL> finally : <EOL> signal . signal ( signal . SIGALRM , old ) <EOL> signal . setitimer ( signal . ITIMER_REAL , <NUM_LIT:0> ) <EOL> return result <EOL> def timeout ( timeout ) : <EOL> """<STR_LIT>""" <EOL> def wrap_function ( function ) : <EOL> return call_with_timeout ( function , timeout ) <EOL> return wrap_function <EOL> def default_timeout ( function ) : <EOL> """<STR_LIT>""" <EOL> return call_with_timeout ( function ) <EOL> def getstatusoutput ( command , timeout = DEFAULT_TIMEOUT ) : <EOL> """<STR_LIT>""" <EOL> _gso = call_with_timeout ( commands . getstatusoutput , timeout ) <EOL> try : <EOL> return _gso ( command ) <EOL> except TimeoutException , e : <EOL> return ( - <NUM_LIT:1> , "<STR_LIT>" % ( command , timeout ) ) </s>
