????
Your IP : 3.135.192.183
U
e5d��@s�dZdZddlZddlZddlZddlZddlZddlZddlZddl m
Z
mZmZddl
mZddlmZmZdd�ee�D�Zd ZeZd
d�Zdd
�Zdd�Zdd�Zdd�Zdd�Zd�dd�Zdd�Zdd�Zdd�Z dd �Z!d�d!d"�Z"d#d$�Z#d%d&�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.d/d0d1d2d3d4hZ/d5d6�Z0Gd7d8�d8e1�Z2d9d:�Z3Gd;d<�d<e&�Z4Gd=d>�d>e&�Z5Gd?d@�d@e5�Z6dAdB�Z7GdCdD�dDe5�Z8GdEdF�dFe8e6�Z9dGdH�Z:dIdJ�Z;GdKdL�dLe&�Z<dMdN�Z=e*dddddfdOdP�Z>d�dQdR�Z?e*dddddfdSdT�Z@GdUdV�dVe&�ZAdWdX�ZBdYdZ�ZCe>e@_&eAe@_De?e@_EeCe@_Fd[e@_Gd\ZHd]ZId^�Jd_d`�eI�K�D��ZLd^�Jdad`�eI�K�D��ZMdbdcdddedfdgdhdidjdkdldmdndodpdqdrdshZNdtdu�ZOdvd�d^�JeHeIeLeMg��K�D�ZPdwdxdyhZQdzhZReQeRBZSePeNBZTeTeSBZUd{d|d}d~dd�d�d�hZVd�d��d�d��d�d��d�d��d��ZWeXeXeXeXd�dddd�d�d d�dd��
ZYd�d��ZZd�d��Z[d�d��Z\d�d��Z]eZe[e\e]d��Z^d�d��Z_Gd�d��d�e5�Z`Gd�d��d�e`e6�ZaGd�d��d�e`�ZbGd�d��d�e`e9�ZcGd�d��d�e5�ZdGd�d��d�e5�ZeGd�d��d�eeebe9�ZfGd�d��d�e&�Zgeg�Zhd�d��ZiGd�d��d�ej�Zkekdd��Zld�d�d��Zmd�d��ZnGd�d��d�e&�Zoepem�epehjq�fZrdasd�d��Ztd�d�d��ZuGd�d��d�e9�Zvd�d��ZwGd�d��d��ZxdS)�)�Mock� MagicMock�patch�sentinel�DEFAULT�ANY�call�create_autospec� AsyncMock�
FILTER_DIR�NonCallableMock�NonCallableMagicMock� mock_open�PropertyMock�sealz1.0�N)�CodeType�
ModuleType�
MethodType)� safe_repr)�wraps�partialcCsh|]}|�d�s|�qS��_��
startswith)�.0�name�r�%/usr/lib64/python3.8/unittest/mock.py� <setcomp>(s
rTcCs>t|�rt|t�sdSt|d�r*t|d�}t�|�p<t�|�S)NF�__func__) �_is_instance_mock�
isinstancer �hasattr�getattr�asyncio�iscoroutinefunction�inspectZisawaitable��objrrr�
_is_async_obj0s
r*cCst|dd�rt�|�SdSdS)N�__code__F)r$r%r&��funcrrr�_is_async_func8s
r.cCstt|�t�S�N)�
issubclass�typerr(rrrr!?sr!cCst|t�pt|t�ot|t�Sr/)r"�
BaseExceptionr1r0r(rrr�
_is_exceptionEs
�r3cCs"t|t�rt|d�r|jS|SdS�N�mock)r"�
FunctionTypesr#r5r(rrr�
_extract_mockLsr7cCs�t|t�r|s|j}d}n,t|t�sFz
|j}Wntk
rDYdSX|rVt|d�}n|}z|t�|�fWSt k
r�YdSXdS)z�
Given an arbitrary, possibly callable object, try to create a suitable
signature object.
Return a (reduced func, signature) tuple, or None.
TN)
r"r1�__init__r6�__call__�AttributeErrorrr'� signature�
ValueError)r-Zas_instanceZeat_selfZsig_funcrrr�_get_signature_objectUs
r=FcsNt|||���dkrdS�\}��fdd�}t||�|t|�_�t|�_dS)Ncs�j||�dSr/��bind��self�args�kwargs��sigrr�checksigwsz"_check_signature.<locals>.checksig)r=�_copy_func_detailsr1�_mock_check_sig�
__signature__)r-r5� skipfirst�instancerFrrDr�_check_signaturers
rLc Cs:dD]0}zt||t||��Wqtk
r2YqXqdS)N)�__name__�__doc__�__text_signature__�
__module__�__defaults__�__kwdefaults__)�setattrr$r:)r-�funcopy� attributerrrrG~s
rGcCs@t|t�rdSt|tttf�r(t|j�St|dd�dk r<dSdS)NTr9F)r"r1�staticmethod�classmethodr� _callabler r$r(rrrrX�s
rXcCst|�ttfkSr/)r1�list�tupler(rrr�_is_list�sr[cCsFt|t�st|dd�dk S|f|jD]}|j�d�dk r&dSq&dS)ztGiven an object, return True if the object is callable.
For classes, return True if instances would be callable.r9NTF)r"r1r$�__mro__�__dict__�get)r)�baserrr�_instance_callable�s
r`cs�t|t�}t|||�}|dkr"|S|\}��fdd�}t||�|j}|��sRd}||d�}d|} t| |�||}
t|
|��|
S)Ncs�j||�dSr/r>�rBrCrDrrrF�sz _set_signature.<locals>.checksigrT)Z
_checksig_r5zYdef %s(*args, **kwargs):
_checksig_(*args, **kwargs)
return mock(*args, **kwargs))r"r1r=rGrM�isidentifier�exec�_setup_func)r5�originalrKrJ�resultr-rFr�context�srcrTrrDr�_set_signature�s$
�
rics���_�fdd�}�fdd�}�fdd�}�fdd�}�fd d
�}�fdd�}�fd
d�} ��fdd�}
d�_d�_d�_t��_t��_t��_�j�_�j �_ �j
�_
|�_|�_|�_
| �_|
�_|�_|�_|�_|�_��_dS)Ncs�j||�Sr/)�assert_called_withra�r5rrrj�sz'_setup_func.<locals>.assert_called_withcs�j||�Sr/)�
assert_calledrarkrrrl�sz"_setup_func.<locals>.assert_calledcs�j||�Sr/)�assert_not_calledrarkrrrm�sz&_setup_func.<locals>.assert_not_calledcs�j||�Sr/)�assert_called_oncerarkrrrn�sz'_setup_func.<locals>.assert_called_oncecs�j||�Sr/)�assert_called_once_withrarkrrro�sz,_setup_func.<locals>.assert_called_once_withcs�j||�Sr/)�assert_has_callsrarkrrrp�sz%_setup_func.<locals>.assert_has_callscs�j||�Sr/)�assert_any_callrarkrrrq�sz$_setup_func.<locals>.assert_any_callcs:t��_t��_����j}t|�r6|�k r6|��dSr/)� _CallList�method_calls�
mock_calls�
reset_mock�return_valuer!)�ret�rTr5rrru�sz_setup_func.<locals>.reset_mockFr)r5�called�
call_count� call_argsrr�call_args_listrsrtrv�side_effect�_mock_childrenrjrorprqrurlrmrnrI�_mock_delegate)rTr5rErjrlrmrnrorprqrurrxrrd�s8rdcsJtjj�_d�_d�_t��_�fdd�}dD]}t�|t||��q.dS)Nrcst�j|�||�Sr/)r$r5)�attrrBrCrkrr�wrapper�sz"_setup_async_mock.<locals>.wrapper)�assert_awaited�assert_awaited_once�assert_awaited_with�assert_awaited_once_with�assert_any_await�assert_has_awaits�assert_not_awaited) r%�
coroutines�
_is_coroutine�await_count�
await_argsrr�await_args_listrSr)r5r�rUrrkr�_setup_async_mock�s
r�cCsd|dd�|kS)N�__%s__����r�rrrr� _is_magicsr�c@s(eZdZdZdd�Zdd�Zdd�ZdS) �_SentinelObjectz!A unique, named, sentinel object.cCs
||_dSr/r��rArrrrr8sz_SentinelObject.__init__cCs
d|jS�Nzsentinel.%sr��rArrr�__repr__sz_SentinelObject.__repr__cCs
d|jSr�r�r�rrr�
__reduce__sz_SentinelObject.__reduce__N)rMrP�__qualname__rNr8r�r�rrrrr�sr�c@s(eZdZdZdd�Zdd�Zdd�ZdS) � _SentinelzAAccess attributes to return a named object, usable as a sentinel.cCs
i|_dSr/)�
_sentinelsr�rrrr8#sz_Sentinel.__init__cCs|dkrt�|j�|t|��S)N� __bases__)r:r��
setdefaultr�r�rrr�__getattr__&sz_Sentinel.__getattr__cCsdS)Nrrr�rrrr�,sz_Sentinel.__reduce__N)rMrPr�rNr8r�r�rrrrr�!sr�rv�_mock_return_valuer}�_mock_side_effect�_mock_parent�_mock_new_parent�
_mock_name�_mock_new_namecCs8t�|�d|}||fdd�}||fdd�}t||�S)NZ_mock_cSs"|j}|dkrt||�St||�Sr/)rr$)rAr� _the_namerErrr�_getAs
z"_delegating_property.<locals>._getcSs*|j}|dkr||j|<nt|||�dSr/)rr]rS)rA�valuerr�rErrr�_setFsz"_delegating_property.<locals>._set)�_allowed_names�add�property)rr�r�r�rrr�_delegating_property>s
r�c@seZdZdd�Zdd�ZdS)rrcCslt|t�st�||�St|�}t|�}||kr2dStd||d�D]"}||||�}||krDdSqDdS)NFr�T)r"rY�__contains__�len�range)rAr�Z len_valueZlen_self�iZsub_listrrrr�Ss
z_CallList.__contains__cCst�t|��Sr/)�pprintZpformatrYr�rrrr�asz_CallList.__repr__N)rMrPr�r�r�rrrrrrQsrrcCs|t|�}t|�sdS|js4|js4|jdk s4|jdk r8dS|}|dk rX||krPdS|j}q<|rh||_||_|rx||_||_dS)NFT)r7r!r�r�r�r�)�parentr�r�new_name�_parentrrr�_check_and_set_parentes*��r�c@seZdZdd�Zdd�ZdS)� _MockItercCst|�|_dSr/)�iterr))rAr)rrrr8�sz_MockIter.__init__cCs
t|j�Sr/)�nextr)r�rrr�__next__�sz_MockIter.__next__N)rMrPr�r8r�rrrrr��sr�c@seZdZeZdZdd�ZdS)�BaseNcOsdSr/rr@rrrr8�sz
Base.__init__)rMrPr�rr�r�r8rrrrr��sr�c@sdeZdZdZdd�ZdLdd�Zd d
�ZdMdd�ZdNd
d�Zdd�Z dd�Z
dZee e
e�Z
edd��Zed�Zed�Zed�Zed�Zed�Zdd�Zdd�Zeee�ZdOddd�d d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�Z dPd3d4�Z!d5d6�Z"d7d8�Z#d9d:�Z$d;d<�Z%d=d>�Z&d?d@�Z'dAdB�Z(dQdCdD�Z)dEdF�Z*dGdH�Z+dRdJdK�Z,dS)Srz A non-callable version of `Mock`c Os�|f}t|t�s^t�tj�}|j|f|�|�j}dd�|��D�}|r^t ||d�r^t
|f}t|j|d|j
i�}tt|��|�}|S)NcSsg|]}|�d�r|�qS��specr�r�argrrr�
<listcomp>�s
�z+NonCallableMock.__new__.<locals>.<listcomp>rrN)r0r r'r;rr8Zbind_partialZ arguments�keysr*�AsyncMockMixinr1rMrN�_safe_super�__new__) �clsrB�kw�basesrEZ
bound_argsZspec_arg�newrKrrrr��s
�zNonCallableMock.__new__N�FcKs�|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)Nr�r�r�r�F�_mock_sealedTr~�_mock_wrapsrZ_mock_calledZ_mock_call_argsrZ_mock_call_countZ_mock_call_args_listZ_mock_mock_callsrs�_mock_unsafe)r]�_mock_add_specrr�configure_mockr�rr8)rAr�rr�spec_setr��_spec_state� _new_name�_new_parent�_spec_as_instance� _eat_selfZunsaferCr]rrrr8�sD
�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�)r7r�r�r�r�rS)rAr5rUZ
inner_mockrrr�attach_mock�szNonCallableMock.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��rAr�r�rrr�
mock_add_spec�szNonCallableMock.mock_add_speccCs�d}d}g}t|�D] }t�t||d��r|�|�q|dk r~t|�s~t|t�rV|}nt|�}t|||�} | ot| d}t|�}|j }
||
d<||
d<||
d<||
d<||
d<dS)Nr��_spec_class� _spec_set�_spec_signature�
_mock_methods�_spec_asyncs)
�dirr%r&r$�appendr[r"r1r=r])rAr�r�r�r�r�r�r�r��resr]rrrr��s,
�zNonCallableMock._mock_add_speccCs8|j}|jdk r|jj}|tkr4|j|dd�}||_|S)N�()�r�r�)r�rrvr�_get_child_mock)rArwrrrZ__get_return_values
�z"NonCallableMock.__get_return_valuecCs,|jdk r||j_n||_t||dd�dS)Nr�)rrvr�r�)rAr�rrrZ__set_return_values
z"NonCallableMock.__set_return_valuez1The value to be returned when the mock is called.cCs|jdkrt|�S|jSr/)r�r1r�rrr� __class__!s
zNonCallableMock.__class__ryrzr{r|rtcCsN|j}|dkr|jS|j}|dk rJt|�sJt|t�sJt|�sJt|�}||_|Sr/)rr�r}�callabler"r�r3)rA� delegatedZsfrrrZ__get_side_effect.s��z!NonCallableMock.__get_side_effectcCs(t|�}|j}|dkr||_n||_dSr/)� _try_iterrr�r})rAr�r�rrrZ__set_side_effect9s
z!NonCallableMock.__set_side_effect)rvr}cCs�|dkrg}t|�|krdS|�t|��d|_d|_d|_t�|_t�|_t�|_|r^t |_
|rhd|_|j�
�D]"}t|t�sr|tkr�qr|�|�qr|j
}t|�r�||k r�|�|�dS)z-Restore the mock object to its initial state.NFr)�idr�ryr{rzrrrtr|rsrr�r�r~�valuesr"�
_SpecState�_deletedrur!)rAZvisitedrvr}�childrwrrrruDs,zNonCallableMock.reset_mockcKsXt|��dd�d�D]>\}}|�d�}|��}|}|D]}t||�}q6t|||�qdS)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�<lambda>o�z0NonCallableMock.configure_mock.<locals>.<lambda>)�keyr�N)�sorted�items�split�popr$rS)rArCr��valrB�finalr)r�rrrr�bs �
zNonCallableMock.configure_mockcCs�|dkrt|��n:|jdk r<||jks.|tkrLtd|��nt|�rLt|��|jsd|�d�rdtd��|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)�assertZassretz1Attributes cannot start with 'assert' or 'assret')r�rrr�r�)r:r��_all_magicsr�r�rr~r^r�r�r$r�r"r�rr�r�rKr�r)rArrfrrrrr�xsF
�
�
zNonCallableMock.__getattr__cCs�|jg}|j}|}d}|dgkr$d}|dk rZ|}|�|j|�d}|jdkrRd}|j}q$tt|��}|jpnd}t|�dkr�|ddkr�|d7}||d<d�|�S)Nr�r�r�r5r�)r�z().r)r�r�r�rY�reversedr�r��join)rAZ
_name_listr�Zlast�dotZ_firstrrr�_extract_mock_name�s(
z"NonCallableMock._extract_mock_namecCs^|��}d}|dkrd|}d}|jdk rDd}|jr8d}||jj}dt|�j||t|�fS)Nr�)r5zmock.z name=%rz spec=%rz spec_set=%rz<%s%s%s id='%s'>)r�r�r�rMr1r�)rArZname_stringZspec_stringrrrr��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_valuerrrr��s�z+NonCallableMock.__dir__.<locals>.<listcomp>cSsg|]}|�d�s|�qSrr�r�errrr��s
cSs"g|]}|�d�rt|�r|�qSr)rr�rrrrr��s
�)r
�object�__dir__r�r�r1rYr]r~r�r��set)rAZextrasZ 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|�|�Sr/r�rBr��rerArrr��r�z-NonCallableMock.__setattr__.<locals>.<lambda>r�r�zCannot set )r�r�__setattr__r�r�r]r:�_unsupported_magicsr�r!rSr1�_get_methodr�r~r�r�r#r�)rArr��msg� mock_namerrrr�s<��
zNonCallableMock.__setattr__cCs�|tkr2|t|�jkr2tt|�|�||jkr2dS|j�|t�}||jkr\tt|�� |�n|t
krlt|��|tk r||j|=t
|j|<dSr/)r�r1r]�delattrr~r^�_missingr�r�__delattr__r�r:)rArr)rrrrs
zNonCallableMock.__delattr__cCs|jpd}t|||�Sr4)r��_format_call_signature�rArBrCrrrr�_format_mock_call_signatures
z+NonCallableMock._format_mock_call_signaturercCs.d}|�||�}|j}|j|�}||||fS)Nz.expected %s not found.
Expected: %s
Actual: %s)rr{)rArBrC�action�message�expected_stringr{Z
actual_stringrrr�_format_mock_failure_messages
z,NonCallableMock._format_mock_failure_messagecCsj|s
|jSd}|�dd��d�}|j}|D]:}|�|�}|dksJt|t�rPqfq*t|�}|j}|j}q*|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.
Nr�r�r�)r��replacer�r~r^r"r�r7)rArrE�namesZchildrenr�rrr�_get_call_signature_from_name's
z-NonCallableMock._get_call_signature_from_namec
Cs�t|t�r&t|�dkr&|�|d�}n|j}|dk r�t|�dkrNd}|\}}n
|\}}}z||j||�fWStk
r�}z|�d�WY�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.
r�rNr�)r"rZr�rr�r?� TypeError�with_traceback)rA�_callrErrBrCrrrr�
_call_matcherHs
"zNonCallableMock._call_matchercCs0|jdkr,d|jpd|j|��f}t|��dS)z/assert that the mock was never called.
rz9Expected '%s' to not have been called. Called %s times.%sr5N�rzr��_calls_repr�AssertionError�rAr
rrrrmbs
��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.r5N)rzr�rr rrrrlls
�zNonCallableMock.assert_calledcCs0|jdks,d|jpd|j|��f}t|��dS)z3assert that the mock was called only once.
r�z:Expected '%s' to have been called once. Called %s times.%sr5Nrr rrrrnts
��z"NonCallableMock.assert_called_oncecs��jdkr.�����}d}d||f}t|�����fdd�}����f�}���j�}||kr~t|t�rn|nd}t|��|�dS)z�assert that the last call was made with the specified arguments.
Raises an AssertionError if the args and keyword args passed in are
different to the last call to the mock.Nznot called.z0expected call not found.
Expected: %s
Actual: %scs�����}|Sr/�r�r
�rBrCrArr�_error_message�sz:NonCallableMock.assert_called_with.<locals>._error_message)r{rrrr"� Exception)rArBrC�expected�actualZ
error_messager$�causerr#rrj~s
�z"NonCallableMock.assert_called_withcOs8|jdks,d|jpd|j|��f}t|��|j||�S)ziassert that the mock was called exactly once and that that call was
with the specified arguments.r�z3Expected '%s' to be called once. Called %s times.%sr5)rzr�rrrj�rArBrCr
rrrro�s
��z'NonCallableMock.assert_called_once_withc s�fdd�|D�}tdd�|D�d�}t�fdd��jD��}|s�||kr�|dkrXd}nd�d d�|D��}t|�d
t|���jdd��d
����|�dSt|�}g}|D]2}z|�|�Wq�t k
r�|�
|�Yq�Xq�|�rtd�jp�dt|�|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�cr�rrr��sz4NonCallableMock.assert_has_calls.<locals>.<listcomp>css|]}t|t�r|VqdSr/�r"r%rrrr� <genexpr>�s
z3NonCallableMock.assert_has_calls.<locals>.<genexpr>Nc3s|]}��|�VqdSr/r*r+r�rrr.�szCalls not found.z+Error processing expected calls.
Errors: {}cSsg|]}t|t�r|nd�qSr/r-rrrrr��s��
Expected: ZActual)�prefixr�z@%r does not contain all of %r in its call list, found %r insteadr5)
r�rrrt�formatrr�rstriprY�remover<r�r�rZ) rA�calls� any_orderr&r(Z all_calls�problem� not_found�kallrr�rrp�sH
��"������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|]}��|��qSrr*r+r�rrr��sz3NonCallableMock.assert_any_call.<locals>.<listcomp>Nz%s call not found)rr|r"r%rr�rArBrCr&r'r(rrr�rrq�s��zNonCallableMock.assert_any_callcKs�|�d�}||jdkr"tf|�St|�}t|t�rB|tkrBt}nbt|t�rp|tksd|j rj||j krjt}q�t}n4t|t
�s�t|t�r�t}q�t|t�r�t
}n
|jd}|jr�d|kr�d|dnd}|��|}t|��|f|�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�r�r�rr�r�)r^r]r r1r0r�_async_method_magicsr��_all_sync_magicsr��
CallableMixinrrrr\r�r�r:)rAr�r��_type�klassrUrrrrr��s2
��
zNonCallableMock._get_child_mock�CallscCs"|js
dSd|�dt|j��d�S)z�Renders self.mock_calls as a string.
Example: "
Calls: [call(1), call(2)]."
If self.mock_calls is empty, an empty string is returned. The
output will be truncated if very long.
r��
z: r�)rtr)rAr0rrrrszNonCallableMock._calls_repr)NNNNNNr�NFNF)F)FF)N)r)F)r?)-rMrPr�rNr�r8r�r�r�Z"_NonCallableMock__get_return_valueZ"_NonCallableMock__set_return_valueZ"_NonCallableMock__return_value_docr�rvr�r�ryrzr{r|rtZ!_NonCallableMock__get_side_effectZ!_NonCallableMock__set_side_effectr}rur�r�r�r�rrrrrrrrmrlrnrjrorprqr�rrrrrr�sp�
-
�
�
''
!
-'rcCsL|dkr|St|�r|St|�r$|Sz
t|�WStk
rF|YSXdSr/)r3rXr�rr(rrrr�s
r�c
@sReZdZddedddddddf
dd�Zdd�Zdd�Zd d
�Zdd�Zd
d�Z dS)r<Nr�c Ks6||jd<tt|�j||||||| |
f|�||_dS)Nr�)r]r�r<r8r})rAr�r}rvrrr�r�r�r�r�rCrrrr8#s
��zCallableMixin.__init__cOsdSr/rr@rrrrH/szCallableMixin._mock_check_sigcOs$|j||�|j||�|j||�Sr/)rH�_increment_mock_call�
_mock_callr@rrrr94szCallableMixin.__call__cOs|j||�Sr/)�_execute_mock_callr@rrrrB<szCallableMixin._mock_callcOsd|_|jd7_t||fdd�}||_|j�|�|jdk }|j}|j}|dk}|j �td||f��|j
}|dk r�|r�|j�t|||f��|jdk }|r�|jd|}t|||f�} |j �| �|jr�|r�d}
nd}
|jdk}|j|
|}|j
}qpdS)NTr���twor�r�r�)ryrz�_Callr{r|r�r�r�r�rtr�rs)rArBrCrZdo_method_callsZmethod_call_nameZmock_call_nameZ is_a_callr�Zthis_mock_callr�rrrrA?s4
z"CallableMixin._increment_mock_callcOs||j}|dk rPt|�r|�n(t|�s:t|�}t|�rD|�n
|||�}|tk rP|S|jtk r`|jS|jdk rv|j||�S|jSr/)r}r3rXr�rr�rvr�)rArBrC�effectrfrrrrCms
z CallableMixin._execute_mock_call)
rMrPr�rr8rHr9rBrArCrrrrr<!s�
.r<c@seZdZdZdS)ra�
Create a new `Mock` object. `Mock` takes several optional arguments
that specify the behaviour of the Mock object:
* `spec`: This can be either a list of strings or an existing object (a
class or instance) that acts as the specification for the mock object. If
you pass in an object then a list of strings is formed by calling dir on
the object (excluding unsupported magic attributes and methods). Accessing
any attribute not in this list will raise an `AttributeError`.
If `spec` is an object (rather than a list of strings) then
`mock.__class__` returns the class of the spec object. This allows mocks
to pass `isinstance` tests.
* `spec_set`: A stricter variant of `spec`. If used, attempting to *set*
or get an attribute on the mock that isn't on the object passed as
`spec_set` will raise an `AttributeError`.
* `side_effect`: A function to be called whenever the Mock is called. See
the `side_effect` attribute. Useful for raising exceptions or
dynamically changing return values. The function is called with the same
arguments as the mock, and unless it returns `DEFAULT`, the return
value of this function is used as the return value.
If `side_effect` is an iterable then each call to the mock will return
the next value from the iterable. If any of the members of the iterable
are exceptions they will be raised instead of returned.
* `return_value`: The value returned when the mock is called. By default
this is a new Mock (created on first access). See the
`return_value` attribute.
* `wraps`: Item for the mock object to wrap. If `wraps` is not None then
calling the Mock will pass the call through to the wrapped object
(returning the real result). Attribute access on the mock will return a
Mock object that wraps the corresponding attribute of the wrapped object
(so attempting to access an attribute that doesn't exist will raise an
`AttributeError`).
If the mock has an explicit `return_value` set then calls are not passed
to the wrapped object and the `return_value` is returned instead.
* `name`: If the mock has a name then it will be used in the repr of the
mock. This can be useful for debugging. The name is propagated to child
mocks.
Mocks can also be called with arbitrary keyword arguments. These will be
used to set attributes on the mock after it is created.
N�rMrPr�rNrrrrr�srcCs8zt||�WStk
r2t|�t||�YSXdSr/)r$r:�
__import__)�thing�comp�import_pathrrr�_dot_lookup�s
rMcCsB|�d�}|�d�}t|�}|D]}|d|7}t|||�}q |S)Nr�rz.%s)r�r�rIrM)�targetZ
componentsrLrJrKrrr� _importer�s
rOc@szeZdZdZgZdd�Zdd�Zdd�Zdd �Ze j
d
d��Zdd
�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�ZdS)�_patchNc
Csn|dk r(|tk rtd��|dk r(td��||_||_||_||_||_||_d|_||_ ||_
| |_g|_dS)Nz,Cannot use 'new' and 'new_callable' togetherz1Cannot use 'autospec' and 'new_callable' togetherF)
rr<�getterrUr��new_callabler��createZ has_localr��autospecrC�additional_patchers)
rArQrUr�r�rSr�rTrRrCrrrr8�s(��z_patch.__init__c
CsHt|j|j|j|j|j|j|j|j|j � }|j
|_
dd�|jD�|_|S)NcSsg|]}|���qSr)�copy)r�prrrr��sz_patch.copy.<locals>.<listcomp>)rPrQrUr�r�rSr�rTrRrC�attribute_namerU)rA�patcherrrrrV�s ��z_patch.copycCs2t|t�r|�|�St�|�r(|�|�S|�|�Sr/)r"r1�decorate_classr'r&�decorate_async_callable�decorate_callable�rAr-rrrr9�s
z_patch.__call__cCsNt|�D]@}|�tj�sqt||�}t|d�s0q|��}t||||��q|S�Nr9)r�rr�TEST_PREFIXr$r#rVrS)rAr>r��
attr_valuerYrrrrZs
z_patch.decorate_classc csrg}t���\}|jD]8}|�|�}|jdk r8|�|�q|jtkr|�|�q|t |�7}||fVW5QRXdSr/)
�
contextlib� ExitStack� patchings�
enter_contextrX�updater�rr�rZ)rA�patchedrB�keywargs�
extra_args�
exit_stack�patchingr�rrr�decoration_helpers
z_patch.decoration_helpercs>t�d�r�j����St�����fdd����g�_�S)Nrcc
s4���||��\}}�||�W5QR�SQRXdSr/�rk�rBrgZnewargsZnewkeywargs�r-rfrArrrf(s�z)_patch.decorate_callable.<locals>.patched�r#rcr�rr]rrnrr\"s
z_patch.decorate_callablecs>t�d�r�j����St�����fdd����g�_�S)Nrcc
�s:���||��"\}}�||�IdHW5QR�SQRXdSr/rlrmrnrrrf9s�z/_patch.decorate_async_callable.<locals>.patchedror]rrnrr[3s
z_patch.decorate_async_callablec Cs�|��}|j}t}d}z|j|}Wn$ttfk
rHt||t�}YnXd}|tkrft|t �rfd|_
|j
s�|tkr�td||f��||fS)NFTz!%s does not have the attribute %r)rQrUrr]r:�KeyErrorr$� _builtinsr"rrS)rArNrre�localrrr�get_originalDs
�z_patch.get_originalcCs�|j|j|j}}}|j|j}}|j}|��|_|dkr@d}|dkrLd}|dkrXd}|dk rp|dk rptd��|dk s�|dk r�|dkr�td��|� �\}}|t
k�r||dk�r|d} |dkr�|}|dkr�|}d}n&|dk r�|dkr�|}d}n|dkr�|}|dk �s|dk �r.|t
k�rtd��t|t��r.d} |dk�rHt
|��rHt}
nt}
i}|dk �r`|}
n^|dk �st|dk �r�|}|dk �r�|}t|��r�d|k}
n
t|�}
t
|��r�t}
n
|
�r�t}
|dk �r�||d <|dk �r�||d
<t|
t��rt|
t��r|j�r|j|d<|�|�|
f|�}| �r�t|��r�|}|dk �rB|}t|��sZt|��sZt}
|�d�|
f|dd
�|��|_nl|dk �r�|t
k �r�td��|t
k�r�td��t|�}|dk�r�|}t|f||jd�|��}n|�r�td��|}||_||_t� �|_!zrt"|j|j|�|j#dk �rpi}|jt
k�r:|||j#<|j$D](}|j!�%|�}|jt
k�r@|�|��q@|WS|WS|j&t'�(���s��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=Truer9r�r�rr�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�rTrCrRrQrNrrsrr"r1r*r rr[r�rr0rrUrer!r`r�rv�boolr�
temp_original�is_localrarb�_exit_stackrSrXrUrd�__exit__�sys�exc_info)rAr�r�r�rTrCrRrerrZinherit�Klass�_kwargsZ this_specZnot_callableZnew_attrrhrjr�rrr� __enter__\s�
�
��
�
�
�
��
z_patch.__enter__cGs�|jr$|jtk r$t|j|j|j�n>t|j|j�|jsbt|j|j�rP|jdkrbt|j|j|j�|`|`|`|j }|` |j
|�S)zUndo the patch.)rNrPrQ�__annotations__rR)rwrvrrSrNrUrrSr#rxry)rAr{rirrrry�s�z_patch.__exit__cCs|��}|j�|�|S)z-Activate a patch, returning any created mock.)r~�_active_patchesr�)rArfrrr�start�sz_patch.startcCs6z|j�|�Wntk
r&YdSX|�ddd�S)zStop an active patch.N)r�r3r<ryr�rrr�stop�s
z_patch.stop)rMrPr�rXr�r8rVr9rZra�contextmanagerrkr\r[rsr~ryr�r�rrrrrP�s
rPc sPz��dd�\�}Wn&ttfk
r:td�f��YnX�fdd�}||fS)Nr�r�z.Need a valid target to patch. You supplied: %rcst��Sr/�rOr�rNrrr�r�z_get_target.<locals>.<lambda>)�rsplitrr<)rNrUrQrr�r�_get_target
s�r�c
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�Sr/rrr�rrr�*r�z_patch_object.<locals>.<lambda>)r1�strrrP)
rNrUr�r�rSr�rTrRrCrQrr�r�
_patch_objects ��r�c
s�t��tkr�fdd�}n�fdd�}|s2td��t|���}|d\} }
t|| |
|||||i� }| |_|dd�D]2\} }
t|| |
|||||i� }| |_|j�|�qt|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��Sr/r�rr�rrr�Hr�z!_patch_multiple.<locals>.<lambda>cs�Sr/rrr�rrr�Jr�z=Must supply at least one keyword argument with patch.multiplerr�N) r1r�r<rYr�rPrXrUr�)
rNr�rSr�rTrRrCrQr�rUr�rYZthis_patcherrr�r�_patch_multiple1sH���r�c
Ks$t|�\}} t|| |||||||� S)a7
`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 an
`AsyncMock if the patched object is an async function or a
`MagicMock` otherwise. 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 `AsyncMock` is
used for async functions and `MagicMock` for the rest.
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.
)r�rP)
rNr�r�rSr�rTrRrCrQrUrrrrbsF�rc@sReZdZdZddd�Zdd�Zdd �Zd
d�Zdd
�Zdd�Z dd�Z
eZe
ZdS)�_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|_dSr/)�in_dict�dictr�re�clear� _original)rAr�r�r�rCrrrr8�s
z_patch_dict.__init__cs.t�t�r����St����fdd��}|S)Ncs&���z�||�W�S���XdSr/)r��
_unpatch_dictr��frArr�_inner�sz$_patch_dict.__call__.<locals>._inner)r"r1rZr)rAr�r�rr�rr9�s
z_patch_dict.__call__cCsXt|�D]J}t||�}|�tj�rt|d�rt|j|j|j �}||�}t
|||�q|Sr^)r�r$rrr_r#r�r�r�r�rS)rAr>r�r`Z decoratorZ decoratedrrrrZ�s
�z_patch_dict.decorate_classcCs|��|jS)zPatch the dict.)r�r�r�rrrr~�sz_patch_dict.__enter__cCs�|j}t|jt�rt|j�|_|j}|j}z|��}Wn.tk
rdi}|D]}||||<qNYnX||_|rxt |�z|�
|�Wn*tk
r�|D]}||||<q�YnXdSr/)r�r"r�r�rOr�rVr:r��_clear_dictre)rAr�r�r�rer�rrrr��s&z_patch_dict._patch_dictcCsR|j}|j}t|�z|�|�Wn*tk
rL|D]}||||<q6YnXdSr/)r�r�r�rer:)rAr�rer�rrrr�sz_patch_dict._unpatch_dictcGs|��dS)zUnpatch the dict.F)r�)rArBrrrrysz_patch_dict.__exit__N)rF)
rMrPr�rNr8r9rZr~r�r�ryr�r�rrrrr��s
r�cCs>z|��Wn,tk
r8t|�}|D]
}||=q(YnXdSr/)r�r:rY)r�r�r�rrrr�&sr�cCsttj�D]}|��q
dS)z7Stop all active patches. LIFO to unroll nested patches.N)r�rPr�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 round trunc floor ceil bool next fspath aiter zHadd sub mul matmul div floordiv mod lshift rshift and xor or pow truediv� ccs|]}d|VqdS)zi%sNr�r�nrrrr.Nsr.ccs|]}d|VqdS)zr%sNrr�rrrr.Os�__get__�__set__�
__delete__�__reversed__�__missing__r��
__reduce_ex__Z__getinitargs__�__getnewargs__�__getstate__�__setstate__�
__getformat__Z
__setformat__r�r�__subclasses__�
__format__�__getnewargs_ex__cs�fdd�}||_|S)z:Turns a callable object (like a mock) into a real functioncs�|f|�|�Sr/r�rArBr�r,rr�method`sz_get_method.<locals>.method)rM)rr-r�rr,rr ^sr cCsh|]}d|�qS)r�r)rr�rrrrfs�
__aenter__� __aexit__� __anext__� __aiter__r�rr8r��__prepare__�__instancecheck__�__subclasscheck__�__del__cCs
t�|�Sr/)r�__hash__r�rrrr�|r�r�cCs
t�|�Sr/)r�__str__r�rrrr�}r�cCs
t�|�Sr/)r�
__sizeof__r�rrrr�~r�cCs"t|�j�d|���dt|���S)N�/)r1rMr�r�r�rrrr�r�)r�r�r��
__fspath__r�y�?g�?)
�__lt__�__gt__�__le__�__ge__�__int__r��__len__ry�__complex__� __float__�__bool__� __index__r�cs�fdd�}|S)Ncs$�jj}|tk r|S�|kr dStS�NT)�__eq__r�r�NotImplemented)�other�ret_valr�rrr��sz_get_eq.<locals>.__eq__r)rAr�rr�r�_get_eq�sr�cs�fdd�}|S)Ncs �jjtk rtS�|krdStS�NF)�__ne__r�rr�)r�r�rrr��s
z_get_ne.<locals>.__ne__r)rAr�rr�r�_get_ne�sr�cs�fdd�}|S)Ncs �jj}|tkrtg�St|�Sr/)�__iter__r�rr��r�r�rrr��sz_get_iter.<locals>.__iter__r)rAr�rr�r� _get_iter�sr�cs�fdd�}|S)Ncs(�jj}|tkrttg��Stt|��Sr/)r�r�r�_AsyncIteratorr�r�r�rrr��sz"_get_async_iter.<locals>.__aiter__r)rAr�rr�r�_get_async_iter�sr�)r�r�r�r�cCsbt�|t�}|tk r||_dSt�|�}|dk rB||�}||_dSt�|�}|dk r^||�|_dSr/)�_return_valuesr^rrv�_calculate_return_value�_side_effect_methodsr})r5r�rZfixedZreturn_calculatorrvZ
side_effectorrrr�_set_return_value�s
r�c@seZdZdd�Zdd�ZdS)�
MagicMixincOs&|��tt|�j||�|��dSr/)�_mock_set_magicsr�r�r8r�rrrr8�szMagicMixin.__init__cCs�ttB}|}t|dd�dk rX|�|j�}t�}||}|D]}|t|�jkr:t||�q:|tt|�j�}t|�}|D]}t ||t
||��qvdS)Nr�)�_magicsr:r$�intersectionr�rr1r]rrS�
MagicProxy)rAZorig_magicsZthese_magicsZ
remove_magicsr�r=rrrr��szMagicMixin._mock_set_magicsN)rMrPr�r8r�rrrrr��sr�c@seZdZdZddd�ZdS)rz-A version of `MagicMock` that isn't callable.FcCs|�||�|��dSr��r�r�r�rrrr��sz"NonCallableMagicMock.mock_add_specN)F�rMrPr�rNr�rrrrr�src@seZdZdd�ZdS)�AsyncMagicMixincOs&|��tt|�j||�|��dSr/)r�r�r�r8r�rrrr8�szAsyncMagicMixin.__init__N�rMrPr�r8rrrrr��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|�||�|��dSr�r�r�rrrr�szMagicMock.mock_add_specN)Fr�rrrrrs
rc@s&eZdZdd�Zdd�Zddd�ZdS) r�cCs||_||_dSr/�rr�)rArr�rrrr8szMagicProxy.__init__cCs8|j}|j}|j|||d�}t|||�t|||�|S)N)rr�r�)rr�r�rSr�)rAr�r��mrrr�create_mocks�zMagicProxy.create_mockNcCs|��Sr/)r�)rAr)r=rrrr�(szMagicProxy.__get__)N)rMrPr�r8r�r�rrrrr�s r�cs�eZdZed�Zed�Zed�Z�fdd�Zdd�Zdd �Z d
d�Z
dd
�Zdd�Zdd�Z
ddd�Zdd�Z�fdd�Z�ZS)r�r�r�r�cs\t�j||�tjj|jd<d|jd<d|jd<t�|jd<ttd�}t j
|_||jd<dS)Nr�rZ_mock_await_countZ_mock_await_argsZ_mock_await_args_list�r�r+)�superr8r%r�r�r]rrrrr'ZCO_COROUTINE�co_flags)rArBrC� code_mock�r�rrr81s
zAsyncMockMixin.__init__c�st||fdd�}|jd7_||_|j�|�|j}|dk r�t|�rL|�nbt|�s�zt|�}Wnt k
rxt
�YnXt|�r�|�n&t�|�r�|||�IdH}n
|||�}|t
k r�|S|jt
k r�|jS|jdk r�t�|j�r�|j||�IdHS|j||�S|jS)NTrDr�)rFr�r�r�r�r}r3rXr��
StopIteration�StopAsyncIterationr%r&rr�rvr�)rArBrCrrGrfrrrrCAs6
z!AsyncMockMixin._execute_mock_callcCs(|jdkr$d|jpd�d�}t|��dS)zA
Assert that the mock was awaited at least once.
r� Expected r5z to have been awaited.N�r�r�rr rrrr�is
zAsyncMockMixin.assert_awaitedcCs0|jdks,d|jpd�d|j�d�}t|��dS)z@
Assert that the mock was awaited exactly once.
r�r�r5�$ to have been awaited once. Awaited � times.Nr�r rrrr�qs
z"AsyncMockMixin.assert_awaited_oncecsz�jdkr&�����}td|�d������fdd�}����f�}���j�}||krvt|t�rf|nd}t|��|�dS)zN
Assert that the last await was with the specified arguments.
NzExpected await: z
Not awaitedcs�j��dd�}|S)N�await)rr!r"r#rrr$�sz:AsyncMockMixin.assert_awaited_with.<locals>._error_message)r�rrrr"r%)rArBrCr&r$r'r(rr#rr�zs
z"AsyncMockMixin.assert_awaited_withcOs8|jdks,d|jpd�d|j�d�}t|��|j||�S)zi
Assert that the mock was awaited exactly once and with the specified
arguments.
r�r�r5r�r�)r�r�rr�r)rrrr��s
z'AsyncMockMixin.assert_awaited_once_withcsZ��||f�}�fdd��jD�}||krVt|t�r8|nd}��||�}td|�|�dS)zU
Assert the mock has ever been awaited with the specified arguments.
csg|]}��|��qSrr*r+r�rrr��sz3AsyncMockMixin.assert_any_await.<locals>.<listcomp>Nz%s await not found)rr�r"r%rrr9rr�rr��s��zAsyncMockMixin.assert_any_awaitFc s��fdd�|D�}tdd�|D�d�}t�fdd��jD��}|s�||kr�|dkrXd}nd�d d�|D��}t|�d
t|��d�j���|�dSt|�}g}|D]2}z|�|�Wq�tk
r�|�|�Yq�Xq�|r�tdt |�f�|�dS)
a�
Assert the mock has been awaited with the specified calls.
The :attr:`await_args_list` list is checked for the awaits.
If `any_order` is False (the default) then the awaits must be
sequential. There can be extra calls before or after the
specified awaits.
If `any_order` is True then the awaits can be in any order, but
they must all appear in :attr:`await_args_list`.
csg|]}��|��qSrr*r+r�rrr��sz4AsyncMockMixin.assert_has_awaits.<locals>.<listcomp>css|]}t|t�r|VqdSr/r-rrrrr.�s
z3AsyncMockMixin.assert_has_awaits.<locals>.<genexpr>Nc3s|]}��|�VqdSr/r*r+r�rrr.�szAwaits not found.z,Error processing expected awaits.
Errors: {}cSsg|]}t|t�r|nd�qSr/r-rrrrr��s�r/z
Actual: z%r not all found in await list)
r�rrr�r1rrYr3r<r�rZ) rAr4r5r&r(Z
all_awaitsr6r7r8rr�rr��s>������z AsyncMockMixin.assert_has_awaitscCs0|jdkr,d|jpd�d|j�d�}t|��dS)z9
Assert that the mock was never awaited.
rr�r5z# to not have been awaited. Awaited r�Nr�r rrrr��s
z!AsyncMockMixin.assert_not_awaitedcs&t�j||�d|_d|_t�|_dS)z0
See :func:`.Mock.reset_mock()`
rN)r�rur�r�rrr�r@r�rrru�szAsyncMockMixin.reset_mock)F)rMrPr�r�r�r�r�r8rCr�r�r�r�r�r�r�ru�
__classcell__rrr�rr�,s(
, r�c@seZdZdZdS)r aa
Enhance :class:`Mock` with features allowing to mock
an async function.
The :class:`AsyncMock` object will behave so the object is
recognized as an async function, and the result of a call is an awaitable:
>>> mock = AsyncMock()
>>> asyncio.iscoroutinefunction(mock)
True
>>> inspect.isawaitable(mock())
True
The result of ``mock()`` is an async function which will have the outcome
of ``side_effect`` or ``return_value``:
- if ``side_effect`` is a function, the async function will return the
result of that function,
- if ``side_effect`` is an exception, the async function will raise the
exception,
- if ``side_effect`` is an iterable, the async function will return the
next value of the iterable, however, if the sequence of result is
exhausted, ``StopIteration`` is raised immediately,
- if ``side_effect`` is not defined, the async function will return the
value defined by ``return_value``, hence, by default, the async function
returns a new :class:`AsyncMock` object.
If the outcome of ``side_effect`` or ``return_value`` is an async function,
the mock async function obtained when the mock object is called will be this
async function itself (and not an async function returning an async
function).
The test author can also specify a wrapped object with ``wraps``. In this
case, the :class:`Mock` object behavior is the same as with an
:class:`.Mock` object: the wrapped object may have methods
defined as async function functions.
Based on Martin Richard's asynctest project.
NrHrrrrr �sr c@s(eZdZdZdd�Zdd�Zdd�ZdS) �_ANYz2A helper object that compares equal to everything.cCsdSr�r�rAr�rrrr� sz_ANY.__eq__cCsdSr�rr�rrrr� sz_ANY.__ne__cCsdS)Nz<ANY>rr�rrrr� sz
_ANY.__repr__N)rMrPr�rNr�r�r�rrrrr� sr�cCs`d|}d}d�dd�|D��}d�dd�|��D��}|r@|}|rX|rP|d7}||7}||S)Nz%s(%%s)r�z, cSsg|]}t|��qSr)�reprr�rrrr�! sz*_format_call_signature.<locals>.<listcomp>cSsg|]\}}d||f�qS)z%s=%rr)rr�r�rrrr�" s)r�r�)rrBrCrZformatted_argsZargs_stringZ
kwargs_stringrrrr s
�rc@s�eZdZdZd!dd�Zd"d d
�Zdd�ZejZd
d�Z dd�Z
dd�Zdd�Zdd�Z
dd�Zedd��Zedd��Zdd�Zdd �ZdS)#rFa�
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�NFTcCs�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)Nr�r�r�)r�r"r�rZr�)r�r�rr�rE� from_kallrBrC�_len�first�secondrrrr�C s.
z
_Call.__new__cCs||_||_||_dSr/)r�r��_mock_from_kall)rAr�rr�rEr�rrrr8c sz_Call.__init__cCsh|tkrdSzt|�}Wntk
r.YdSXd}t|�dkrJ|\}}n
|\}}}t|dd�r|t|dd�r||j|jkr|dSd}|dkr�di}}n�|dkr�|\}}}n�|d kr�|\} t| t�r�| }i}n"t| t�r�| }di}}nd}| }nV|dk�r@|\}
}t|
t��r4|
}t|t��r(|i}}n
d|}}n
|
|}}ndS|�rX||k�rXdS||f||fkS)
NTFr�r�r�rrr�r�)rr�rr$r�r"rZr�)rAr�Z len_otherZ self_nameZ self_argsZself_kwargsZ
other_nameZ
other_argsZother_kwargsr�r�r�rrrr�j sR
�
z_Call.__eq__cOs<|jdkrtd||fdd�S|jd}t|j||f||d�S)Nr�r�r�r��r�rFrrrrr9� s
z_Call.__call__cCs2|jdkrt|dd�Sd|j|f}t||dd�S)NF)rr�z%s.%s)rr�r�r�)rAr�rrrrr�� s
z_Call.__getattr__cCs|tjkrt�t�||�Sr/)rZr]r:�__getattribute__)rAr�rrrr�� s
z_Call.__getattribute__cOs|�d�||�S)Nr��r�r@rrrr�� sz_Call.countcOs|�d�||�S)N�indexr�r@rrrr�� sz_Call.indexcCs(t|�dkr|\}}n
|\}}}||fS)Nr�)r�rrrr�_get_call_arguments� s
z_Call._get_call_argumentscCs|��dS�Nr�r�r�rrrrB� sz
_Call.argscCs|��dS)Nr�r�r�rrrrC� sz_Call.kwargscCs||js&|jpd}|�d�r"d|}|St|�dkr@d}|\}}n0|\}}}|sTd}n|�d�shd|}nd|}t|||�S)Nrr�zcall%sr�zcall.%s)r�r�rr�r)rArrBrCrrrr�� s
z_Call.__repr__cCs4g}|}|dk r(|jr |�|�|j}qtt|��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)r�r�r�rrr�)rAZvalsrJrrr� call_list� s
z_Call.call_list)rr�NFT)rNNFT)rMrPr�rNr�r8r�rr�r9r�r�r�r�r�r�rBrCr�r�rrrrrF0 s*�
�
7
rF)r�c Kslt|�rt|�}t|t�}t|�}d|i}|r8d|i}n|dkrDi}|rT|rTd|d<|�|�t} t�|�rri}n8|r�|r�td��t } n"t
|�s�t} n|r�|r�t|�s�t} |�
d|�}|}
|dkr�d}
| f|||
|d �|��}t|t��rt||�}|�rt|�nt||||�|dk �r,|�s,||j|<|�rV|�sVd
|k�rVt||dd|d�|_t|�D�]}t|��rr�q^zt||�}
Wntk
�r�Y�q^YnXd|
i}|�r�d|
i}t|
t��s�t|
||||�}||j|<np|}t|t��r�|j}t|||�}||d
<t�|
��rt }nt}|f||||d�|��}||j|<t|
||d�t|t��r^t|||��q^|S)aICreate a mock object using another object as a spec. Attributes on the
mock will use the corresponding attribute on the `spec` object as their
spec.
Functions or methods being mocked will have their arguments checked
to check that they are called with the correct signature.
If `spec_set` is True then attempting to set attributes that don't exist
on the spec object will raise an `AttributeError`.
If a class is used as a spec then the return value of the mock (the
instance of the class) will have the same spec. You can use a class as the
spec for an instance object by passing `instance=True`. The returned mock
will only be callable if instances of the mock are callable.
`create_autospec` also takes arbitrary keyword arguments that are passed to
the constructor of the created mock.r�r�NTr�zJInstance can not be True when create_autospec is mocking an async functionrr�)r�r�r�rrvr�)rKrtr�r�)r�rr�r�)rJ)r[r1r"r.rerr'Zisdatadescriptor�RuntimeErrorr rXrr`r�r6rir�rLr~rrvr�r�r$r:r�r5�
_must_skipr%r&rS)r�r�rKr�rtrC�is_typeZ
is_async_funcr}r|r�r5r�rer�r�rJZchild_klassrrrr� s�
��
�
��
rcCsxt|t�s$|t|di�krdS|j}|jD]H}|j�|t�}|tkrFq*t|tt f�rZdSt|t
�rl|SdSq*|S)z[
Return whether we should skip the first argument on spec's `entry`
attribute.
r]F)r"r1r$r�r\r]r^rrVrWr6)r�r�r�r>rfrrrr�w
s
r�c@seZdZddd�ZdS)r�FNcCs(||_||_||_||_||_||_dSr/)r��idsr�r�rKr)rAr�r�r�rr�rKrrrr8�
sz_SpecState.__init__)FNNNFr�rrrrr��
s
�r�cCs"t|t�rt�|�St�|�SdSr/)r"�bytes�io�BytesIO�StringIO)� read_datarrr�
_to_stream�
s
rr�cs&t��}|dg���fdd�}��fdd�}��fdd����fdd ����fd
d�}tdkr�ddl}ttt|j���tt|j����a|dkr�t d
t
d�}t td����j_d�j
_d�j_d�j_d�j_|�j_���d<�d�j_|�j_��j_|�j_����fdd�}||_�|_|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`, `readline` and `readlines` of the
file handle to return. This is an empty string by default.
Ncs$�jjdk r�jjS�dj||�Sr�)� readlinesrvra��_state�handlerr�_readlines_side_effect�
sz)mock_open.<locals>._readlines_side_effectcs$�jjdk r�jjS�dj||�Sr�)�readrvrarrr�_read_side_effect�
sz$mock_open.<locals>._read_side_effectc?s$��EdH�dj||�VqdSr�)�readlinera)�_iter_side_effectrrr�_readline_side_effect�
sz(mock_open.<locals>._readline_side_effectc3s0�jjdk r�jjVq�dD]
}|Vq dSr�)rrv)�linerrrr
�
sz$mock_open.<locals>._iter_side_effectcs �jjdk r�jjSt�d�Sr�)rrvr�rrrr�_next_side_effect�
sz$mock_open.<locals>._next_side_effectr�open)rr�r�r�cs6t���d<�jj�dkr2���d<�d�j_tS)Nrr�)rrr}rra)rrrrrr�
reset_data�
s
zmock_open.<locals>.reset_data)r� file_spec�_iorYrr��
TextIOWrapper�unionrrrr~rv�writer
rrr}r�r�)r5rZ
_read_datar rrrrr)r
rrrrrr
�
s8"
r
c@s*eZdZdZdd�Zd dd�Zdd�ZdS)
raW
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|�Sr/)r)rArCrrrr�szPropertyMock._get_child_mockNcCs|�Sr/r)rAr)Zobj_typerrrr�szPropertyMock.__get__cCs||�dSr/r)rAr)r�rrrr�
szPropertyMock.__set__)N)rMrPr�rNr�r�r�rrrrr�
s
rc Cs^d|_t|�D]J}zt||�}Wntk
r8YqYnXt|t�sFq|j|krt|�qdS)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�r$r:r"rr�r)r5r�r�rrrrs
rc@s(eZdZdZdd�Zdd�Zdd�ZdS) r�z8
Wraps an iterator in an asynchronous iterator.
cCs&||_ttd�}tj|_||jd<dS)Nr�r+)�iteratorrrr'ZCO_ITERABLE_COROUTINEr�r])rArr�rrrr8+s
z_AsyncIterator.__init__cCs|Sr/rr�rrrr�1sz_AsyncIterator.__aiter__c�s*zt|j�WStk
r YnXt�dSr/)r�rr�r�r�rrrr�4s
z_AsyncIterator.__anext__N)rMrPr�rNr8r�r�rrrrr�'sr�)F)F)NFNNN)FFNN)Nr�)y�__all__�__version__r%rarr'r�rz�builtins�typesrrrZ
unittest.utilr� functoolsrrr�rqr
r�r�r*r.r!r3r7r=rLrGrXr[r`rirdr�r�rr�r�rr�MISSINGr
ZDELETEDr�r�r�rYrrr�r�r�rr�r<rrMrOrPr�r�r�rr�r�r�r�ZmultipleZstopallr_Z
magic_methodsZnumericsr�r�Zinplace�rightZ
_non_defaultsr r�r:Z_sync_async_magicsZ
_async_magicsr;r�rr�r�r�r�r�r�r�r�r�r�rr�rr�r�r r�rrrZrFrrr�r�r1r�r6rrr
rrr�rrrr�<module>s~
1� h4<�
�
2�
Mw ��� �
���
� 8+B
�
�
N