<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> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> __version__ = '<STR_LIT>' <EOL> import pprint <EOL> import sys <EOL> try : <EOL> import inspect <EOL> except ImportError : <EOL> inspect = None <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> BaseException <EOL> except NameError : <EOL> BaseException = Exception <EOL> try : <EOL> next <EOL> except NameError : <EOL> def next ( obj ) : <EOL> return obj . next ( ) <EOL> BaseExceptions = ( BaseException , ) <EOL> if '<STR_LIT>' in sys . platform : <EOL> import java <EOL> BaseExceptions = ( BaseException , java . lang . Throwable ) <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> _super = super <EOL> self = '<STR_LIT>' <EOL> builtin = '<STR_LIT>' <EOL> if inPy3k : <EOL> self = '<STR_LIT>' <EOL> builtin = '<STR_LIT>' <EOL> FILTER_DIR = True <EOL> def _is_instance_mock ( obj ) : <EOL> return issubclass ( type ( obj ) , NonCallableMock ) <EOL> def _is_exception ( obj ) : <EOL> return ( <EOL> isinstance ( obj , BaseExceptions ) or <EOL> isinstance ( obj , ClassTypes ) and issubclass ( obj , BaseExceptions ) <EOL> ) <EOL> class _slotted ( object ) : <EOL> __slots__ = [ '<STR_LIT:a>' ] <EOL> DescriptorTypes = ( <EOL> type ( _slotted . a ) , <EOL> property , <EOL> ) <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> return <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 _is_list ( obj ) : <EOL> return type ( obj ) in ( list , tuple ) <EOL> def _instance_callable ( obj ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( obj , ClassTypes ) : <EOL> return hasattr ( obj , '<STR_LIT>' ) <EOL> klass = obj <EOL> if '<STR_LIT>' in klass . __dict__ : <EOL> return True <EOL> for base in klass . __bases__ : <EOL> if _instance_callable ( base ) : <EOL> return True <EOL> return False <EOL> def _set_signature ( mock , original ) : <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> if not _isidentifier ( name ) : <EOL> name = '<STR_LIT>' <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 _is_instance_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 assert_has_calls ( * args , ** kwargs ) : <EOL> return mock . assert_has_calls ( * args , ** kwargs ) <EOL> def assert_any_call ( * args , ** kwargs ) : <EOL> return mock . assert_any_call ( * args , ** kwargs ) <EOL> def reset_mock ( ) : <EOL> funcopy . method_calls = _CallList ( ) <EOL> funcopy . mock_calls = _CallList ( ) <EOL> mock . reset_mock ( ) <EOL> ret = funcopy . return_value <EOL> if _is_instance_mock ( ret ) 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 = _CallList ( ) <EOL> funcopy . method_calls = _CallList ( ) <EOL> funcopy . mock_calls = _CallList ( ) <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 . assert_has_calls = assert_has_calls <EOL> funcopy . assert_any_call = assert_any_call <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 ( <EOL> [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' <EOL> ] <EOL> ) <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 _CallList ( list ) : <EOL> def __contains__ ( self , value ) : <EOL> if not isinstance ( value , list ) : <EOL> return list . __contains__ ( self , value ) <EOL> len_value = len ( value ) <EOL> len_self = len ( self ) <EOL> if len_value > len_self : <EOL> return False <EOL> for i in range ( <NUM_LIT:0> , len_self - len_value + <NUM_LIT:1> ) : <EOL> sub_list = self [ i : i + len_value ] <EOL> if sub_list == value : <EOL> return True <EOL> return False <EOL> def __repr__ ( self ) : <EOL> return pprint . pformat ( list ( self ) ) <EOL> def _check_and_set_parent ( parent , value , name , new_name ) : <EOL> if not _is_instance_mock ( value ) : <EOL> return False <EOL> if ( ( value . _mock_name or value . _mock_new_name ) or <EOL> ( value . _mock_parent is not None ) or <EOL> ( value . _mock_new_parent is not None ) ) : <EOL> return False <EOL> _parent = parent <EOL> while _parent is not None : <EOL> if _parent is value : <EOL> return False <EOL> _parent = _parent . _mock_new_parent <EOL> if new_name : <EOL> value . _mock_new_parent = parent <EOL> value . _mock_new_name = new_name <EOL> if name : <EOL> value . _mock_parent = parent <EOL> value . _mock_name = name <EOL> return True <EOL> class Base ( object ) : <EOL> _mock_return_value = DEFAULT <EOL> _mock_side_effect = None <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> pass <EOL> class NonCallableMock ( Base ) : <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__ ( <EOL> self , spec = None , wraps = None , name = None , spec_set = None , <EOL> parent = None , _spec_state = None , _new_name = '<STR_LIT>' , _new_parent = None , <EOL> ** kwargs <EOL> ) : <EOL> if _new_parent is None : <EOL> _new_parent = parent <EOL> self . _mock_parent = parent <EOL> self . _mock_name = name <EOL> self . _mock_new_name = _new_name <EOL> self . _mock_new_parent = _new_parent <EOL> if spec_set is not None : <EOL> spec = spec_set <EOL> spec_set = True <EOL> self . _mock_add_spec ( spec , spec_set ) <EOL> self . _mock_children = { } <EOL> self . _mock_wraps = wraps <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 = _CallList ( ) <EOL> self . _mock_mock_calls = _CallList ( ) <EOL> self . reset_mock ( ) <EOL> self . configure_mock ( ** kwargs ) <EOL> _super ( NonCallableMock , self ) . __init__ ( <EOL> spec , wraps , name , spec_set , parent , <EOL> _spec_state , ** kwargs <EOL> ) <EOL> def attach_mock ( self , mock , attribute ) : <EOL> """<STR_LIT>""" <EOL> mock . _mock_parent = None <EOL> mock . _mock_new_parent = None <EOL> mock . _mock_name = '<STR_LIT>' <EOL> mock . _mock_new_name = None <EOL> setattr ( self , attribute , mock ) <EOL> def mock_add_spec ( self , spec , spec_set = False ) : <EOL> """<STR_LIT>""" <EOL> self . _mock_add_spec ( spec , spec_set ) <EOL> def _mock_add_spec ( self , spec , spec_set ) : <EOL> _spec_class = None <EOL> if spec is not None and not _is_list ( spec ) : <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> 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> _new_parent = self , _new_name = '<STR_LIT>' <EOL> ) <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> _check_and_set_parent ( self , value , None , '<STR_LIT>' ) <EOL> __return_value_doc = "<STR_LIT>" <EOL> return_value = property ( __get_return_value , __set_return_value , <EOL> __return_value_doc ) <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> mock_calls = _mock_signature_property ( '<STR_LIT>' ) <EOL> def __get_side_effect ( self ) : <EOL> sig = self . _mock_signature <EOL> if sig is None : <EOL> return self . _mock_side_effect <EOL> return sig . side_effect <EOL> def __set_side_effect ( self , value ) : <EOL> value = _try_iter ( value ) <EOL> sig = self . _mock_signature <EOL> if sig is None : <EOL> self . _mock_side_effect = value <EOL> else : <EOL> sig . side_effect = value <EOL> side_effect = property ( __get_side_effect , __set_side_effect ) <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 . mock_calls = _CallList ( ) <EOL> self . call_args_list = _CallList ( ) <EOL> self . method_calls = _CallList ( ) <EOL> for child in self . _mock_children . values ( ) : <EOL> child . reset_mock ( ) <EOL> ret = self . _mock_return_value <EOL> if _is_instance_mock ( ret ) 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 __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 ( <EOL> parent = self , name = name , wraps = wraps , _new_name = name , <EOL> _new_parent = self <EOL> ) <EOL> self . _mock_children [ name ] = result <EOL> elif isinstance ( result , _SpecState ) : <EOL> result = create_autospec ( <EOL> result . spec , result . spec_set , result . instance , <EOL> None , result . parent , result . name <EOL> ) <EOL> self . _mock_children [ name ] = result <EOL> return result <EOL> def __repr__ ( self ) : <EOL> _name_list = [ self . _mock_new_name ] <EOL> _parent = self . _mock_new_parent <EOL> last = self <EOL> dot = '<STR_LIT:.>' <EOL> if _name_list == [ '<STR_LIT>' ] : <EOL> dot = '<STR_LIT>' <EOL> seen = set ( ) <EOL> while _parent is not None : <EOL> last = _parent <EOL> _name_list . append ( _parent . _mock_new_name + dot ) <EOL> dot = '<STR_LIT:.>' <EOL> if _parent . _mock_new_name == '<STR_LIT>' : <EOL> dot = '<STR_LIT>' <EOL> _parent = _parent . _mock_new_parent <EOL> if id ( _parent ) in seen : <EOL> break <EOL> seen . add ( id ( _parent ) ) <EOL> _name_list = list ( reversed ( _name_list ) ) <EOL> _first = last . _mock_name or '<STR_LIT>' <EOL> if len ( _name_list ) > <NUM_LIT:1> : <EOL> if _name_list [ <NUM_LIT:1> ] not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> _first += '<STR_LIT:.>' <EOL> _name_list [ <NUM_LIT:0> ] = _first <EOL> name = '<STR_LIT>' . join ( _name_list ) <EOL> name_string = '<STR_LIT>' <EOL> if name not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> name_string = '<STR_LIT>' % name <EOL> spec_string = '<STR_LIT>' <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>" % ( <EOL> type ( self ) . __name__ , <EOL> name_string , <EOL> spec_string , <EOL> id ( self ) <EOL> ) <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 <EOL> name not in self . _mock_methods and <EOL> name not in self . __dict__ and <EOL> name not in _allowed_names ) : <EOL> raise AttributeError ( "<STR_LIT>" % name ) <EOL> elif 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 _is_instance_mock ( value ) : <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> _check_and_set_parent ( self , value , None , name ) <EOL> setattr ( type ( self ) , name , value ) <EOL> elif name in _allowed_names : <EOL> pass <EOL> elif _check_and_set_parent ( self , value , name , name ) : <EOL> self . _mock_children [ 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> if name not in self . __dict__ : <EOL> return <EOL> return object . __delattr__ ( self , name ) <EOL> def _format_mock_call_signature ( self , args , kwargs ) : <EOL> name = self . _mock_name or '<STR_LIT>' <EOL> return _format_call_signature ( name , args , kwargs ) <EOL> def _format_mock_failure_message ( self , args , kwargs ) : <EOL> message = '<STR_LIT>' <EOL> expected_string = self . _format_mock_call_signature ( args , kwargs ) <EOL> call_args = self . call_args <EOL> if len ( call_args ) == <NUM_LIT:3> : <EOL> call_args = call_args [ <NUM_LIT:1> : ] <EOL> actual_string = self . _format_mock_call_signature ( * call_args ) <EOL> return message % ( expected_string , actual_string ) <EOL> def assert_called_with ( _mock_self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self = _mock_self <EOL> if self . call_args is None : <EOL> expected = self . _format_mock_call_signature ( args , kwargs ) <EOL> raise AssertionError ( '<STR_LIT>' % ( expected , ) ) <EOL> if self . call_args != ( args , kwargs ) : <EOL> msg = self . _format_mock_failure_message ( args , kwargs ) <EOL> raise AssertionError ( msg ) <EOL> def assert_called_once_with ( _mock_self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self = _mock_self <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 assert_has_calls ( self , calls , any_order = False ) : <EOL> """<STR_LIT>""" <EOL> if not any_order : <EOL> if calls not in self . mock_calls : <EOL> raise AssertionError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( calls , self . mock_calls ) <EOL> ) <EOL> return <EOL> all_calls = list ( self . mock_calls ) <EOL> not_found = [ ] <EOL> for kall in calls : <EOL> try : <EOL> all_calls . remove ( kall ) <EOL> except ValueError : <EOL> not_found . append ( kall ) <EOL> if not_found : <EOL> raise AssertionError ( <EOL> '<STR_LIT>' % ( tuple ( not_found ) , ) <EOL> ) <EOL> def assert_any_call ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> kall = call ( * args , ** kwargs ) <EOL> if not kall in self . call_args_list : <EOL> expected_string = self . _format_mock_call_signature ( args , kwargs ) <EOL> raise AssertionError ( <EOL> '<STR_LIT>' % expected_string <EOL> ) <EOL> def _get_child_mock ( self , ** kw ) : <EOL> """<STR_LIT>""" <EOL> _type = type ( self ) <EOL> if not issubclass ( _type , CallableMixin ) : <EOL> if issubclass ( _type , NonCallableMagicMock ) : <EOL> klass = MagicMock <EOL> elif issubclass ( _type , NonCallableMock ) : <EOL> klass = Mock <EOL> else : <EOL> klass = _type . __mro__ [ <NUM_LIT:1> ] <EOL> return klass ( ** kw ) <EOL> def _try_iter ( obj ) : <EOL> if obj is None : <EOL> return obj <EOL> if _is_exception ( obj ) : <EOL> return obj <EOL> if _callable ( obj ) : <EOL> return obj <EOL> try : <EOL> return iter ( obj ) <EOL> except TypeError : <EOL> return obj <EOL> class CallableMixin ( Base ) : <EOL> def __init__ ( self , spec = None , side_effect = None , return_value = DEFAULT , <EOL> wraps = None , name = None , spec_set = None , parent = None , <EOL> _spec_state = None , _new_name = '<STR_LIT>' , _new_parent = None , ** kwargs ) : <EOL> self . _mock_return_value = return_value <EOL> _super ( CallableMixin , self ) . __init__ ( <EOL> spec , wraps , name , spec_set , parent , <EOL> _spec_state , _new_name , _new_parent , ** kwargs <EOL> ) <EOL> self . side_effect = side_effect <EOL> def _mock_check_sig ( self , * args , ** kwargs ) : <EOL> pass <EOL> def __call__ ( _mock_self , * args , ** kwargs ) : <EOL> _mock_self . _mock_check_sig ( * args , ** kwargs ) <EOL> return _mock_self . _mock_call ( * args , ** kwargs ) <EOL> def _mock_call ( _mock_self , * args , ** kwargs ) : <EOL> self = _mock_self <EOL> self . called = True <EOL> self . call_count += <NUM_LIT:1> <EOL> self . call_args = _Call ( ( args , kwargs ) , two = True ) <EOL> self . call_args_list . append ( _Call ( ( args , kwargs ) , two = True ) ) <EOL> _new_name = self . _mock_new_name <EOL> _new_parent = self . _mock_new_parent <EOL> self . mock_calls . append ( _Call ( ( '<STR_LIT>' , args , kwargs ) ) ) <EOL> seen = set ( ) <EOL> skip_next_dot = _new_name == '<STR_LIT>' <EOL> while _new_parent is not None : <EOL> this_call = _Call ( ( _new_name , args , kwargs ) ) <EOL> if _new_parent . _mock_new_name : <EOL> dot = '<STR_LIT:.>' <EOL> if skip_next_dot : <EOL> dot = '<STR_LIT>' <EOL> skip_next_dot = False <EOL> if _new_parent . _mock_new_name == '<STR_LIT>' : <EOL> skip_next_dot = True <EOL> _new_name = _new_parent . _mock_new_name + dot + _new_name <EOL> _new_parent . mock_calls . append ( this_call ) <EOL> _new_parent = _new_parent . _mock_new_parent <EOL> if id ( _new_parent ) in seen : <EOL> break <EOL> seen . add ( id ( _new_parent ) ) <EOL> parent = self . _mock_parent <EOL> name = self . _mock_name <EOL> while parent is not None : <EOL> parent . method_calls . append ( _Call ( ( 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 _is_exception ( self . side_effect ) : <EOL> raise self . side_effect <EOL> if not _callable ( self . side_effect ) : <EOL> return next ( 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 <EOL> 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> class Mock ( CallableMixin , NonCallableMock ) : <EOL> """<STR_LIT>""" <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> def _is_started ( patcher ) : <EOL> return hasattr ( patcher , '<STR_LIT>' ) <EOL> class _patch ( object ) : <EOL> attribute_name = None <EOL> def __init__ ( <EOL> self , getter , attribute , new , spec , create , <EOL> mocksignature , spec_set , autospec , new_callable , kwargs <EOL> ) : <EOL> if new_callable is not None : <EOL> if new is not DEFAULT : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> ) <EOL> if autospec is not False : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> ) <EOL> self . getter = getter <EOL> self . attribute = attribute <EOL> self . new = new <EOL> self . new_callable = new_callable <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> self . additional_patchers = [ ] <EOL> def copy ( self ) : <EOL> patcher = _patch ( <EOL> self . getter , self . attribute , self . new , self . spec , <EOL> self . create , self . mocksignature , self . spec_set , <EOL> self . autospec , self . new_callable , self . kwargs <EOL> ) <EOL> patcher . attribute_name = self . attribute_name <EOL> patcher . additional_patchers = [ <EOL> p . copy ( ) for p in self . additional_patchers <EOL> ] <EOL> return patcher <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> if not attr . startswith ( patch . TEST_PREFIX ) : <EOL> continue <EOL> attr_value = getattr ( klass , attr ) <EOL> if not hasattr ( attr_value , "<STR_LIT>" ) : <EOL> continue <EOL> patcher = self . copy ( ) <EOL> setattr ( klass , attr , patcher ( 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> entered_patchers = [ ] <EOL> try : <EOL> try : <EOL> for patching in patched . patchings : <EOL> arg = patching . __enter__ ( ) <EOL> entered_patchers . append ( patching ) <EOL> if patching . attribute_name is not None : <EOL> keywargs . update ( arg ) <EOL> elif patching . new is DEFAULT : <EOL> extra_args . append ( arg ) <EOL> args += tuple ( extra_args ) <EOL> return func ( * args , ** keywargs ) <EOL> except : <EOL> if ( patching not in entered_patchers and <EOL> _is_started ( patching ) ) : <EOL> entered_patchers . append ( patching ) <EOL> raise <EOL> finally : <EOL> for patching in reversed ( entered_patchers ) : <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 . getter ( ) <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 , spec_set = self . new , self . spec , self . spec_set <EOL> autospec , kwargs = self . autospec , self . kwargs <EOL> new_callable = self . new_callable <EOL> self . target = self . getter ( ) <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> elif spec == True : <EOL> spec = original <EOL> if ( spec or spec_set ) is not None : <EOL> if isinstance ( original , ClassTypes ) : <EOL> inherit = True <EOL> Klass = MagicMock <EOL> _kwargs = { } <EOL> if new_callable is not None : <EOL> Klass = new_callable <EOL> elif ( spec or spec_set ) is not None : <EOL> if not _callable ( spec or spec_set ) : <EOL> Klass = NonCallableMagicMock <EOL> if spec is not None : <EOL> _kwargs [ '<STR_LIT>' ] = spec <EOL> if spec_set is not None : <EOL> _kwargs [ '<STR_LIT>' ] = spec_set <EOL> if ( isinstance ( Klass , type ) and <EOL> issubclass ( Klass , NonCallableMock ) and self . attribute ) : <EOL> _kwargs [ '<STR_LIT:name>' ] = self . attribute <EOL> _kwargs . update ( kwargs ) <EOL> new = Klass ( ** _kwargs ) <EOL> if inherit and _is_instance_mock ( new ) : <EOL> if ( not _is_list ( spec or spec_set ) and not <EOL> _instance_callable ( spec or spec_set ) ) : <EOL> Klass = NonCallableMagicMock <EOL> _kwargs . pop ( '<STR_LIT:name>' ) <EOL> new . return_value = Klass ( _new_parent = new , _new_name = '<STR_LIT>' , <EOL> ** _kwargs ) <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> if autospec is True : <EOL> autospec = original <EOL> new = create_autospec ( autospec , spec_set = spec_set , configure = kwargs , <EOL> _name = self . attribute ) <EOL> elif 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> if self . attribute_name is not None : <EOL> extra_args = { } <EOL> if self . new is DEFAULT : <EOL> extra_args [ self . attribute_name ] = new <EOL> for patching in self . additional_patchers : <EOL> arg = patching . __enter__ ( ) <EOL> if patching . new is DEFAULT : <EOL> extra_args . update ( arg ) <EOL> return extra_args <EOL> return new <EOL> def __exit__ ( self , * _ ) : <EOL> """<STR_LIT>""" <EOL> if not _is_started ( self ) : <EOL> raise RuntimeError ( '<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> del self . target <EOL> for patcher in reversed ( self . additional_patchers ) : <EOL> if _is_started ( patcher ) : <EOL> patcher . __exit__ ( ) <EOL> start = __enter__ <EOL> stop = __exit__ <EOL> def _get_target ( target ) : <EOL> try : <EOL> target , attribute = target . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> except ( TypeError , ValueError ) : <EOL> raise TypeError ( "<STR_LIT>" % <EOL> ( target , ) ) <EOL> getter = lambda : _importer ( target ) <EOL> return getter , attribute <EOL> def _patch_object ( <EOL> target , attribute , new = DEFAULT , spec = None , <EOL> create = False , mocksignature = False , spec_set = None , autospec = False , <EOL> new_callable = None , ** kwargs <EOL> ) : <EOL> """<STR_LIT>""" <EOL> getter = lambda : target <EOL> return _patch ( <EOL> getter , attribute , new , spec , create , mocksignature , <EOL> spec_set , autospec , new_callable , kwargs <EOL> ) <EOL> def _patch_multiple ( target , spec = None , create = False , <EOL> mocksignature = False , spec_set = None , autospec = False , <EOL> new_callable = None , ** kwargs <EOL> ) : <EOL> """<STR_LIT>""" <EOL> if type ( target ) in ( unicode , str ) : <EOL> getter = lambda : _importer ( target ) <EOL> else : <EOL> getter = lambda : target <EOL> if not kwargs : <EOL> raise ValueError ( <EOL> '<STR_LIT>' <EOL> ) <EOL> items = list ( kwargs . items ( ) ) <EOL> attribute , new = items [ <NUM_LIT:0> ] <EOL> patcher = _patch ( <EOL> getter , attribute , new , spec , create , mocksignature , spec_set , <EOL> autospec , new_callable , { } <EOL> ) <EOL> patcher . attribute_name = attribute <EOL> for attribute , new in items [ <NUM_LIT:1> : ] : <EOL> this_patcher = _patch ( <EOL> getter , attribute , new , spec , create , mocksignature , spec_set , <EOL> autospec , new_callable , { } <EOL> ) <EOL> this_patcher . attribute_name = attribute <EOL> patcher . additional_patchers . append ( this_patcher ) <EOL> return patcher <EOL> def patch ( <EOL> target , new = DEFAULT , spec = None , create = False , <EOL> mocksignature = False , spec_set = None , autospec = False , <EOL> new_callable = None , ** kwargs <EOL> ) : <EOL> """<STR_LIT>""" <EOL> getter , attribute = _get_target ( target ) <EOL> return _patch ( <EOL> getter , attribute , new , spec , create , mocksignature , <EOL> spec_set , autospec , new_callable , kwargs <EOL> ) <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 ( patch . TEST_PREFIX ) and <EOL> 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> 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> """<STR_LIT>""" <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> patch . multiple = _patch_multiple <EOL> patch . TEST_PREFIX = '<STR_LIT:test>' <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 MagicMixin ( object ) : <EOL> def __init__ ( self , * args , ** kw ) : <EOL> _super ( MagicMixin , self ) . __init__ ( * args , ** kw ) <EOL> self . _mock_set_magics ( ) <EOL> def _mock_set_magics ( self ) : <EOL> these_magics = _magics <EOL> if self . _mock_methods is not None : <EOL> these_magics = _magics . intersection ( self . _mock_methods ) <EOL> remove_magics = _magics - these_magics <EOL> for entry in remove_magics : <EOL> if entry in type ( self ) . __dict__ : <EOL> delattr ( self , entry ) <EOL> for entry in these_magics : <EOL> if entry in type ( self ) . __dict__ : <EOL> continue <EOL> setattr ( self , entry , _create_proxy ( entry , self ) ) <EOL> class NonCallableMagicMock ( MagicMixin , NonCallableMock ) : <EOL> """<STR_LIT>""" <EOL> def mock_add_spec ( self , spec , spec_set = False ) : <EOL> """<STR_LIT>""" <EOL> self . _mock_add_spec ( spec , spec_set ) <EOL> self . _mock_set_magics ( ) <EOL> class MagicMock ( MagicMixin , Mock ) : <EOL> """<STR_LIT>""" <EOL> def mock_add_spec ( self , spec , spec_set = False ) : <EOL> """<STR_LIT>""" <EOL> self . _mock_add_spec ( spec , spec_set ) <EOL> self . _mock_set_magics ( ) <EOL> def _create_proxy ( entry , self ) : <EOL> def create_mock ( ) : <EOL> m = MagicMock ( name = entry , _new_name = entry , _new_parent = self ) <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> def _format_call_signature ( name , args , kwargs ) : <EOL> message = '<STR_LIT>' % name <EOL> formatted_args = '<STR_LIT>' <EOL> args_string = '<STR_LIT:U+002CU+0020>' . join ( [ repr ( arg ) for arg in args ] ) <EOL> kwargs_string = '<STR_LIT:U+002CU+0020>' . join ( [ <EOL> '<STR_LIT>' % ( key , value ) for key , value in kwargs . items ( ) <EOL> ] ) <EOL> if args_string : <EOL> formatted_args = args_string <EOL> if kwargs_string : <EOL> if formatted_args : <EOL> formatted_args += '<STR_LIT:U+002CU+0020>' <EOL> formatted_args += kwargs_string <EOL> return message % formatted_args <EOL> class _Call ( tuple ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( cls , value = ( ) , name = None , parent = None , two = False , <EOL> from_kall = True ) : <EOL> name = '<STR_LIT>' <EOL> args = ( ) <EOL> kwargs = { } <EOL> if len ( value ) == <NUM_LIT:3> : <EOL> name , args , kwargs = value <EOL> elif len ( value ) == <NUM_LIT:2> : <EOL> first , second = value <EOL> if isinstance ( first , basestring ) : <EOL> name = first <EOL> if isinstance ( second , tuple ) : <EOL> args = second <EOL> else : <EOL> kwargs = second <EOL> else : <EOL> args , kwargs = first , second <EOL> elif len ( value ) == <NUM_LIT:1> : <EOL> value , = value <EOL> if isinstance ( value , basestring ) : <EOL> name = value <EOL> elif isinstance ( value , tuple ) : <EOL> args = value <EOL> else : <EOL> kwargs = value <EOL> if two : <EOL> return tuple . __new__ ( cls , ( args , kwargs ) ) <EOL> return tuple . __new__ ( cls , ( name , args , kwargs ) ) <EOL> def __init__ ( self , value = ( ) , name = None , parent = None , two = False , <EOL> from_kall = True ) : <EOL> self . name = name <EOL> self . parent = parent <EOL> self . from_kall = from_kall <EOL> def __eq__ ( self , other ) : <EOL> try : <EOL> len ( other ) <EOL> except TypeError : <EOL> return False <EOL> self_name = '<STR_LIT>' <EOL> if len ( self ) == <NUM_LIT:2> : <EOL> self_args , self_kwargs = self <EOL> else : <EOL> self_name , self_args , self_kwargs = self <EOL> other_name = '<STR_LIT>' <EOL> if len ( other ) == <NUM_LIT:0> : <EOL> other_args , other_kwargs = ( ) , { } <EOL> elif len ( other ) == <NUM_LIT:3> : <EOL> other_name , other_args , other_kwargs = other <EOL> elif len ( other ) == <NUM_LIT:1> : <EOL> value , = other <EOL> if isinstance ( value , tuple ) : <EOL> other_args = value <EOL> other_kwargs = { } <EOL> elif isinstance ( value , basestring ) : <EOL> other_name = value <EOL> other_args , other_kwargs = ( ) , { } <EOL> else : <EOL> other_args = ( ) <EOL> other_kwargs = value <EOL> else : <EOL> first , second = other <EOL> if isinstance ( first , basestring ) : <EOL> other_name = first <EOL> if isinstance ( second , tuple ) : <EOL> other_args , other_kwargs = second , { } <EOL> else : <EOL> other_args , other_kwargs = ( ) , second <EOL> else : <EOL> other_args , other_kwargs = first , second <EOL> if self_name and other_name != self_name : <EOL> return False <EOL> return ( self_args , self_kwargs ) == ( other_args , other_kwargs ) <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> if self . name is None : <EOL> return _Call ( ( '<STR_LIT>' , args , kwargs ) , name = '<STR_LIT>' ) <EOL> name = self . name + '<STR_LIT>' <EOL> return _Call ( ( self . name , args , kwargs ) , name = name , parent = self ) <EOL> def __getattr__ ( self , attr ) : <EOL> if self . name is None : <EOL> return _Call ( name = attr , from_kall = False ) <EOL> name = '<STR_LIT>' % ( self . name , attr ) <EOL> return _Call ( name = name , parent = self , from_kall = False ) <EOL> def __repr__ ( self ) : <EOL> if not self . from_kall : <EOL> name = self . name or '<STR_LIT>' <EOL> if name . startswith ( '<STR_LIT>' ) : <EOL> name = '<STR_LIT>' % name <EOL> return name <EOL> if len ( self ) == <NUM_LIT:2> : <EOL> name = '<STR_LIT>' <EOL> args , kwargs = self <EOL> else : <EOL> name , args , kwargs = self <EOL> if not name : <EOL> name = '<STR_LIT>' <EOL> elif not name . startswith ( '<STR_LIT>' ) : <EOL> name = '<STR_LIT>' % name <EOL> else : <EOL> name = '<STR_LIT>' % name <EOL> return _format_call_signature ( name , args , kwargs ) <EOL> def call_list ( self ) : <EOL> """<STR_LIT>""" <EOL> vals = [ ] <EOL> thing = self <EOL> while thing is not None : <EOL> if thing . from_kall : <EOL> vals . append ( thing ) <EOL> thing = thing . parent <EOL> return _CallList ( reversed ( vals ) ) <EOL> call = _Call ( from_kall = False ) <EOL> def create_autospec ( spec , spec_set = False , instance = False , <EOL> configure = None , _parent = None , _name = None ) : <EOL> """<STR_LIT>""" <EOL> if configure is None : <EOL> configure = { } <EOL> if _is_list ( spec ) : <EOL> spec = type ( spec ) <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> Klass = MagicMock <EOL> if type ( spec ) in DescriptorTypes : <EOL> kwargs = { } <EOL> elif not _callable ( spec ) : <EOL> Klass = NonCallableMagicMock <EOL> elif is_type and instance and not _instance_callable ( spec ) : <EOL> Klass = NonCallableMagicMock <EOL> _new_name = _name <EOL> if _parent is None : <EOL> _new_name = '<STR_LIT>' <EOL> mock = Klass ( parent = _parent , _new_parent = _parent , _new_name = _new_name , <EOL> name = _name , ** kwargs ) <EOL> if isinstance ( spec , FunctionTypes ) : <EOL> mock = _set_signature ( mock , spec ) <EOL> else : <EOL> _check_signature ( spec , mock , is_type ) <EOL> if _parent is not None and not instance : <EOL> _parent . _mock_children [ _name ] = mock <EOL> if is_type and not instance : <EOL> mock . return_value = create_autospec ( spec , spec_set , instance = True , <EOL> _name = '<STR_LIT>' , _parent = mock ) <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 , mock , entry , 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 , _new_name = entry , <EOL> _new_parent = parent , ** 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 , 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 . 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> '<STR_LIT>' , <EOL> ] ) </s>
