<s> __all__ = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> __version__ = '<STR_LIT>' <EOL> __unittest = True <EOL> import sys <EOL> try : <EOL> import inspect <EOL> except ImportError : <EOL> inspect = None <EOL> try : <EOL> BaseException <EOL> except NameError : <EOL> BaseException = Exception <EOL> try : <EOL> from functools import wraps <EOL> except ImportError : <EOL> def wraps ( original ) : <EOL> def inner ( f ) : <EOL> f . __name__ = original . __name__ <EOL> f . __doc__ = original . __doc__ <EOL> f . __module__ = original . __module__ <EOL> return f <EOL> return inner <EOL> try : <EOL> unicode <EOL> except NameError : <EOL> basestring = unicode = str <EOL> try : <EOL> long <EOL> except NameError : <EOL> long = int <EOL> try : <EOL> _isidentifier = str . isidentifier <EOL> except AttributeError : <EOL> import keyword <EOL> import re <EOL> regex = re . compile ( r'<STR_LIT>' , re . I ) <EOL> def _isidentifier ( string ) : <EOL> if string in keyword . kwlist : <EOL> return False <EOL> return regex . match ( string ) <EOL> inPy3k = sys . version_info [ <NUM_LIT:0> ] == <NUM_LIT:3> <EOL> self = '<STR_LIT>' <EOL> builtin = '<STR_LIT>' <EOL> if inPy3k : <EOL> self = '<STR_LIT>' <EOL> builtin = '<STR_LIT>' <EOL> _super = super <EOL> FILTER_DIR = True <EOL> def _getsignature ( func , skipfirst ) : <EOL> if inspect is None : <EOL> raise ImportError ( '<STR_LIT>' ) <EOL> if inspect . isclass ( func ) : <EOL> func = func . __init__ <EOL> skipfirst = True <EOL> elif not ( inspect . ismethod ( func ) or inspect . isfunction ( func ) ) : <EOL> func = func . __call__ <EOL> regargs , varargs , varkwargs , defaults = inspect . getargspec ( func ) <EOL> if getattr ( func , self , None ) is not None : <EOL> regargs = regargs [ <NUM_LIT:1> : ] <EOL> _msg = "<STR_LIT>" <EOL> assert '<STR_LIT>' not in regargs , _msg <EOL> if varargs is not None : <EOL> assert '<STR_LIT>' not in varargs , _msg <EOL> if varkwargs is not None : <EOL> assert '<STR_LIT>' not in varkwargs , _msg <EOL> if skipfirst : <EOL> regargs = regargs [ <NUM_LIT:1> : ] <EOL> signature = inspect . formatargspec ( regargs , varargs , varkwargs , defaults , <EOL> formatvalue = lambda value : "<STR_LIT>" ) <EOL> return signature [ <NUM_LIT:1> : - <NUM_LIT:1> ] , func <EOL> def _getsignature2 ( func , skipfirst ) : <EOL> if inspect is None : <EOL> raise ImportError ( '<STR_LIT>' ) <EOL> if isinstance ( func , ClassTypes ) : <EOL> try : <EOL> func = func . __init__ <EOL> except AttributeError : <EOL> return <EOL> skipfirst = True <EOL> elif not isinstance ( func , FunctionTypes ) : <EOL> func = func . __call__ <EOL> try : <EOL> regargs , varargs , varkwargs , defaults = inspect . getargspec ( func ) <EOL> except TypeError : <EOL> return <EOL> if getattr ( func , self , None ) is not None : <EOL> regargs = regargs [ <NUM_LIT:1> : ] <EOL> if skipfirst : <EOL> regargs = regargs [ <NUM_LIT:1> : ] <EOL> signature = inspect . formatargspec ( regargs , varargs , varkwargs , defaults , <EOL> formatvalue = lambda value : "<STR_LIT>" ) <EOL> return signature [ <NUM_LIT:1> : - <NUM_LIT:1> ] , func <EOL> def _check_signature ( func , mock , skipfirst ) : <EOL> if not _callable ( func ) : <EOL> return <EOL> result = _getsignature2 ( func , skipfirst ) <EOL> if result is None : <EOL> return <EOL> signature , func = result <EOL> src = "<STR_LIT>" % signature <EOL> checksig = eval ( src , { } ) <EOL> _copy_func_details ( func , checksig ) <EOL> type ( mock ) . _mock_check_sig = checksig <EOL> def _copy_func_details ( func , funcopy ) : <EOL> funcopy . __name__ = func . __name__ <EOL> funcopy . __doc__ = func . __doc__ <EOL> funcopy . __module__ = func . __module__ <EOL> if not inPy3k : <EOL> funcopy . func_defaults = func . func_defaults <EOL> else : <EOL> funcopy . __defaults__ = func . __defaults__ <EOL> funcopy . __kwdefaults__ = func . __kwdefaults__ <EOL> def _callable ( obj ) : <EOL> if isinstance ( obj , ClassTypes ) : <EOL> return True <EOL> if getattr ( obj , '<STR_LIT>' , None ) is not None : <EOL> return True <EOL> return False <EOL> def _set_signature ( mock , original , skipfirst ) : <EOL> if not _callable ( original ) : <EOL> return <EOL> skipfirst = isinstance ( original , ClassTypes ) <EOL> result = _getsignature2 ( original , skipfirst ) <EOL> if result is None : <EOL> return <EOL> signature , func = result <EOL> src = "<STR_LIT>" % signature <EOL> context = { '<STR_LIT>' : mock } <EOL> checksig = eval ( src , context ) <EOL> _copy_func_details ( func , checksig ) <EOL> name = original . __name__ <EOL> import keyword <EOL> if not _isidentifier ( name ) : <EOL> name = funcopy <EOL> context = { '<STR_LIT>' : checksig , '<STR_LIT>' : mock } <EOL> src = """<STR_LIT>""" % name <EOL> exec ( src , context ) <EOL> funcopy = context [ name ] <EOL> _setup_func ( funcopy , mock ) <EOL> return funcopy <EOL> def mocksignature ( func , mock = None , skipfirst = False ) : <EOL> """<STR_LIT>""" <EOL> if mock is None : <EOL> mock = Mock ( ) <EOL> signature , func = _getsignature ( func , skipfirst ) <EOL> src = "<STR_LIT>" % { <EOL> '<STR_LIT>' : signature <EOL> } <EOL> funcopy = eval ( src , dict ( _mock_ = mock ) ) <EOL> _copy_func_details ( func , funcopy ) <EOL> _setup_func ( funcopy , mock ) <EOL> return funcopy <EOL> def _setup_func ( funcopy , mock ) : <EOL> funcopy . mock = mock <EOL> if not isinstance ( mock , Mock ) : <EOL> return <EOL> def assert_called_with ( * args , ** kwargs ) : <EOL> return mock . assert_called_with ( * args , ** kwargs ) <EOL> def assert_called_once_with ( * args , ** kwargs ) : <EOL> return mock . assert_called_once_with ( * args , ** kwargs ) <EOL> def reset_mock ( ) : <EOL> funcopy . method_calls = [ ] <EOL> mock . reset_mock ( ) <EOL> ret = funcopy . return_value <EOL> if isinstance ( ret , Mock ) and not ret is mock : <EOL> ret . reset_mock ( ) <EOL> funcopy . called = False <EOL> funcopy . call_count = <NUM_LIT:0> <EOL> funcopy . call_args = None <EOL> funcopy . call_args_list = [ ] <EOL> funcopy . method_calls = [ ] <EOL> funcopy . return_value = mock . return_value <EOL> funcopy . side_effect = mock . side_effect <EOL> funcopy . _mock_children = mock . _mock_children <EOL> funcopy . assert_called_with = assert_called_with <EOL> funcopy . assert_called_once_with = assert_called_once_with <EOL> funcopy . reset_mock = reset_mock <EOL> mock . _mock_signature = funcopy <EOL> def _is_magic ( name ) : <EOL> return '<STR_LIT>' % name [ <NUM_LIT:2> : - <NUM_LIT:2> ] == name <EOL> class SentinelObject ( object ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . name <EOL> class Sentinel ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . _sentinels = { } <EOL> def __getattr__ ( self , name ) : <EOL> if name == '<STR_LIT>' : <EOL> raise AttributeError <EOL> return self . _sentinels . setdefault ( name , SentinelObject ( name ) ) <EOL> sentinel = Sentinel ( ) <EOL> DEFAULT = sentinel . DEFAULT <EOL> class OldStyleClass : <EOL> pass <EOL> ClassType = type ( OldStyleClass ) <EOL> def _copy ( value ) : <EOL> if type ( value ) in ( dict , list , tuple , set ) : <EOL> return type ( value ) ( value ) <EOL> return value <EOL> ClassTypes = ( type , ) <EOL> if not inPy3k : <EOL> ClassTypes = ( type , ClassType ) <EOL> _allowed_names = set ( [ '<STR_LIT>' ] ) <EOL> def _mock_signature_property ( name ) : <EOL> _allowed_names . add ( name ) <EOL> def _get ( self ) : <EOL> sig = self . _mock_signature <EOL> if sig is None : <EOL> return getattr ( self , '<STR_LIT>' + name ) <EOL> return getattr ( sig , name ) <EOL> def _set ( self , value ) : <EOL> sig = self . _mock_signature <EOL> if sig is None : <EOL> setattr ( self , '<STR_LIT>' + name , value ) <EOL> else : <EOL> setattr ( sig , name , value ) <EOL> return property ( _get , _set ) <EOL> class Mock ( object ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( cls , * args , ** kw ) : <EOL> new = type ( cls . __name__ , ( cls , ) , { '<STR_LIT>' : cls . __doc__ } ) <EOL> return object . __new__ ( new ) <EOL> def __init__ ( self , spec = None , side_effect = None , return_value = DEFAULT , <EOL> wraps = None , name = None , spec_set = None , parent = None , <EOL> _old_name = None , _spec_state = None , ** kwargs ) : <EOL> self . _mock_parent = parent <EOL> self . _mock_name = name <EOL> self . _mock_old_name = _old_name <EOL> self . _spec_state = _spec_state <EOL> _spec_class = None <EOL> if spec_set is not None : <EOL> spec = spec_set <EOL> spec_set = True <EOL> if spec is not None and type ( spec ) is not list : <EOL> if isinstance ( spec , ClassTypes ) : <EOL> _spec_class = spec <EOL> else : <EOL> _spec_class = _get_class ( spec ) <EOL> spec = dir ( spec ) <EOL> self . _spec_class = _spec_class <EOL> self . _spec_set = spec_set <EOL> self . _mock_methods = spec <EOL> self . _mock_children = { } <EOL> self . _mock_return_value = return_value <EOL> self . _mock_side_effect = side_effect <EOL> self . _mock_wraps = wraps <EOL> self . mock_calls = [ ] <EOL> self . _mock_signature = None <EOL> self . _mock_called = False <EOL> self . _mock_call_args = None <EOL> self . _mock_call_count = <NUM_LIT:0> <EOL> self . _mock_call_args_list = [ ] <EOL> self . reset_mock ( ) <EOL> self . configure_mock ( ** kwargs ) <EOL> @ property <EOL> def __class__ ( self ) : <EOL> if self . _spec_class is None : <EOL> return type ( self ) <EOL> return self . _spec_class <EOL> called = _mock_signature_property ( '<STR_LIT>' ) <EOL> call_count = _mock_signature_property ( '<STR_LIT>' ) <EOL> call_args = _mock_signature_property ( '<STR_LIT>' ) <EOL> call_args_list = _mock_signature_property ( '<STR_LIT>' ) <EOL> side_effect = _mock_signature_property ( '<STR_LIT>' ) <EOL> def reset_mock ( self ) : <EOL> "<STR_LIT>" <EOL> self . called = False <EOL> self . call_args = None <EOL> self . call_count = <NUM_LIT:0> <EOL> self . call_args_list = [ ] <EOL> self . method_calls = [ ] <EOL> for child in self . _mock_children . values ( ) : <EOL> child . reset_mock ( ) <EOL> ret = self . _mock_return_value <EOL> if isinstance ( ret , Mock ) and ret is not self : <EOL> ret . reset_mock ( ) <EOL> def configure_mock ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> for arg , val in sorted ( kwargs . items ( ) , <EOL> key = lambda entry : entry [ <NUM_LIT:0> ] . count ( '<STR_LIT:.>' ) ) : <EOL> args = arg . split ( '<STR_LIT:.>' ) <EOL> final = args . pop ( ) <EOL> obj = self <EOL> for entry in args : <EOL> obj = getattr ( obj , entry ) <EOL> setattr ( obj , final , val ) <EOL> def __get_return_value ( self ) : <EOL> ret = self . _mock_return_value <EOL> if self . _mock_signature is not None : <EOL> ret = self . _mock_signature . return_value <EOL> if ret is DEFAULT : <EOL> ret = self . _get_child_mock ( ) <EOL> self . return_value = ret <EOL> return ret <EOL> def __set_return_value ( self , value ) : <EOL> if self . _mock_signature is not None : <EOL> self . _mock_signature . return_value = value <EOL> else : <EOL> self . _mock_return_value = value <EOL> __return_value_doc = "<STR_LIT>" <EOL> return_value = property ( __get_return_value , __set_return_value , <EOL> __return_value_doc ) <EOL> def _mock_check_sig ( self , * args , ** kwargs ) : <EOL> pass <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> self . _mock_check_sig ( * args , ** kwargs ) <EOL> return self . _mock_call ( * args , ** kwargs ) <EOL> def _mock_call ( self , * args , ** kwargs ) : <EOL> self . called = True <EOL> self . call_count += <NUM_LIT:1> <EOL> self . call_args = callargs ( ( args , kwargs ) ) <EOL> self . call_args_list . append ( callargs ( ( args , kwargs ) ) ) <EOL> parent = self . _mock_parent <EOL> name = self . _mock_name <EOL> while parent is not None : <EOL> parent . method_calls . append ( callargs ( ( name , args , kwargs ) ) ) <EOL> if parent . _mock_parent is None : <EOL> break <EOL> name = parent . _mock_name + '<STR_LIT:.>' + name <EOL> parent = parent . _mock_parent <EOL> ret_val = DEFAULT <EOL> if self . side_effect is not None : <EOL> if ( isinstance ( self . side_effect , BaseException ) or <EOL> isinstance ( self . side_effect , ClassTypes ) and <EOL> issubclass ( self . side_effect , BaseException ) ) : <EOL> raise self . side_effect <EOL> ret_val = self . side_effect ( * args , ** kwargs ) <EOL> if ret_val is DEFAULT : <EOL> ret_val = self . return_value <EOL> if self . _mock_wraps is not None and self . _mock_return_value is DEFAULT : <EOL> return self . _mock_wraps ( * args , ** kwargs ) <EOL> if ret_val is DEFAULT : <EOL> ret_val = self . return_value <EOL> return ret_val <EOL> def __getattr__ ( self , name ) : <EOL> if name == '<STR_LIT>' : <EOL> raise AttributeError ( name ) <EOL> elif self . _mock_methods is not None : <EOL> if name not in self . _mock_methods or name in _all_magics : <EOL> raise AttributeError ( "<STR_LIT>" % name ) <EOL> elif _is_magic ( name ) : <EOL> raise AttributeError ( name ) <EOL> result = self . _mock_children . get ( name ) <EOL> if result is None : <EOL> wraps = None <EOL> if self . _mock_wraps is not None : <EOL> wraps = getattr ( self . _mock_wraps , name ) <EOL> result = self . _get_child_mock ( parent = self , name = name , wraps = wraps ) <EOL> self . _mock_children [ name ] = result <EOL> elif isinstance ( result , _SpecState ) : <EOL> result = create_autospec ( <EOL> result . spec , result . spec_set , result . inherit , None , <EOL> result . parent , result . name , result . instance <EOL> ) <EOL> self . _mock_children [ name ] = result <EOL> return result <EOL> def __repr__ ( self ) : <EOL> if self . _mock_name is None and self . _spec_class is None : <EOL> return object . __repr__ ( self ) <EOL> name_string = '<STR_LIT>' <EOL> spec_string = '<STR_LIT>' <EOL> if self . _mock_name is not None : <EOL> def get_name ( name ) : <EOL> if name is None : <EOL> return '<STR_LIT>' <EOL> return name <EOL> parent = self . _mock_parent <EOL> name = self . _mock_name <EOL> while parent is not None : <EOL> name = get_name ( parent . _mock_name ) + '<STR_LIT:.>' + name <EOL> parent = parent . _mock_parent <EOL> name_string = '<STR_LIT>' % name <EOL> if self . _spec_class is not None : <EOL> spec_string = '<STR_LIT>' <EOL> if self . _spec_set : <EOL> spec_string = '<STR_LIT>' <EOL> spec_string = spec_string % self . _spec_class . __name__ <EOL> return "<STR_LIT>" % ( type ( self ) . __name__ , <EOL> name_string , <EOL> spec_string , <EOL> id ( self ) ) <EOL> def __dir__ ( self ) : <EOL> extras = self . _mock_methods or [ ] <EOL> from_type = dir ( type ( self ) ) <EOL> from_dict = list ( self . __dict__ ) <EOL> if FILTER_DIR : <EOL> from_type = [ e for e in from_type if not e . startswith ( '<STR_LIT:_>' ) ] <EOL> from_dict = [ e for e in from_dict if not e . startswith ( '<STR_LIT:_>' ) or <EOL> _is_magic ( e ) ] <EOL> return sorted ( set ( extras + from_type + from_dict + <EOL> list ( self . _mock_children ) ) ) <EOL> def __setattr__ ( self , name , value ) : <EOL> if not '<STR_LIT>' in self . __dict__ : <EOL> return object . __setattr__ ( self , name , value ) <EOL> if ( self . _spec_set and self . _mock_methods is not None and name not in <EOL> self . _mock_methods and name not in self . __dict__ and <EOL> name not in _allowed_names ) : <EOL> raise AttributeError ( "<STR_LIT>" % name ) <EOL> if name in _unsupported_magics : <EOL> msg = '<STR_LIT>' % name <EOL> raise AttributeError ( msg ) <EOL> elif name in _all_magics : <EOL> if self . _mock_methods is not None and name not in self . _mock_methods : <EOL> raise AttributeError ( "<STR_LIT>" % name ) <EOL> if isinstance ( value , MagicProxy ) : <EOL> setattr ( type ( self ) , name , value ) <EOL> return <EOL> if not isinstance ( value , Mock ) : <EOL> setattr ( type ( self ) , name , _get_method ( name , value ) ) <EOL> original = value <EOL> real = lambda * args , ** kw : original ( self , * args , ** kw ) <EOL> value = mocksignature ( value , real , skipfirst = True ) <EOL> else : <EOL> setattr ( type ( self ) , name , value ) <EOL> return object . __setattr__ ( self , name , value ) <EOL> def __delattr__ ( self , name ) : <EOL> if name in _all_magics and name in type ( self ) . __dict__ : <EOL> delattr ( type ( self ) , name ) <EOL> return object . __delattr__ ( self , name ) <EOL> def assert_called_with ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if self . call_args is None : <EOL> raise AssertionError ( '<STR_LIT>' % ( ( args , kwargs ) , ) ) <EOL> if not self . call_args == ( args , kwargs ) : <EOL> raise AssertionError ( <EOL> '<STR_LIT>' % ( ( args , kwargs ) , self . call_args ) <EOL> ) <EOL> def assert_called_once_with ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not self . call_count == <NUM_LIT:1> : <EOL> msg = ( "<STR_LIT>" % <EOL> self . call_count ) <EOL> raise AssertionError ( msg ) <EOL> return self . assert_called_with ( * args , ** kwargs ) <EOL> def _get_child_mock ( self , ** kw ) : <EOL> klass = type ( self ) . __mro__ [ <NUM_LIT:1> ] <EOL> return klass ( ** kw ) <EOL> class callargs ( tuple ) : <EOL> """<STR_LIT>""" <EOL> def __eq__ ( self , other ) : <EOL> if len ( self ) == <NUM_LIT:3> : <EOL> if other [ <NUM_LIT:0> ] != self [ <NUM_LIT:0> ] : <EOL> return False <EOL> args_kwargs = self [ <NUM_LIT:1> : ] <EOL> other_args_kwargs = other [ <NUM_LIT:1> : ] <EOL> else : <EOL> args_kwargs = tuple ( self ) <EOL> other_args_kwargs = other <EOL> if len ( other_args_kwargs ) == <NUM_LIT:0> : <EOL> other_args , other_kwargs = ( ) , { } <EOL> elif len ( other_args_kwargs ) == <NUM_LIT:1> : <EOL> if isinstance ( other_args_kwargs [ <NUM_LIT:0> ] , tuple ) : <EOL> other_args = other_args_kwargs [ <NUM_LIT:0> ] <EOL> other_kwargs = { } <EOL> else : <EOL> other_args = ( ) <EOL> other_kwargs = other_args_kwargs [ <NUM_LIT:0> ] <EOL> else : <EOL> other_args , other_kwargs = other_args_kwargs <EOL> return tuple ( args_kwargs ) == ( other_args , other_kwargs ) <EOL> def _dot_lookup ( thing , comp , import_path ) : <EOL> try : <EOL> return getattr ( thing , comp ) <EOL> except AttributeError : <EOL> __import__ ( import_path ) <EOL> return getattr ( thing , comp ) <EOL> def _importer ( target ) : <EOL> components = target . split ( '<STR_LIT:.>' ) <EOL> import_path = components . pop ( <NUM_LIT:0> ) <EOL> thing = __import__ ( import_path ) <EOL> for comp in components : <EOL> import_path += "<STR_LIT>" % comp <EOL> thing = _dot_lookup ( thing , comp , import_path ) <EOL> return thing <EOL> class _patch ( object ) : <EOL> def __init__ ( self , target , attribute , new , spec , create , <EOL> mocksignature , spec_set , autospec , kwargs ) : <EOL> self . target = target <EOL> self . attribute = attribute <EOL> self . new = new <EOL> self . spec = spec <EOL> self . create = create <EOL> self . has_local = False <EOL> self . mocksignature = mocksignature <EOL> self . spec_set = spec_set <EOL> self . autospec = autospec <EOL> self . kwargs = kwargs <EOL> def copy ( self ) : <EOL> return _patch ( self . target , self . attribute , self . new , self . spec , <EOL> self . create , self . mocksignature , self . spec_set , <EOL> self . autospec , self . kwargs ) <EOL> def __call__ ( self , func ) : <EOL> if isinstance ( func , ClassTypes ) : <EOL> return self . decorate_class ( func ) <EOL> return self . decorate_callable ( func ) <EOL> def decorate_class ( self , klass ) : <EOL> for attr in dir ( klass ) : <EOL> attr_value = getattr ( klass , attr ) <EOL> if attr . startswith ( "<STR_LIT:test>" ) and hasattr ( attr_value , "<STR_LIT>" ) : <EOL> setattr ( klass , attr , self . copy ( ) ( attr_value ) ) <EOL> return klass <EOL> def decorate_callable ( self , func ) : <EOL> if hasattr ( func , '<STR_LIT>' ) : <EOL> func . patchings . append ( self ) <EOL> return func <EOL> @ wraps ( func ) <EOL> def patched ( * args , ** keywargs ) : <EOL> extra_args = [ ] <EOL> for patching in patched . patchings : <EOL> arg = patching . __enter__ ( ) <EOL> if patching . new is DEFAULT : <EOL> extra_args . append ( arg ) <EOL> args += tuple ( extra_args ) <EOL> try : <EOL> return func ( * args , ** keywargs ) <EOL> finally : <EOL> for patching in reversed ( getattr ( patched , '<STR_LIT>' , [ ] ) ) : <EOL> patching . __exit__ ( ) <EOL> patched . patchings = [ self ] <EOL> if hasattr ( func , '<STR_LIT>' ) : <EOL> patched . compat_co_firstlineno = getattr ( <EOL> func , "<STR_LIT>" , <EOL> func . func_code . co_firstlineno <EOL> ) <EOL> return patched <EOL> def get_original ( self ) : <EOL> target = self . target <EOL> name = self . attribute <EOL> original = DEFAULT <EOL> local = False <EOL> try : <EOL> original = target . __dict__ [ name ] <EOL> except ( AttributeError , KeyError ) : <EOL> original = getattr ( target , name , DEFAULT ) <EOL> else : <EOL> local = True <EOL> if not self . create and original is DEFAULT : <EOL> raise AttributeError ( <EOL> "<STR_LIT>" % ( target , name ) <EOL> ) <EOL> return original , local <EOL> def __enter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> new , spec = self . new , self . spec <EOL> spec_set , autospec = self . spec_set , self . autospec <EOL> kwargs = self . kwargs <EOL> original , local = self . get_original ( ) <EOL> if new is DEFAULT and autospec is False : <EOL> inherit = False <EOL> if spec_set == True : <EOL> spec_set = original <EOL> if isinstance ( spec_set , ClassTypes ) : <EOL> inherit = True <EOL> elif spec == True : <EOL> spec = original <EOL> if isinstance ( spec , ClassTypes ) : <EOL> inherit = True <EOL> new = MagicMock ( spec = spec , spec_set = spec_set , ** kwargs ) <EOL> if inherit : <EOL> new . return_value = Mock ( spec = spec , spec_set = spec_set ) <EOL> elif autospec is not False : <EOL> if new is not DEFAULT : <EOL> raise TypeError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> spec_set = bool ( spec_set ) <EOL> _kwargs = { '<STR_LIT>' : getattr ( original , '<STR_LIT>' , None ) } <EOL> if autospec is True : <EOL> autospec = original <EOL> new = create_autospec ( autospec , spec_set , inherit = True , <EOL> configure = kwargs , ** _kwargs ) <EOL> elif self . kwargs : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> new_attr = new <EOL> if self . mocksignature : <EOL> new_attr = mocksignature ( original , new ) <EOL> self . temp_original = original <EOL> self . is_local = local <EOL> setattr ( self . target , self . attribute , new_attr ) <EOL> return new <EOL> def __exit__ ( self , * _ ) : <EOL> """<STR_LIT>""" <EOL> if self . is_local and self . temp_original is not DEFAULT : <EOL> setattr ( self . target , self . attribute , self . temp_original ) <EOL> else : <EOL> delattr ( self . target , self . attribute ) <EOL> if not self . create and not hasattr ( self . target , self . attribute ) : <EOL> setattr ( self . target , self . attribute , self . temp_original ) <EOL> del self . temp_original <EOL> del self . is_local <EOL> start = __enter__ <EOL> stop = __exit__ <EOL> def _patch_object ( target , attribute , new = DEFAULT , spec = None , <EOL> create = False , mocksignature = False , spec_set = None , <EOL> autospec = False , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return _patch ( target , attribute , new , spec , create , mocksignature , <EOL> spec_set , autospec , kwargs ) <EOL> def patch ( target , new = DEFAULT , spec = None , create = False , mocksignature = False , <EOL> spec_set = None , autospec = False , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> target , attribute = target . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> except ( TypeError , ValueError ) : <EOL> raise TypeError ( "<STR_LIT>" % <EOL> ( target , ) ) <EOL> target = _importer ( target ) <EOL> return _patch ( target , attribute , new , spec , create , mocksignature , <EOL> spec_set , autospec , kwargs ) <EOL> class _patch_dict ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , in_dict , values = ( ) , clear = False , ** kwargs ) : <EOL> if isinstance ( in_dict , basestring ) : <EOL> in_dict = _importer ( in_dict ) <EOL> self . in_dict = in_dict <EOL> self . values = dict ( values ) <EOL> self . values . update ( kwargs ) <EOL> self . clear = clear <EOL> self . _original = None <EOL> def __call__ ( self , f ) : <EOL> if isinstance ( f , ClassTypes ) : <EOL> return self . decorate_class ( f ) <EOL> @ wraps ( f ) <EOL> def _inner ( * args , ** kw ) : <EOL> self . _patch_dict ( ) <EOL> try : <EOL> return f ( * args , ** kw ) <EOL> finally : <EOL> self . _unpatch_dict ( ) <EOL> return _inner <EOL> def decorate_class ( self , klass ) : <EOL> for attr in dir ( klass ) : <EOL> attr_value = getattr ( klass , attr ) <EOL> if attr . startswith ( "<STR_LIT:test>" ) and hasattr ( attr_value , "<STR_LIT>" ) : <EOL> decorator = _patch_dict ( self . in_dict , self . values , self . clear ) <EOL> decorated = decorator ( attr_value ) <EOL> setattr ( klass , attr , decorated ) <EOL> return klass <EOL> def __enter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _patch_dict ( ) <EOL> def _patch_dict ( self ) : <EOL> """<STR_LIT>""" <EOL> values = self . values <EOL> in_dict = self . in_dict <EOL> clear = self . clear <EOL> try : <EOL> original = in_dict . copy ( ) <EOL> except AttributeError : <EOL> original = { } <EOL> for key in in_dict : <EOL> original [ key ] = in_dict [ key ] <EOL> self . _original = original <EOL> if clear : <EOL> _clear_dict ( in_dict ) <EOL> try : <EOL> in_dict . update ( values ) <EOL> except AttributeError : <EOL> for key in values : <EOL> in_dict [ key ] = values [ key ] <EOL> def _unpatch_dict ( self ) : <EOL> in_dict = self . in_dict <EOL> original = self . _original <EOL> _clear_dict ( in_dict ) <EOL> try : <EOL> in_dict . update ( original ) <EOL> except AttributeError : <EOL> for key in original : <EOL> in_dict [ key ] = original [ key ] <EOL> def __exit__ ( self , * args ) : <EOL> self . _unpatch_dict ( ) <EOL> return False <EOL> start = __enter__ <EOL> stop = __exit__ <EOL> def _clear_dict ( in_dict ) : <EOL> try : <EOL> in_dict . clear ( ) <EOL> except AttributeError : <EOL> keys = list ( in_dict ) <EOL> for key in keys : <EOL> del in_dict [ key ] <EOL> patch . object = _patch_object <EOL> patch . dict = _patch_dict <EOL> class _isolate ( object ) : <EOL> def __init__ ( self , target , excludes = None ) : <EOL> self . target = target <EOL> self . excludes = [ ] <EOL> if excludes is not None : <EOL> self . excludes = excludes <EOL> self . names_under_test = set ( self . get_names_under_test ( ) ) <EOL> def get_names_under_test ( self ) : <EOL> module = sys . modules [ self . target . __module__ ] <EOL> for name , value in module . __dict__ . items ( ) : <EOL> if value is self . target or name in self . excludes : <EOL> yield name <EOL> def __enter__ ( self ) : <EOL> module_name = self . target . __module__ <EOL> self . module = sys . modules [ module_name ] <EOL> old_module_dict = self . module . __dict__ . copy ( ) <EOL> module_keys = set ( self . module . __dict__ . keys ( ) ) <EOL> dunders = set ( [ k for k in module_keys <EOL> if k . startswith ( '<STR_LIT>' ) and k . endswith ( '<STR_LIT>' ) ] ) <EOL> replaced_keys = ( module_keys - dunders - self . names_under_test ) <EOL> for key in replaced_keys : <EOL> self . module . __dict__ [ key ] = Mock ( ) <EOL> self . module . __dict__ [ '<STR_LIT>' ] = old_module_dict <EOL> def __exit__ ( self , * _ ) : <EOL> old_module_dict = self . module . __dict__ [ '<STR_LIT>' ] <EOL> self . module . __dict__ . clear ( ) <EOL> self . module . __dict__ . update ( old_module_dict ) <EOL> def __call__ ( self , thing , * args , ** kwargs ) : <EOL> if isinstance ( thing , ClassTypes ) : <EOL> return self . decorate_class ( thing ) <EOL> else : <EOL> return self . decorate_callable ( thing ) <EOL> def decorate_callable ( self , func ) : <EOL> @ wraps ( func ) <EOL> def patched ( * args , ** keywargs ) : <EOL> self . __enter__ ( ) <EOL> try : <EOL> return func ( * args , ** keywargs ) <EOL> finally : <EOL> self . __exit__ ( ) <EOL> if hasattr ( func , '<STR_LIT>' ) : <EOL> patched . compat_co_firstlineno = getattr ( func , "<STR_LIT>" , <EOL> func . func_code . co_firstlineno ) <EOL> return patched <EOL> def decorate_class ( self , klass , * args ) : <EOL> setup = getattr ( klass , '<STR_LIT>' , None ) <EOL> teardown = getattr ( klass , '<STR_LIT>' , None ) <EOL> if not setup : <EOL> setattr ( klass , '<STR_LIT>' , self . start ) <EOL> else : <EOL> def wrap_setup ( * args ) : <EOL> self . start ( ) <EOL> setup ( * args ) <EOL> setattr ( klass , setup . __name__ , wrap_setup ) <EOL> if not teardown : <EOL> setattr ( klass , '<STR_LIT>' , self . stop ) <EOL> else : <EOL> def wrap_teardown ( * args ) : <EOL> self . stop ( ) <EOL> teardown ( * args ) <EOL> setattr ( klass , teardown . __name__ , wrap_teardown ) <EOL> return klass <EOL> start = __enter__ <EOL> stop = __exit__ <EOL> def isolate ( target , excludes = None ) : <EOL> """<STR_LIT>""" <EOL> target = _importer ( target ) <EOL> return _isolate ( target , excludes ) <EOL> def _isolate_object ( * args , ** kwargs ) : <EOL> return _isolate ( * args , ** kwargs ) <EOL> isolate . object = _isolate_object <EOL> magic_methods = ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> numerics = "<STR_LIT>" <EOL> inplace = '<STR_LIT:U+0020>' . join ( '<STR_LIT>' % n for n in numerics . split ( ) ) <EOL> right = '<STR_LIT:U+0020>' . join ( '<STR_LIT>' % n for n in numerics . split ( ) ) <EOL> extra = '<STR_LIT>' <EOL> if inPy3k : <EOL> extra = '<STR_LIT>' <EOL> else : <EOL> extra = '<STR_LIT>' <EOL> _non_defaults = set ( '<STR_LIT>' % method for method in [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> ] ) <EOL> def _get_method ( name , func ) : <EOL> "<STR_LIT>" <EOL> def method ( self , * args , ** kw ) : <EOL> return func ( self , * args , ** kw ) <EOL> method . __name__ = name <EOL> return method <EOL> _magics = set ( <EOL> '<STR_LIT>' % method for method in <EOL> '<STR_LIT:U+0020>' . join ( [ magic_methods , numerics , inplace , right , extra ] ) . split ( ) <EOL> ) <EOL> _all_magics = _magics | _non_defaults <EOL> _unsupported_magics = set ( [ <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] ) <EOL> _calculate_return_value = { <EOL> '<STR_LIT>' : lambda self : object . __hash__ ( self ) , <EOL> '<STR_LIT>' : lambda self : object . __str__ ( self ) , <EOL> '<STR_LIT>' : lambda self : object . __sizeof__ ( self ) , <EOL> '<STR_LIT>' : lambda self : unicode ( object . __str__ ( self ) ) , <EOL> } <EOL> _side_effect_methods = { <EOL> '<STR_LIT>' : lambda self : lambda other : self is other , <EOL> '<STR_LIT>' : lambda self : lambda other : self is not other , <EOL> } <EOL> _return_values = { <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : iter ( [ ] ) , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT:1.0> , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : long ( <NUM_LIT:1> ) , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> } <EOL> def _get_eq ( self ) : <EOL> def __eq__ ( other ) : <EOL> ret_val = self . __eq__ . _mock_return_value <EOL> if ret_val is not DEFAULT : <EOL> return ret_val <EOL> return self is other <EOL> return __eq__ <EOL> def _get_ne ( self ) : <EOL> def __ne__ ( other ) : <EOL> if self . __ne__ . _mock_return_value is not DEFAULT : <EOL> return DEFAULT <EOL> return self is not other <EOL> return __ne__ <EOL> _side_effect_methods = { <EOL> '<STR_LIT>' : _get_eq , <EOL> '<STR_LIT>' : _get_ne , <EOL> } <EOL> def _set_return_value ( mock , method , name ) : <EOL> return_value = DEFAULT <EOL> if name in _return_values : <EOL> return_value = _return_values [ name ] <EOL> elif name in _calculate_return_value : <EOL> try : <EOL> return_value = _calculate_return_value [ name ] ( mock ) <EOL> except AttributeError : <EOL> return_value = AttributeError ( name ) <EOL> elif name in _side_effect_methods : <EOL> side_effect = _side_effect_methods [ name ] ( mock ) <EOL> method . side_effect = side_effect <EOL> if return_value is not DEFAULT : <EOL> method . return_value = return_value <EOL> class MagicMock ( Mock ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kw ) : <EOL> _super ( MagicMock , self ) . __init__ ( * args , ** kw ) <EOL> these_magics = _magics <EOL> if self . _mock_methods is not None : <EOL> these_magics = _magics . intersection ( self . _mock_methods ) <EOL> for entry in these_magics : <EOL> setattr ( self , entry , _create_proxy ( entry , self ) ) <EOL> def _create_proxy ( entry , self ) : <EOL> def create_mock ( ) : <EOL> m = MagicMock ( name = entry ) <EOL> setattr ( self , entry , m ) <EOL> _set_return_value ( self , m , entry ) <EOL> return m <EOL> return MagicProxy ( create_mock ) <EOL> class MagicProxy ( object ) : <EOL> def __init__ ( self , create_mock ) : <EOL> self . create_mock = create_mock <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> m = self . create_mock ( ) <EOL> return m ( * args , ** kwargs ) <EOL> def __get__ ( self , obj , _type = None ) : <EOL> return self . create_mock ( ) <EOL> class _ANY ( object ) : <EOL> "<STR_LIT>" <EOL> def __eq__ ( self , other ) : <EOL> return True <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' <EOL> ANY = _ANY ( ) <EOL> class _Call ( object ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , name = None ) : <EOL> self . name = name <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> if self . name is None : <EOL> return ( args , kwargs ) <EOL> return ( self . name , args , kwargs ) <EOL> def __getattr__ ( self , attr ) : <EOL> if self . name is None : <EOL> return _Call ( attr ) <EOL> name = '<STR_LIT>' % ( self . name , attr ) <EOL> return _Call ( name ) <EOL> def __repr__ ( self ) : <EOL> if self . name is None : <EOL> return '<STR_LIT>' <EOL> return '<STR_LIT>' % self . name <EOL> call = _Call ( ) <EOL> def create_autospec ( spec , spec_set = False , inherit = False , configure = None , <EOL> _parent = None , _name = None , _instance = False ) : <EOL> """<STR_LIT>""" <EOL> if configure is None : <EOL> configure = { } <EOL> if type ( spec ) == list : <EOL> spec = list <EOL> is_type = isinstance ( spec , ClassTypes ) <EOL> kwargs = { '<STR_LIT>' : spec } <EOL> if spec_set : <EOL> kwargs = { '<STR_LIT>' : spec } <EOL> elif spec is None : <EOL> kwargs = { } <EOL> kwargs . update ( configure ) <EOL> mock = MagicMock ( parent = _parent , name = _name , ** kwargs ) <EOL> if isinstance ( spec , FunctionTypes ) : <EOL> mock = _set_signature ( mock , spec , False ) <EOL> else : <EOL> _check_signature ( spec , mock , is_type ) <EOL> if _parent is not None : <EOL> _parent . _mock_children [ _name ] = mock <EOL> if is_type and inherit and not _instance : <EOL> mock . return_value = create_autospec ( spec , spec_set , inherit , <EOL> _instance = True ) <EOL> for entry in dir ( spec ) : <EOL> if _is_magic ( entry ) : <EOL> continue <EOL> if isinstance ( spec , FunctionTypes ) and entry in FunctionAttributes : <EOL> continue <EOL> original = getattr ( spec , entry ) <EOL> kwargs = { '<STR_LIT>' : original } <EOL> if spec_set : <EOL> kwargs = { '<STR_LIT>' : original } <EOL> if not isinstance ( original , FunctionTypes ) : <EOL> new = _SpecState ( original , spec_set , inherit , mock , entry , <EOL> _instance ) <EOL> mock . _mock_children [ entry ] = new <EOL> else : <EOL> parent = mock <EOL> if isinstance ( spec , FunctionTypes ) : <EOL> parent = mock . mock <EOL> new = MagicMock ( parent = parent , name = entry , ** kwargs ) <EOL> mock . _mock_children [ entry ] = new <EOL> skipfirst = _must_skip ( spec , entry , is_type ) <EOL> _check_signature ( original , new , skipfirst = skipfirst ) <EOL> if isinstance ( new , FunctionTypes ) : <EOL> setattr ( mock , entry , new ) <EOL> return mock <EOL> def _must_skip ( spec , entry , skipfirst ) : <EOL> if not isinstance ( spec , ClassTypes ) : <EOL> if entry in getattr ( spec , '<STR_LIT>' , { } ) : <EOL> return False <EOL> spec = spec . __class__ <EOL> if not hasattr ( spec , '<STR_LIT>' ) : <EOL> return skipfirst <EOL> for klass in spec . __mro__ : <EOL> result = klass . __dict__ . get ( entry , DEFAULT ) <EOL> if result is DEFAULT : <EOL> continue <EOL> if isinstance ( result , ( staticmethod , classmethod ) ) : <EOL> return False <EOL> return skipfirst <EOL> return skipfirst <EOL> def _get_class ( obj ) : <EOL> try : <EOL> return obj . __class__ <EOL> except AttributeError : <EOL> return type ( obj ) <EOL> class _SpecState ( object ) : <EOL> def __init__ ( self , spec , spec_set = False , inherit = False , parent = None , <EOL> name = None , ids = None , instance = False ) : <EOL> self . spec = spec <EOL> self . ids = ids <EOL> self . spec_set = spec_set <EOL> self . inherit = inherit <EOL> self . parent = parent <EOL> self . instance = instance <EOL> self . name = name <EOL> FunctionTypes = ( <EOL> type ( create_autospec ) , <EOL> type ( ANY . __eq__ ) , <EOL> type ( _ANY . __eq__ ) , <EOL> ) <EOL> FunctionAttributes = set ( [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) </s>
