<s> """<STR_LIT>""" <EOL> import sys <EOL> import re <EOL> import copy <EOL> from threading import Condition , Lock , current_thread <EOL> from contextlib import contextmanager <EOL> from time import time <EOL> PY3K = sys . version_info >= ( <NUM_LIT:3> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> if PY3K : <EOL> import html . entities as htmlentitydefs <EOL> import _thread as thread <EOL> unichr = chr <EOL> import builtins <EOL> exec_ = getattr ( builtins , "<STR_LIT>" ) <EOL> def reraise ( tp , value , tb = None ) : <EOL> if value . __traceback__ is not tb : <EOL> raise value . with_traceback ( tb ) <EOL> raise value <EOL> else : <EOL> import htmlentitydefs <EOL> import thread <EOL> def exec_ ( _code_ , _globs_ = None , _locs_ = None ) : <EOL> """<STR_LIT>""" <EOL> if _globs_ is None : <EOL> frame = sys . _getframe ( <NUM_LIT:1> ) <EOL> _globs_ = frame . f_globals <EOL> if _locs_ is None : <EOL> _locs_ = frame . f_locals <EOL> del frame <EOL> elif _locs_ is None : <EOL> _locs_ = _globs_ <EOL> exec ( """<STR_LIT>""" ) <EOL> exec_ ( """<STR_LIT>""" ) <EOL> def entities_unescape ( text ) : <EOL> def fixup ( m ) : <EOL> text = m . group ( <NUM_LIT:0> ) <EOL> if text [ : <NUM_LIT:2> ] == "<STR_LIT>" : <EOL> try : <EOL> if text [ : <NUM_LIT:3> ] == "<STR_LIT>" : <EOL> return unichr ( int ( text [ <NUM_LIT:3> : - <NUM_LIT:1> ] , <NUM_LIT:16> ) ) <EOL> else : <EOL> return unichr ( int ( text [ <NUM_LIT:2> : - <NUM_LIT:1> ] ) ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> try : <EOL> if text [ <NUM_LIT:1> : - <NUM_LIT:1> ] == "<STR_LIT>" : <EOL> text = "<STR_LIT>" <EOL> elif text [ <NUM_LIT:1> : - <NUM_LIT:1> ] == "<STR_LIT>" : <EOL> text = "<STR_LIT>" <EOL> elif text [ <NUM_LIT:1> : - <NUM_LIT:1> ] == "<STR_LIT>" : <EOL> text = "<STR_LIT>" <EOL> else : <EOL> text = unichr ( htmlentitydefs . name2codepoint [ text [ <NUM_LIT:1> : - <NUM_LIT:1> ] ] ) <EOL> except KeyError : <EOL> pass <EOL> return text <EOL> return re . sub ( "<STR_LIT>" , fixup , text ) <EOL> class Singleton ( object ) : <EOL> def __init__ ( self , decorated ) : <EOL> decorated . __lock_obj = thread . allocate_lock ( ) <EOL> decorated . __instance = None <EOL> self . __decorated = decorated <EOL> def instance ( self ) : <EOL> with self . __decorated . __lock_obj : <EOL> if self . __decorated . __instance is None : <EOL> self . __decorated . __instance = self . __decorated ( ) <EOL> return self . __decorated . __instance <EOL> def __call__ ( self ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __instancecheck__ ( self , inst ) : <EOL> return isinstance ( inst , self . __decorated ) <EOL> class RWLock ( object ) : <EOL> def __init__ ( self , lock = None ) : <EOL> """<STR_LIT>""" <EOL> if lock is None : <EOL> self . __condition = Condition ( Lock ( ) ) <EOL> else : <EOL> self . __condition = Condition ( lock ) <EOL> self . __writer = None <EOL> self . __upgradewritercount = <NUM_LIT:0> <EOL> self . __pendingwriters = [ ] <EOL> self . __readers = { } <EOL> def acquireRead ( self , blocking = True , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> if not blocking : <EOL> endtime = - <NUM_LIT:1> <EOL> elif timeout is not None : <EOL> endtime = time ( ) + timeout <EOL> else : <EOL> endtime = None <EOL> me = current_thread ( ) <EOL> self . __condition . acquire ( ) <EOL> try : <EOL> if self . __writer is me : <EOL> self . __writercount += <NUM_LIT:1> <EOL> return <EOL> while True : <EOL> if self . __writer is None : <EOL> if self . __upgradewritercount or self . __pendingwriters : <EOL> if me in self . __readers : <EOL> self . __readers [ me ] += <NUM_LIT:1> <EOL> return <EOL> else : <EOL> self . __readers [ me ] = self . __readers . get ( me , <NUM_LIT:0> ) + <NUM_LIT:1> <EOL> return <EOL> if timeout is not None : <EOL> remaining = endtime - time ( ) <EOL> if remaining <= <NUM_LIT:0> : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> self . __condition . wait ( remaining ) <EOL> else : <EOL> self . __condition . wait ( ) <EOL> finally : <EOL> self . __condition . release ( ) <EOL> def acquireWrite ( self , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> if timeout is not None : <EOL> endtime = time ( ) + timeout <EOL> me , upgradewriter = current_thread ( ) , False <EOL> self . __condition . acquire ( ) <EOL> try : <EOL> if self . __writer is me : <EOL> self . __writercount += <NUM_LIT:1> <EOL> return <EOL> elif me in self . __readers : <EOL> if self . __upgradewritercount : <EOL> if timeout is not None : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> upgradewriter = True <EOL> self . __upgradewritercount = self . __readers . pop ( me ) <EOL> else : <EOL> self . __pendingwriters . append ( me ) <EOL> while True : <EOL> if not self . __readers and self . __writer is None : <EOL> if self . __upgradewritercount : <EOL> if upgradewriter : <EOL> self . __writer = me <EOL> self . __writercount = self . __upgradewritercount + <NUM_LIT:1> <EOL> self . __upgradewritercount = <NUM_LIT:0> <EOL> return <EOL> elif self . __pendingwriters [ <NUM_LIT:0> ] is me : <EOL> self . __writer = me <EOL> self . __writercount = <NUM_LIT:1> <EOL> self . __pendingwriters = self . __pendingwriters [ <NUM_LIT:1> : ] <EOL> return <EOL> if timeout is not None : <EOL> remaining = endtime - time ( ) <EOL> if remaining <= <NUM_LIT:0> : <EOL> if upgradewriter : <EOL> self . __readers [ me ] = self . __upgradewritercount <EOL> self . __upgradewritercount = <NUM_LIT:0> <EOL> else : <EOL> self . __pendingwriters . remove ( me ) <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> self . __condition . wait ( remaining ) <EOL> else : <EOL> self . __condition . wait ( ) <EOL> finally : <EOL> self . __condition . release ( ) <EOL> def release ( self ) : <EOL> """<STR_LIT>""" <EOL> me = current_thread ( ) <EOL> self . __condition . acquire ( ) <EOL> try : <EOL> if self . __writer is me : <EOL> self . __writercount -= <NUM_LIT:1> <EOL> if not self . __writercount : <EOL> self . __writer = None <EOL> self . __condition . notifyAll ( ) <EOL> elif me in self . __readers : <EOL> self . __readers [ me ] -= <NUM_LIT:1> <EOL> if not self . __readers [ me ] : <EOL> del self . __readers [ me ] <EOL> if not self . __readers : <EOL> self . __condition . notifyAll ( ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> finally : <EOL> self . __condition . release ( ) <EOL> @ property <EOL> @ contextmanager <EOL> def readlock ( self ) : <EOL> self . acquireRead ( ) <EOL> try : <EOL> yield <EOL> finally : <EOL> self . release ( ) <EOL> @ property <EOL> @ contextmanager <EOL> def writelock ( self ) : <EOL> self . acquireWrite ( ) <EOL> try : <EOL> yield <EOL> finally : <EOL> self . release ( ) <EOL> class Future ( object ) : <EOL> def __init__ ( self , func , * args , ** kwargs ) : <EOL> self . __done = False <EOL> self . __result = None <EOL> self . __cond = Condition ( ) <EOL> self . __func = func <EOL> self . __args = args <EOL> self . __kwargs = kwargs <EOL> self . __except = None <EOL> def __call__ ( self ) : <EOL> with self . __cond : <EOL> try : <EOL> self . __result = self . __func ( * self . __args , ** self . __kwargs ) <EOL> except : <EOL> self . __result = None <EOL> self . __except = sys . exc_info ( ) <EOL> self . __done = True <EOL> self . __cond . notify ( ) <EOL> def result ( self ) : <EOL> with self . __cond : <EOL> while not self . __done : <EOL> self . __cond . wait ( ) <EOL> if self . __except : <EOL> exc = self . __except <EOL> reraise ( exc [ <NUM_LIT:0> ] , exc [ <NUM_LIT:1> ] , exc [ <NUM_LIT:2> ] ) <EOL> result = self . __result <EOL> return copy . deepcopy ( result ) </s>
