<s> import time <EOL> from concurrence import TaskLocal <EOL> class _Timeout ( object ) : <EOL> def __init__ ( self ) : <EOL> self . _timeout_time = [ - <NUM_LIT:1> ] <EOL> def current ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _timeout_time [ - <NUM_LIT:1> ] < <NUM_LIT:0> : <EOL> return - <NUM_LIT:1> <EOL> else : <EOL> timeout = self . _timeout_time [ - <NUM_LIT:1> ] - time . time ( ) <EOL> if timeout < <NUM_LIT:0> : timeout = <NUM_LIT:0.0> <EOL> return timeout <EOL> def push ( self , timeout = - <NUM_LIT:1> ) : <EOL> current_timeout = self . _timeout_time [ - <NUM_LIT:1> ] <EOL> if timeout < <NUM_LIT:0> and current_timeout < <NUM_LIT:0> : <EOL> self . _timeout_time . append ( timeout ) <EOL> elif timeout < <NUM_LIT:0> and current_timeout >= <NUM_LIT:0> : <EOL> self . _timeout_time . append ( current_timeout ) <EOL> else : <EOL> _timeout_time = time . time ( ) + timeout <EOL> if current_timeout < <NUM_LIT:0> : <EOL> self . _timeout_time . append ( _timeout_time ) <EOL> else : <EOL> self . _timeout_time . append ( min ( _timeout_time , current_timeout ) ) <EOL> def pop ( self ) : <EOL> assert len ( self . _timeout_time ) > <NUM_LIT:1> , "<STR_LIT>" <EOL> self . _timeout_time . pop ( ) <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def __exit__ ( self , type , value , traceback ) : <EOL> self . pop ( ) <EOL> class Timeout : <EOL> """<STR_LIT>""" <EOL> _local = TaskLocal ( ) <EOL> @ classmethod <EOL> def push ( cls , timeout ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> t = cls . _local . t <EOL> except AttributeError : <EOL> t = _Timeout ( ) <EOL> cls . _local . t = t <EOL> t . push ( timeout ) <EOL> return t <EOL> @ classmethod <EOL> def pop ( cls ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> t = cls . _local . t <EOL> t . pop ( ) <EOL> except AttributeError : <EOL> assert False , "<STR_LIT>" <EOL> @ classmethod <EOL> def current ( cls ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> t = cls . _local . t <EOL> except AttributeError : <EOL> t = None <EOL> if t is None : <EOL> return - <NUM_LIT:1> <EOL> else : <EOL> return t . current ( ) </s>
