U
��\=Q�@sddlmZdZddlmZddlZddlZddlZddlZzddl Z Wne
k
rdddlZ YnXddlm
Z
mZddlmZddlZddlmZdZed d
�e�d�D��ZddlZz
ejZWn"ek
r�ddlZejZYnXzeWnek
�reZZYnXze Wnek
�r2e!Z YnXej"�rNe#Z$e$fdd
�Z#[$dd�e%e �D�Z&z
ej'Z(Wn<ek
�r�ddl)Z)ddl*Z*e*�+de*j,�Z-dd�Z(YnXdZ.e/Z0dd�Z1dd�Z2Gdd�de3�Z4e5e4j6�e7fZ8dd�Z9d�dd�Z:dd �Z;d!d"�Z<d#d$�Z=d%d&�Z>d�d'd(�Z?d)d*�Z@d+d,�ZAGd-d.�d.e3�ZBd/d0�ZCGd1d2�d2e3�ZDeD�ZEeEjFZFeEjGZHeEjIZJGd3d4�d4�ZKe5eK�ZLe5fZMej"�r�e5eLfZMd5d6d7d8d9d:d;d<hZNd=d>�ZOGd?d@�d@eP�ZQdAdB�ZRGdCdD�dDe3�ZSGdEdF�dFe3�ZTGdGdH�dHeT�ZUdIdJ�ZVGdKdL�dLeT�ZWGdMdN�dNeWeU�ZXdOdP�ZYdQdR�ZZdSdT�Z[GdUdV�dVe3�Z\dWdX�Z]eFdddddfdYdZ�Z^d�d[d\�Z_eFdddddfd]d^�Z`Gd_d`�d`e3�Zadadb�Zbdcdd�Zce^e`_3eae`_de_e`_eece`_fdee`_gdfZhdgZiejj�r�eidh7Zidi�kdjd
�ei��D��Zldi�kdkd
�ei��D��ZmdlZnejj�r6dmZnejdnk�r:endo7ZnndpZndqdrdsdtdudvdwdxdydzd{d|d}d~dd�d�d�d�d�d�d�hZod�d��Zpd�d�di�keheielemeng���D�ZqeqeoBZrd�d�d�d�d�d�d�d�hZsd�d��d�d��d�d��d�d��d�d��d��Zteueueueud�dddd�d�ddd�d�e d��d�d��Zvd�d��Zwd�d��Zxd�d��Zyewexeyd��Zzd�d��Z{Gd�d��d�e3�Z|Gd�d��d�e|eU�Z}Gd�d��d�e|eX�Z~Gd�d��d�e3�ZGd�d��d�e3�Z�e��Z�d�d��Z�Gd�d��d�e�Z�e�dd��Z�d�d�d��Z�d�d��Z�Gd�d��d�e3�Z�e5e��e5e�j��fZ�e5e�j�j��fZ�da�d�d��Z�d�d�d��Z�Gd�dd�eX�Z�d�dĄZ�dS)��)�absolute_import)�__version__�version_info�Mock� MagicMock�patch�sentinel�DEFAULT�ANY�call�create_autospec�
FILTER_DIR�
CallableMixin�NonCallableMock�NonCallableMagicMock� mock_open�PropertyMock�seal)�partialN)�
ModuleType�
MethodType)� safe_repr)�wrapsz3.0.5ccs|]}t|�VqdS�N)�int��.0�p�r�/mock.py� <genexpr>Isr �.cCst|dd�r|��S||�S)N�__next__)�getattrr")�obj�_nextrrr�nextdsr&cCsh|]}|�d�s|�qS��_��
startswith)r�namerrr� <setcomp>ls
r,z^[a-z_][a-z0-9_]*$cCs|tjkrdSt�|�S�NF)�keyword�kwlist�regex�match)�stringrrr�
_isidentifierus
r3TcCstt|�t�Sr)�
issubclass�typer�r$rrr�_is_instance_mock�sr7cCst|t�pt|t�ot|t�Sr)�
isinstance�
BaseException�
ClassTypesr4r6rrr�
_is_exception�s
�r;c@seZdZdgZdS)�_slotted�aN)�__name__�
__module__�__qualname__� __slots__rrrrr<�sr<cCs�t|t�r6|s6z
|j}Wntk
r.YdSXd}n,t|t�sbz
|j}Wntk
r`YdSX|rrt|d�}n|}z|t|�fWStk
r�YdSXdS)z�
Given an arbitrary, possibly callable object, try to create a suitable
signature object.
Return a (reduced func, signature) tuple, or None.
NT) r8r:�__init__�AttributeError�
FunctionTypes�__call__r�inspectsignature�
ValueError)�funcZas_instanceZeat_selfZsig_funcrrr�_get_signature_object�s$
rIFcsNt|||���dkrdS�\}��fdd�}t||�|t|�_�t|�_dS)Ncs�j||�dSr��bind��
_mock_self�args�kwargs��sigrr�checksig�sz"_check_signature.<locals>.checksig)rI�_copy_func_detailsr5�_mock_check_sig�
__signature__)rH�mock� skipfirst�instancerRrrPr�_check_signature�s
rYc CsbdD]0}zt||t||��Wqtk
r2YqXqtjr^z|j|_Wntk
r\YnXdS)N)r>�__doc__�__text_signature__r?�__defaults__�__kwdefaults__)�setattrr#rC�six�PY2Z
func_defaults)rH�funcopy� attributerrrrS�srScCs@t|t�rdSt|tttf�r(t|j�St|dd�dk r<dSdS)NTrEF)r8r:�staticmethod�classmethodr� _callable�__func__r#r6rrrre�s
recCst|�ttfkSr)r5�list�tupler6rrr�_is_list�sricCs�t|t�st|dd�dk StjrJ|f|jD]}|j�d�dk r,dSq,n2|}|j�d�dk rbdS|jD]}t |�rhdSqhdS)ztGiven an object, return True if the object is callable.
For classes, return True if instances would be callable.rENTF)
r8r:r#r_�PY3�__mro__�__dict__�get� __bases__�_instance_callable)r$�base�klassrrrro�s
rocs�t|t�}t|||�}|dkr"|S|\}��fdd�}t||�|j}t|�sRd}||d�}d|} t�| |�||}
t|
|��|
S)Ncs�j||�dSrrJ�rNrOrPrrrR
sz _set_signature.<locals>.checksigra)Z
_checksig_rVzYdef %s(*args, **kwargs):
_checksig_(*args, **kwargs)
return mock(*args, **kwargs)) r8r:rIrSr>r3r_Zexec_�_setup_func)rV�originalrXrW�resultrHrRr+�context�srcrarrPr�_set_signatures$
�rxcs���_�fdd�}�fdd�}�fdd�}�fdd�}�fd d
�}�fdd�}�fd
d�} ��fdd�}
d�_d�_d�_t��_t��_t��_�j�_�j �_ �j
�_
|�_|�_|�_
| �_|
�_|�_|�_|�_|�_��_dS)Ncs�j||�Sr)�
assert_calledrr�rVrrry!sz"_setup_func.<locals>.assert_calledcs�j||�Sr)�assert_not_calledrrrzrrr{#sz&_setup_func.<locals>.assert_not_calledcs�j||�Sr)�assert_called_oncerrrzrrr|%sz'_setup_func.<locals>.assert_called_oncecs�j||�Sr)�assert_called_withrrrzrrr}'sz'_setup_func.<locals>.assert_called_withcs�j||�Sr)�assert_called_once_withrrrzrrr~)sz,_setup_func.<locals>.assert_called_once_withcs�j||�Sr)�assert_has_callsrrrzrrr+sz%_setup_func.<locals>.assert_has_callscs�j||�Sr)�assert_any_callrrrzrrr�-sz$_setup_func.<locals>.assert_any_callcs:t��_t��_����j}t|�r6|�k r6|��dSr)� _CallList�method_calls�
mock_calls�
reset_mock�return_valuer7)�ret�rarVrrr�/sz_setup_func.<locals>.reset_mockFr)rV�called�
call_count� call_argsr��call_args_listr�r�r��side_effect�_mock_childrenr}r~rr�r�ryr{r|rU�_mock_delegate)rarVrQryr{r|r}r~rr�r�rr�rrss8rscCsd|dd�|kS)N�__%s__����r�r+rrr� _is_magicOsr�c@s(eZdZdZdd�Zdd�Zdd�ZdS) �_SentinelObjectz!A unique, named, sentinel object.cCs
||_dSrr���selfr+rrrrBUsz_SentinelObject.__init__cCs
d|jS)Nzsentinel.%sr��r�rrr�__repr__Xsz_SentinelObject.__repr__cCst|jffSr)�_unpickle_sentinelr+r�rrr�
__reduce__[sz_SentinelObject.__reduce__N)r>r?r@rZrBr�r�rrrrr�Ssr�cCs
tt|�Sr)r#rr�rrrr�_sr�c@s eZdZdZdd�Zdd�ZdS)� _SentinelzAAccess attributes to return a named object, usable as a sentinel.cCs
i|_dSr)�
_sentinelsr�rrrrBesz_Sentinel.__init__cCs|dkrt�|j�|t|��S)Nrn)rCr��
setdefaultr�r�rrr�__getattr__hsz_Sentinel.__getattr__N)r>r?r@rZrBr�rrrrr�csr�c@seZdZdS)�
OldStyleClassN)r>r?r@rrrrr�vsr�r��_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)r�r#)r�r+� _the_namerQrrr�_get�s
z"_delegating_property.<locals>._getcSs*|j}|dkr||j|<nt|||�dSr)r�rlr^)r��valuer+r�rQrrr�_set�sz"_delegating_property.<locals>._set)�_allowed_names�add�property)r+r�r�r�rrr�_delegating_property�s
r�c@seZdZdd�Zdd�ZdS)r�cCslt|t�st�||�St|�}t|�}||kr2dStd||d�D]"}||||�}||krDdSqDdS)NFr�T)r8rg�__contains__�len�range)r�r�Z len_valueZlen_self�iZsub_listrrrr��s
z_CallList.__contains__cCst�t|��Sr)�pprintZpformatrgr�rrrr��sz_CallList.__repr__N)r>r?r@r�r�rrrrr��sr�cCs�t|t�r*z
|j}Wntk
r(YnXt|�s6dS|jsV|jsV|jdk sV|jdk rZdS|}|dk rz||krrdS|j}q^|r�||_||_|r�||_||_dS)NFT) r8rDrVrCr7r�r�r�r�)�parentr�r+�new_name�_parentrrr�_check_and_set_parent�s2
��r�c@seZdZdd�Zdd�ZdS)� _MockItercCst|�|_dSr)�iterr$)r�r$rrrrB�sz_MockIter.__init__cCs
t|j�Sr)r&r$r�rrrr"�sz_MockIter.__next__N)r>r?r@rBr"rrrrr��sr�c@seZdZeZdZdd�ZdS)�BaseNcOsdSrr�r�rNrOrrrrB�sz
Base.__init__)r>r?r@r r�r�rBrrrrr��sr�c@sReZdZdZdd�ZdHdd�Zd d
�ZdIdd�ZdJd
d�Zdd�Z dd�Z
dZee e
e�Z
edd��Zed�Zed�Zed�Zed�Zed�Zdd�Zdd�Zeee�ZdKdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�Z d1d2�Z!d3d4�Z"d5d6�Z#d7d8�Z$d9d:�Z%d;d<�Z&d=d>�Z'dLd?d@�Z(dAdB�Z)dCdD�Z*dMdFdG�Z+dS)Nrz A non-callable version of `Mock`cOs$t|j|fd|ji�}t�|�}|S)NrZ)r5r>rZ�object�__new__)�clsrN�kw�newrXrrrr��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_wrapsr�Z_mock_calledZ_mock_call_argsrZ_mock_call_countZ_mock_call_args_listZ_mock_mock_callsr��_mock_unsafe)rl�_mock_add_specr��configure_mock�_safe_superrrB)r��specrr+�spec_setr��_spec_state� _new_name�_new_parent�_spec_as_instance� _eat_selfZunsaferOrlrrrrB�sD
�zNonCallableMock.__init__cCs(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�r�r�r�r^)r�rVrbrrr�attach_mocks
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��r�r�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)rir8r:r5rI�dirrl) r�r�r�r�r�r�r��resrlrrrr�+s"
�zNonCallableMock._mock_add_speccCs8|j}|jdk r|jj}|tkr4|j|dd�}||_|S)N�()�r�r�)r�r�r�r �_get_child_mock)r�r�rrrZ__get_return_valueBs
�z"NonCallableMock.__get_return_valuecCs,|jdk r||j_n||_t||dd�dS)Nr�)r�r�r�r�)r�r�rrrZ__set_return_valueOs
z"NonCallableMock.__set_return_valuez1The value to be returned when the mock is called.cCs|jdkrt|�S|jSr)r�r5r�rrr� __class__[s
zNonCallableMock.__class__r�r�r�r�r�cCsN|j}|dkr|jS|j}|dk rJt|�sJt|t�sJt|�sJt|�}||_|Sr)r�r�r��callabler8r�r;)r�� delegatedZsfrrrZ__get_side_effecths��z!NonCallableMock.__get_side_effectcCs(t|�}|j}|dkr||_n||_dSr)� _try_iterr�r�r�)r�r�r�rrrZ__set_side_effectss
z!NonCallableMock.__set_side_effectcCs�|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)�id�appendr�r�r�r�r�r�r�r r�r�r��valuesr8�
_SpecState�_deletedr�r7)r�Zvisitedr�r�Zchildr�rrrr�~s,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)Nrr!)�count)�entryrrr�<lambda>��z0NonCallableMock.configure_mock.<locals>.<lambda>)�keyr!N)�sorted�items�split�popr#r^)r�rO�arg�valrN�finalr$r�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�r+rr�r�)rCr��_all_magicsr�r�r*r�rmr�r�r#r�r8r�rr�r�rXr�r+)r�r+rurrrrr��sF
�
�
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�rVr�)r�z().r)r�r�r�rg�reversedr�r��join)r�Z
_name_listr�Zlast�dotZ_firstrrr�_extract_mock_name�s(
z"NonCallableMock._extract_mock_namecCs^|��}d}|dkrd|}d}|jdk rDd}|jr8d}||jj}d�t|�j||t|��S)Nr�)rVzmock.z name=%rz spec=%rz spec_set=%rz<{}{}{} id='{}'>)r�r�r�r>�formatr5r�)r�r+Zname_stringZspec_stringrrrr��s
�zNonCallableMock.__repr__cCs�tjsttdd�rt�|�S|jp$g}tt|��}t|j �}dd�|j
��D�}tjrrdd�|D�}dd�|D�}tt
||||��S)z8Filter the output of `dir(mock)` to only useful members.�__dir__NcSsg|]\}}|tk r|�qSr)r�)rZm_nameZm_valuerrr�
<listcomp>s�z+NonCallableMock.__dir__.<locals>.<listcomp>cSsg|]}|�d�s|�qSr'r)�r�errrr�s
cSs"g|]}|�d�rt|�r|�qSr')r*r�r�rrrr�s
�)rVr
r#r�r�r�r�r5rgrlr�r�r��set)r�ZextrasZ from_typeZ from_dictZfrom_child_mocksrrrr�s
�zNonCallableMock.__dir__csP|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�rBt�|��sB���d|}td|��t��||�S)Nz!Mock object has no attribute '%s'z.Attempting to set unsupported magic method %r.cs��f|�|�Srr�rNr��rtr�rrr�0r�z-NonCallableMock.__setattr__.<locals>.<lambda>r�r!zCannot set )r�r��__setattr__r�r�rlrC�_unsupported_magicsr�r7r^r5�_get_methodr�r�r�r��hasattrr�)r�r+r��msg� mock_namerr�rr�s<��
zNonCallableMock.__setattr__cCs�|tkr2|t|�jkr2tt|�|�||jkr2dS|j�|t�}||jkr\tt|�� |�n|t
krlt|��|tk r||j|=t
|j|<dSr)r�r5rl�delattrr�rm�_missingr�r�__delattr__r�rC)r�r+r$rrrrEs
zNonCallableMock.__delattr__cCs|jpd}t|||�S)NrV)r��_format_call_signature�r�rNrOr+rrr�_format_mock_call_signatureWs
z+NonCallableMock._format_mock_call_signaturecCs,d}|�||�}|j}|j|�}|||fS)N�0expected call not found.
Expected: %s
Actual: %s)r
r�)r�rNrO�message�expected_stringr�Z
actual_stringrrr�_format_mock_failure_message\s
z,NonCallableMock._format_mock_failure_messagec
Cs�|j}|dk rxt|�dkr(d}|\}}n
|\}}}z||j||�fWStk
rt}zd|_|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.
Nr�r�)r�r�rK� TypeError�
__traceback__)r��_callrQr+rNrOr�rrr�
_call_matcherds
zNonCallableMock._call_matchercCs4|}|jdkr0d|jpd|j|��f}t|��dS)z/assert that the mock was never called.
rz9Expected '%s' to not have been called. Called %s times.%srVN�r�r��_calls_repr�AssertionError�rMr�rrrrr{zs
��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.rVN)r�r�rrrrrry�s
��zNonCallableMock.assert_calledcCs4|}|jdks0d|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.%srVNrrrrrr|�s
��z"NonCallableMock.assert_called_oncecs�|��jdkr2�����}d}d||f}t|�����fdd�}����f�}���j�}||kr�t|t�rr|nd}t�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.Nznot called.rcs.�����}tjr*|dk r*d�|t|��}|S)Nz{}
{})rr_r`r��str)�causer�rNrOr�rr�_error_message�sz:NonCallableMock.assert_called_with.<locals>._error_message)r�r
rrr8� Exceptionr_�
raise_from)rMrNrO�expected�actualZ
error_messagerrrrrr}�s
�z"NonCallableMock.assert_called_withcOs<|}|jdks0d|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.%srV)r�r�rrr})rMrNrOr�rrrrr~�s
��z'NonCallableMock.assert_called_once_withc s��fdd�|D�}t|t�r |nd}t�fdd��jD��}|sp||krlt�tdt|��jdd�f�|�dSt|�}g}|D]2}z|� |�Wq�t
k
r�|�|�Yq�Xq�|r�t�td �jp�d
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�cr�rrr��sz4NonCallableMock.assert_has_calls.<locals>.<listcomp>Nc3s|]}��|�VqdSrrr r�rrr �sz3NonCallableMock.assert_has_calls.<locals>.<genexpr>zCalls not found.
Expected: %r%sZActual)�prefixz@%r does not contain all of %r in its call list, found %r insteadrV)r8rr�r�r_rrrrg�removerGr�r�rh)r�ZcallsZ any_orderrrZ all_callsZ not_foundZkallrr�rr�s>
�������z NonCallableMock.assert_has_callscs`��||f�}�fdd��jD�}||kr\t|t�r8|nd}��||�}t�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|]}��|��qSrrr r�rrr��sz3NonCallableMock.assert_any_call.<locals>.<listcomp>Nz%s call not found)rr�r8rr
r_rr)r�rNrOrrrr
rr�rr��s��zNonCallableMock.assert_any_callcKsxt|�}t|t�s2t|t�r"t}q<t|t�r<t}n
|jd}|jrnd|krVd|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!r�)r5r4rrrrrrkr�r�rC)r�r��_typerqrbrrrrr��s
zNonCallableMock._get_child_mock�CallscCs$|js
dSd|dt|j�dS)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!)r�r)r�r"rrrrszNonCallableMock._calls_repr)NNNNNNr�NFNF)F)FF)NFF)F)r%),r>r?r@rZr�rBr�r�r�Z"_NonCallableMock__get_return_valueZ"_NonCallableMock__set_return_valueZ"_NonCallableMock__return_value_docr�r�r�r�r�r�r�r�r�Z!_NonCallableMock__get_side_effectZ!_NonCallableMock__set_side_effectr�r�r�r�r�r�r�r�rr
rrr{ryr|r}r~rr�r�rrrrrr�sn
�
-
�
�
&'
%rcCsL|dkr|St|�r|St|�r$|Sz
t|�WStk
rF|YSXdSr)r;rer�rr6rrrr�s
r�c
@sBeZdZddedddddddf
dd�Zdd�Zdd�Zd d
�ZdS)rNr�c Ks6||jd<tt|�j||||||| |
f|�||_dS)Nr�)rlr�rrBr�)r�r�r�r�rr+r�r�r�r�r�rOrrrrB.s
��zCallableMixin.__init__cOsdSrrr�rrrrT;szCallableMixin._mock_check_sigcOs|j||�|j||�Sr)rT�
_mock_callrLrrrrE@szCallableMixin.__call__cOs�|}d|_|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
} qt|j}|dk �r\t
|��r"|�n,t|��sDt|�}
t
|
��rN|
�n
|||�}
|
tk �r\|
S|jtk �rn|jS|jdk �r�|j||�S|jS)NTr�)�twor�r�r!)r�r��_Callr�r�r�r�r�r�r�r�r�r�r;rer&r r�r�r�)rMrNrOr�rZdo_method_callsZmethod_call_nameZmock_call_nameZ is_a_callr�Zthis_mock_callr�Zeffectrurrrr'GsV
zCallableMixin._mock_call)r>r?r@r rBrTrEr'rrrrr,s�
rc@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.
Alternatively `side_effect` can be an exception class or instance. In
this case the exception will be raised when the mock is called.
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)r>r?r@rZrrrrr�srcCs8zt||�WStk
r2t|�t||�YSXdSr)r#rC�
__import__)�thing�comp�import_pathrrr�_dot_lookup�s
r.cCsB|�d�}|�d�}t|�}|D]}|d|7}t|||�}q |S)Nr!rz.%s)r�r�r*r.)�targetZ
componentsr-r+r,rrr� _importer�s
r0cCs
t|d�S)N�is_local)r)�patcherrrr�_is_started�sr3c@sdeZdZdZgZdd�Zdd�Zdd�Zdd �Zd
d�Z dd
�Z
dd�Zdd�Zdd�Z
dd�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)
r rG�getterrbr��new_callabler��createZ has_localr��autospecrO�additional_patchers)
r�r5rbr�r�r7r�r8r6rOrrrrB�s(��z_patch.__init__c
CsHt|j|j|j|j|j|j|j|j|j � }|j
|_
dd�|jD�|_|S)NcSsg|]}|���qSr)�copyrrrrr�sz_patch.copy.<locals>.<listcomp>)r4r5rbr�r�r7r�r8r6rO�attribute_namer9)r�r2rrrr:�s ��z_patch.copycCst|t�r|�|�S|�|�Sr)r8r:�decorate_class�decorate_callable�r�rHrrrrE s
z_patch.__call__cCsNt|�D]@}|�tj�sqt||�}t|d�s0q|��}t||||��q|S�NrE)r�r*r�TEST_PREFIXr#rr:r^)r�rq�attr�
attr_valuer2rrrr<s
z_patch.decorate_classcs<t�d�r�j�|��St����fdd���|g�_�S)N� patchingsc s�g}g}t�}z�zd�jD]@}|��}|�|�|jdk rD|�|�q|jt kr|�|�q|t|�7}�||�WW�6S||kr�t
|�r�|�|�t��}�YnXW5t|�D]}|j|�q�XdSr)
rhr��__exit__rC� __enter__r�r;�updater�r r3�sys�exc_info)rNZkeywargs�
extra_argsZentered_patchersrH�patchingr��rH�patchedrrrL"s,
�
z)_patch.decorate_callable.<locals>.patched)rrCr�rr>rrKrr=s
z_patch.decorate_callablec Cs�|��}|j}t}d}z|j|}Wn$ttfk
rHt||t�}YnXd}|tkrft|t �rfd|_
|j
s�|tkr�td�||���||fS)NFTz#{} does not have the attribute {!r})r5rbr rlrC�KeyErrorr#� _builtinsr8rr7r�)r�r/r+rt�localrrr�get_originalEs
�z_patch.get_originalcCs8|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|dd
�|��|_nl|dk �r�|t
k �rntd��|t
k�r�td��t|�}|dk�r�|}t|f||jd�|��}n|�r�td��|}||_||_t|j|j|�|jdk �r4i}|jt
k�r|||j<|j D]$}|�!�}|jt
k�r
|�|��q
|S|S)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=TruerEr�r�r+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�r8rOr6r5r/rrPr r8r:rrirerr5r4rrbrFr7ror�r��boolr�
temp_originalr1r^r;r9rE)r�r�r�r�r8rOr6rtrOZinherit�Klass�_kwargsZ this_specZnot_callableZnew_attrrIrJr�rrrrE]s�
�
��
�
�
�
��
z_patch.__enter__cGs�t|�sdS|jr0|jtk r0t|j|j|j�n>t|j|j�|jsnt |j|j�r\|jdkrnt|j|j|j�|`|`|`t
|j�D]}t|�r�|j|�q�dS)zUndo the patch.N)rZr?r\�__annotations__r])
r3r1rSr r^r/rbrr7rr�r9rD)r�rHr2rrrrD�s�z_patch.__exit__cCs|��}|j�|�|S)z-Activate a patch, returning any created mock.)rE�_active_patchesr�)r�rurrr�start�sz_patch.startcCs.z|j�|�Wntk
r$YnX|��S)zStop an active patch.)rWr#rGrDr�rrr�stop�s
z_patch.stop)r>r?r@r;rWrBr:rEr<r=rPrErDrXrYrrrrr4�s
(~r4c sPz��dd�\�}Wn&ttfk
r:td�f��YnX�fdd�}||fS)Nr!r�z.Need a valid target to patch. You supplied: %rcst��Sr�r0r�r/rrr�r�z_get_target.<locals>.<lambda>)�rsplitrrG)r/rbr5rr[r�_get_targets�r]c
s$�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.
cs�Srrrr[rrr�"r�z_patch_object.<locals>.<lambda>)r4)
r/rbr�r�r7r�r8r6rOr5rr[r�
_patch_objects�r^c
s�t��ttfkr�fdd�}n�fdd�}|s6td��t|���}|d\} }
t|| |
|||||i� }| |_|dd�D]2\} }
t|| |
|||||i� }| |_|j� |�qx|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��SrrZrr[rrr�@r�z!_patch_multiple.<locals>.<lambda>cs�Srrrr[rrr�Br�z=Must supply at least one keyword argument with patch.multiplerr�N)
r5�unicoderrGrgr�r4r;r9r�)
r/r�r7r�r8r6rOr5r�rbr�r2Zthis_patcherrr[r�_patch_multiple)sH���r`c
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.
)r]r4)
r/r�r�r7r�r8r6rOr5rbrrrrZsE�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�rF�clear� _original)r�rbr�rdrOrrrrB�s
z_patch_dict.__init__cs.t�t�r����St����fdd��}|S)Ncs&���z�||�W�S���XdSr)ra�
_unpatch_dictr���fr�rr�_inner�sz$_patch_dict.__call__.<locals>._inner)r8r:r<r)r�rhrirrgrrE�s
z_patch_dict.__call__cCsXt|�D]J}t||�}|�tj�rt|d�rt|j|j|j �}||�}t
|||�q|Sr?)r�r#r*rr@rrarbr�rdr^)r�rqrArBZ decoratorZ decoratedrrrr<�s
�z_patch_dict.decorate_classcCs|��dS)zPatch the dict.N)rar�rrrrE�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�r8rb�
basestringr0rdr:rCre�_clear_dictrF)r�r�rbrdrtr�rrrra�s&z_patch_dict._patch_dictcCsR|j}|j}t|�z|�|�Wn*tk
rL|D]}||||<q6YnXdSr)rbrerkrFrC)r�rbrtr�rrrrfsz_patch_dict._unpatch_dictcGs|��dS)zUnpatch the dict.F)rf)r�rNrrrrDsz_patch_dict.__exit__N)rF)
r>r?r@rZrBrEr<rErarfrDrXrYrrrrra�s
racCs>z|��Wn,tk
r8t|�}|D]
}||=q(YnXdSr)rdrCrg)rb�keysr�rrrrksrkcCsttj�D]}|��q
dS)z7Stop all active patches. LIFO to unroll nested patches.N)r�r4rWrY)rrrr�_patch_stopall%srmZtestz�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 z@add sub mul matmul div floordiv mod lshift rshift and xor or powz truediv� ccs|]}d|VqdS)zi%sNr�r�nrrrr Csccs|]}d|VqdS)zr%sNrrorrrr Dsr�z
bool next )��zfspath z.unicode long nonzero oct hex truediv rtruediv Z__cmp__Z__getslice__Z__setslice__Z
__coerce__�__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|�|�Srr�r�rNr��rHrr�method]sz_get_method.<locals>.method)r>)r+rHr�rr�rr[srcCsh|]}d|�qS)r�r)rr�rrrr,csr�r�rBr��__prepare__�__instancecheck__�__subclasscheck__�__del__cCs
t�|�Sr)r��__hash__r�rrrr�rr�r�cCs
t�|�Sr)r��__str__r�rrrr�sr�cCs
t�|�Sr)r��
__sizeof__r�rrrr�tr�cCstt�|��Sr)r_r�r�r�rrrr�ur�cCs&t|�jd|��dtt|��S)N�/)r5r>r�rr�r�rrrr�vr�)r�r�r�Z__unicode__�
__fspath__r�y�?g�?�1Z0x1)�__lt__�__gt__�__le__�__ge__�__int__r��__len__rD�__complex__� __float__�__bool__Z__nonzero__Z__oct__Z__hex__Z__long__� __index__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)r�r�rr�r�_get_eq�sr�cs�fdd�}|S)Ncs �jjtk rtS�|krdStSr-)�__ne__r�r r�)r�r�rrr��s
z_get_ne.<locals>.__ne__r)r�r�rr�r�_get_ne�sr�cs�fdd�}|S)Ncs �jj}|tkrtg�St|�Sr)�__iter__r�r r�)r�r�rrr��sz_get_iter.<locals>.__iter__r)r�r�rr�r� _get_iter�sr�)r�r�r�cCs�t�|t�}|tk r||_dSt�|�}|dk rdz||�}Wntk
rXt|�}YnX||_dSt�|�}|dk r�||�|_dSr)�_return_valuesrmr r��_calculate_return_valuerC�_side_effect_methodsr�)rVr�r+ZfixedZreturn_calulatorr�Z
side_effectorrrr�_set_return_value�s
r�c@seZdZdd�Zdd�ZdS)�
MagicMixincOs&|��tt|�j||�|��dSr)�_mock_set_magicsr�r�rBr�rrrrB�szMagicMixin.__init__cCs�t}t|dd�dk rPt�|j�}t�}t|}|D]}|t|�jkr2t||�q2|tt|�j�}t|�}|D]}t||t ||��qndS)Nr�)
�_magicsr#�intersectionr�r�r5rlrr^�
MagicProxy)r�Zthese_magicsZ
remove_magicsr�r$rrrr��szMagicMixin._mock_set_magicsN)r>r?r@rBr�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�r>r?r@rZr�rrrrr�src@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�rrrrr�s
rc@s&eZdZdd�Zdd�Zddd�ZdS) r�cCs||_||_dSr�r+r�)r�r+r�rrrrBszMagicProxy.__init__cCs8|j}|j}|j|||d�}t|||�t|||�|S)N)r+r�r�)r+r�r�r^r�)r�r�r��mrrr�create_mocks�zMagicProxy.create_mockNcCs|��Sr)r�)r�r$r$rrrrsszMagicProxy.__get__)N)r>r?r@rBr�rsrrrrr�s r�c@s,eZdZdZdd�Zdd�Zdd�ZdZdS) �_ANYz2A helper object that compares equal to everything.cCsdSr�r�r�r�rrrr�!sz_ANY.__eq__cCsdSr-rr�rrrr�$sz_ANY.__ne__cCsdS)Nz<ANY>rr�rrrr�'sz
_ANY.__repr__N)r>r?r@rZr�r�r�r�rrrrr�s
r�cspd|}d}d�dd�|D��}dd��d��fdd�t|���D��}|rP|}|rh|r`|d7}||7}||S) Nz%s(%%s)r�z, cSsg|]}t|��qSr)�repr)rr�rrrr�3sz*_format_call_signature.<locals>.<listcomp>cSs"tjrt|t�r|�d�S|SdS)Nzutf-8)r_r`r8r_�encode)�itemrrr�encode_item5s
z+_format_call_signature.<locals>.encode_itemcs g|]\}}d��|�|��qS)z{}={!r})r�)rr�r��r�rrr�;s)r�r�r�)r+rNrOrZformatted_argsZargs_stringZ
kwargs_stringrr�rr0s
�rc@s�eZdZdZd!dd�Zd"d d
�Zdd�Zd
d�ZdZdd�Z dd�Z
dd�Zdd�Zdd�Z
edd��Zedd��Zdd�Zdd �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�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)Nrrqr�r�)r�r8rjrhr�)r�r�r+r�r(� from_kallrNrO�_len�first�secondrrrr�\s.
z
_Call.__new__cCs||_||_||_dSr)r�r��_mock_from_kall)r�r�r+r�r(r�rrrrB|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�rrrqr�)r
r�rr#r�r8rhrj)r�r�Z len_otherZ self_nameZ self_argsZself_kwargsZ
other_nameZ
other_argsZother_kwargsr�r�r�rrrr��sR
�
z_Call.__eq__cCs|�|�Sr)r�r�rrrr��sz_Call.__ne__cOs<|jdkrtd||fdd�S|jd}t|j||f||d�S)Nr�r�r�r�)r�r)r rrrrE�s
z_Call.__call__cCs2|jdkrt|dd�Sd�|j|�}t||dd�S)NF)r+r�z{}.{})r+r�r�)r�r)r�)r�rAr+rrrr��s
z_Call.__getattr__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�r rrr�_get_call_arguments�s
z_Call._get_call_argumentscCs|��dS�Nr�r�r�rrrrN�sz
_Call.argscCs|��dS)Nr�r�r�rrrrO�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�r*r�r)r�r+rNrOrrrr��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�r�r�)r�Zvalsr+rrr� call_list�s
z_Call.call_list)rr�NFT)rNNFT)r>r?r@rZr�rBr�r�r�rEr�r�r�r�r�rNrOr�r�rrrrr)Is*�
�
7
r))r�c KsHt|�rt|�}t|t�}d|i}|r0d|i}n|dkr<i}|rL|rLd|d<|�|�t}t�|�rji}n"t|�sxt }n|r�|r�t
|�s�t }|�d|�}|} |dkr�d} |f||| |d�|��}
t|t�r�t
|
|�}
nt||
||�|dk r�|s�|
|j|<|�r$|�s$d |k�r$t||dd
|
d�|
_t|�D�]}tj�rV|�d��s,|�d
��rV�q,t|��rd�q,zt||�}Wntk
�r�Y�q,YnXd|i}|�r�d|i}t|t��s�t|||
||�}
|
|
j|<nZ|
}t|t��r�|
j}t|||�}||d<tf||||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�r+r�)r�r�r�r+r�r�)rXrQr�Zim_Zfunc_r�)r�r+r�r�)rW)rir5r8r:rFr�inspectZisdatadescriptorrerror�rDrxrYr�rr�r�r_r`r*r�r#rCr�rV�
_must_skipr^)r�r�rXr�rQrO�is_typerUrTr�rVr�rtr�r�rWrrrr
s�
��
�
��
rcCs�t|t�s$|t|di�krdS|j}t|d�s2|S|jD]P}|j�|t�}|tkrTq8t|t t
f�rhdStt|dd�t�r�|SdSq8|S)z[
Return whether we should skip the first argument on spec's `entry`
attribute.
rlFrkrsN)r8r:r#r�rrkrlrmr rcrd�MethodWrapperTypes)r�r�r�rqrurrrr�� s
r�c@seZdZddd�ZdS)r�FNcCs(||_||_||_||_||_||_dSr)r��idsr�r�rXr+)r�r�r�r�r+r�rXrrrrB� sz_SpecState.__init__)FNNNF)r>r?r@rBrrrrr�� s
�r�cCs"t|t�rt�|�St�|�SdSr)r8�bytes�io�BytesIO�StringIO)� read_datarrr�
_to_stream� s
r�cst��}|dg���fdd�}��fdd�}��fdd����fdd ��tdkr�tjr�d
dl}ttt|j��� tt|j
����anta|dkr�tdt
d�}ttd
����j_d�j_d�j_d�j_d�j_|�j_���d<�d�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�)� readlinesr�rr��_state�handlerr�_readlines_side_effect� sz)mock_open.<locals>._readlines_side_effectcs$�jjdk r�jjS�dj||�Sr�)�readr�rrr�rr�_read_side_effect� sz$mock_open.<locals>._read_side_effectc?s*��D]
}|Vq�dj||�VqdSr�)�readline)rNrOr�)�_iter_side_effectr�rr�_readline_side_effect� s
z(mock_open.<locals>._readline_side_effectc3s0�jjdk r�jjVq�dD]
}|Vq dSr�)r�r�)�liner�rrr�� sz$mock_open.<locals>._iter_side_effectr�open)r+r�)r�r�cs6t���d<�jj�dkr2���d<�d�j_tS)Nrr�)r�r�r�r rr)r�r�r�r�rr�
reset_data
s
zmock_open.<locals>.reset_data)r�� file_specr_rj�_iorgr�r��
TextIOWrapper�unionr��filerr�rEr��writer�r�r�r�r�)rVr�Z
_read_datar�r�r�r�r)r�r�r�r�r�rr� s8$
rc@s(eZdZdZdd�Zdd�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)r�rOrrrr�
szPropertyMock._get_child_mockcCs|�Srr)r�r$Zobj_typerrrrs"
szPropertyMock.__get__cCs||�dSrr)r�r$r�rrrrt$
szPropertyMock.__set__N)r>r?r@rZr�rsrtrrrrr
src 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#rCr8rr�r)rVrAr�rrrr(
s
r)F)F)NFNNN)FFNN)Nr�)�Z
__future__r�__all__� functoolsrr�r�r�rG�builtins�ImportErrorZ__builtin__�typesrrZ
unittest.utilrr_rrrhr�rrVZ signaturerFrCZfuncsigsr_� NameErrorrrjZlongrr`r&r%r�rN�isidentifierr3r.�re�compile�Ir0r
�superr�r7r;r�r<r5r=r�ZDescriptorTypesrIrYrSrerirorxrsr�r�r�r�rr �MISSINGrZDELETEDr�r�Z ClassTyper:r�r�rgr�r�r�r�rr�rrr.r0r3r4r]r^r`rrarkrmrcZmultipleZstopallr@Z
magic_methodsZnumericsrjr�Zinplace�rightZextraZ
_non_defaultsrr�r�rr�r�r�r�r�r�r�r�r�rrr�r�r
rr)rrr�r�r�rDrsr�r�r�rrrrrrr�<module>"s�
�!
1�!D`8,�
�
2�
Lv ��
�
��� �
�
>
�
~�
�
M |