<s> import sys <EOL> import functools <EOL> import operator <EOL> import weakref <EOL> import inspect <EOL> PY2 = sys . version_info [ <NUM_LIT:0> ] == <NUM_LIT:2> <EOL> PY3 = sys . version_info [ <NUM_LIT:0> ] == <NUM_LIT:3> <EOL> if PY3 : <EOL> string_types = str , <EOL> else : <EOL> string_types = basestring , <EOL> def with_metaclass ( meta , * bases ) : <EOL> """<STR_LIT>""" <EOL> return meta ( "<STR_LIT>" , bases , { } ) <EOL> class _ObjectProxyMethods ( object ) : <EOL> @ property <EOL> def __module__ ( self ) : <EOL> return self . __wrapped__ . __module__ <EOL> @ __module__ . setter <EOL> def __module__ ( self , value ) : <EOL> self . __wrapped__ . __module__ = value <EOL> @ property <EOL> def __doc__ ( self ) : <EOL> return self . __wrapped__ . __doc__ <EOL> @ __doc__ . setter <EOL> def __doc__ ( self , value ) : <EOL> self . __wrapped__ . __doc__ = value <EOL> @ property <EOL> def __dict__ ( self ) : <EOL> return self . __wrapped__ . __dict__ <EOL> @ property <EOL> def __weakref__ ( self ) : <EOL> return self . __wrapped__ . __weakref__ <EOL> class _ObjectProxyMetaType ( type ) : <EOL> def __new__ ( cls , name , bases , dictionary ) : <EOL> dictionary . update ( vars ( _ObjectProxyMethods ) ) <EOL> return type . __new__ ( cls , name , bases , dictionary ) <EOL> class ObjectProxy ( with_metaclass ( _ObjectProxyMetaType ) ) : <EOL> __slots__ = '<STR_LIT>' <EOL> def __init__ ( self , wrapped ) : <EOL> object . __setattr__ ( self , '<STR_LIT>' , wrapped ) <EOL> try : <EOL> object . __setattr__ ( self , '<STR_LIT>' , wrapped . __qualname__ ) <EOL> except AttributeError : <EOL> pass <EOL> @ property <EOL> def __name__ ( self ) : <EOL> return self . __wrapped__ . __name__ <EOL> @ __name__ . setter <EOL> def __name__ ( self , value ) : <EOL> self . __wrapped__ . __name__ = value <EOL> @ property <EOL> def __class__ ( self ) : <EOL> return self . __wrapped__ . __class__ <EOL> @ __class__ . setter <EOL> def __class__ ( self , value ) : <EOL> self . __wrapped__ . __class__ = value <EOL> @ property <EOL> def __annotations__ ( self ) : <EOL> return self . __wrapped__ . __anotations__ <EOL> @ __annotations__ . setter <EOL> def __annotations__ ( self , value ) : <EOL> self . __wrapped__ . __annotations__ = value <EOL> def __dir__ ( self ) : <EOL> return dir ( self . __wrapped__ ) <EOL> def __str__ ( self ) : <EOL> return str ( self . __wrapped__ ) <EOL> if PY3 : <EOL> def __bytes__ ( self ) : <EOL> return bytes ( self . __wrapped__ ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> type ( self ) . __name__ , id ( self ) , <EOL> type ( self . __wrapped__ ) . __name__ , <EOL> id ( self . __wrapped__ ) ) <EOL> def __reversed__ ( self ) : <EOL> return reversed ( self . __wrapped__ ) <EOL> if PY3 : <EOL> def __round__ ( self ) : <EOL> return round ( self . __wrapped__ ) <EOL> def __lt__ ( self , other ) : <EOL> return self . __wrapped__ < other <EOL> def __le__ ( self , other ) : <EOL> return self . __wrapped__ <= other <EOL> def __eq__ ( self , other ) : <EOL> return self . __wrapped__ == other <EOL> def __ne__ ( self , other ) : <EOL> return self . __wrapped__ != other <EOL> def __gt__ ( self , other ) : <EOL> return self . __wrapped__ > other <EOL> def __ge__ ( self , other ) : <EOL> return self . __wrapped__ >= other <EOL> def __hash__ ( self ) : <EOL> return hash ( self . __wrapped__ ) <EOL> def __nonzero__ ( self ) : <EOL> return bool ( self . __wrapped__ ) <EOL> def __bool__ ( self ) : <EOL> return bool ( self . __wrapped__ ) <EOL> def __setattr__ ( self , name , value ) : <EOL> if name . startswith ( '<STR_LIT>' ) : <EOL> object . __setattr__ ( self , name , value ) <EOL> elif name == '<STR_LIT>' : <EOL> object . __setattr__ ( self , name , value ) <EOL> try : <EOL> object . __delattr__ ( self , '<STR_LIT>' ) <EOL> except AttributeError : <EOL> pass <EOL> try : <EOL> object . __setattr__ ( self , '<STR_LIT>' , value . __qualname__ ) <EOL> except AttributeError : <EOL> pass <EOL> elif name == '<STR_LIT>' : <EOL> setattr ( self . __wrapped__ , name , value ) <EOL> object . __setattr__ ( self , name , value ) <EOL> elif hasattr ( type ( self ) , name ) : <EOL> object . __setattr__ ( self , name , value ) <EOL> else : <EOL> setattr ( self . __wrapped__ , name , value ) <EOL> def __getattr__ ( self , name ) : <EOL> if name == '<STR_LIT>' : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return getattr ( self . __wrapped__ , name ) <EOL> def __delattr__ ( self , name ) : <EOL> if name . startswith ( '<STR_LIT>' ) : <EOL> object . __delattr__ ( self , name ) <EOL> elif name == '<STR_LIT>' : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> elif name == '<STR_LIT>' : <EOL> object . __delattr__ ( self , name ) <EOL> delattr ( self . __wrapped__ , name ) <EOL> elif hasattr ( type ( self ) , name ) : <EOL> object . __delattr__ ( self , name ) <EOL> else : <EOL> delattr ( self . __wrapped__ , name ) <EOL> def __add__ ( self , other ) : <EOL> return self . __wrapped__ + other <EOL> def __sub__ ( self , other ) : <EOL> return self . __wrapped__ - other <EOL> def __mul__ ( self , other ) : <EOL> return self . __wrapped__ * other <EOL> def __div__ ( self , other ) : <EOL> return operator . div ( self . __wrapped__ , other ) <EOL> def __truediv__ ( self , other ) : <EOL> return operator . truediv ( self . __wrapped__ , other ) <EOL> def __floordiv__ ( self , other ) : <EOL> return self . __wrapped__ // other <EOL> def __mod__ ( self , other ) : <EOL> return self . __wrapped__ % other <EOL> def __divmod__ ( self , other ) : <EOL> return divmod ( self . __wrapped__ , other ) <EOL> def __pow__ ( self , other , * args ) : <EOL> return pow ( self . __wrapped__ , other , * args ) <EOL> def __lshift__ ( self , other ) : <EOL> return self . __wrapped__ << other <EOL> def __rshift__ ( self , other ) : <EOL> return self . __wrapped__ >> other <EOL> def __and__ ( self , other ) : <EOL> return self . __wrapped__ & other <EOL> def __xor__ ( self , other ) : <EOL> return self . __wrapped__ ^ other <EOL> def __or__ ( self , other ) : <EOL> return self . __wrapped__ | other <EOL> def __radd__ ( self , other ) : <EOL> return other + self . __wrapped__ <EOL> def __rsub__ ( self , other ) : <EOL> return other - self . __wrapped__ <EOL> def __rmul__ ( self , other ) : <EOL> return other * self . __wrapped__ <EOL> def __rdiv__ ( self , other ) : <EOL> return operator . div ( other , self . __wrapped__ ) <EOL> def __rtruediv__ ( self , other ) : <EOL> return operator . truediv ( other , self . __wrapped__ ) <EOL> def __rfloordiv__ ( self , other ) : <EOL> return other // self . __wrapped__ <EOL> def __rmod__ ( self , other ) : <EOL> return other % self . __wrapped__ <EOL> def __rdivmod__ ( self , other ) : <EOL> return divmod ( other , self . __wrapped__ ) <EOL> def __rpow__ ( self , other , * args ) : <EOL> return pow ( other , self . __wrapped__ , * args ) <EOL> def __rlshift__ ( self , other ) : <EOL> return other << self . __wrapped__ <EOL> def __rrshift__ ( self , other ) : <EOL> return other >> self . __wrapped__ <EOL> def __rand__ ( self , other ) : <EOL> return other & self . __wrapped__ <EOL> def __rxor__ ( self , other ) : <EOL> return other ^ self . __wrapped__ <EOL> def __ror__ ( self , other ) : <EOL> return other | self . __wrapped__ <EOL> def __iadd__ ( self , other ) : <EOL> self . __wrapped__ += other <EOL> return self <EOL> def __isub__ ( self , other ) : <EOL> self . __wrapped__ -= other <EOL> return self <EOL> def __imul__ ( self , other ) : <EOL> self . __wrapped__ *= other <EOL> return self <EOL> def __idiv__ ( self , other ) : <EOL> self . __wrapped__ = operator . idiv ( self . __wrapped__ , other ) <EOL> return self <EOL> def __itruediv__ ( self , other ) : <EOL> self . __wrapped__ = operator . itruediv ( self . __wrapped__ , other ) <EOL> return self <EOL> def __ifloordiv__ ( self , other ) : <EOL> self . __wrapped__ //= other <EOL> return self <EOL> def __imod__ ( self , other ) : <EOL> self . __wrapped__ %= other <EOL> return self <EOL> def __ipow__ ( self , other ) : <EOL> self . __wrapped__ **= other <EOL> return self <EOL> def __ilshift__ ( self , other ) : <EOL> self . __wrapped__ <<= other <EOL> return self <EOL> def __irshift__ ( self , other ) : <EOL> self . __wrapped__ >>= other <EOL> return self <EOL> def __iand__ ( self , other ) : <EOL> self . __wrapped__ &= other <EOL> return self <EOL> def __ixor__ ( self , other ) : <EOL> self . __wrapped__ ^= other <EOL> return self <EOL> def __ior__ ( self , other ) : <EOL> self . __wrapped__ |= other <EOL> return self <EOL> def __neg__ ( self ) : <EOL> return - self . __wrapped__ <EOL> def __pos__ ( self ) : <EOL> return + self . __wrapped__ <EOL> def __abs__ ( self ) : <EOL> return abs ( self . __wrapped__ ) <EOL> def __invert__ ( self ) : <EOL> return ~ self . __wrapped__ <EOL> def __int__ ( self ) : <EOL> return int ( self . __wrapped__ ) <EOL> def __long__ ( self ) : <EOL> return long ( self . __wrapped__ ) <EOL> def __float__ ( self ) : <EOL> return float ( self . __wrapped__ ) <EOL> def __oct__ ( self ) : <EOL> return oct ( self . __wrapped__ ) <EOL> def __hex__ ( self ) : <EOL> return hex ( self . __wrapped__ ) <EOL> def __index__ ( self ) : <EOL> return operator . index ( self . __wrapped__ ) <EOL> def __len__ ( self ) : <EOL> return len ( self . __wrapped__ ) <EOL> def __contains__ ( self , value ) : <EOL> return value in self . __wrapped__ <EOL> def __getitem__ ( self , key ) : <EOL> return self . __wrapped__ [ key ] <EOL> def __setitem__ ( self , key , value ) : <EOL> self . __wrapped__ [ key ] = value <EOL> def __delitem__ ( self , key ) : <EOL> del self . __wrapped__ [ key ] <EOL> def __getslice__ ( self , i , j ) : <EOL> return self . __wrapped__ [ i : j ] <EOL> def __setslice__ ( self , i , j , value ) : <EOL> self . __wrapped__ [ i : j ] = value <EOL> def __delslice__ ( self , i , j ) : <EOL> del self . __wrapped__ [ i : j ] <EOL> def __enter__ ( self ) : <EOL> return self . __wrapped__ . __enter__ ( ) <EOL> def __exit__ ( self , * args , ** kwargs ) : <EOL> return self . __wrapped__ . __exit__ ( * args , ** kwargs ) <EOL> def __iter__ ( self ) : <EOL> return iter ( self . __wrapped__ ) <EOL> class CallableObjectProxy ( ObjectProxy ) : <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> return self . __wrapped__ ( * args , ** kwargs ) <EOL> class _FunctionWrapperBase ( ObjectProxy ) : <EOL> __slots__ = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __init__ ( self , wrapped , instance , wrapper , enabled = None , <EOL> binding = '<STR_LIT>' , parent = None ) : <EOL> super ( _FunctionWrapperBase , self ) . __init__ ( wrapped ) <EOL> object . __setattr__ ( self , '<STR_LIT>' , instance ) <EOL> object . __setattr__ ( self , '<STR_LIT>' , wrapper ) <EOL> object . __setattr__ ( self , '<STR_LIT>' , enabled ) <EOL> object . __setattr__ ( self , '<STR_LIT>' , binding ) <EOL> object . __setattr__ ( self , '<STR_LIT>' , parent ) <EOL> def __get__ ( self , instance , owner ) : <EOL> if self . _self_parent is None : <EOL> if not inspect . isclass ( self . __wrapped__ ) : <EOL> descriptor = self . __wrapped__ . __get__ ( instance , owner ) <EOL> return self . __bound_function_wrapper__ ( descriptor , instance , <EOL> self . _self_wrapper , self . _self_enabled , <EOL> self . _self_binding , self ) <EOL> return self <EOL> if self . _self_instance is None and self . _self_binding == '<STR_LIT>' : <EOL> descriptor = self . _self_parent . __wrapped__ . __get__ ( <EOL> instance , owner ) <EOL> return self . _self_parent . __bound_function_wrapper__ ( <EOL> descriptor , instance , self . _self_wrapper , <EOL> self . _self_enabled , self . _self_binding , <EOL> self . _self_parent ) <EOL> return self <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> if self . _self_enabled is not None : <EOL> if callable ( self . _self_enabled ) : <EOL> if not self . _self_enabled ( ) : <EOL> return self . __wrapped__ ( * args , ** kwargs ) <EOL> elif not self . _self_enabled : <EOL> return self . __wrapped__ ( * args , ** kwargs ) <EOL> if self . _self_binding == '<STR_LIT>' : <EOL> if self . _self_instance is None : <EOL> instance = getattr ( self . __wrapped__ , '<STR_LIT>' , None ) <EOL> if instance is not None : <EOL> return self . _self_wrapper ( self . __wrapped__ , instance , <EOL> args , kwargs ) <EOL> return self . _self_wrapper ( self . __wrapped__ , self . _self_instance , <EOL> args , kwargs ) <EOL> class BoundFunctionWrapper ( _FunctionWrapperBase ) : <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> if self . _self_enabled is not None : <EOL> if callable ( self . _self_enabled ) : <EOL> if not self . _self_enabled ( ) : <EOL> return self . __wrapped__ ( * args , ** kwargs ) <EOL> elif not self . _self_enabled : <EOL> return self . __wrapped__ ( * args , ** kwargs ) <EOL> if self . _self_binding == '<STR_LIT>' : <EOL> if self . _self_instance is None : <EOL> if not args : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> instance , args = args [ <NUM_LIT:0> ] , args [ <NUM_LIT:1> : ] <EOL> wrapped = functools . partial ( self . __wrapped__ , instance ) <EOL> return self . _self_wrapper ( wrapped , instance , args , kwargs ) <EOL> return self . _self_wrapper ( self . __wrapped__ , self . _self_instance , <EOL> args , kwargs ) <EOL> else : <EOL> instance = getattr ( self . __wrapped__ , '<STR_LIT>' , None ) <EOL> return self . _self_wrapper ( self . __wrapped__ , instance , args , <EOL> kwargs ) <EOL> class FunctionWrapper ( _FunctionWrapperBase ) : <EOL> __bound_function_wrapper__ = BoundFunctionWrapper <EOL> def __init__ ( self , wrapped , wrapper , enabled = None ) : <EOL> if isinstance ( wrapped , classmethod ) : <EOL> binding = '<STR_LIT>' <EOL> elif isinstance ( wrapped , staticmethod ) : <EOL> binding = '<STR_LIT>' <EOL> elif hasattr ( wrapped , '<STR_LIT>' ) : <EOL> if inspect . isclass ( wrapped . __self__ ) : <EOL> binding = '<STR_LIT>' <EOL> else : <EOL> binding = '<STR_LIT>' <EOL> else : <EOL> binding = '<STR_LIT>' <EOL> super ( FunctionWrapper , self ) . __init__ ( wrapped , None , wrapper , <EOL> enabled , binding ) <EOL> try : <EOL> from . _wrappers import ( ObjectProxy , CallableObjectProxy , FunctionWrapper , <EOL> BoundFunctionWrapper , _FunctionWrapperBase ) <EOL> except ImportError : <EOL> pass <EOL> def resolve_path ( module , name ) : <EOL> if isinstance ( module , string_types ) : <EOL> __import__ ( module ) <EOL> module = sys . modules [ module ] <EOL> parent = module <EOL> path = name . split ( '<STR_LIT:.>' ) <EOL> attribute = path [ <NUM_LIT:0> ] <EOL> original = getattr ( parent , attribute ) <EOL> for attribute in path [ <NUM_LIT:1> : ] : <EOL> parent = original <EOL> if inspect . isclass ( original ) : <EOL> for cls in inspect . getmro ( original ) : <EOL> if attribute in vars ( original ) : <EOL> original = vars ( original ) [ attribute ] <EOL> break <EOL> else : <EOL> original = getattr ( original , attribute ) <EOL> else : <EOL> original = getattr ( original , attribute ) <EOL> return ( parent , attribute , original ) <EOL> def apply_patch ( parent , attribute , replacement ) : <EOL> setattr ( parent , attribute , replacement ) <EOL> def wrap_object ( module , name , factory , args = ( ) , kwargs = { } ) : <EOL> ( parent , attribute , original ) = resolve_path ( module , name ) <EOL> wrapper = factory ( original , * args , ** kwargs ) <EOL> apply_patch ( parent , attribute , wrapper ) <EOL> return wrapper <EOL> class AttributeWrapper ( object ) : <EOL> def __init__ ( self , attribute , factory , args , kwargs ) : <EOL> self . attribute = attribute <EOL> self . factory = factory <EOL> self . args = args <EOL> self . kwargs = kwargs <EOL> def __get__ ( self , instance , owner ) : <EOL> value = instance . __dict__ [ self . attribute ] <EOL> return self . factory ( value , * self . args , ** self . kwargs ) <EOL> def __set__ ( self , instance , value ) : <EOL> instance . __dict__ [ self . attribute ] = value <EOL> def __delete__ ( self , instance ) : <EOL> del instance . __dict__ [ self . attribute ] <EOL> def wrap_object_attribute ( module , name , factory , args = ( ) , kwargs = { } ) : <EOL> path , attribute = name . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> parent = resolve_path ( module , path ) [ <NUM_LIT:2> ] <EOL> wrapper = AttributeWrapper ( attribute , factory , args , kwargs ) <EOL> apply_patch ( parent , attribute , wrapper ) <EOL> return wrapper <EOL> def function_wrapper ( wrapper ) : <EOL> def _wrapper ( wrapped , instance , args , kwargs ) : <EOL> target_wrapped = args [ <NUM_LIT:0> ] <EOL> if instance is None : <EOL> target_wrapper = wrapper <EOL> elif inspect . isclass ( instance ) : <EOL> target_wrapper = wrapper . __get__ ( None , instance ) <EOL> else : <EOL> target_wrapper = wrapper . __get__ ( instance , type ( instance ) ) <EOL> return FunctionWrapper ( target_wrapped , target_wrapper ) <EOL> return FunctionWrapper ( wrapper , _wrapper ) <EOL> def wrap_function_wrapper ( module , name , wrapper ) : <EOL> return wrap_object ( module , name , FunctionWrapper , ( wrapper , ) ) <EOL> def patch_function_wrapper ( module , name ) : <EOL> def _wrapper ( wrapper ) : <EOL> return wrap_object ( module , name , FunctionWrapper , ( wrapper , ) ) <EOL> return _wrapper <EOL> def transient_function_wrapper ( module , name ) : <EOL> def _decorator ( wrapper ) : <EOL> def _wrapper ( wrapped , instance , args , kwargs ) : <EOL> target_wrapped = args [ <NUM_LIT:0> ] <EOL> if instance is None : <EOL> target_wrapper = wrapper <EOL> elif inspect . isclass ( instance ) : <EOL> target_wrapper = wrapper . __get__ ( None , instance ) <EOL> else : <EOL> target_wrapper = wrapper . __get__ ( instance , type ( instance ) ) <EOL> def _execute ( wrapped , instance , args , kwargs ) : <EOL> ( parent , attribute , original ) = resolve_path ( module , name ) <EOL> replacement = FunctionWrapper ( original , target_wrapper ) <EOL> setattr ( parent , attribute , replacement ) <EOL> try : <EOL> return wrapped ( * args , ** kwargs ) <EOL> finally : <EOL> setattr ( parent , attribute , original ) <EOL> return FunctionWrapper ( target_wrapped , _execute ) <EOL> return FunctionWrapper ( wrapper , _wrapper ) <EOL> return _decorator <EOL> def _weak_function_proxy_callback ( ref , proxy , callback ) : <EOL> if proxy . _self_expired : <EOL> return <EOL> proxy . _self_expired = True <EOL> if callback is not None : <EOL> callback ( proxy ) <EOL> class WeakFunctionProxy ( ObjectProxy ) : <EOL> __slots__ = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __init__ ( self , wrapped , callback = None ) : <EOL> _callback = callback and functools . partial ( <EOL> _weak_function_proxy_callback , proxy = self , <EOL> callback = callback ) <EOL> self . _self_expired = False <EOL> if isinstance ( wrapped , _FunctionWrapperBase ) : <EOL> self . _self_instance = weakref . ref ( wrapped . _self_instance , <EOL> _callback ) <EOL> if wrapped . _self_parent is not None : <EOL> super ( WeakFunctionProxy , self ) . __init__ ( <EOL> weakref . proxy ( wrapped . _self_parent , _callback ) ) <EOL> else : <EOL> super ( WeakFunctionProxy , self ) . __init__ ( <EOL> weakref . proxy ( wrapped , _callback ) ) <EOL> return <EOL> try : <EOL> self . _self_instance = weakref . ref ( wrapped . __self__ , _callback ) <EOL> super ( WeakFunctionProxy , self ) . __init__ ( <EOL> weakref . proxy ( wrapped . __func__ , _callback ) ) <EOL> except AttributeError : <EOL> self . _self_instance = None <EOL> super ( WeakFunctionProxy , self ) . __init__ ( <EOL> weakref . proxy ( wrapped , _callback ) ) <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> instance = self . _self_instance and self . _self_instance ( ) <EOL> function = self . __wrapped__ and self . __wrapped__ <EOL> if instance is None : <EOL> return self . __wrapped__ ( * args , ** kwargs ) <EOL> return function . __get__ ( instance , type ( instance ) ) ( * args , ** kwargs ) </s>
