<s> """<STR_LIT>""" <EOL> import threading <EOL> _CLASS_LOCKS = { } <EOL> _CLASS_LOCKS_LOCK = threading . Lock ( ) <EOL> _INSTANCES = { } <EOL> class Error ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class AlreadyHasSingletonMethodError ( Error ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cls ) : <EOL> Error . __init__ ( self ) <EOL> self . cls = cls <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % self . cls <EOL> class NotConstructedError ( Error ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cls ) : <EOL> Error . __init__ ( self ) <EOL> self . cls = cls <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % self . cls <EOL> class ConstructorCalledAgainError ( Error ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cls , args , kws ) : <EOL> Error . __init__ ( self ) <EOL> self . cls = cls <EOL> self . args = args <EOL> self . kws = kws <EOL> def __str__ ( self ) : <EOL> return ( '<STR_LIT>' <EOL> % ( self . cls , self . args , self . kws ) ) <EOL> def _GetClassLock ( cls ) : <EOL> """<STR_LIT>""" <EOL> with _CLASS_LOCKS_LOCK : <EOL> if cls not in _CLASS_LOCKS : <EOL> _CLASS_LOCKS [ cls ] = threading . Lock ( ) <EOL> return _CLASS_LOCKS [ cls ] <EOL> def Singleton ( cls ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( cls , '<STR_LIT>' ) : <EOL> raise AlreadyHasSingletonMethodError ( cls ) <EOL> class _Singleton ( cls ) : <EOL> def __init__ ( self , * args , ** kws ) : <EOL> class_lock = _GetClassLock ( cls ) <EOL> with class_lock : <EOL> if cls not in _INSTANCES : <EOL> cls . __init__ ( self , * args , ** kws ) <EOL> _INSTANCES [ cls ] = self <EOL> else : <EOL> raise ConstructorCalledAgainError ( cls , args , kws ) <EOL> @ staticmethod <EOL> def Singleton ( ) : <EOL> class_lock = _GetClassLock ( cls ) <EOL> with class_lock : <EOL> if cls not in _INSTANCES : <EOL> raise NotConstructedError ( cls ) <EOL> else : <EOL> return _INSTANCES [ cls ] <EOL> return _Singleton </s>
