B
    v9a=                @   sZ  d Z dZddlZddlZddlZddlZddlZddlmZm	Z	 ddl
mZmZ dd eeD ZefZdejkrddlZeejjfZd	ZeZd
d Zdd Zdd ZdddZdd Zdd Zdd Zdd ZdddZ dd Z!dd  Z"G d!d" d"e#Z$G d#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-d.hZ-d/d0 Z.G d1d2 d2e/Z0d3d4 Z1G d5d6 d6e#Z2G d7d8 d8e#Z3G d9d: d:e3Z4d;d< Z5G d=d> d>e3Z6G d?d@ d@e6e4Z7dAdB Z8dCdD Z9dEdF Z:G dGdH dHe#Z;dIdJ Z<e'dddddfdKdLZ=ddMdNZ>e'dddddfdOdPZ?G dQdR dRe#Z@dSdT ZAdUdV ZBe=e?_#e@e?_Ce>e?_DeBe?_EdWe?_FdXZGdYZHdZId[d\ eHJ D ZKdZId]d\ eHJ D ZLd^d_d`dadbdcdddedfdgdhdidjdkdldmdndohZMdpdq ZNdrd dZIeGeHeKeLgJ D ZOeOeMB ZPdsdtdudvdwdxdydzhZQd{d| d}d| d~d| dZReSeSeSeSddddddd	ddZTdd ZUdd ZVdd ZWeUeVeWdZXdd ZYG dd de#ZZG dd deZe4Z[G dd deZe7Z\G dd de#Z]G dd de#Z^e^ Z_dd Z`G dd deaZbebddZcdddZddd Zedd ZfG dd de#Zgehedehe_jifZjehe_jijkfZldamdd ZndddZoG dd de7Zpdd ZqdS ))Mock	MagicMockpatchsentinelDEFAULTANYcallcreate_autospec
FILTER_DIRNonCallableMockNonCallableMagicMock	mock_openPropertyMocksealz1.0    N)
ModuleType
MethodType)wrapspartialc             C   s   h | ]}| d s|qS )_)
startswith).0name r   /usr/lib/python3.7/mock.py	<setcomp>%   s    r   javaTc             C   s   t t| tS )N)
issubclasstyper
   )objr   r   r   _is_instance_mock4   s    r   c             C   s   t | tpt | tot| tS )N)
isinstanceBaseExceptionsr   r   )r   r   r   r   _is_exception:   s    
r"   c             C   s   t | tr4|s4y
| j} W n tk
r,   d S X d}n*t | ts^y
| j} W n tk
r\   d S X |rnt| d }n| }y| t|fS  t	k
r   d S X d S )NT)
r    r   __init__AttributeErrorFunctionTypes__call__r   inspectZ	signature
ValueError)funcZas_instanceZeat_selfZsig_funcr   r   r   _get_signature_objectA   s$    


r*   Fc                sN   t | ||  d krd S  \}   fdd}t| | |t|_ t|_d S )Nc                s    j || d S )N)bind)
_mock_selfargskwargs)sigr   r   checksigf   s    z"_check_signature.<locals>.checksig)r*   _copy_func_detailsr   _mock_check_sig__signature__)r)   mock	skipfirstinstancer0   r   )r/   r   _check_signaturea   s    

r7   c          	   C   s>   x8dD ]0}yt ||t| | W q tk
r4   Y qX qW d S )N)__name____doc____text_signature__
__module____defaults____kwdefaults__)setattrgetattrr$   )r)   funcopy	attributer   r   r   r1   m   s    r1   c             C   s@   t | trdS t | tttfr(t| jS t| dd d k	r<dS dS )NTr&   F)r    r   staticmethodclassmethodr   	_callable__func__r?   )r   r   r   r   rD   z   s    

rD   c             C   s   t | ttfkS )N)r   listtuple)r   r   r   r   _is_list   s    rH   c             C   sH   t | tst| dd d k	S x(| f| j D ]}|jdd k	r(dS q(W dS )Nr&   TF)r    r   r?   __mro____dict__get)r   baser   r   r   _instance_callable   s    
rM   c                s   t |sd S t|t}t|||}|d kr.| S |\}  fdd}t|| |j}| s^d}|| d}d| }	t|	| || }
t|
|   |
S )Nc                 s    j | | d S )N)r+   )r-   r.   )r/   r   r   r0      s    z _set_signature.<locals>.checksigr@   )Z
_checksig_r4   zYdef %s(*args, **kwargs):
    _checksig_(*args, **kwargs)
    return mock(*args, **kwargs))	rD   r    r   r*   r1   r8   isidentifierexec_setup_func)r4   originalr6   r5   resultr)   r0   r   contextsrcr@   r   )r/   r   _set_signature   s$    



rU   c                s    _ tsd S fdd}fdd}fdd}fdd}fd	d
}fdd}fdd}	 fdd}
d _d _d  _t  _t  _t  _j	 _	j
 _
j _| _| _| _|	 _|
 _| _| _| _| _ _d S )Nc                 s    j | |S )N)assert_called_with)r-   r.   )r4   r   r   rV      s    z'_setup_func.<locals>.assert_called_withc                 s    j | |S )N)assert_called)r-   r.   )r4   r   r   rW      s    z"_setup_func.<locals>.assert_calledc                 s    j | |S )N)assert_not_called)r-   r.   )r4   r   r   rX      s    z&_setup_func.<locals>.assert_not_calledc                 s    j | |S )N)assert_called_once)r-   r.   )r4   r   r   rY      s    z'_setup_func.<locals>.assert_called_oncec                 s    j | |S )N)assert_called_once_with)r-   r.   )r4   r   r   rZ      s    z,_setup_func.<locals>.assert_called_once_withc                 s    j | |S )N)assert_has_calls)r-   r.   )r4   r   r   r[      s    z%_setup_func.<locals>.assert_has_callsc                 s    j | |S )N)assert_any_call)r-   r.   )r4   r   r   r\      s    z$_setup_func.<locals>.assert_any_callc                 s:   t   _t   _   j} t| r6| k	r6|   d S )N)	_CallListmethod_calls
mock_calls
reset_mockreturn_valuer   )ret)r@   r4   r   r   r`      s    z_setup_func.<locals>.reset_mockFr   )r4   r   called
call_count	call_argsr]   call_args_listr^   r_   ra   side_effect_mock_childrenrV   rZ   r[   r\   r`   rW   rX   rY   r3   _mock_delegate)r@   r4   r/   rV   rW   rX   rY   rZ   r[   r\   r`   r   )r@   r4   r   rP      s<    rP   c             C   s   d| dd  | kS )Nz__%s__   r   )r   r   r   r   	_is_magic   s    rl   c               @   s$   e Zd Zdd Zdd Zdd ZdS )_SentinelObjectc             C   s
   || _ d S )N)r   )selfr   r   r   r   r#      s    z_SentinelObject.__init__c             C   s
   d| j  S )Nzsentinel.%s)r   )rn   r   r   r   __repr__   s    z_SentinelObject.__repr__c             C   s
   d| j  S )Nzsentinel.%s)r   )rn   r   r   r   
__reduce__   s    z_SentinelObject.__reduce__N)r8   r;   __qualname__r#   ro   rp   r   r   r   r   rm      s   rm   c               @   s$   e Zd Zdd Zdd Zdd ZdS )	_Sentinelc             C   s
   i | _ d S )N)
_sentinels)rn   r   r   r   r#      s    z_Sentinel.__init__c             C   s   |dkrt | j|t|S )N	__bases__)r$   rs   
setdefaultrm   )rn   r   r   r   r   __getattr__   s    z_Sentinel.__getattr__c             C   s   dS )Nr   r   )rn   r   r   r   rp     s    z_Sentinel.__reduce__N)r8   r;   rq   r#   rv   rp   r   r   r   r   rr      s   rr   c             C   s$   t | ttttfkr t | | S | S )N)r   dictrF   rG   set)valuer   r   r   _copy  s    rz   ra   _mock_return_valuerg   _mock_side_effect_mock_parent_mock_new_parent
_mock_name_mock_new_namec             C   s8   t |  d|  }| |fdd}| |fdd}t||S )NZ_mock_c             S   s"   | j }|d krt| |S t||S )N)ri   r?   )rn   r   	_the_namer/   r   r   r   _get!  s    
z"_delegating_property.<locals>._getc             S   s*   | j }|d kr|| j|< nt||| d S )N)ri   rJ   r>   )rn   ry   r   r   r/   r   r   r   _set&  s    z"_delegating_property.<locals>._set)_allowed_namesaddproperty)r   r   r   r   r   r   r   _delegating_property  s
    
r   c               @   s   e Zd Zdd Zdd ZdS )r]   c             C   sn   t |tst| |S t|}t| }||kr2dS x6td|| d D ] }| |||  }||krFdS qFW dS )NFr      T)r    rF   __contains__lenrange)rn   ry   Z	len_valueZlen_selfiZsub_listr   r   r   r   3  s    
z_CallList.__contains__c             C   s   t t| S )N)pprintZpformatrF   )rn   r   r   r   ro   A  s    z_CallList.__repr__N)r8   r;   rq   r   ro   r   r   r   r   r]   1  s   r]   c             C   s   t |tr*y
|j}W n tk
r(   Y nX t|s6dS |jsV|jsV|jd k	sV|jd k	rZdS | }x|d k	r|||krtdS |j}q`W |r| |_||_|r| |_||_dS )NFT)	r    r%   r4   r$   r   r   r   r}   r~   )parentry   r   new_name_parentr   r   r   _check_and_set_parentE  s.    





r   c               @   s$   e Zd Zdd Zdd Zdd ZdS )	_MockIterc             C   s   t || _d S )N)iterr   )rn   r   r   r   r   r#   g  s    z_MockIter.__init__c             C   s   | S )Nr   )rn   r   r   r   __iter__i  s    z_MockIter.__iter__c             C   s
   t | jS )N)nextr   )rn   r   r   r   __next__k  s    z_MockIter.__next__N)r8   r;   rq   r#   r   r   r   r   r   r   r   f  s   r   c               @   s   e Zd ZeZdZdd ZdS )BaseNc             O   s   d S )Nr   )rn   r-   r.   r   r   r   r#   q  s    zBase.__init__)r8   r;   rq   r   r{   r|   r#   r   r   r   r   r   n  s   r   c               @   sL  e Zd Zdd ZdEddZdd	 ZdFd
dZdGddZdd Zdd Z	dZ
eee	e
Zedd ZedZedZedZedZedZdd Zdd ZeeeZdHddddd Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Z d3d4 Z!d5d6 Z"d7d8 Z#d9d: Z$d;d< Z%d=d> Z&dId?d@Z'dAdB Z(dCdD Z)dS )Jr
   c             O   s$   t | j| fd| ji}t|}|S )Nr9   )r   r8   r9   object__new__)clsr-   kwnewr6   r   r   r   r   y  s    
zNonCallableMock.__new__N Fc             K   s   |d kr|}| j }||d< ||d< ||d< ||d< d|d< |d k	rJ|}d}|
d krZ|d k	}
| |||	|
 i |d< ||d	< d |d
< d|d< d |d< d|d< t |d< t |d< t |d< ||d< |r| jf | tt| |||||| d S )Nr}   r   r   r~   F_mock_sealedTrh   _mock_wrapsri   Z_mock_calledZ_mock_call_argsr   Z_mock_call_countZ_mock_call_args_listZ_mock_mock_callsr^   _mock_unsafe)rJ   _mock_add_specr]   configure_mock_safe_superr
   r#   )rn   specr   r   spec_setr   _spec_state	_new_name_new_parent_spec_as_instance	_eat_selfZunsafer.   rJ   r   r   r   r#     s:    




zNonCallableMock.__init__c             C   s(   d |_ d |_d|_d |_t| || d S )Nr   )r}   r~   r   r   r>   )rn   r4   rA   r   r   r   attach_mock  s
    zNonCallableMock.attach_mockc             C   s   |  || d S )N)r   )rn   r   r   r   r   r   mock_add_spec  s    zNonCallableMock.mock_add_specc       	      C   sz   d }d }|d k	rPt |sPt|tr(|}nt|}t|||}|oF|d }t|}| j}||d< ||d< ||d< ||d< d S )Nr   _spec_class	_spec_set_spec_signature_mock_methods)rH   r    r   
_get_classr*   dirrJ   )	rn   r   r   r   r   r   r   ZresrJ   r   r   r   r     s    
zNonCallableMock._mock_add_specc             C   s8   | j }| jd k	r| jj}|tkr4| j| dd}|| _|S )Nz())r   r   )r{   ri   ra   r   _get_child_mock)rn   rb   r   r   r   Z__get_return_value  s    

z"NonCallableMock.__get_return_valuec             C   s,   | j d k	r|| j _n|| _t| |d d d S )Nz())ri   ra   r{   r   )rn   ry   r   r   r   Z__set_return_value  s    

z"NonCallableMock.__set_return_valuez1The value to be returned when the mock is called.c             C   s   | j d krt| S | j S )N)r   r   )rn   r   r   r   	__class__  s    
zNonCallableMock.__class__rc   rd   re   rf   r_   c             C   sN   | j }|d kr| jS |j}|d k	rJt|sJt|tsJt|sJt|}||_|S )N)ri   r|   rg   callabler    r   r"   )rn   	delegatedZsfr   r   r   Z__get_side_effect  s    z!NonCallableMock.__get_side_effectc             C   s(   t |}| j}|d kr|| _n||_d S )N)	_try_iterri   r|   rg   )rn   ry   r   r   r   r   Z__set_side_effect  s
    z!NonCallableMock.__set_side_effect)ra   rg   c            C   s   |d krg }t | |krd S |t |  d| _d | _d| _t | _t | _t | _|r^t	| _
|rhd | _x0| j D ]"}t|tst|tkrqt|| qtW | j
}t|r|| k	r|| d S )NFr   )idappendrc   re   rd   r]   r_   rf   r^   r   r{   r|   rh   valuesr    
_SpecState_deletedr`   r   )rn   Zvisitedra   rg   Zchildrb   r   r   r   r`     s,    zNonCallableMock.reset_mockc             K   s`   xZt | dd dD ]B\}}|d}| }| }x|D ]}t||}q:W t||| qW d S )Nc             S   s   | d  dS )Nr   .)count)entryr   r   r   <lambda>C      z0NonCallableMock.configure_mock.<locals>.<lambda>)keyr   )sorteditemssplitpopr?   r>   )rn   r.   argvalr-   finalr   r   r   r   r   r   6  s    	


zNonCallableMock.configure_mockc             C   s   |dkrt |n:| jd k	r<|| jks.|tkrLt d| nt|rLt || jsd|drdt || j|}|tkrt |np|d krd }| j	d k	rt
| j	|}| j| |||| d}|| j|< n.t|trt|j|j|j|j|j}|| j|< |S )N>   r   r   zMock object has no attribute %r)assertZassret)r   r   r   r   r   )r$   r   _all_magicsrl   r   r   rh   rK   r   r   r?   r   r    r   r   r   r   r6   r   r   )rn   r   rR   r   r   r   r   rv   L  s6    






zNonCallableMock.__getattr__c             C   s   | j g}| j}| }d}|dgkr$d}t }xT|d k	r~|}||j |  d}|j dkrZd}|j}t||krnP |t| q,W tt|}|jpd}t	|dkr|d dkr|d7 }||d< d
|S )Nr   z()r   r4   r   )z()z().r   )r   r~   rx   r   r   r   rF   reversedr   r   join)rn   Z
_name_listr   ZlastdotseenZ_firstr   r   r   _extract_mock_namer  s0    



z"NonCallableMock._extract_mock_namec             C   s^   |   }d}|dkrd| }d}| jd k	rDd}| jr8d}|| jj }dt| j||t| f S )Nr   )r4   zmock.z name=%rz spec=%rz spec_set=%rz<%s%s%s id='%s'>)r   r   r   r8   r   r   )rn   r   Zname_stringZspec_stringr   r   r   ro     s    
zNonCallableMock.__repr__c             C   sv   t st| S | jpg }tt| }t| j}dd | j	 D }dd |D }dd |D }t
t|| | | S )Nc             S   s   g | ]\}}|t k	r|qS r   )r   )r   Zm_nameZm_valuer   r   r   
<listcomp>  s    z+NonCallableMock.__dir__.<locals>.<listcomp>c             S   s   g | ]}| d s|qS )r   )r   )r   er   r   r   r     s    c             S   s"   g | ]}| d rt|r|qS )r   )r   rl   )r   r   r   r   r   r     s    )r	   r   __dir__r   r   r   rF   rJ   rh   r   r   rx   )rn   extrasZ	from_typeZ	from_dictZfrom_child_mocksr   r   r   r     s    


zNonCallableMock.__dir__c                sT  |t krt||S jrHjd k	rH|jkrH|jkrHtd| n|tkrbd| }t|n|tkrjd k	r|jkrtd| t	|st
t|t|| |  fdd}n(t|d | t
t|| |j|< n,|dkr|_d S t|||r|j|< jrFt|sF  d| }td| t||S )Nz!Mock object has no attribute '%s'z.Attempting to set unsupported magic method %r.c                 s    f| |S )Nr   )r-   r   )rQ   rn   r   r   r     r   z-NonCallableMock.__setattr__.<locals>.<lambda>r   r   zCannot set )r   r   __setattr__r   r   rJ   r$   _unsupported_magicsr   r   r>   r   _get_methodr   rh   r   r   hasattrr   )rn   r   ry   msg	mock_namer   )rQ   rn   r   r     s8    



zNonCallableMock.__setattr__c             C   s   |t kr2|t| jkr2tt| | || jkr2d S | j|t}|| jkr\tt| 	| n|t
krlt||tk	r|| j|= t
| j|< d S )N)r   r   rJ   delattrrh   rK   _missingr   r
   __delattr__r   r$   )rn   r   r   r   r   r   r     s    

zNonCallableMock.__delattr__c             C   s   | j pd}t|||S )Nr4   )r   _format_call_signature)rn   r-   r.   r   r   r   r   _format_mock_call_signature  s    
z+NonCallableMock._format_mock_call_signaturec             C   sD   d}|  ||}| j}t|dkr.|dd  }| j | }|||f S )Nz!Expected call: %s
Actual call: %s   r   )r   re   r   )rn   r-   r.   messageexpected_stringre   Zactual_stringr   r   r   _format_mock_failure_message  s    
z,NonCallableMock._format_mock_failure_messagec          
   C   sv   | j }|d k	rnt|dkr(d}|\}}n
|\}}}y||j||fS  tk
rj } z
|d S d }~X Y qrX n|S d S )Nrj   r   )r   r   r+   	TypeErrorwith_traceback)rn   _callr/   r   r-   r.   r   r   r   r   _call_matcher  s    

zNonCallableMock._call_matcherc             C   s.   | }|j dkr*d|jpd|j f }t|d S )Nr   z7Expected '%s' to not have been called. Called %s times.r4   )rd   r   AssertionError)r,   rn   r   r   r   r   rX     s
    
z!NonCallableMock.assert_not_calledc             C   s(   | }|j dkr$d|j pd}t|d S )Nr   z"Expected '%s' to have been called.r4   )rd   r   r   )r,   rn   r   r   r   r   rW     s
    
zNonCallableMock.assert_calledc             C   s.   | }|j dks*d|jpd|j f }t|d S )Nr   z8Expected '%s' to have been called once. Called %s times.r4   )rd   r   r   )r,   rn   r   r   r   r   rY   (  s
    
z"NonCallableMock.assert_called_oncec                s|   | j d kr( }td|f  fdd} f}j }||krxt|trh|nd }t| |d S )NzExpected call: %s
Not calledc                 s     } | S )N)r   )r   )r-   r.   rn   r   r   _error_message;  s    z:NonCallableMock.assert_called_with.<locals>._error_message)re   r   r   r   r    	Exception)r,   r-   r.   expectedr   actualcauser   )r-   r.   rn   r   rV   1  s    
z"NonCallableMock.assert_called_withc             O   s6   | }|j dks*d|jpd|j f }t||j||S )Nr   z1Expected '%s' to be called once. Called %s times.r4   )rd   r   r   rV   )r,   r-   r.   rn   r   r   r   r   rZ   E  s    
z'NonCallableMock.assert_called_once_withc          	      s    fdd|D }t |tr |nd }t fdd jD }|sd||kr`tdt| jf |d S t|}g }x:|D ]2}y|| W qv tk
r   || Y qvX qvW |rtdt	|f |d S )Nc                s   g | ]}  |qS r   )r   )r   c)rn   r   r   r   Z  s    z4NonCallableMock.assert_has_calls.<locals>.<listcomp>c             3   s   | ]}  |V  qd S )N)r   )r   r   )rn   r   r   	<genexpr>\  s    z3NonCallableMock.assert_has_calls.<locals>.<genexpr>z(Calls not found.
Expected: %r
Actual: %rz%r not all found in call list)
r    r   r]   r_   r   rF   remover(   r   rG   )rn   ZcallsZ	any_orderr   r   Z	all_callsZ	not_foundZkallr   )rn   r   r[   P  s*    

z NonCallableMock.assert_has_callsc                sZ     ||f} fdd jD }||krVt|tr8|nd } ||}td| |d S )Nc                s   g | ]}  |qS r   )r   )r   r   )rn   r   r   r   z  s    z3NonCallableMock.assert_any_call.<locals>.<listcomp>z%s call not found)r   rf   r    r   r   r   )rn   r-   r.   r   r   r   r   r   )rn   r   r\   s  s    zNonCallableMock.assert_any_callc             K   sx   t | }t|ts2t|tr"t}q<t|tr<t}n
|jd }| jrnd|krVd|d  nd}| 	 | }t
||f |S )Nr   r   r   z())r   r   CallableMixinr   r   r
   r   rI   r   r   r$   )rn   r   _typeklassrA   r   r   r   r   r     s    



zNonCallableMock._get_child_mock)NNNNNNr   NFNF)F)FF)N)F)*r8   r;   rq   r   r#   r   r   r   Z"_NonCallableMock__get_return_valueZ"_NonCallableMock__set_return_valueZ"_NonCallableMock__return_value_docr   ra   r   r   rc   rd   re   rf   r_   Z!_NonCallableMock__get_side_effectZ!_NonCallableMock__set_side_effectrg   r`   r   rv   r   ro   r   r   r   r   r   r   rX   rW   rY   rV   rZ   r[   r\   r   r   r   r   r   r
   v  sR   
  
*
	 

& '
			
#r
   c             C   sF   | d kr| S t | r| S t| r$| S yt| S  tk
r@   | S X d S )N)r"   rD   r   r   )r   r   r   r   r     s    r   c            
   @   sB   e Zd Zddedddddddf
ddZdd Zdd Zd	d
 ZdS )r   Nr   c          	   K   s6   || j d< tt| j|||||||	|
f| || _d S )Nr{   )rJ   r   r   r#   rg   )rn   r   rg   ra   r   r   r   r   r   r   r   r.   r   r   r   r#     s
    


zCallableMixin.__init__c             O   s   d S )Nr   )rn   r-   r.   r   r   r   r2     s    zCallableMixin._mock_check_sigc             O   s   | j || | j||S )N)r2   
_mock_call)r,   r-   r.   r   r   r   r&     s    zCallableMixin.__call__c             O   s  | }d|_ | jd7  _t||fdd}||_|j| t }|jd k	}|j}|j	}|dk}	|j
td||f |j}
x|
d k	r*|r|
jt|||f |
jd k	}|r|
jd | }t|||f}|
j
| |
j	r|	rd}nd}|
j	dk}	|
j	| | }|
j}
t|
}||krP || q|W |j}|d k	rt|rL|n,t|snt|}t|rx|n
|||}|tk	r|S |jtk	r|jS |jd k	r|j||S |jS )NTr   )twoz()r   r   )rc   rd   _Callre   rf   r   rx   r}   r   r   r_   r~   r^   r   r   rg   r"   rD   r   r   r{   ra   r   )r,   r-   r.   rn   r   r   Zdo_method_callsZmethod_call_nameZmock_call_nameZ	is_a_callr   Zthis_mock_callr   Z_new_parent_idZeffectrR   r   r   r   r     s`    









zCallableMixin._mock_call)r8   r;   rq   r   r#   r2   r&   r   r   r   r   r   r     s   r   c               @   s   e Zd ZdS )r   N)r8   r;   rq   r   r   r   r   r     s   1r   c             C   s2   y
t | |S  tk
r,   t| t | |S X d S )N)r?   r$   
__import__)thingcompimport_pathr   r   r   _dot_lookupK  s
    
r   c             C   sF   |  d}|d}t|}x$|D ]}|d| 7 }t|||}q"W |S )Nr   r   z.%s)r   r   r   r   )targetZ
componentsr   r   r   r   r   r   	_importerS  s    


r   c             C   s
   t | dS )Nis_local)r   )patcherr   r   r   _is_started^  s    r   c               @   sd   e Zd ZdZg Zdd Zdd Zdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd ZdS )_patchNc
       
      C   sn   |d k	r(|t k	rtd|d k	r(td|| _|| _|| _|| _|| _|| _d| _|| _	|| _
|	| _g | _d S )Nz,Cannot use 'new' and 'new_callable' togetherz1Cannot use 'autospec' and 'new_callable' togetherF)r   r(   getterrA   r   new_callabler   createZ	has_localr   autospecr.   additional_patchers)
rn   r  rA   r   r   r  r   r  r  r.   r   r   r   r#   h  s$    z_patch.__init__c          
   C   sH   t | j| j| j| j| j| j| j| j| j		}| j
|_
dd | jD |_|S )Nc             S   s   g | ]}|  qS r   )copy)r   pr   r   r   r     s    z_patch.copy.<locals>.<listcomp>)r  r  rA   r   r   r  r   r  r  r.   attribute_namer  )rn   r   r   r   r   r    s    z_patch.copyc             C   s   t |tr| |S | |S )N)r    r   decorate_classdecorate_callable)rn   r)   r   r   r   r&     s    

z_patch.__call__c             C   sR   xLt |D ]@}|tjsq
t||}t|ds2q
|  }t|||| q
W |S )Nr&   )r   r   r   TEST_PREFIXr?   r   r  r>   )rn   r   attr
attr_valuer   r   r   r   r
    s    

z_patch.decorate_classc                s<   t  dr j|   S t  fdd| g_S )N	patchingsc           	      s   g }g }t  }zybxJjD ]@}| }|| |jd k	rF|| q|jtkr|| qW | t |7 }  | |S    ||krt|r|| t	
 } Y nX W d xt|D ]}|j|  qW X d S )N)rG   r  	__enter__r   r	  updater   r   r   sysexc_infor   __exit__)r-   Zkeywargs
extra_argsZentered_patchersr  patchingr   )r)   patchedr   r   r    s*    




z)_patch.decorate_callable.<locals>.patched)r   r  r   r   )rn   r)   r   )r)   r  r   r    s    
z_patch.decorate_callablec          	   C   s   |   }| j}t}d}y|j| }W n$ ttfk
rH   t||t}Y nX d}|tkrft|t	rfd| _
| j
s|tkrtd||f ||fS )NFTz!%s does not have the attribute %r)r  rA   r   rJ   r$   KeyErrorr?   	_builtinsr    r   r  )rn   r   r   rQ   localr   r   r   get_original  s    z_patch.get_originalc             C   s<  | j | j| j  }}}| j| j }}| j}|  | _|dkr@d }|dkrLd }|dkrXd }|d k	rp|d k	rptd|d k	s|d k	r|dkrtd| 	 \}}|t
krR|d krRd}	|dkr|}|dkr|}d }n&|d k	r|dkr|}d }n|dkr|}|d k	s|d k	r.|t
krtdt|tr.d}	t}
i }|d k	rF|}
nN|d k	sZ|d k	r|}|d k	rl|}t|rd|k}n
t| }|rt}
|d k	r||d< |d k	r||d	< t|
trt|
tr| jr| j|d
< || |
f |}|	rt|r|}|d k	r|}t|s0t|s0t}
|d
 |
f |dd||_nl|d k	r|t
k	rntd|t
krtdt|}|dkr|}t|f|| jd|}n|rtd|}|| _|| _t| j| j| | jd k	r8i }| j t
kr||| j< x.| jD ]$}|  }|j t
kr|| qW |S |S )NFzCan't specify spec and autospec)TNz6Can't provide explicit spec_set *and* spec or autospecTz!Can't use 'spec' with create=Truer&   r   r   r   z())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  r.   r  r  r   r   r  r   r    r   r   rH   r   r   r   r
   rA   r  r   rM   r   ra   boolr   temp_originalr   r>   r	  r  r  )rn   r   r   r   r  r.   r  rQ   r  ZinheritKlass_kwargsZ	this_specZnot_callableZnew_attrr  r  r   r   r   r   r    s    




















z_patch.__enter__c             G   s   t | std| jr4| jtk	r4t| j| j| j n>t| j| j | j	srt
| j| jr`| jdkrrt| j| j| j | `| `| `x$t| jD ]}t |r|j|  qW d S )Nz stop called on unstarted patcher)r9   r;   r<   __annotations__r=   )r   RuntimeErrorr   r  r   r>   r   rA   r   r  r   r   r  r  )rn   r  r   r   r   r   r  b  s    
z_patch.__exit__c             C   s   |   }| j|  |S )N)r  _active_patchesr   )rn   rR   r   r   r   startz  s    z_patch.startc             C   s.   y| j |  W n tk
r$   Y nX |  S )N)r#  r   r(   r  )rn   r   r   r   stop  s
    z_patch.stop)r8   r;   rq   r	  r#  r#   r  r&   r
  r  r  r  r  r$  r%  r   r   r   r   r  c  s   (~r  c          	      sP   y  dd\ }W n& ttfk
r:   td f Y nX  fdd}||fS )Nr   r   z.Need a valid target to patch. You supplied: %rc                  s   t  S )N)r   r   )r   r   r   r     r   z_get_target.<locals>.<lambda>)rsplitr   r(   )r   rA   r  r   )r   r   _get_target  s    r'  c       
   
      s$    fdd}	t |	||||||||	S )Nc                  s    S )Nr   r   )r   r   r   r     r   z_patch_object.<locals>.<lambda>)r  )
r   rA   r   r   r  r   r  r  r.   r  r   )r   r   _patch_object  s    
r(  c                s   t  tkr fdd}n fdd}|s2tdt| }|d \}	}
t||	|
|||||i 	}|	|_xB|dd  D ]2\}	}
t||	|
|||||i 	}|	|_|j| qvW |S )Nc                  s   t  S )N)r   r   )r   r   r   r     r   z!_patch_multiple.<locals>.<lambda>c                  s    S )Nr   r   )r   r   r   r     r   z=Must supply at least one keyword argument with patch.multipler   r   )	r   strr(   rF   r   r  r	  r  r   )r   r   r  r   r  r  r.   r  r   rA   r   r   Zthis_patcherr   )r   r   _patch_multiple  s&    

r*  c       
   
   K   s$   t | \}}	t||	|||||||	S )N)r'  r  )
r   r   r   r  r   r  r  r.   r  rA   r   r   r   r     s    E
r   c               @   sN   e Zd ZdddZdd Zdd Zd	d
 Zdd Zdd Zdd Z	eZ
e	ZdS )_patch_dictr   Fc             K   s,   || _ t|| _| j| || _d | _d S )N)in_dictrw   r   r  clear	_original)rn   r,  r   r-  r.   r   r   r   r#   J  s
    
z_patch_dict.__init__c                s.   t  tr S t  fdd}|S )Nc                 s"      z
 | |S   X d S )N)r+  _unpatch_dict)r-   r   )frn   r   r   _innerV  s    
z$_patch_dict.__call__.<locals>._inner)r    r   r
  r   )rn   r0  r1  r   )r0  rn   r   r&   S  s    

z_patch_dict.__call__c             C   s\   xVt |D ]J}t||}|tjr
t|dr
t| j| j| j	}||}t
||| q
W |S )Nr&   )r   r?   r   r   r  r   r+  r,  r   r-  r>   )rn   r   r  r  Z	decoratorZ	decoratedr   r   r   r
  a  s    

z_patch_dict.decorate_classc             C   s   |    d S )N)r+  )rn   r   r   r   r  l  s    z_patch_dict.__enter__c             C   s   | j }t| jtrt| j| _| j}| j}y| }W n2 tk
rh   i }x|D ]}|| ||< qPW Y nX || _|r|t	| y|
| W n. tk
r   x|D ]}|| ||< qW Y nX d S )N)r   r    r,  r)  r   r-  r  r$   r.  _clear_dictr  )rn   r   r,  r-  rQ   r   r   r   r   r+  q  s&    

z_patch_dict._patch_dictc             C   sV   | j }| j}t| y|| W n. tk
rP   x|D ]}|| ||< q8W Y nX d S )N)r,  r.  r2  r  r$   )rn   r,  rQ   r   r   r   r   r/    s    
z_patch_dict._unpatch_dictc             G   s   |    dS )NF)r/  )rn   r-   r   r   r   r    s    z_patch_dict.__exit__N)r   F)r8   r;   rq   r#   r&   r
  r  r+  r/  r  r$  r%  r   r   r   r   r+  -  s   
	r+  c             C   sB   y|    W n0 tk
r<   t| }x|D ]
}| |= q*W Y nX d S )N)r-  r$   rF   )r,  keysr   r   r   r   r2    s    
r2  c              C   s    xt tjD ]} |   qW d S )N)r   r  r#  r%  )r   r   r   r   _patch_stopall  s    r4  Ztestzlt 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 c             c   s   | ]}d | V  qdS )zi%sNr   )r   nr   r   r   r     s    r   c             c   s   | ]}d | V  qdS )zr%sNr   )r   r6  r   r   r   r     s    __get____set__
__delete____reversed____missing__rp   __reduce_ex__Z__getinitargs____getnewargs____getstate____setstate____getformat__Z__setformat__ro   r   __subclasses__
__format____getnewargs_ex__c                s    fdd}| |_ |S )Nc                s    | f||S )Nr   )rn   r-   r   )r)   r   r   method  s    z_get_method.<locals>.method)r8   )r   r)   rD  r   )r)   r   r     s    r   c             C   s   h | ]}d | qS )z__%s__r   )r   rD  r   r   r   r     s   rv   r   r#   r   __prepare____instancecheck____subclasscheck____del__c             C   s
   t | S )N)r   __hash__)rn   r   r   r   r     r   r   c             C   s
   t | S )N)r   __str__)rn   r   r   r   r     r   c             C   s
   t | S )N)r   
__sizeof__)rn   r   r   r   r     r   )rI  rJ  rK  r   y              ?g      ?)__lt____gt____le____ge____int__r   __len__r  __complex__	__float____bool__	__index__c                s    fdd}|S )Nc                s$    j j}|tk	r|S  | kr dS tS )NT)__eq__r{   r   NotImplemented)otherret_val)rn   r   r   rV    s    z_get_eq.<locals>.__eq__r   )rn   rV  r   )rn   r   _get_eq  s    rZ  c                s    fdd}|S )Nc                s     j jtk	rtS  | krdS tS )NF)__ne__r{   r   rW  )rX  )rn   r   r   r[    s
    z_get_ne.<locals>.__ne__r   )rn   r[  r   )rn   r   _get_ne  s    r\  c                s    fdd}|S )Nc                 s     j j} | tkrtg S t| S )N)r   r{   r   r   )rY  )rn   r   r   r     s    z_get_iter.<locals>.__iter__r   )rn   r   r   )rn   r   	_get_iter  s    r]  )rV  r[  r   c             C   s   t |t}|tk	r||_d S t|}|d k	rdy|| }W n tk
rX   t|}Y nX ||_d S t|}|d k	r|| |_d S )N)_return_valuesrK   r   ra   _calculate_return_valuer$   _side_effect_methodsrg   )r4   rD  r   ZfixedZreturn_calulatorra   Zside_effectorr   r   r   _set_return_value*  s    

ra  c               @   s   e Zd Zdd Zdd ZdS )
MagicMixinc             O   s&   |    tt| j|| |    d S )N)_mock_set_magicsr   rb  r#   )rn   r-   r   r   r   r   r#   B  s    zMagicMixin.__init__c             C   s   t }t| dd d k	rTt | j}t }t | }x$|D ]}|t| jkr4t| | q4W |tt| j }t| }x|D ]}t||t	||  qtW d S )Nr   )
_magicsr?   intersectionr   rx   r   rJ   r   r>   
MagicProxy)rn   Zthese_magicsZremove_magicsr   r   r   r   r   rc  H  s    

zMagicMixin._mock_set_magicsN)r8   r;   rq   r#   rc  r   r   r   r   rb  A  s   rb  c               @   s   e Zd ZdddZdS )r   Fc             C   s   |  || |   d S )N)r   rc  )rn   r   r   r   r   r   r   a  s    z"NonCallableMagicMock.mock_add_specN)F)r8   r;   rq   r   r   r   r   r   r   _  s   r   c               @   s   e Zd ZdddZdS )r   Fc             C   s   |  || |   d S )N)r   rc  )rn   r   r   r   r   r   r   w  s    zMagicMock.mock_add_specN)F)r8   r;   rq   r   r   r   r   r   r   l  s   r   c               @   s.   e Zd Zdd Zdd Zdd Zd
dd	ZdS )rf  c             C   s   || _ || _d S )N)r   r   )rn   r   r   r   r   r   r#     s    zMagicProxy.__init__c             O   s   |   }|||S )N)create_mock)rn   r-   r.   mr   r   r   r&     s    zMagicProxy.__call__c             C   s8   | j }| j}|j|||d}t||| t||| |S )N)r   r   r   )r   r   r   r>   ra  )rn   r   r   rh  r   r   r   rg    s    zMagicProxy.create_mockNc             C   s   |   S )N)rg  )rn   r   r   r   r   r   r7    s    zMagicProxy.__get__)N)r8   r;   rq   r#   r&   rg  r7  r   r   r   r   rf    s   	rf  c               @   s$   e Zd Zdd Zdd Zdd ZdS )_ANYc             C   s   dS )NTr   )rn   rX  r   r   r   rV    s    z_ANY.__eq__c             C   s   dS )NFr   )rn   rX  r   r   r   r[    s    z_ANY.__ne__c             C   s   dS )Nz<ANY>r   )rn   r   r   r   ro     s    z_ANY.__repr__N)r8   r;   rq   rV  r[  ro   r   r   r   r   ri    s   ri  c             C   sd   d|  }d}d dd |D }d dd t| D }|rD|}|r\|rT|d7 }||7 }|| S )Nz%s(%%s)r   z, c             S   s   g | ]}t |qS r   )repr)r   r   r   r   r   r     s    z*_format_call_signature.<locals>.<listcomp>c             S   s   g | ]\}}d ||f qS )z%s=%rr   )r   r   ry   r   r   r   r     s    )r   r   r   )r   r-   r.   r   Zformatted_argsargs_stringZkwargs_stringr   r   r   r     s    r   c               @   s^   e Zd ZdddZddd	Zd
d ZejZdd Zdd Z	dd Z
dd Zdd Zdd ZdS )r   r   r   NFTc             C   s   d}i }t |}|dkr$|\}}}nr|dkrd|\}	}
t|	trX|	}t|
trR|
}qb|
}q|	|
 }}n2|dkr|\}t|tr|}nt|tr|}n|}|rt| ||fS t| |||fS )Nr   r   rj   r   )r   r    r)  rG   r   )r   ry   r   r   r   	from_kallr-   r.   _lenfirstsecondr   r   r   r     s.    



z_Call.__new__c             C   s   || _ || _|| _d S )N)r   r}   _mock_from_kall)rn   ry   r   r   r   rl  r   r   r   r#     s    z_Call.__init__c             C   sf  |t krdS yt|}W n tk
r,   dS X d}t| dkrH| \}}n
| \}}}t| dd rzt|dd rz| j|jkrzdS d}|dkrdi  }}n|dkr|\}}}n|d	kr|\}	t|	tr|	}i }n"t|	tr|	}di  }}nd}|	}nV|dkr>|\}
}t|
tr2|
}t|tr&|i  }}n
d| }}n
|
| }}ndS |rV||krVdS ||f||fkS )
NTFr   rj   r}   r   r   r   r   )r   r   r   r?   r}   r    rG   r)  )rn   rX  Z	len_otherZ	self_nameZ	self_argsZself_kwargsZ
other_nameZ
other_argsZother_kwargsry   rn  ro  r   r   r   rV    sP    




z_Call.__eq__c             O   s<   | j d krtd||fddS | j d }t| j ||f|| dS )Nr   z())r   )r   r   )r   r   )rn   r-   r.   r   r   r   r   r&   /  s    

z_Call.__call__c             C   s2   | j d krt|ddS d| j |f }t|| ddS )NF)r   rl  z%s.%s)r   r   rl  )r   r   )rn   r  r   r   r   r   rv   7  s    
z_Call.__getattr__c             O   s   |  d||S )Nr   )rv   )rn   r-   r.   r   r   r   r   >  s    z_Call.countc             O   s   |  d||S )Nindex)rv   )rn   r-   r.   r   r   r   rq  A  s    z_Call.indexc             C   s|   | j s&| jpd}|dr"d| }|S t| dkr@d}| \}}n0| \}}}|sTd}n|dshd| }nd| }t|||S )Nr   z()zcall%srj   zcall.%s)rp  r   r   r   r   )rn   r   r-   r.   r   r   r   ro   D  s    





z_Call.__repr__c             C   s8   g }| }x"|d k	r*|j r"|| |j}q
W tt|S )N)rp  r   r}   r]   r   )rn   Zvalsr   r   r   r   	call_listY  s    


z_Call.call_list)r   r   NFT)r   NNFT)r8   r;   rq   r   r#   rV  r   r[  r&   rv   r   rq  ro   rr  r   r   r   r   r     s    
 
7r   )rl  c          	   K   s$  t | rt| } t| t}d| i}|r0d| i}n| d kr<i }|rL|rLd|d< || t}t| rji }n"t| sxt}n|r|rt	| st}|
d|}|}	|d krd}	|f |||	|d|}
t| trt|
| }
nt| |
|| |d k	r|s|
|j|< |r$|s$d|kr$t| |dd	|
d
|
_xt| D ]}t|r@q.yt| |}W n tk
rh   w.Y nX d|i}|rd|i}t|tst|||
||}||
j|< nZ|
}t| tr|
j}t| ||}||d< tf ||||d|}||
j|< t|||d t|tr.t|
|| q.W |
S )Nr   r   Tr   r   r   )r   r   r   r   ra   z())r6   r  r   r   )r   r   r   r   )r5   )rH   r   r    r  r   r'   ZisdatadescriptorrD   r   rM   r   r%   rU   r7   rh   r   ra   r   rl   r?   r$   r   r4   
_must_skipr>   )r   r   r6   r   r  r.   is_typer   r  r   r4   r   rQ   r   r   r5   r   r   r   r   j  st    










r   c             C   s|   t | ts$|t| di krdS | j} xR| jD ]H}|j|t}|tkrHq,t |tt	frZdS t t|dd t
rp|S dS q,W |S )NrJ   Fr7  )r    r   r?   r   rI   rJ   rK   r   rB   rC   MethodWrapperTypes)r   r   rt  r   rR   r   r   r   rs    s    
rs  c             C   s$   y| j S  tk
r   t| S X d S )N)r   r$   r   )r   r   r   r   r     s    r   c               @   s   e Zd ZdddZdS )r   FNc             C   s(   || _ || _|| _|| _|| _|| _d S )N)r   idsr   r   r6   r   )rn   r   r   r   r   rv  r6   r   r   r   r#   		  s    z_SpecState.__init__)FNNNF)r8   r;   rq   r#   r   r   r   r   r   	  s    r   c             C   s"   t | trt| S t| S d S )N)r    bytesioBytesIOStringIO)	read_datar   r   r   
_to_stream"	  s    

r|  r   c                s&  t }|d gfdd}fdd} fddfdd fd	d
}td krdd l}ttt|jtt|ja| d krt	dt
d} t	tdj_d j_d j_d j_d j_|j_ d< d j_|j_ j_|j_fdd}|| _| _| S )Nc                 s$   j jd k	rj jS  d j | |S )Nr   )	readlinesra   )r-   r.   )_statehandler   r   _readlines_side_effect8	  s    z)mock_open.<locals>._readlines_side_effectc                 s$   j jd k	rj jS  d j | |S )Nr   )readra   )r-   r.   )r~  r  r   r   _read_side_effect=	  s    z$mock_open.<locals>._read_side_effectc              ?   s(     E d H  xd j | |V  qW d S )Nr   )readline)r-   r.   )_iter_side_effectr~  r   r   _readline_side_effectB	  s    z(mock_open.<locals>._readline_side_effectc              3   s8   j jd k	rxj jV  qW x d D ]
} | V  q&W d S )Nr   )r  ra   )line)r~  r  r   r   r  G	  s
    z$mock_open.<locals>._iter_side_effectc                  s    j jd k	rj jS t d S )Nr   )r  ra   r   r   )r~  r  r   r   _next_side_effectN	  s    z$mock_open.<locals>._next_side_effectr   open)r   r   )r   r   c                 s6   t d< jjd kr2  d< d j_tS )Nr   r   )r|  r  rg   r   )r-   r.   )r  r~  r  r{  r   r   
reset_dataj	  s
    
zmock_open.<locals>.reset_data)r|  	file_spec_iorF   rx   r   TextIOWrapperunionry  r   r  r  ra   writer  r  r}  rg   r   r   )r4   r{  Z
_read_datar  r  r  r  r  r   )r  r  r~  r  r{  r   r   )	  s8    "

r   c               @   s$   e Zd Zdd Zdd Zdd ZdS )r   c             K   s
   t f |S )N)r   )rn   r.   r   r   r   r   	  s    zPropertyMock._get_child_mockc             C   s   |  S )Nr   )rn   r   Zobj_typer   r   r   r7  	  s    zPropertyMock.__get__c             C   s   | | d S )Nr   )rn   r   r   r   r   r   r8  	  s    zPropertyMock.__set__N)r8   r;   rq   r   r7  r8  r   r   r   r   r   w	  s   	r   c          	   C   s`   d| _ xTt| D ]H}yt| |}W n tk
r8   wY nX t|tsFq|j| krt| qW d S )NT)r   r   r?   r$   r    r
   r~   r   )r4   r  rh  r   r   r   r   	  s    


r   )F)F)NFNNN)FFNN)Nr   )r__all____version__rx  r'   r   r  builtinstypesr   r   	functoolsr   r   r   r  BaseExceptionr!   platformr   langZ	Throwabler	   superr   r   r"   r*   r7   r1   rD   rH   rM   rU   rP   rl   r   rm   rr   r   r   MISSINGr   ZDELETEDr   rz   r   r   rF   r]   r   r   r   r
   r   r   r   r   r   r   r  r'  r(  r*  r   r+  r2  r4  rw   ZmultipleZstopallr  Zmagic_methodsZnumericsr   r   ZinplacerightZ_non_defaultsr   rd  r   r   r_  rW  r^  rZ  r\  r]  r`  ra  rb  r   r   rf  ri  r   r   rG   r   r   r   rs  r   r   r   rV  r%   r7  ru  r  r|  r   r   r   r   r   r   r   <module>   s
  
 


5!    +i5  , 
1Jv	

	 ,
 
w
N