B � f�C�@s\dZdZddlZddlZddlZddlZddlZddlZddlm Z m Z ddl m Z m Z dd�ee�D�ZefZdejkr�ddlZeejjfZd ZeZd d �Zd d �Zdd�Zdd�Zd�dd�Zdd�Zdd�Zdd�Z dd�Z!d�dd�Z"dd �Z#d!d"�Z$Gd#d$�d$e%�Z&Gd%d&�d&e%�Z'e'�Z(e(j)Z)e(j*Z+e(j,Z-d'd(�Z.d)d*d+d,d-d.d/d0hZ/d1d2�Z0Gd3d4�d4e1�Z2d5d6�Z3Gd7d8�d8e%�Z4Gd9d:�d:e%�Z5Gd;d<�d�Z7Gd?d@�d@e5�Z8GdAdB�dBe8e6�Z9dCdD�Z:dEdF�Z;dGdH�Ze)dddddfdMdN�Z?d�dOdP�Z@e)dddddfdQdR�ZAGdSdT�dTe%�ZBdUdV�ZCdWdX�ZDe?eA_%eBeA_Ee@eA_FeDeA_GdYeA_HdZZId[ZJd\�Kd]d^�eJ�L�D��ZMd\�Kd_d^�eJ�L�D��ZNd`dadbdcdddedfdgdhdidjdkdldmdndodpdqhZOdrds�ZPdtd�d\�KeIeJeMeNg��L�D�ZQeQeOBZRdudvdwdxdydzd{d|hZSd}d~�dd~�d�d~�d��ZTeUeUeUeUd�dddd�d�d d�d�� ZVd�d��ZWd�d��ZXd�d��ZYeWeXeYd��ZZd�d��Z[Gd�d��d�e%�Z\Gd�d��d�e\e6�Z]Gd�d��d�e\e9�Z^Gd�d��d�e%�Z_Gd�d��d�e%�Z`e`�Zad�d��ZbGd�d��d�ec�Zdeddd��Zed�d�d��Zfd�d��Zgd�d��ZhGd�d��d�e%�Ziejef�ejeajk�fZldamd�d��Znd�d�d��ZoGd�d��d�e9�Zpd�d��ZqdS)�)�Mock� MagicMock�patch�sentinel�DEFAULT�ANY�call�create_autospec� FILTER_DIR�NonCallableMock�NonCallableMagicMock� mock_open� PropertyMock�sealz1.0�N)� ModuleType� MethodType)�wraps�partialcCsh|]}|�d�s|�qS)�_)� startswith)�.0�name�r�2/opt/alt/python37/lib64/python3.7/unittest/mock.py� &sr�javaTcCstt|�t�S)N)� issubclass�typer )�objrrr�_is_instance_mock5srcCst|t�pt|t�ot|t�S)N)� isinstance�BaseExceptionsrr)rrrr� _is_exception;s r"cCs"t|t�rt|d�r|jS|SdS)N�mock)r � FunctionTypes�hasattrr#)rrrr� _extract_mockBsr&cCs�t|t�r4|s4y |j}Wntk r,dSXd}n*t|t�s^y |j}Wntk r\dSX|rnt|d�}n|}y|t�|�fSt k r�dSXdS)z� Given an arbitrary, possibly callable object, try to create a suitable signature object. Return a (reduced func, signature) tuple, or None. NT) r r�__init__�AttributeErrorr$�__call__r�inspectZ signature� ValueError)�funcZ as_instanceZeat_selfZsig_funcrrr�_get_signature_objectKs$    r-FcsNt|||���dkrdS�\}��fdd�}t||�|t|�_�t|�_dS)Ncs�j||�dS)N)�bind)� _mock_self�args�kwargs)�sigrr�checksigpsz"_check_signature..checksig)r-�_copy_func_detailsr�_mock_check_sig� __signature__)r,r#� skipfirst�instancer3r)r2r�_check_signatureks    r9c Cs>x8dD]0}yt||t||��Wqtk r4YqXqWdS)N)�__name__�__doc__�__text_signature__� __module__� __defaults__�__kwdefaults__)�setattr�getattrr()r,�funcopy� attributerrrr4ws r4cCs@t|t�rdSt|tttf�r(t|j�St|dd�dk r.checksigrB)Z _checksig_r#zYdef %s(*args, **kwargs): _checksig_(*args, **kwargs) return mock(*args, **kwargs)) rFr rr-r4r:� isidentifier�exec� _setup_func) r#�originalr8r7�resultr,r3r�context�srcrBr)r2r�_set_signature�s$       rWc s���_t��sdS�fdd�}�fdd�}�fdd�}�fdd�}�fd d �}�fd d �}�fd d�} ��fdd�} d�_d�_d�_t��_t��_t��_�j �_ �j �_ �j �_ |�_ |�_ |�_| �_| �_|�_|�_|�_|�_��_dS)Ncs �j||�S)N)�assert_called_with)r0r1)r#rrrX�sz'_setup_func..assert_called_withcs �j||�S)N)� assert_called)r0r1)r#rrrY�sz"_setup_func..assert_calledcs �j||�S)N)�assert_not_called)r0r1)r#rrrZ�sz&_setup_func..assert_not_calledcs �j||�S)N)�assert_called_once)r0r1)r#rrr[�sz'_setup_func..assert_called_oncecs �j||�S)N)�assert_called_once_with)r0r1)r#rrr\�sz,_setup_func..assert_called_once_withcs �j||�S)N)�assert_has_calls)r0r1)r#rrr]�sz%_setup_func..assert_has_callscs �j||�S)N)�assert_any_call)r0r1)r#rrr^�sz$_setup_func..assert_any_callcs:t��_t��_����j}t|�r6|�k r6|��dS)N)� _CallList� method_calls� mock_calls� reset_mock� return_valuer)�ret)rBr#rrrb�s z_setup_func..reset_mockFr)r#r�called� call_count� call_argsr_�call_args_listr`rarc� side_effect�_mock_childrenrXr\r]r^rbrYrZr[r6�_mock_delegate) rBr#r2rXrYrZr[r\r]r^rbr)rBr#rrR�s<       rRcCsd|dd�|kS)Nz__%s__������r)rrrr� _is_magic�srnc@s(eZdZdZdd�Zdd�Zdd�ZdS) �_SentinelObjectz!A unique, named, sentinel object.cCs ||_dS)N)r)�selfrrrrr'�sz_SentinelObject.__init__cCs d|jS)Nz sentinel.%s)r)rprrr�__repr__�sz_SentinelObject.__repr__cCs d|jS)Nz sentinel.%s)r)rprrr� __reduce__sz_SentinelObject.__reduce__N)r:r=� __qualname__r;r'rqrrrrrrro�sroc@s(eZdZdZdd�Zdd�Zdd�ZdS) � _SentinelzAAccess attributes to return a named object, usable as a sentinel.cCs i|_dS)N)� _sentinels)rprrrr'sz_Sentinel.__init__cCs|dkr t�|j�|t|��S)N� __bases__)r(ru� setdefaultro)rprrrr� __getattr__ sz_Sentinel.__getattr__cCsdS)Nrr)rprrrrrsz_Sentinel.__reduce__N)r:r=rsr;r'rxrrrrrrrtsrtcCs$t|�ttttfkr t|�|�S|S)N)r�dictrHrI�set)�valuerrr�_copys r|rc�_mock_return_valueri�_mock_side_effect� _mock_parent�_mock_new_parent� _mock_name�_mock_new_namecCs8t�|�d|}||fdd�}||fdd�}t||�S)NZ_mock_cSs"|j}|dkrt||�St||�S)N)rkrA)rpr� _the_namer2rrr�_get+s z"_delegating_property.._getcSs*|j}|dkr||j|<n t|||�dS)N)rkrLr@)rpr{rr�r2rrr�_set0s z"_delegating_property.._set)�_allowed_names�add�property)rr�r�r�rrr�_delegating_property(s  r�c@seZdZdd�Zdd�ZdS)r_cCsnt|t�st�||�St|�}t|�}||kr2dSx6td||d�D] }||||�}||krFdSqFWdS)NFr�T)r rH� __contains__�len�range)rpr{Z len_valueZlen_self�iZsub_listrrrr�=s  z_CallList.__contains__cCst�t|��S)N)�pprintZpformatrH)rprrrrqKsz_CallList.__repr__N)r:r=rsr�rqrrrrr_;sr_cCs�t|�}t|�sdS|js4|js4|jdk s4|jdk r8dS|}x|dk rZ||krRdS|j}q>W|rl||_||_|r|||_||_dS)NFT)r&rr�r�rr�)�parentr{r�new_name�_parentrrr�_check_and_set_parentOs&     r�c@s$eZdZdd�Zdd�Zdd�ZdS)� _MockItercCst|�|_dS)N)�iterr)rprrrrr'ksz_MockIter.__init__cCs|S)Nr)rprrr�__iter__msz_MockIter.__iter__cCs t|j�S)N)�nextr)rprrr�__next__osz_MockIter.__next__N)r:r=rsr'r�r�rrrrr�jsr�c@seZdZeZdZdd�ZdS)�BaseNcOsdS)Nr)rpr0r1rrrr'usz Base.__init__)r:r=rsrr}r~r'rrrrr�rsr�c @sXeZdZdZdd�ZdHdd�Zd d �ZdId d �ZdJd d�Zdd�Z dd�Z dZ e e e e �Z e dd��Zed�Zed�Zed�Zed�Zed�Zdd�Zdd�Ze ee�ZdKddd�d d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�Z d2d3�Z!d4d5�Z"d6d7�Z#d8d9�Z$d:d;�Z%dd?�Z'd@dA�Z(dLdBdC�Z)dDdE�Z*dFdG�Z+dS)Mr z A non-callable version of `Mock`cOs$t|j|fd|ji�}t�|�}|S)Nr;)rr:r;�object�__new__)�clsr0�kw�newr8rrrr�}s zNonCallableMock.__new__N�Fc Ks�|dkr |}|j} || d<|| d<|| d<|| d<d| d<|dk rJ|}d}| dkrZ|dk } |�||| | �i| d<|| d <d| d <d| d <d| d <d | d<t�| d<t�| d<t�| d<| | d<| r�|jf| �tt|��||||||�dS)Nrr�r�r�F� _mock_sealedTrj� _mock_wrapsrkZ _mock_calledZ_mock_call_argsrZ_mock_call_countZ_mock_call_args_listZ_mock_mock_callsr`� _mock_unsafe)rL�_mock_add_specr_�configure_mock� _safe_superr r')rp�specrr�spec_setr�� _spec_state� _new_name� _new_parent�_spec_as_instance� _eat_selfZunsafer1rLrrrr'�s:      zNonCallableMock.__init__cCs0t|�}d|_d|_d|_d|_t|||�dS)z� Attach a mock as an attribute of this one, replacing its name and parent. Calls to the attached mock will be recorded in the `method_calls` and `mock_calls` attributes of this one.Nr�)r&rr�r�r�r@)rpr#rCZ inner_mockrrr� attach_mock�s zNonCallableMock.attach_mockcCs|�||�dS)z�Add 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)r�)rpr�r�rrr� mock_add_spec�szNonCallableMock.mock_add_specc Cszd}d}|dk rPt|�sPt|t�r(|}nt|�}t|||�}|oF|d}t|�}|j}||d<||d<||d<||d<dS)Nr�� _spec_class� _spec_set�_spec_signature� _mock_methods)rJr r� _get_classr-�dirrL) rpr�r�r�r�r�r�ZresrLrrrr��s  zNonCallableMock._mock_add_speccCs8|j}|jdk r|jj}|tkr4|j|dd�}||_|S)Nz())r�r�)r}rkrcr�_get_child_mock)rprdrrrZ__get_return_value�s  z"NonCallableMock.__get_return_valuecCs,|jdk r||j_n||_t||dd�dS)Nz())rkrcr}r�)rpr{rrrZ__set_return_value�s  z"NonCallableMock.__set_return_valuez1The value to be returned when the mock is called.cCs|jdkrt|�S|jS)N)r�r)rprrr� __class__�s zNonCallableMock.__class__rerfrgrhracCsN|j}|dkr|jS|j}|dk rJt|�sJt|t�sJt|�sJt|�}||_|S)N)rkr~ri�callabler r�r")rp� delegatedZsfrrrZ__get_side_effectsz!NonCallableMock.__get_side_effectcCs(t|�}|j}|dkr||_n||_dS)N)� _try_iterrkr~ri)rpr{r�rrrZ__set_side_effects z!NonCallableMock.__set_side_effect)rcricCs�|dkr g}t|�|krdS|�t|��d|_d|_d|_t�|_t�|_t�|_|r^t |_ |rhd|_ x0|j � �D]"}t|t�st|tkr�qt|�|�qtW|j }t|�r�||k r�|�|�dS)z-Restore the mock object to its initial state.NFr)�id�appendrergrfr_rarhr`rr}r~rj�valuesr � _SpecState�_deletedrbr)rpZvisitedrcri�childrdrrrrbs, zNonCallableMock.reset_mockcKs`xZt|��dd�d�D]B\}}|�d�}|��}|}x|D]}t||�}q:Wt|||�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)cSs|d�d�S)Nr�.)�count)�entryrrr�I�z0NonCallableMock.configure_mock..)�keyr�N)�sorted�items�split�poprAr@)rpr1�arg�valr0�finalrr�rrrr�<s   zNonCallableMock.configure_mockcCs�|dkrt|��n:|jdk r<||jks.|tkrLtd|��nt|�rLt|��|jsd|�d�rdt|��|j�|�}|tkr�t|��np|dkr�d}|j dk r�t |j |�}|j |||||d�}||j|<n.t |t �r�t|j|j|j|j|j�}||j|<|S)N>r�r�zMock object has no attribute %r)�assertZassret)r�rrr�r�)r(r�� _all_magicsrnr�rrjrMr�r�rAr�r r�rr�r�r8r�r)rprrTrrrrrxRs6            zNonCallableMock.__getattr__cCs�|jg}|j}|}d}|dgkr$d}t�}xT|dk r~|}|�|j|�d}|jdkrZd}|j}t|�|krnP|�t|��q,Wtt|��}|jp�d}t |�dkr�|ddkr�|d7}||d<d� |�S)Nr�z()r�r#r�)z()z().r) r�r�rzr�r�r�rH�reversedr�r��join)rpZ _name_listr�Zlast�dot�seenZ_firstrrr�_extract_mock_namexs0        z"NonCallableMock._extract_mock_namecCs^|��}d}|dkrd|}d}|jdk rDd}|jr8d}||jj}dt|�j||t|�fS)Nr�)r#zmock.z name=%rz spec=%rz spec_set=%rz<%s%s%s id='%s'>)r�r�r�r:rr�)rprZ name_stringZ spec_stringrrrrq�s  zNonCallableMock.__repr__cCsvtst�|�S|jpg}tt|��}t|j�}dd�|j� �D�}dd�|D�}dd�|D�}t t ||||��S)z8Filter the output of `dir(mock)` to only useful members.cSsg|]\}}|tk r|�qSr)r�)rZm_nameZm_valuerrr� �sz+NonCallableMock.__dir__..cSsg|]}|�d�s|�qS)r)r)r�errrr��scSs"g|]}|�d�rt|�r|�qS)r)rrn)rr�rrrr��s ) r r��__dir__r�r�rrHrLrjr�r�rz)rpZextrasZ from_typeZ from_dictZfrom_child_mocksrrrr��s    zNonCallableMock.__dir__csT|tkrt��||�S�jrH�jdk rH|�jkrH|�jkrHtd|��n�|tkrbd|}t|��n�|tkr�jdk r�|�jkr�td|��t |�s�t t ��|t ||��|���fdd�}n(t �|d|�t t ��||�|�j|<n,|dkr�|�_dSt �|||��r|�j|<�j�rFt�|��sF����d|��}td|����t��||�S)Nz!Mock object has no attribute '%s'z.Attempting to set unsupported magic method %r.cs��f|�|�S)Nr)r0r�)rSrprrr��r�z-NonCallableMock.__setattr__..r�r�z Cannot set )r�r�� __setattr__r�r�rLr(�_unsupported_magicsr�rr@r� _get_methodr�rjr�r�r%r�)rprr{�msg� mock_namer)rSrprr��s8      zNonCallableMock.__setattr__cCs�|tkr2|t|�jkr2tt|�|�||jkr2dS|j�|t�}||jkr\tt|�� |�n|t krlt |��|tk r||j|=t |j|<dS)N) r�rrL�delattrrjrM�_missingr�r � __delattr__r�r()rprrrrrr��s  zNonCallableMock.__delattr__cCs|jpd}t|||�S)Nr#)r��_format_call_signature)rpr0r1rrrr�_format_mock_call_signature�s z+NonCallableMock._format_mock_call_signaturecCsDd}|�||�}|j}t|�dkr.|dd�}|j|�}|||fS)Nz!Expected call: %s Actual call: %s�r�)r�rgr�)rpr0r1�message�expected_stringrgZ actual_stringrrr�_format_mock_failure_message�s    z,NonCallableMock._format_mock_failure_messagecCsl|s |jSd}|�dd��d�}|j}x@|D]8}|�|�}|dksLt|t�rPPq,t|�}|j}|j}q,W|S)aH * If call objects are asserted against a method/function like obj.meth1 then there could be no name for the call object to lookup. Hence just return the spec_signature of the method/function being asserted against. * If the name is not empty then remove () and split by '.' to get list of names to iterate through the children until a potential match is found. A child mock is created only during attribute access so if we get a _SpecState then no attributes of the spec were accessed and can be safely exited. Nz()r�r�)r��replacer�rjrMr r�r&)rprr2�namesZchildrenr�rrr�_get_call_signature_from_names    z-NonCallableMock._get_call_signature_from_namec Cs�t|t�r&t|�dkr&|�|d�}n|j}|dk r�t|�dkrNd}|\}}n |\}}}y||j||�fStk r�}z |�d�Sd}~XYq�Xn|SdS)a Given a call (or simply an (args, kwargs) tuple), return a comparison key suitable for matching with other calls. This is a best effort method which relies on the spec's signature, if available, or falls back on the arguments themselves. rlrNr�)r rIr�r�r�r.� TypeError�with_traceback)rp�_callr2rr0r1r�rrr� _call_matcher(s   zNonCallableMock._call_matchercCs.|}|jdkr*d|jpd|jf}t|��dS)z/assert that the mock was never called. rz7Expected '%s' to not have been called. Called %s times.r#N)rfr��AssertionError)r/rpr�rrrrZBs  z!NonCallableMock.assert_not_calledcCs(|}|jdkr$d|jpd}t|��dS)z6assert that the mock was called at least once rz"Expected '%s' to have been called.r#N)rfr�r�)r/rpr�rrrrYKs   zNonCallableMock.assert_calledcCs.|}|jdks*d|jpd|jf}t|��dS)z3assert that the mock was called only once. r�z8Expected '%s' to have been called once. Called %s times.r#N)rfr�r�)r/rpr�rrrr[Ts  z"NonCallableMock.assert_called_oncecs||��jdkr(�����}td|f�����fdd�}����f�}���j�}||krxt|t�rh|nd}t|��|�dS)z�assert that the mock was called with the specified arguments. Raises an AssertionError if the args and keyword args passed in are different to the last call to the mock.NzExpected call: %s Not calledcs�����}|S)N)r�)r�)r0r1rprr�_error_messagegs z:NonCallableMock.assert_called_with.._error_message)rgr�r�r�r � Exception)r/r0r1�expectedr��actual�causer)r0r1rprrX]s   z"NonCallableMock.assert_called_withcOs6|}|jdks*d|jpd|jf}t|��|j||�S)ziassert that the mock was called exactly once and that that call was with the specified arguments.r�z1Expected '%s' to be called once. Called %s times.r#)rfr�r�rX)r/r0r1rpr�rrrr\qs  z'NonCallableMock.assert_called_once_withc s��fdd�|D�}tdd�|D�d�}t�fdd��jD��}|s�||kr�|dkrXd}nd�d d�|D��}td |t|��jf�|�dSt|�}g}x:|D]2}y|�|�Wq�tk r�|�|�Yq�Xq�W|r�td t |�f�|�dS) a�assert 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|]}��|��qSr)r�)r�c)rprrr��sz4NonCallableMock.assert_has_calls..css|]}t|t�r|VqdS)N)r r�)rr�rrr� �sz3NonCallableMock.assert_has_calls..Nc3s|]}��|�VqdS)N)r�)rr�)rprrr��szCalls not found.z+Error processing expected calls. Errors: {}cSsg|]}t|t�r|nd�qS)N)r r�)rr�rrrr��sz%s Expected: %r Actual: %rz%r not all found in call list) r�r_ra�formatr�rH�remover+r�rI) rpZcallsZ any_orderr�r�Z all_callsZproblemZ not_foundZkallr)rprr]|s4   z NonCallableMock.assert_has_callscsZ��||f�}�fdd��jD�}||krVt|t�r8|nd}��||�}td|�|�dS)z�assert 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|]}��|��qSr)r�)rr�)rprrr��sz3NonCallableMock.assert_any_call..Nz%s call not found)r�rhr r�r�r�)rpr0r1r�r�r�r�r)rprr^�s zNonCallableMock.assert_any_callcKsxt|�}t|t�s2t|t�r"t}q.) rrrCr�r�r r�r rr1�attribute_namer )rprrrrr �sz _patch.copycCst|t�r|�|�S|�|�S)N)r r�decorate_class�decorate_callable)rpr,rrrr)�s  z_patch.__call__cCsRxLt|�D]@}|�tj�sq t||�}t|d�s2q |��}t||||��q W|S)Nr))r�rr� TEST_PREFIXrAr%r r@)rpr��attr� attr_valuerrrrr�s   z_patch.decorate_classcs<t�d�r�j�|��St����fdd���|g�_�S)N� patchingsc srg}t���\}xB�jD]8}|�|�}|jdk r:|�|�q|jtkr|�|�qW|t |�7}�||�SQRXdS)N) � contextlib� ExitStackr� enter_contextr�updater�rr�rI)r0Zkeywargs� extra_args� exit_stack�patchingr�)r,�patchedrrr�s       z)_patch.decorate_callable..patched)r%rr�r)rpr,r)r,rrr�s   z_patch.decorate_callablec Cs�|��}|j}t}d}y|j|}Wn$ttfk rHt||t�}YnXd}|tkrft|t �rfd|_ |j s�|tkr�td||f��||fS)NFTz!%s does not have the attribute %r) rrCrrLr(�KeyErrorrA� _builtinsr rr )rprrrS�localrrr� get_original�sz_patch.get_originalcCsn|j|j|j}}}|j|j}}|j}|��|_|dkr@d}|dkrLd}|dkrXd}|dk rp|dk rptd��|dk s�|dk r�|dkr�td��|� �\}}|t k�rR|dk�rRd} |dkr�|}|dkr�|}d}n&|dk r�|dkr�|}d}n |dkr�|}|dk �s |dk �r.|t k�rtd��t |t ��r.d} t } i} |dk �rF|} nN|dk �sZ|dk �r�|} |dk �rl|} t| ��r�d| k} n t| � } | �r�t} |dk �r�|| d <|dk �r�|| d <t | t ��r�t| t��r�|j�r�|j| d <| �|�| f| �}| �r�t|��r�|} |dk �r|} t| ��s0t| ��s0t} | �d �| f|d d �| ��|_nl|dk �r�|t k �rntd��|t k�r�td��t|�}|dk�r�|}t|f||jd�|��}n|�r�td��|}||_||_t��|_yrt |j|j|�|j!dk �rHi}|jt k�r|||j!<x2|j"D](}|j�#|�}|jt k�r|�|��qW|S|S|j$t%�&���sd�YnXdS)zPerform the patch.FNzCan't specify spec and autospec)TNz6Can't provide explicit spec_set *and* spec or autospecTz!Can't use 'spec' with create=Truer)r�r�rz())r�r�zBautospec creates the mock for you. Can't specify autospec and new.z%Can't use 'autospec' with create=True)r��_namez.Can't pass kwargs to a mock we aren't creating)'r�r�r�r r1rrrr�r rr rrrJr�r rr rCrrrOr�rc�boolr� temp_originalrrr� _exit_stackr@rr r�__exit__�sys�exc_info)rpr�r�r�r r1rrSrZinherit�Klass�_kwargsZ this_specZ not_callableZnew_attrrrr�rrr� __enter__s�                                z_patch.__enter__cGs�t|�std��|jr4|jtk r4t|j|j|j�n>t|j|j�|j srt |j|j�r`|jdkrrt|j|j|j�|`|`|`|j }|` |j |�S)zUndo the patch.z stop called on unstarted patcher)r;r=r>�__annotations__r?) r� RuntimeErrorrr#rr@rrCr�r r%r$r%)rpr'rrrrr%�s z_patch.__exit__cCs|��}|j�|�|S)z-Activate a patch, returning any created mock.)r*�_active_patchesr�)rprTrrr�start�s z _patch.startcCs4y|j�|�Wntk r$YnX|�ddd�S)zStop an active patch.N)r-r�r+r%)rprrr�stop�s z _patch.stop)r:r=rsrr-r'r r)rrr r*r%r.r/rrrrr�s rc sPy��dd�\�}Wn&ttfk r:td�f��YnX�fdd�}||fS)Nr�r�z.Need a valid target to patch. You supplied: %rcst��S)N)rr)rrrr��r�z_get_target..)�rsplitr�r+)rrCrr)rr� _get_target�s r1c s>t��tkrt��d����fdd�} 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. z3 must be the actual object to be patched, not a strcs�S)Nrr)rrrr��r�z_patch_object..)r�strr�r) rrCr�r�r r�r rr1rr)rr� _patch_object�s    r3c s�t��tkr�fdd�}n �fdd�}|s2td��t|���}|d\} } t|| | |||||i� } | | _xB|dd�D]2\} } t|| | |||||i� } | | _| j�| �qvW| S)a�Perform 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. cst��S)N)rr)rrrr��r�z!_patch_multiple..cs�S)Nrr)rrrr��r�z=Must supply at least one keyword argument with patch.multiplerr�N) rr2r+rHr�rrr r�) rr�r r�r rr1rr�rCr�rZ this_patcherr)rr�_patch_multiple�s&        r4c Ks$t|�\}} 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. )r1r) rr�r�r r�r rr1rrCrrrr sE  rc@sReZdZdZddd�Zdd�Zdd �Zd d �Zd d �Zdd�Z dd�Z 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. rFcKs,||_t|�|_|j�|�||_d|_dS)N)�in_dictryr�r�clear� _original)rpr6r�r7r1rrrr'ts   z_patch_dict.__init__cs.t�t�r����St����fdd��}|S)Ncs"���z �||�S���XdS)N)r5� _unpatch_dict)r0r�)�frprr�_inner�s z$_patch_dict.__call__.._inner)r rrr)rpr:r;r)r:rprr)}s  z_patch_dict.__call__cCs\xVt|�D]J}t||�}|�tj�r t|d�r t|j|j|j �}||�}t |||�q W|S)Nr)) r�rArrrr%r5r6r�r7r@)rpr�rrZ decoratorZ decoratedrrrr�s   z_patch_dict.decorate_classcCs |��dS)zPatch the dict.N)r5)rprrrr*�sz_patch_dict.__enter__cCs�|j}t|jt�rt|j�|_|j}|j}y |��}Wn2tk rhi}x|D]}||||<qPWYnX||_|r|t |�y|� |�Wn.tk r�x|D]}||||<q�WYnXdS)N) r�r r6r2rr7r r(r8� _clear_dictr)rpr�r6r7rSr�rrrr5�s&     z_patch_dict._patch_dictcCsV|j}|j}t|�y|�|�Wn.tk rPx|D]}||||<q8WYnXdS)N)r6r8r<rr()rpr6rSr�rrrr9�s z_patch_dict._unpatch_dictcGs |��dS)zUnpatch the dict.F)r9)rpr0rrrr%�sz_patch_dict.__exit__N)rF) r:r=rsr;r'r)rr*r5r9r%r.r/rrrrr5Ws   r5cCsBy |��Wn0tk r<t|�}x|D] }||=q*WYnXdS)N)r7r(rH)r6�keysr�rrrr<�s   r<cCs xttj�D] }|��q WdS)z7Stop all active patches. LIFO to unroll nested patches.N)r�rr-r/)rrrr�_patch_stopall�sr>Ztestz�lt le gt ge eq ne getitem setitem delitem len contains iter hash str sizeof enter exit divmod rdivmod neg pos abs invert complex int float index trunc floor ceil bool next zHadd sub mul matmul div floordiv mod lshift rshift and xor or pow truediv� ccs|]}d|VqdS)zi%sNr)r�nrrrr��sr�ccs|]}d|VqdS)zr%sNr)rr@rrrr��s�__get__�__set__� __delete__� __reversed__� __missing__rr� __reduce_ex__Z__getinitargs__�__getnewargs__� __getstate__� __setstate__� __getformat__Z __setformat__rqr��__subclasses__� __format__�__getnewargs_ex__cs�fdd�}||_|S)z:Turns a callable object (like a mock) into a real functioncs�|f|�|�S)Nr)rpr0r�)r,rr�methodsz_get_method..method)r:)rr,rNr)r,rr�s r�cCsh|] }d|�qS)z__%s__r)rrNrrrr srxr�r'r�� __prepare__�__instancecheck__�__subclasscheck__�__del__cCs t�|�S)N)r��__hash__)rprrrr�r�r�cCs t�|�S)N)r��__str__)rprrrr�r�cCs t�|�S)N)r�� __sizeof__)rprrrr�r�)rSrTrUr�y�?g�?) �__lt__�__gt__�__le__�__ge__�__int__r��__len__r%� __complex__� __float__�__bool__� __index__cs�fdd�}|S)Ncs$�jj}|tk r|S�|kr dStS)NT)�__eq__r}r�NotImplemented)�other�ret_val)rprrr`0s z_get_eq..__eq__r)rpr`r)rpr�_get_eq/s rdcs�fdd�}|S)Ncs �jjtk rtS�|krdStS)NF)�__ne__r}rra)rb)rprrre:s  z_get_ne..__ne__r)rprer)rpr�_get_ne9s rfcs�fdd�}|S)Ncs �jj}|tkrtg�St|�S)N)r�r}rr�)rc)rprrr�Csz_get_iter..__iter__r)rpr�r)rpr� _get_iterBs rg)r`rer�cCs�t�|t�}|tk r||_dSt�|�}|dk rdy ||�}Wntk rXt|�}YnX||_dSt�|�}|dk r�||�|_dS)N)�_return_valuesrMrrc�_calculate_return_valuer(�_side_effect_methodsri)r#rNrZfixedZreturn_calculatorrcZ side_effectorrrr�_set_return_valueTs    rkc@seZdZdd�Zdd�ZdS)� MagicMixincOs&|��tt|�j||�|��dS)N)�_mock_set_magicsr�rlr')rpr0r�rrrr'lszMagicMixin.__init__cCs�t}t|dd�dk rTt�|j�}t�}t|}x$|D]}|t|�jkr4t||�q4W|tt|�j�}t|�}x|D]}t||t ||��qtWdS)Nr�) �_magicsrA� intersectionr�rzrrLr�r@� MagicProxy)rpZ these_magicsZ remove_magicsr�r�rrrrmrs   zMagicMixin._mock_set_magicsN)r:r=rsr'rmrrrrrlksrlc@seZdZdZddd�ZdS)r z-A version of `MagicMock` that isn't callable.FcCs|�||�|��dS)z�Add 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)r�rm)rpr�r�rrrr��s z"NonCallableMagicMock.mock_add_specN)F)r:r=rsr;r�rrrrr �sr c@seZdZdZddd�ZdS)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|�||�|��dS)z�Add 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)r�rm)rpr�r�rrrr��s zMagicMock.mock_add_specN)F)r:r=rsr;r�rrrrr�s rc@s.eZdZdd�Zdd�Zdd�Zd dd �ZdS) rpcCs||_||_dS)N)rr�)rprr�rrrr'�szMagicProxy.__init__cOs|��}|||�S)N)� create_mock)rpr0r1�mrrrr)�szMagicProxy.__call__cCs8|j}|j}|j|||d�}t|||�t|||�|S)N)rr�r�)rr�r�r@rk)rpr�r�rrrrrrq�s  zMagicProxy.create_mockNcCs|��S)N)rq)rprr�rrrrA�szMagicProxy.__get__)N)r:r=rsr'r)rqrArrrrrp�s rpc@s(eZdZdZdd�Zdd�Zdd�ZdS) �_ANYz2A helper object that compares equal to everything.cCsdS)NTr)rprbrrrr`�sz _ANY.__eq__cCsdS)NFr)rprbrrrre�sz _ANY.__ne__cCsdS)Nzr)rprrrrq�sz _ANY.__repr__N)r:r=rsr;r`rerqrrrrrs�srscCsdd|}d}d�dd�|D��}d�dd�t|���D��}|rD|}|r\|rT|d7}||7}||S)Nz%s(%%s)r�z, cSsg|] }t|��qSr)�repr)rr�rrrr��sz*_format_call_signature..cSsg|]\}}d||f�qS)z%s=%rr)rr�r{rrrr��s)r�r�r�)rr0r1r�Zformatted_argsZ args_stringZ kwargs_stringrrrr��sr�c@sbeZdZdZddd�Zdd d �Zd d �ZejZd d�Z dd�Z dd�Z dd�Z dd�Z dd�ZdS)r�a� 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. rr�NFTc Cs�d}i}t|�}|dkr$|\}}}nr|dkrd|\} } t| t�rX| }t| t�rR| }qb| }q�| | }}n2|dkr�|\}t|t�r�|}nt|t�r�|}n|}|r�t�|||f�St�||||f�S)Nrr�rlr�)r�r r2rIr�) r�r{rr�r�� from_kallr0r1�_len�first�secondrrrr��s.      z _Call.__new__cCs||_||_||_dS)N)r�r�_mock_from_kall)rpr{rr�r�rurrrr'sz_Call.__init__c Csf|tkr dSy t|�}Wntk r,dSXd}t|�dkrH|\}}n |\}}}t|dd�rzt|dd�rz|j|jkrzdSd}|dkr�di}}n�|dkr�|\}}}n�|d kr�|\} t| t�r�| }i}n"t| t�r�| }di}}nd}| }nV|dk�r>|\} } t| t��r2| }t| t��r&| i}}n d| }}n | | }}ndS|�rV||k�rVdS||f||fkS) NTFr�rlrrrr�r�)rr�r�rArr rIr2) rprbZ len_otherZ self_nameZ self_argsZ self_kwargsZ other_nameZ other_argsZ other_kwargsr{rwrxrrrr`sP                z _Call.__eq__cOs<|jdkrtd||fdd�S|jd}t|j||f||d�S)Nr�z())r)rr�)r�r�)rpr0r1rrrrr)Ys  z_Call.__call__cCs2|jdkrt|dd�Sd|j|f}t||dd�S)NF)rruz%s.%s)rr�ru)r�r�)rprrrrrrxas  z_Call.__getattr__cOs|�d�||�S)Nr�)rx)rpr0r1rrrr�hsz _Call.countcOs|�d�||�S)N�index)rx)rpr0r1rrrrzksz _Call.indexcCs||js&|jpd}|�d�r"d|}|St|�dkr@d}|\}}n0|\}}}|sTd}n|�d�shd|}nd|}t|||�S)Nrz()zcall%srlzcall.%s)ryr�rr�r�)rprr0r1rrrrqns       z_Call.__repr__cCs8g}|}x"|dk r*|jr"|�|�|j}q Wtt|��S)z�For a call object that represents multiple calls, `call_list` returns a list of all the intermediate calls as well as the final call.N)ryr�rr_r�)rpZvalsr�rrr� call_list�s   z_Call.call_list)rr�NFT)rNNFT)r:r=rsr;r�r'r`r�rer)rxr�rzrqr{rrrrr��s  7r�)ruc Ks$t|�rt|�}t|t�}d|i}|r0d|i}n |dkr._readlines_side_effectcs$�jjdk r�jjS�dj||�S)Nr)�readrc)r0r1)r�r�rr�_read_side_effectc s z$mock_open.._read_side_effectc?s(��EdHx�dj||�VqWdS)Nr)�readline)r0r1)�_iter_side_effectr�rr�_readline_side_effecth s z(mock_open.._readline_side_effectc3s8�jjdk rx�jjVqWx�dD] }|Vq&WdS)Nr)r�rc)�line)r�r�rrr�m s  z$mock_open.._iter_side_effectcs �jjdk r�jjSt�d�S)Nr)r�rcr�r)r�r�rr�_next_side_effectt s z$mock_open.._next_side_effectr�open)rr�)r�r�cs6t���d<�jj�dkr2���d<�d�j_tS)Nrr�)r�r�rir)r0r1)r�r�r�r�rr� reset_data� s    zmock_open..reset_data)r�� file_spec�_iorHrzr�� TextIOWrapper�unionr�rr�r*rc�writer�r�r�rir�r�)r#r�Z _read_datar�r�r�r�r�r)r�r�r�r�r�rr O s8 "    r c@s(eZdZdZdd�Zdd�Zdd�ZdS) 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 tf|�S)N)r)rpr1rrrr�� szPropertyMock._get_child_mockcCs|�S)Nr)rprZobj_typerrrrA� szPropertyMock.__get__cCs ||�dS)Nr)rprr�rrrrB� szPropertyMock.__set__N)r:r=rsr;r�rArBrrrrr � sr c Cs`d|_xTt|�D]H}yt||�}Wntk r8wYnXt|t�sFq|j|krt|�qWdS)a�Disable the automatic generation of child mocks. Given an input Mock, seals it to ensure no further mocks will be generated when accessing an attribute that was not already defined. The operation recursively seals the mock passed in, meaning that the mock itself, any mocks generated by accessing one of its attributes, and all assigned mocks without a name or spec will be sealed. TN)r�r�rAr(r r r�r)r#rrrrrrr� s   r)F)F)NFNNN)FFNN)Nr�)r�__all__� __version__r�r*r�r&�builtinsr�typesrr� functoolsrrr�r� BaseExceptionr!�platformrZlangZ Throwabler �superr�rr"r&r-r9r4rFrJrOrWrRrnr�rortrr�MISSINGr�ZDELETEDr�r|r�r�rHr_r�r�r�r r�r�rrrrrr1r3r4rr5r<r>ryZmultipleZstopallrZ magic_methodsZnumericsr�r�Zinplace�rightZ _non_defaultsr�rnr�r�rirarhrdrfrgrjrkrlr rrprsrr�rIr�rrr|r�r�rr`r$r�r�r r rrrrr�s        5 Zi5    1Jv        ,  w  N