B
    u9a`                 @   sZ  d dl Z d dlZd dlZd dlmZ d dlmZ dddddd	d
dddddgZG dd de jZ	G dd	 d	e jZ
G dd deZG dd dZG dd dee	eZG dd dee
Zdd Zdd ZG dd de	ZG dd de	ZG dd deZG dd deZG d d de	ZG d!d" d"ZG d#d dee	ZG d$d
 d
ee
ZG d%d de	ZdS )&    N)deque)wrapsasynccontextmanagercontextmanagerclosingnullcontextAbstractContextManagerAbstractAsyncContextManagerAsyncExitStackContextDecorator	ExitStackredirect_stdoutredirect_stderrsuppressc               @   s.   e Zd Zdd Zejdd Zedd ZdS )r   c             C   s   | S )N )selfr   r    /usr/lib/python3.7/contextlib.py	__enter__   s    z AbstractContextManager.__enter__c             C   s   d S )Nr   )r   exc_type	exc_value	tracebackr   r   r   __exit__   s    zAbstractContextManager.__exit__c             C   s   | t krt|ddS tS )Nr   r   )r   _collections_abc_check_methodsNotImplemented)clsCr   r   r   __subclasshook__   s    z'AbstractContextManager.__subclasshook__N)	__name__
__module____qualname__r   abcabstractmethodr   classmethodr   r   r   r   r   r      s   c               @   s.   e Zd Zdd Zejdd Zedd ZdS )r	   c                s   | S )Nr   )r   r   r   r   
__aenter__&   s    z&AbstractAsyncContextManager.__aenter__c                s   d S )Nr   )r   r   r   r   r   r   r   	__aexit__*   s    z%AbstractAsyncContextManager.__aexit__c             C   s   | t krt|ddS tS )Nr$   r%   )r	   r   r   r   )r   r   r   r   r   r   /   s    z,AbstractAsyncContextManager.__subclasshook__N)	r   r   r    r$   r!   r"   r%   r#   r   r   r   r   r   r	   "   s   c               @   s   e Zd Zdd Zdd ZdS )r   c             C   s   | S )Nr   )r   r   r   r   _recreate_cm:   s    
zContextDecorator._recreate_cmc                s   t   fdd}|S )Nc           	      s       | |S Q R X d S )N)r&   )argskwds)funcr   r   r   innerG   s    
z(ContextDecorator.__call__.<locals>.inner)r   )r   r)   r*   r   )r)   r   r   __call__F   s    zContextDecorator.__call__N)r   r   r    r&   r+   r   r   r   r   r   7   s   c               @   s   e Zd Zdd ZdS )_GeneratorContextManagerBasec             C   sJ   |||| _ |||  | _| _| _t|dd }|d kr@t| j}|| _d S )N__doc__)genr)   r'   r(   getattrtyper-   )r   r)   r'   r(   docr   r   r   __init__Q   s    
z%_GeneratorContextManagerBase.__init__N)r   r   r    r2   r   r   r   r   r,   N   s   r,   c               @   s$   e Zd Zdd Zdd Zdd ZdS )_GeneratorContextManagerc             C   s   |  | j| j| jS )N)	__class__r)   r'   r(   )r   r   r   r   r&   e   s    z%_GeneratorContextManager._recreate_cmc             C   s:   | ` | `| `y
t| jS  tk
r4   tdd Y nX d S )Nzgenerator didn't yield)r'   r(   r)   nextr.   StopIterationRuntimeError)r   r   r   r   r   k   s
    
z"_GeneratorContextManager.__enter__c          
   C   s   |d kr6yt | j W n tk
r*   dS X tdn|d krD| }y| j||| W n tk
r~ } z||k	S d }~X Y nf tk
r } z(||krdS |tkr|j|krdS  W d d }~X Y n"   t d |krdS  Y nX tdd S )NFzgenerator didn't stop   z#generator didn't stop after throw())r5   r.   r6   r7   throw	__cause__sysexc_info)r   r0   valuer   excr   r   r   r   t   s.    
z!_GeneratorContextManager.__exit__N)r   r   r    r&   r   r   r   r   r   r   r3   `   s   	r3   c               @   s   e Zd Zdd Zdd ZdS )_AsyncGeneratorContextManagerc                s4   y| j  I d H S  tk
r.   tdd Y nX d S )Nzgenerator didn't yield)r.   	__anext__StopAsyncIterationr7   )r   r   r   r   r$      s    z(_AsyncGeneratorContextManager.__aenter__c          
      s  |d kr<y| j  I d H  W n tk
r0   d S X tdn|d krJ| }y"| j |||I d H  tdW n tk
r } z||k	S d }~X Y nz tk
r } z.||krdS t|ttfr|j|krdS  W d d }~X Y n0 tk
r
 } z||k	 r W d d }~X Y nX d S )Nzgenerator didn't stopz#generator didn't stop after throw()F)	r.   r@   rA   r7   athrow
isinstancer6   r:   BaseException)r   typr=   r   r>   r   r   r   r%      s.    


z'_AsyncGeneratorContextManager.__aexit__N)r   r   r    r$   r%   r   r   r   r   r?      s   r?   c                s   t   fdd}|S )Nc                 s   t  | |S )N)r3   )r'   r(   )r)   r   r   helper   s    zcontextmanager.<locals>.helper)r   )r)   rF   r   )r)   r   r      s    c                s   t   fdd}|S )Nc                 s   t  | |S )N)r?   )r'   r(   )r)   r   r   rF     s    z#asynccontextmanager.<locals>.helper)r   )r)   rF   r   )r)   r   r      s    c               @   s$   e Zd Zdd Zdd Zdd ZdS )r   c             C   s
   || _ d S )N)thing)r   rG   r   r   r   r2   %  s    zclosing.__init__c             C   s   | j S )N)rG   )r   r   r   r   r   '  s    zclosing.__enter__c             G   s   | j   d S )N)rG   close)r   r<   r   r   r   r   )  s    zclosing.__exit__N)r   r   r    r2   r   r   r   r   r   r   r     s   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )_RedirectStreamNc             C   s   || _ g | _d S )N)_new_target_old_targets)r   
new_targetr   r   r   r2   1  s    z_RedirectStream.__init__c             C   s*   | j tt| j tt| j| j | jS )N)rK   appendr/   r;   _streamsetattrrJ   )r   r   r   r   r   6  s    z_RedirectStream.__enter__c             C   s   t t| j| j  d S )N)rO   r;   rN   rK   pop)r   exctypeexcinstexctbr   r   r   r   ;  s    z_RedirectStream.__exit__)r   r   r    rN   r2   r   r   r   r   r   r   rI   -  s   rI   c               @   s   e Zd ZdZdS )r   stdoutN)r   r   r    rN   r   r   r   r   r   ?  s   c               @   s   e Zd ZdZdS )r   stderrN)r   r   r    rN   r   r   r   r   r   O  s   c               @   s$   e Zd Zdd Zdd Zdd ZdS )r   c             G   s
   || _ d S )N)_exceptions)r   
exceptionsr   r   r   r2   `  s    zsuppress.__init__c             C   s   d S )Nr   )r   r   r   r   r   c  s    zsuppress.__enter__c             C   s   |d k	ot || jS )N)
issubclassrV   )r   rQ   rR   rS   r   r   r   r   f  s    
zsuppress.__exit__N)r   r   r    r2   r   r   r   r   r   r   r   U  s   c               @   s^   e Zd Zedd Zedd Zdd Zdd Zd	d
 Zdd Z	dd Z
dd ZdddZdS )_BaseExitStackc                s    fdd}|S )Nc                s    | ||S )Nr   )r   r>   tb)cmcm_exitr   r   _exit_wrapperx  s    z:_BaseExitStack._create_exit_wrapper.<locals>._exit_wrapperr   )r[   r\   r]   r   )r[   r\   r   _create_exit_wrapperv  s    z#_BaseExitStack._create_exit_wrapperc                 s    ^  fdd}|S )Nc                s     d S )Nr   )r   r>   rZ   )r'   callbackr(   r   r   r]     s    z8_BaseExitStack._create_cb_wrapper.<locals>._exit_wrapperr   )r'   r(   r]   r   )r'   r_   r(   r   _create_cb_wrapper|  s    z!_BaseExitStack._create_cb_wrapperc             C   s   t  | _d S )N)r   _exit_callbacks)r   r   r   r   r2     s    z_BaseExitStack.__init__c             C   s   t |  }| j|_t | _|S )N)r0   ra   r   )r   	new_stackr   r   r   pop_all  s    
z_BaseExitStack.pop_allc             C   sB   t |}y
|j}W n tk
r0   | | Y nX | || |S )N)r0   r   AttributeError_push_exit_callback_push_cm_exit)r   exit_cb_typeexit_methodr   r   r   push  s    	
z_BaseExitStack.pushc             C   s(   t |}|j}||}| || |S )N)r0   r   r   rf   )r   r[   _cm_type_exitresultr   r   r   enter_context  s
    
z_BaseExitStack.enter_contextc              O   s|   t | dkr| ^}}} n>| s&tdn0d|krB|d}| ^}} ntdt | d  |j|f| |}||_|| |S )N   zBdescriptor 'callback' of '_BaseExitStack' object needs an argumentr_   z8callback expected at least 1 positional argument, got %dr8   )len	TypeErrorrP   r`   __wrapped__re   )r'   r(   r   r_   r]   r   r   r   r_     s    



z_BaseExitStack.callbackc             C   s"   |  ||}||_| |d d S )NT)r^   __self__re   )r   r[   r\   r]   r   r   r   rf     s    z_BaseExitStack._push_cm_exitTc             C   s   | j ||f d S )N)ra   rM   )r   r_   is_syncr   r   r   re     s    z"_BaseExitStack._push_exit_callbackN)T)r   r   r    staticmethodr^   r`   r2   rc   rj   rn   r_   rf   re   r   r   r   r   rY   s  s   rY   c               @   s$   e Zd Zdd Zdd Zdd ZdS )r   c             C   s   | S )Nr   )r   r   r   r   r     s    zExitStack.__enter__c       
         s   |d d k	}t  d   fdd}d}d}x`| jr| j \}}y|| rXd}d}d}W q.   t  }||d |d  d}|}Y q.X q.W |ry|d j}	|d W n  tk
r   |	|d _ Y nX |o|S )Nr   r8   c                s8   x,| j }||krd S |d ks$| kr&P |} qW || _ d S )N)__context__)new_excold_excexc_context)	frame_excr   r   _fix_exception_context  s    z2ExitStack.__exit__.<locals>._fix_exception_contextFT)NNN)r;   r<   ra   rP   rv   rD   )
r   exc_detailsreceived_excr{   suppressed_excpending_raisert   cbnew_exc_details	fixed_ctxr   )rz   r   r     s2    

zExitStack.__exit__c             C   s   |  d d d  d S )N)r   )r   r   r   r   rH     s    zExitStack.closeN)r   r   r    r   r   rH   r   r   r   r   r     s   1c               @   s\   e Zd Zedd Ze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 )r
   c                s    fdd}|S )Nc                s    | ||I d H S )Nr   )r   r>   rZ   )r[   r\   r   r   r]   '  s    z@AsyncExitStack._create_async_exit_wrapper.<locals>._exit_wrapperr   )r[   r\   r]   r   )r[   r\   r   _create_async_exit_wrapper%  s    z)AsyncExitStack._create_async_exit_wrapperc                 s    ^  fdd}|S )Nc                s    I d H  d S )Nr   )r   r>   rZ   )r'   r_   r(   r   r   r]   .  s    z>AsyncExitStack._create_async_cb_wrapper.<locals>._exit_wrapperr   )r'   r(   r]   r   )r'   r_   r(   r   _create_async_cb_wrapper+  s    z'AsyncExitStack._create_async_cb_wrapperc                s.   t |}|j}||I d H }| || |S )N)r0   r%   r$   _push_async_cm_exit)r   r[   rk   rl   rm   r   r   r   enter_async_context2  s
    z"AsyncExitStack.enter_async_contextc             C   sD   t |}y
|j}W n  tk
r2   | |d Y nX | || |S )NF)r0   r%   rd   re   r   )r   rg   rh   ri   r   r   r   push_async_exit>  s    
zAsyncExitStack.push_async_exitc              O   s~   t | dkr| ^}}} n>| s&tdn0d|krB|d}| ^}} ntdt | d  |j|f| |}||_||d |S )Nro   zMdescriptor 'push_async_callback' of 'AsyncExitStack' object needs an argumentr_   zCpush_async_callback expected at least 1 positional argument, got %dr8   F)rp   rq   rP   r   rr   re   )r'   r(   r   r_   r]   r   r   r   push_async_callbackP  s    


z"AsyncExitStack.push_async_callbackc                s   |  d d d I d H  d S )N)r%   )r   r   r   r   aclosei  s    zAsyncExitStack.aclosec             C   s"   |  ||}||_| |d d S )NF)r   rs   re   )r   r[   r\   r]   r   r   r   r   m  s    z"AsyncExitStack._push_async_cm_exitc                s   | S )Nr   )r   r   r   r   r$   t  s    zAsyncExitStack.__aenter__c                s   |d d k	}t  d   fdd}d}d}xx| jr| j \}}y0|rR|| }n|| I d H }|rpd}d}d}W q.   t  }	||	d |d  d}|	}Y q.X q.W |ry|d j}
|d W n  tk
r   |
|d _ Y nX |o|S )Nr   r8   c                s8   x,| j }||krd S |d ks$| kr&P |} qW || _ d S )N)rv   )rw   rx   ry   )rz   r   r   r{   }  s    z8AsyncExitStack.__aexit__.<locals>._fix_exception_contextFT)NNN)r;   r<   ra   rP   rv   rD   )r   r|   r}   r{   r~   r   rt   r   cb_suppressr   r   r   )rz   r   r%   w  s8    


zAsyncExitStack.__aexit__N)r   r   r    ru   r   r   r   r   r   r   r   r$   r%   r   r   r   r   r
     s   c               @   s&   e Zd ZdddZdd Zdd ZdS )	r   Nc             C   s
   || _ d S )N)enter_result)r   r   r   r   r   r2     s    znullcontext.__init__c             C   s   | j S )N)r   )r   r   r   r   r     s    znullcontext.__enter__c             G   s   d S )Nr   )r   excinfor   r   r   r     s    znullcontext.__exit__)N)r   r   r    r2   r   r   r   r   r   r   r     s   
)r!   r;   r   collectionsr   	functoolsr   __all__ABCr   r	   objectr   r,   r3   r?   r   r   r   rI   r   r   r   rY   r   r
   r   r   r   r   r   <module>   s:   
B-!!`E 