j f,@sdddddddddd d d d f Zd ZddlZddlZddlZddlmZmZefZ dej krddl Z ee j j fZ ndZeZddZddZGdddeZeejefZddZdddZddZd d!Zd"d#Zd$d%Zdd&d'Zd(d)Z d*d+Z!Gd,d-d-eZ"Gd.d/d/eZ#e#Z$e$j%Z%e$j&Z'e$j(Z)d0d1Z*e+d2d3d4d5d6d7d8d9gZ,d:d;Z-Gd<d=d=e.Z/d>d?Z0Gd@dAdAeZ1GdBdCdCeZ2GdDd d e2Z3dEdFZ4GdGdHdHe2Z5GdIdde5e3Z6dJdKZ7dLdMZ8dNdOZ9GdPdQdQeZ:dRdSZ;e%ddddddTdUZ<ddddddVdWZ=e%ddddddXdZ>GdYdZdZeZ?d[d\Z@d]d^ZAe<e>_e?e>_Be=e>_CeAe>_Dd_e>_Ed`ZFdaZGdbjHdcddeGjIDZJdbjHdeddeGjIDZKe+dfdddgdhdidjdkdldmdndodpdqdrdsdtdudvdwgDZLdxdyZMe+dzdddbjHeFeGeJeKgjIDZNeNeLBZOe+d{d|d}d~dddgZPiddd6ddd6ddd6ZQi eRd6eRd6eRd6eRd6dd6dd6dd6dd6dd6dd6dd6dd6ZSddZTddZUddZVieTd6eUd6eVd6ZWddZXGdddeZYGdd d eYe3ZZGdddeYe6Z[GdddeZ\GdddeZ]e]Z^ddZ_Gddde`ZaeaddZbddddddZcddZdddZeGdddeZfeecee^jgfZhee^jgjifZjdakddZldddd ZmGdd d e6ZndS)Mock MagicMockpatchsentinelDEFAULTANYcallcreate_autospec FILTER_DIRNonCallableMockNonCallableMagicMock mock_open PropertyMockz1.0N)wrapspartialjavaTcCstt|tS)N) issubclasstyper )objr2/opt/alt/python34/lib64/python3.4/unittest/mock.py_is_instance_mock.srcCs+t|tp*t|to*t|tS)N) isinstanceBaseExceptionsrr)rrrr _is_exception4src@seZdZdgZdS)_slottedaN)__name__ __module__ __qualname__ __slots__rrrrr;s rcCst|trE| rEy |j}Wntk r;dSYnXd}n8t|ts}y |j}Wq}tk rydSYq}Xn|rt|d}n|}y|tj|fSWnt k rdSYnXdS)z Given an arbitrary, possibly callable object, try to create a suitable signature object. Return a (reduced func, signature) tuple, or None. NT) rr__init__AttributeError FunctionTypes__call__rinspectZ signature ValueError)funcZ as_instanceZeat_selfZsig_funcrrr_get_signature_objectEs$        r(Fcs`t|||dkr"dS\}fdd}t|||t|_dS)Ncsj||dS)N)bind) _mock_selfargskwargs)sigrrchecksigjsz"_check_signature..checksig)r(_copy_func_detailsr_mock_check_sig)r'mock skipfirstinstancer.r)r-r_check_signaturees   r4c#Cs|j|_|j|_y|j|_Wntk r<YnXy|j|_Wntk raYnXy|j|_Wntk rYnXy|j|_Wntk rYnXdS)N)r__doc____text_signature__r"r __defaults____kwdefaults__)r'funcopyrrrr/ps$      r/cCs3t|trdSt|dddk r/dSdS)NTr$F)rrgetattr)rrrr _callables r;cCst|ttfkS)N)rlisttuple)rrrr_is_listsr>cCs`t|ts%t|dddk Sx4|f|jD]"}|jjddk r6dSq6WdS)ztGiven an object, return True if the object is callable. For classes, return True if instances would be callable.r$NTF)rrr:__mro____dict__get)rbaserrr_instance_callables rCc st|sdSt|t}t|||}|dkrAdS|\}fdd}t|||j}|jsd}ni|d6|d6}d|} t| |||} t| || S)Ncsj||dS)N)r))r+r,)r-rrr.sz _set_signature..checksigr9Z _checksig_r1zYdef %s(*args, **kwargs): _checksig_(*args, **kwargs) return mock(*args, **kwargs)) r;rrr(r/r isidentifierexec _setup_func) r1originalr3r2resultr'r.namecontextsrcr9r)r-r_set_signatures$           rLcs_tsdSfdd}fdd}fdd}fdd}fd d }d _d _d_t_t_t_j _ j _ j _ |_ |_ |_|_|__dS) Ncsj||S)N)assert_called_with)r+r,)r1rrrMsz'_setup_func..assert_called_withcsj||S)N)assert_called_once_with)r+r,)r1rrrNsz,_setup_func..assert_called_once_withcsj||S)N)assert_has_calls)r+r,)r1rrrOsz%_setup_func..assert_has_callscsj||S)N)assert_any_call)r+r,)r1rrrPsz$_setup_func..assert_any_callcsTt_t_jj}t|rP|k rP|jndS)N) _CallList method_calls mock_calls reset_mock return_valuer)ret)r9r1rrrTs     z_setup_func..reset_mockFr)r1rcalled call_count call_argsrQcall_args_listrRrSrU side_effect_mock_childrenrMrNrOrPrT_mock_delegate)r9r1rMrNrOrPrTr)r9r1rrFs.                rFcCsd|dd|kS)Nz__%s__r)rIrrr _is_magicsr`c@s.eZdZdZddZddZdS)_SentinelObjectz!A unique, named, sentinel object.cCs ||_dS)N)rI)selfrIrrrr!sz_SentinelObject.__init__cCs d|jS)Nz sentinel.%s)rI)rbrrr__repr__sz_SentinelObject.__repr__N)rrrr5r!rcrrrrras  rac@s.eZdZdZddZddZdS) _SentinelzAAccess attributes to return a named object, usable as a sentinel.cCs i|_dS)N) _sentinels)rbrrrr!sz_Sentinel.__init__cCs.|dkrtn|jj|t|S)N __bases__)r"re setdefaultra)rbrIrrr __getattr__s  z_Sentinel.__getattr__N)rrrr5r!rhrrrrrds  rdcCs2t|ttttfkr.t||S|S)N)rdictr<r=set)valuerrr_copy srlrU_mock_return_valuer[_mock_side_effect _mock_parent_mock_new_parent _mock_name_mock_new_namecCsHtj|d|}||dd}||dd}t||S)NZ_mock_cSs/|j}|dkr"t||St||S)N)r]r:)rbrI _the_namer-rrr_gets   z"_delegating_property.._getcSs9|j}|dkr%||j|._set)_allowed_namesaddproperty)rIrsrtrvrrr_delegating_propertys   rzc@s(eZdZddZddZdS)rQcCst|tstj||St|}t|}||krGdSxCtd||dD]*}||||}||kr_dSq_WdS)NFrT)rr< __contains__lenrange)rbrkZ len_valueZlen_selfiZsub_listrrrr|0s    z_CallList.__contains__cCstjt|S)N)pprintZpformatr<)rbrrrrc>sz_CallList.__repr__N)rrrr|rcrrrrrQ.s  rQcCst|sdS|js@|js@|jdk s@|jdk rDdS|}x)|dk ru||kridS|j}qMW|r||_||_n|r||_||_ndS)NFT)rrqrrrorp)parentrkrInew_name_parentrrr_check_and_set_parentBs$       rc@s4eZdZddZddZddZdS) _MockItercCst||_dS)N)iterr)rbrrrrr!\sz_MockIter.__init__cCs|S)Nr)rbrrr__iter__^sz_MockIter.__iter__cCs t|jS)N)nextr)rbrrr__next__`sz_MockIter.__next__N)rrrr!rrrrrrr[s   rc@s(eZdZeZdZddZdS)BaseNcOsdS)Nr)rbr+r,rrrr!fsz Base.__init__)rrrrrmrnr!rrrrrcs rc @seZdZdZddZdddddddddddd Zd d Zdd d Zddd dZddZ ddZ dZ e e e e Z e ddZedZedZedZedZedZddZddZe eeZddd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Z d1d2Z!d3d4Z"d5d6Z#dd7d8Z$d9d:Z%d;d<Z&dS)=r z A non-callable version of `Mock`cOs5t|j|fi|jd6}tj|}|S)Nr5)rrr5object__new__)clsr+kwnewr3rrrrns"zNonCallableMock.__new__NFc Ks4|dkr|}n|j} || d<|| d<|| d<|| d<|dk ra|}d}n| dkr||dk } n|j||| | i| d<|| drr _get_classr(dirr@) rbrrrrrrresr@rrrrs       zNonCallableMock._mock_add_speccCs[|j}|jdk r'|jj}n|tkrW|jd|dd}||_n|S)Nrrz())rmr]rUr_get_child_mock)rbrVrrrZ__get_return_values    z"NonCallableMock.__get_return_valuecCs>|jdk r||j_n||_t||dddS)Nz())r]rUrmr)rbrkrrrZ__set_return_values z"NonCallableMock.__set_return_valuez1The value to be returned when the mock is called.cCs |jdkrt|S|jS)N)rr)rbrrr __class__s zNonCallableMock.__class__rWrXrYrZrScCsj|j}|dkr|jS|j}|dk rft| rft|t rft|}||_n|S)N)r]rnr[callablerr)rb delegatedZsfrrrZ__get_side_effects   )  z!NonCallableMock.__get_side_effectcCs:t|}|j}|dkr-||_n ||_dS)N) _try_iterr]rnr[)rbrkrrrrZ__set_side_effects     z!NonCallableMock.__set_side_effectcCs|dkrg}nt||kr+dS|jt|d|_d|_d|_t|_t|_t|_x9|j j D](}t |t rqn|j |qW|j}t|r||k r|j |ndS)z-Restore the mock object to its initial state.NFr)idappendrWrYrXrQrSrZrRr\valuesr _SpecStaterTrmr)rbZvisitedZchildrVrrrrT s$         zNonCallableMock.reset_mockcKsx}t|jdddD]]\}}|jd}|j}|}x|D]}t||}qSWt|||qWdS)aZSet attributes on the mock through keyword arguments. Attributes plus return values and side effects can be set on child mocks using standard dot notation and unpacking a dictionary in the method call: >>> attrs = {'method.return_value': 3, 'other.side_effect': KeyError} >>> mock.configure_mock(**attrs)keycSs|djdS)Nr.)count)entryrrr0sz0NonCallableMock.configure_mock..rN)sorteditemssplitpopr:ru)rbr,argvalr+finalrrrrrr#s   zNonCallableMock.configure_mockc Cs]|dkrt|n[|jdk r[||jksE|tkrvtd|qvnt|rvt|n|jj|}|tkrt|n|dkrd}|jdk rt|j|}n|j d|d|d|d|d|}||j|)rr)zmockr)rrrprjrrrxr<reversedrqr}joinrrrr) rbZ _name_listrlastdotseenZ_firstrIZ name_stringZ spec_stringrrrrc\sL            zNonCallableMock.__repr__cCststj|S|jpg}tt|}t|j}dd|D}dd|D}tt |||t|j S)z8Filter the output of `dir(mock)` to only useful members.cSs%g|]}|jds|qS)_) startswith).0errr s z+NonCallableMock.__dir__..cSs2g|](}|jd s(t|r|qS)r)rr`)rrrrrrs ) r r__dir__rrrr<r@rrjr\)rbZextrasZ from_typeZ from_dictrrrrs zNonCallableMock.__dir__cs|tkrtj||Sjrhjdk rh|jkrh|jkrhtd|n"|tkrd|}t|n|tkrLjdk r|jkrtd|nt |st t |t |||fdd}qt |d|t t |||j||dkre|_dSt |||r|j|.r)rwr __setattr__rrr@r"_unsupported_magicsrrrur _get_methodrr\r)rbrIrkmsgr)rGrbrrs2       zNonCallableMock.__setattr__cCs|tkrJ|t|jkrJtt||||jkrJdSn||jkrltj||n|jj|t}|t krt |n|tk r|j|=nt |j|._error_message)rYrAssertionErrorrr Exception)r*r+r,expectedractualcauser)r+r,rbrrMs z"NonCallableMock.assert_called_withcOsP|}|jdks@d|jp$d|jf}t|n|j||S)zVassert that the mock was called exactly once and with the specified arguments.r{z1Expected '%s' to be called once. Called %s times.r1)rXrqrrM)r*r+r,rbrrrrrN s z'NonCallableMock.assert_called_once_withc s fdd|D}t|tr.|nd}tfddjD}|s||krtd|jf|ndSt|}g}xA|D]9}y|j|Wqtk r|j|YqXqW|rtdt |f|ndS)aassert the mock has been called with the specified calls. The `mock_calls` list is checked for the calls. If `any_order` is False (the default) then the calls must be sequential. There can be extra calls before or after the specified calls. If `any_order` is True then the calls can be in any order, but they must all appear in `mock_calls`.csg|]}j|qSr)r)rc)rbrrr!s z4NonCallableMock.assert_has_calls..Nc3s|]}j|VqdS)N)r)rr)rbrr #sz3NonCallableMock.assert_has_calls..z(Calls not found. Expected: %r Actual: %rz%r not all found in call list) rrrQrSrr<remover&rr=)rbZcallsZ any_orderrrZ all_callsZ not_foundZkallr)rbrrOs* "     z NonCallableMock.assert_has_callscsj||f}fddjD}||krt|trR|nd}j||}td||ndS)zassert the mock has been called with the specified arguments. The assert passes if the mock has *ever* been called, unlike `assert_called_with` and `assert_called_once_with` that only pass if the call is the most recent one.csg|]}j|qSr)r)rr)rbrrrAs z3NonCallableMock.assert_any_call..Nz%s call not found)rrZrrrr)rbr+r,rrrrr)rbrrP:s  zNonCallableMock.assert_any_callcKset|}t|tsNt|tr3t}q[t|tr[t}q[n |jd}||S)aPCreate the child mocks for attributes and return value. By default child mocks will be the same type as the parent. Subclasses of Mock may want to override this to customize the way child mocks are made. For non-callable mocks the callable variant will be used (rather than any custom subclass).r{)rr CallableMixinr rr rr?)rbr_typeklassrrrrJs    zNonCallableMock._get_child_mock)'rrrr5rr!rrrZ"_NonCallableMock__get_return_valueZ"_NonCallableMock__set_return_valueZ"_NonCallableMock__return_value_docryrUrrzrWrXrYrZrSZ!_NonCallableMock__get_side_effectZ!_NonCallableMock__set_side_effectr[rTrrhrcrrrrrrrMrNrOrPrrrrrr ksL   (             # 2  "     # c Cs[|dkr|St|r |St|r0|Syt|SWntk rV|SYnXdS)N)rr;rr)rrrrr^s    rc @s^eZdZddeddddddddd ZddZddZd d ZdS) rNrc KsH||jd.) rrrrrrrrrr,attribute_namer)rbrrrrr@s  z _patch.copycCs)t|tr|j|S|j|S)N)rrdecorate_classdecorate_callable)rbr'rrrr$Ms z_patch.__call__cCsyxrt|D]d}|jtjs+q nt||}t|dsOq n|j}t||||q W|S)Nr$)rrr TEST_PREFIXr:rrru)rbrattr attr_valuerrrrrSs z_patch.decorate_classcsTtdr#jj|Stfdd|g_S)N patchingscsg}g}t}zyxhjD]]}|j}|j||jdk rc|j|q%|jtkr%|j|q%q%W|t|7}||SWn?||krt|r|j|nt j }YnXWdx!t |D]}|j |qWXdS)N) r=r __enter__rrupdaterrrsysexc_infor__exit__)r+Zkeywargs extra_argsZentered_patchersr patchingr)r'patchedrrr fs*       z)_patch.decorate_callable..patched)rrrr)rbr'r)r'r rras ! z_patch.decorate_callablec Cs|j}|j}t}d}y|j|}Wn*ttfk r^t||t}YnXd}|j r|tkrtd||fn||fS)NFTz!%s does not have the attribute %r)rrrr@r"KeyErrorr:r)rbrrIrGlocalrrr get_originals  z_patch.get_originalcCs|j|j|j}}}|j|j}}|j}|j|_|dkr]d}n|dkrrd}n|dkrd}n|dk r|dk rtdn|dk s|dk r|dkrtdn|j \}}|t kr\|dkr\d} |dkrA|}|dkr|}d}qn?|dk rk|dkr|}d}qn|dkr|}n|dk s|dk r|t krtdnt |t rd} qnt } i} |dk r|} nm|dk s|dk r\|} |dk r"|} nt| r=d| k} n t|  } | r\t} q\n|dk ru|| drr rr rrrrCrrUboolr temp_originalrrurrr)rbrrrrr,rrGrZinheritKlass_kwargsZ this_specZ not_callableZnew_attrr r rrrrrs                                               z_patch.__enter__cGst|stdn|jrO|jtk rOt|j|j|jnOt|j|j|j rt |j|j rt|j|j|jn|`|`|`x3t |j D]"}t|r|j |qqWdS)zUndo the patch.z stop called on unstarted patcherN)r RuntimeErrorrrrrurrrrrrrr )rbr rrrrr s   z_patch.__exit__cCs |j}|jj||S)z-Activate a patch, returning any created mock.)r_active_patchesr)rbrHrrrstart1s z _patch.startc Cs3y|jj|Wntk r(YnX|jS)zStop an active patch.)rrr&r )rbrrrstop8s  z _patch.stop)rrrrrr!rr$rrrrr rrrrrrr s     (  ~  rc sfyjdd\}Wn+ttfk rItdfYnXfdd}||fS)Nrr{z.Need a valid target to patch. You supplied: %rcs tS)N)rr)rrrrJsz_get_target..)rsplitrr&)rrrr)rr _get_targetDsrc s4fdd} t| |||||||| S)a patch the named member (`attribute`) on an object (`target`) with a mock object. `patch.object` can be used as a decorator, class decorator or a context manager. Arguments `new`, `spec`, `create`, `spec_set`, `autospec` and `new_callable` have the same meaning as for `patch`. Like `patch`, `patch.object` takes arbitrary keyword arguments for configuring the mock object it creates. When used as a class decorator `patch.object` honours `patch.TEST_PREFIX` for choosing which methods to wrap. csS)Nrr)rrrr`sz_patch_object..)r) rrrrrrrrr,rr)rr _patch_objectNsrc sttkr'fdd}nfdd}|sNtdnt|j}|d\} } t|| | |||||i } | | _x[|ddD]I\} } t|| | |||||i } | | _| jj| qW| S)aPerform multiple patches in a single call. It takes the object to be patched (either as an object or a string to fetch the object by importing) and keyword arguments for the patches:: with patch.multiple(settings, FIRST_PATCH='one', SECOND_PATCH='two'): ... Use `DEFAULT` as the value if you want `patch.multiple` to create mocks for you. In this case the created mocks are passed into a decorated function by keyword, and a dictionary is returned when `patch.multiple` is used as a context manager. `patch.multiple` can be used as a decorator, class decorator or a context manager. The arguments `spec`, `spec_set`, `create`, `autospec` and `new_callable` have the same meaning as for `patch`. These arguments will be applied to *all* patches done by `patch.multiple`. When used as a class decorator `patch.multiple` honours `patch.TEST_PREFIX` for choosing which methods to wrap. cs tS)N)rr)rrrr~sz!_patch_multiple..csS)Nrr)rrrrsz=Must supply at least one keyword argument with patch.multiplerr{N) rstrr&r<rrrrr) rrrrrrr,rrrrrZ this_patcherr)rr_patch_multiplegs&   rc Ks4t|\}} t|| ||||||| S)a `patch` acts as a function decorator, class decorator or a context manager. Inside the body of the function or with statement, the `target` is patched with a `new` object. When the function/with statement exits the patch is undone. If `new` is omitted, then the target is replaced with a `MagicMock`. If `patch` is used as a decorator and `new` is omitted, the created mock is passed in as an extra argument to the decorated function. If `patch` is used as a context manager the created mock is returned by the context manager. `target` should be a string in the form `'package.module.ClassName'`. The `target` is imported and the specified object replaced with the `new` object, so the `target` must be importable from the environment you are calling `patch` from. The target is imported when the decorated function is executed, not at decoration time. The `spec` and `spec_set` keyword arguments are passed to the `MagicMock` if patch is creating one for you. In addition you can pass `spec=True` or `spec_set=True`, which causes patch to pass in the object being mocked as the spec/spec_set object. `new_callable` allows you to specify a different class, or callable object, that will be called to create the `new` object. By default `MagicMock` is used. A more powerful form of `spec` is `autospec`. If you set `autospec=True` then the mock will be created with a spec from the object being replaced. All attributes of the mock will also have the spec of the corresponding attribute of the object being replaced. Methods and functions being mocked will have their arguments checked and will raise a `TypeError` if they are called with the wrong signature. For mocks replacing a class, their return value (the 'instance') will have the same spec as the class. Instead of `autospec=True` you can pass `autospec=some_object` to use an arbitrary object as the spec instead of the one being replaced. By default `patch` will fail to replace attributes that don't exist. If you pass in `create=True`, and the attribute doesn't exist, patch will create the attribute for you when the patched function is called, and delete it again afterwards. This is useful for writing tests against attributes that your production code creates at runtime. It is off by default because it can be dangerous. With it switched on you can write passing tests against APIs that don't actually exist! Patch can be used as a `TestCase` class decorator. It works by decorating each test method in the class. This reduces the boilerplate code when your test methods share a common patchings set. `patch` finds tests by looking for method names that start with `patch.TEST_PREFIX`. By default this is `test`, which matches the way `unittest` finds tests. You can specify an alternative prefix by setting `patch.TEST_PREFIX`. Patch can be used as a context manager, with the with statement. Here the patching applies to the indented block after the with statement. If you use "as" then the patched object will be bound to the name after the "as"; very useful if `patch` is creating a mock object for you. `patch` takes arbitrary keyword arguments. These will be passed to the `Mock` (or `new_callable`) on construction. `patch.dict(...)`, `patch.multiple(...)` and `patch.object(...)` are available for alternate use-cases. )rr) rrrrrrrr,rrrrrrsEc@s|eZdZdZfdddZddZddZd d Zd d Zd dZ ddZ eZ e Z dS) _patch_dicta# Patch a dictionary, or dictionary like object, and restore the dictionary to its original state after the test. `in_dict` can be a dictionary or a mapping like container. If it is a mapping then it must at least support getting, setting and deleting items plus iterating over keys. `in_dict` can also be a string specifying the name of the dictionary, which will then be fetched by importing it. `values` can be a dictionary of values to set in the dictionary. `values` can also be an iterable of `(key, value)` pairs. If `clear` is True then the dictionary will be cleared before the new values are set. `patch.dict` can also be called with arbitrary keyword arguments to set values in the dictionary:: with patch.dict('sys.modules', mymodule=Mock(), other_module=Mock()): ... `patch.dict` can be used as a context manager, decorator or class decorator. When used as a class decorator `patch.dict` honours `patch.TEST_PREFIX` for choosing which methods to wrap. FcKs\t|trt|}n||_t||_|jj|||_d|_dS)N) rrrin_dictrirrclear _original)rbr rr!r,rrrr!s  z_patch_dict.__init__csAttrjStfdd}|S)Nc s-jz||SWdjXdS)N)r _unpatch_dict)r+r)frbrr_inners z$_patch_dict.__call__.._inner)rrrr)rbr$r%r)r$rbrr$ s !z_patch_dict.__call__cCsx~t|D]p}t||}|jtjr t|dr t|j|j|j }||}t |||q q W|S)Nr$) rr:rrrrrr rr!ru)rbrrrZ decoratorZ decoratedrrrrs z_patch_dict.decorate_classcCs|jdS)zPatch the dict.N)r)rbrrrr%sz_patch_dict.__enter__cCs|j}|j}|j}y|j}Wn7tk rdi}x|D]}||||.method)r)rIr'r+r)r'rrs rccs|]}d|VqdS)z__%s__Nr)rr+rrrrsrhrr!rZ__prepare____instancecheck____subclasscheck____del__cCs tj|S)N)r__hash__)rbrrrrsrr3cCs tj|S)N)r__str__)rbrrrrsr4cCs tj|S)N)r __sizeof__)rbrrrrsr5__lt____gt____le____ge__r{__int__r|__len__r y? __complex__g? __float____bool__ __index__csfdd}|S)Ncs&jj}|tk r|S|kS)N)__eq__rmr)otherr)rbrrr@s  z_get_eq..__eq__r)rbr@r)rbr_get_eqsrBcsfdd}|S)Ncs jjtk rtS|k S)N)__ne__rmr)rA)rbrrrCsz_get_ne..__ne__r)rbrCr)rbr_get_nesrDcsfdd}|S)Ncs,jj}|tkr"tgSt|S)N)rrmrr)r)rbrrrs   z_get_iter..__iter__r)rbrr)rbr _get_itersrEr@rCrc Cstj|t}|tk r+||_dStj|}|dk ry||}Wntk rvt|}YnX||_dStj|}|dk r|||_ndS)N)_return_valuesrArrU_calculate_return_valuer"_side_effect_methodsr[)r1r+rIZfixedZreturn_calulatorrUZ side_effectorrrr_set_return_values      rIc@s(eZdZddZddZdS) MagicMixincOs1|jtt|j|||jdS)N)_mock_set_magicsrrJr!)rbr+rrrrr!s zMagicMixin.__init__cCst}t|dddk r|tj|j}t}t|}x6|D]+}|t|jkrJt||qJqJWn|tt|j}t|}x'|D]}t||t ||qWdS)Nr) _magicsr: intersectionrrjrr@rru MagicProxy)rbZ these_magicsZ remove_magicsrrrrrrKs     zMagicMixin._mock_set_magicsN)rrrr!rKrrrrrJs  rJc@s%eZdZdZdddZdS)r z-A version of `MagicMock` that isn't callable.FcCs|j|||jdS)zAdd a spec to a mock. `spec` can either be an object or a list of strings. Only attributes on the `spec` can be fetched as attributes from the mock. If `spec_set` is True then only attributes on the spec can be set.N)rrK)rbrrrrrrsz"NonCallableMagicMock.mock_add_specN)rrrr5rrrrrr s c@s%eZdZdZdddZdS)ra MagicMock is a subclass of Mock with default implementations of most of the magic methods. You can use MagicMock without having to configure the magic methods yourself. If you use the `spec` or `spec_set` arguments then *only* magic methods that exist in the spec will be created. Attributes and the return value of a `MagicMock` will also be `MagicMocks`. FcCs|j|||jdS)zAdd a spec to a mock. `spec` can either be an object or a list of strings. Only attributes on the `spec` can be fetched as attributes from the mock. If `spec_set` is True then only attributes on the spec can be set.N)rrK)rbrrrrrr(szMagicMock.mock_add_specN)rrrr5rrrrrrs c@sCeZdZddZddZddZddd ZdS) rNcCs||_||_dS)N)rIr)rbrIrrrrr!4s zMagicProxy.__init__cOs|j}|||S)N) create_mock)rbr+r,mrrrr$8s zMagicProxy.__call__cCsT|j}|j}|jd|d|d|}t|||t||||S)NrIrr)rIrrrurI)rbrrrPrrrrO<s   zMagicProxy.create_mockNcCs |jS)N)rO)rbrrrrr__get__EszMagicProxy.__get__)rrrr!r$rOrQrrrrrN3s    rNc@s:eZdZdZddZddZddZdS) _ANYz2A helper object that compares equal to everything.cCsdS)NTr)rbrArrrr@Msz _ANY.__eq__cCsdS)NFr)rbrArrrrCPsz _ANY.__ne__cCsdS)Nzr)rbrrrrcSsz _ANY.__repr__N)rrrr5r@rCrcrrrrrRJs   rRcCsd|}d}djdd|D}djdd|jD}|r]|}n|r|rv|d7}n||7}n||S)Nz%s(%%s)rz, cSsg|]}t|qSr)r/)rrrrrr]s z*_format_call_signature..cSs&g|]\}}d||fqS)z%s=%rr)rrrkrrrr_s )rr)rIr+r,rZformatted_argsZ args_stringZ kwargs_stringrrrrZs    rc@seZdZdZfddddddZfddddddZd d Zd d Zd dZddZ ddZ ddZ dS)ra A tuple for holding the results of a call to a mock, either in the form `(args, kwargs)` or `(name, args, kwargs)`. If args or kwargs are empty then a call tuple will compare equal to a tuple without those values. This makes comparisons less verbose:: _Call(('name', (), {})) == ('name',) _Call(('name', (1,), {})) == ('name', (1,)) _Call(((), {'a': 'b'})) == ({'a': 'b'},) The `_Call` object provides a useful shortcut for comparing with call:: _Call(((1, 2), {'a': 3})) == call(1, 2, a=3) _Call(('foo', (1, 2), {'a': 3})) == call.foo(1, 2, a=3) If the _Call has no name then it will match any name. NFTc Csd}f}i}t|}|dkr<|\}}}n|dkr|\} } t| tr| }t| tr| }q| }q| | }}nN|dkr|\}t|tr|}qt|tr|}q|}n|rtj|||fStj||||fS)Nrrr^r{)r}rrr=r) rrkrIrr from_kallr+r,_lenfirstsecondrrrrs0           z _Call.__new__cCs||_||_||_dS)N)rIrrS)rbrkrIrrrSrrrr!s  z_Call.__init__c Cs|tkrdSyt|}Wntk r8dSYnXd}t|dkr`|\}}n|\}}}d}|dkrfi}}n|dkr|\}}}n|dkr|\} t| tr| }i}qt| tr| }fi}}qf}| }np|dkr|\} } t| trr| }t| trb| i}}qf| }}q| | }}ndS|r||krdS||f||fkS)NTFrr^rrr{)rr}rrr=r) rbrAZ len_otherZ self_nameZ self_argsZ self_kwargsZ other_nameZ other_argsZ other_kwargsrkrUrVrrrr@sJ           z _Call.__eq__cCs|j| S)N)r@)rbrArrrrCsz _Call.__ne__cOsW|jdkr(td||fddS|jd}t|j||fd|d|S)NrrIz()r)rIr)rbr+r,rIrrrr$s z_Call.__call__cCsN|jdkr"td|ddSd|j|f}td|d|ddS)NrIrSFz%s.%sr)rIr)rbrrIrrrrhsz_Call.__getattr__cCs|js8|jpd}|jdr4d|}n|St|dkr_d}|\}}nD|\}}}|s}d}n&|jdsd|}n d|}t|||S)Nrz()zcall%sr^zcall.%s)rSrIrr}r)rbrIr+r,rrrrcs     z_Call.__repr__cCsQg}|}x2|dk r@|jr4|j|n|j}qWtt|S)zFor a call object that represents multiple calls, `call_list` returns a list of all the intermediate calls as well as the final call.N)rSrrrQr)rbvalsrrrr call_lists  z_Call.call_list) rrrr5rr!r@rCr$rhrcrXrrrrrls    3    rrScKst|rt|}nt|t}i|d6}|rMi|d6}n|dkrbi}n|r{|r{d|drrrrDescriptorTypesr;r rCrr#rLr4r\rrUrr`r:r"rr1 _must_skipru)rrr3rrr,is_typerrrr1rrGrrr2rrrrst                      cCst|ts7|t|dikr+dS|j}nxt|jD]i}|jj|t}|tkrnqAnt|tt frdStt|ddt r|SdSqAW|S)z[ Return whether we should skip the first argument on spec's `entry` attribute. r@FrQN) rrr:rr?r@rAr staticmethod classmethodMethodWrapperTypes)rrr[rrHrrrrZs  rZc Cs.y |jSWntk r)t|SYnXdS)N)rr"r)rrrrrs  rc@s+eZdZdddddddZdS)rFNcCs:||_||_||_||_||_||_dS)N)ridsrrr3rI)rbrrrrIr_r3rrrr!s      z_SpecState.__init__)rrrr!rrrrrs rc#st|trdndfdd|jD}|dkr`|dd}n|ddd |d .r{rcrcrcrc)rbytesr) read_dataZ data_as_listliner)rbr_iterate_read_datas" rgrcswfdd}fdd}fddtdkrddl}ttt|jjtt|jan|dkrtd d d t }ntd tj _ t dgdj _ dj_ dj_ dj_ |j_d <d j_|j_fd d}||_|_ |S)a A helper function to create a mock to replace the use of `open`. It works for `open` called directly or used as a context manager. The `mock` argument is the mock object to configure. If `None` (the default) then a `MagicMock` will be created for you, with the API limited to methods or attributes available on standard file handles. `read_data` is a string for the `read` methoddline`, and `readlines` of the file handle to return. This is an empty string by default. cs*jjdk rjjStdS)Nr) readlinesrUr<)r+r,)_statehandlerr_readlines_side_effects z)mock_open.._readlines_side_effectcs6jjdk rjjStjdS)Nr)readrUrr)r+r,)rirjrerr_read_side_effects z$mock_open.._read_side_effectc3sEjjdk r'xjjVqWnxdD] }|Vq2WdS)Nr)readlinerU)rf)rirjrr_readline_side_effects z(mock_open.._readline_side_effectNrrIopenrr{csJtd<jjdkrFd<dj_ntS)Nrr{)rgrnr[r)r+r,)rorirjrerr reset_data s  zmock_open..reset_data) file_spec_ior<rjr TextIOWrapperunionBytesIOrrprrUrgwriterlrnrhr[)r1rerkrmrsrqr)rorirjrerr s.   6           c@s:eZdZdZddZddZddZdS) r aW A mock intended to be used as a property, or other descriptor, on a class. `PropertyMock` provides `__get__` and `__set__` methods so you can specify a return value when it is fetched. Fetching a `PropertyMock` instance from an object calls the mock, with no args. Setting it calls the mock with the value being set. cKs t|S)N)r)rbr,rrrr* szPropertyMock._get_child_mockcCs|S)Nr)rbrZobj_typerrrrQ- szPropertyMock.__get__cCs||dS)Nr)rbrrrrr__set__/ szPropertyMock.__set__N)rrrr5rrQrxrrrrr ! s   )o__all__ __version__r%rr functoolsrr BaseExceptionrplatformrZlangZ Throwabler superrrrrrrrryrYr(r4r/r;r>rCrLrFr`rardrrZMISSINGrZDELETEDrrlrjrwrzr<rQrrrr rrrrrrrrrrrrr&r(riZmultipleZstopallrZ magic_methodsZnumericsrrZinplacerightZ _non_defaultsrrLrrrGNotImplementedrFrBrDrErHrIrJr rrNrRrrr=rrrrZrrr@r#rQr^rrrgr r rrrrs                +            e5  %   1 Jv      ""   (               w    B