a
    ze0`                     @   sV  d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZm	Z	 g d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)
MethodTypeGenericAlias)asynccontextmanagercontextmanagerclosingnullcontextAbstractContextManagerAbstractAsyncContextManagerAsyncExitStackContextDecorator	ExitStackredirect_stdoutredirect_stderrsuppressc                   @   s6   e Zd Zee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.9/contextlib.py	__enter__   s    z AbstractContextManager.__enter__c                 C   s   d S r   r   r   exc_type	exc_value	tracebackr   r   r   __exit__   s    zAbstractContextManager.__exit__c                 C   s   | t u 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__classmethodr   __class_getitem__r   abcabstractmethodr   r$   r   r   r   r   r      s   
r   c                   @   s6   e Zd ZeeZdd Zejdd Z	edd Z
dS )r   c                    s   | S r   r   r   r   r   r   
__aenter__+   s    z&AbstractAsyncContextManager.__aenter__c                    s   d S r   r   r   r   r   r   	__aexit__/   s    z%AbstractAsyncContextManager.__aexit__c                 C   s   | t u rt|ddS tS )Nr,   r-   )r   r   r   r    r!   r   r   r   r$   4   s
    z,AbstractAsyncContextManager.__subclasshook__N)r%   r&   r'   r(   r   r)   r,   r*   r+   r-   r$   r   r   r   r   r   %   s   
r   c                   @   s   e Zd Zdd Zdd ZdS )r   c                 C   s   | S r   r   r   r   r   r   _recreate_cm?   s    
zContextDecorator._recreate_cmc                    s   t   fdd}|S )Nc                     s:       | i |W  d    S 1 s,0    Y  d S r   )r.   argskwdsfuncr   r   r   innerL   s    
z(ContextDecorator.__call__.<locals>.innerr   )r   r3   r4   r   r2   r   __call__K   s    zContextDecorator.__call__N)r%   r&   r'   r.   r5   r   r   r   r   r   <   s   r   c                   @   s   e Zd Zdd Zdd ZdS )_GeneratorContextManagerBasec                 C   sN   ||i || _ |||  | _| _| _t|dd }|d u rDt| j}|| _d S )N__doc__)genr3   r0   r1   getattrtyper7   )r   r3   r0   r1   docr   r   r   __init__V   s    
z%_GeneratorContextManagerBase.__init__c                 C   s   |  | j| j| jS r   )	__class__r3   r0   r1   r   r   r   r   r.   d   s    z)_GeneratorContextManagerBase._recreate_cmN)r%   r&   r'   r<   r.   r   r   r   r   r6   S   s   r6   c                   @   s   e Zd Zdd Zdd ZdS )_GeneratorContextManagerc                 C   s:   | ` | `| `zt| jW S  ty4   tdd Y n0 d S Nzgenerator didn't yield)r0   r1   r3   nextr8   StopIterationRuntimeErrorr   r   r   r   r   r   s
    z"_GeneratorContextManager.__enter__c              
   C   s   |d u r6zt | j W n ty*   Y dS 0 tdn|d u rD| }z| j||| W n ty } z||uW  Y d }~S d }~0  ty } zF||u rW Y d }~dS t|tr|j|u rW Y d }~dS  W Y d }~n<d }~0  ty } z||u r W Y d }~dS d }~0 0 tdd S )NFgenerator didn't stopz#generator didn't stop after throw())r@   r8   rA   rB   throw
isinstance	__cause__BaseExceptionr   typvaluer   excr   r   r   r   {   s4    

z!_GeneratorContextManager.__exit__N)r%   r&   r'   r   r   r   r   r   r   r>   k   s   	r>   c                   @   s   e Zd Zdd Zdd ZdS )_AsyncGeneratorContextManagerc                    s@   | ` | `| `z| j I d H W S  ty:   tdd Y n0 d S r?   )r0   r1   r3   r8   	__anext__StopAsyncIterationrB   r   r   r   r   r,      s
    z(_AsyncGeneratorContextManager.__aenter__c              
      s4  |d u r>z| j  I d H  W n ty0   Y dS 0 td n|d u rL| }z| j |||I d H  W n ty } z||uW  Y d }~S d }~0  t y } zJ||u rW Y d }~dS t|ttfr|j|u rW Y d }~dS  W Y d }~n<d }~0  ty& } z||ur W Y d }~dS d }~0 0 tdd S )NFrC   z$generator didn't stop after athrow())	r8   rM   rN   rB   athrowrE   rA   rF   rG   rH   r   r   r   r-      s4    
z'_AsyncGeneratorContextManager.__aexit__N)r%   r&   r'   r,   r-   r   r   r   r   rL      s   	rL   c                    s   t   fdd}|S )Nc                     s   t  | |S r   )r>   r/   r3   r   r   helper  s    zcontextmanager.<locals>.helperr   r3   rQ   r   rP   r   r      s    r   c                    s   t   fdd}|S )Nc                     s   t  | |S r   )rL   r/   rP   r   r   rQ   &  s    z#asynccontextmanager.<locals>.helperr   rR   r   rP   r   r     s    r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )r	   c                 C   s
   || _ d S r   thing)r   rT   r   r   r   r<   =  s    zclosing.__init__c                 C   s   | j S r   rS   r   r   r   r   r   ?  s    zclosing.__enter__c                 G   s   | j   d S r   )rT   close)r   exc_infor   r   r   r   A  s    zclosing.__exit__Nr%   r&   r'   r<   r   r   r   r   r   r   r	   ,  s   r	   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 r   )_new_target_old_targets)r   
new_targetr   r   r   r<   I  s    z_RedirectStream.__init__c                 C   s*   | j tt| j tt| j| j | jS r   )rZ   appendr9   sys_streamsetattrrY   r   r   r   r   r   N  s    z_RedirectStream.__enter__c                 C   s   t t| j| j  d S r   )r_   r]   r^   rZ   popr   exctypeexcinstexctbr   r   r   r   S  s    z_RedirectStream.__exit__)r%   r&   r'   r^   r<   r   r   r   r   r   r   rX   E  s   rX   c                   @   s   e Zd ZdZdS )r   stdoutNr%   r&   r'   r^   r   r   r   r   r   W  s   r   c                   @   s   e Zd ZdZdS )r   stderrNrf   r   r   r   r   r   g  s   r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )r   c                 G   s
   || _ d S r   )_exceptions)r   
exceptionsr   r   r   r<   x  s    zsuppress.__init__c                 C   s   d S r   r   r   r   r   r   r   {  s    zsuppress.__enter__c                 C   s   |d uot || jS r   )
issubclassrh   ra   r   r   r   r   ~  s    
zsuppress.__exit__NrW   r   r   r   r   r   m  s   r   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                 C   s
   t || S r   r   cmcm_exitr   r   r   _create_exit_wrapper  s    z#_BaseExitStack._create_exit_wrapperc                   s    fdd}|S )Nc                    s    i  d S r   r   r   rK   tbr0   callbackr1   r   r   _exit_wrapper  s    z8_BaseExitStack._create_cb_wrapper.<locals>._exit_wrapperr   rt   r0   r1   ru   r   rs   r   _create_cb_wrapper  s    z!_BaseExitStack._create_cb_wrapperc                 C   s   t  | _d S r   )r   _exit_callbacksr   r   r   r   r<     s    z_BaseExitStack.__init__c                 C   s   t |  }| j|_t | _|S r   )r:   rx   r   )r   	new_stackr   r   r   pop_all  s    
z_BaseExitStack.pop_allc                 C   s@   t |}z
|j}W n ty.   | | Y n0 | || |S r   )r:   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 r   )r:   r   r   r}   r   rn   _cm_type_exitresultr   r   r   enter_context  s
    
z_BaseExitStack.enter_contextc                O   s,   | j |g|R i |}||_| | |S r   )rw   __wrapped__r|   r   rt   r0   r1   ru   r   r   r   rt     s    
z_BaseExitStack.callbackc                 C   s   |  ||}| |d d S )NT)rp   r|   r   rn   ro   ru   r   r   r   r}     s    z_BaseExitStack._push_cm_exitTc                 C   s   | j ||f d S r   )rx   r\   )r   rt   is_syncr   r   r   r|     s    z"_BaseExitStack._push_exit_callbackN)T)r%   r&   r'   staticmethodrp   rw   r<   rz   r   r   rt   r}   r|   r   r   r   r   rk     s   

rk   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )r   c                 C   s   | S r   r   r   r   r   r   r     s    zExitStack.__enter__c           
         s   |d d u}t  d   fdd}d}d}| jr| j \}}z|| rVd}d}d}W q,   t  }||d |d  d}|}Y q,0 q,|rz|d j}	|d W n ty   |	|d _ Y n0 |o|S )Nr      c                    s4   | j }|d u s||u rd S | u r$q*|} q || _ d S r   __context__new_excold_excexc_context	frame_excr   r   _fix_exception_context  s    z2ExitStack.__exit__.<locals>._fix_exception_contextFTNNNr]   rV   rx   r`   r   rG   )
r   exc_detailsreceived_excr   suppressed_excpending_raiser   cbnew_exc_details	fixed_ctxr   r   r   r     s2    

zExitStack.__exit__c                 C   s   |  d d d  d S r   )r   r   r   r   r   rU     s    zExitStack.closeN)r%   r&   r'   r   r   rU   r   r   r   r   r     s   1r   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 ZdS )r   c                 C   s
   t || S r   rl   rm   r   r   r   _create_async_exit_wrapper-  s    z)AsyncExitStack._create_async_exit_wrapperc                   s    fdd}|S )Nc                    s    i I d H  d S r   r   rq   rs   r   r   ru   3  s    z>AsyncExitStack._create_async_cb_wrapper.<locals>._exit_wrapperr   rv   r   rs   r   _create_async_cb_wrapper1  s    z'AsyncExitStack._create_async_cb_wrapperc                    s.   t |}|j}||I d H }| || |S r   )r:   r-   r,   _push_async_cm_exitr   r   r   r   enter_async_context7  s
    z"AsyncExitStack.enter_async_contextc                 C   sB   t |}z
|j}W n ty0   | |d Y n0 | || |S NF)r:   r-   r{   r|   r   r~   r   r   r   push_async_exitC  s    
zAsyncExitStack.push_async_exitc                O   s.   | j |g|R i |}||_| |d |S r   )r   r   r|   r   r   r   r   push_async_callbackU  s    z"AsyncExitStack.push_async_callbackc                    s   |  d d d I d H  d S r   )r-   r   r   r   r   acloseb  s    zAsyncExitStack.aclosec                 C   s   |  ||}| |d d S r   )r   r|   r   r   r   r   r   f  s    z"AsyncExitStack._push_async_cm_exitc                    s   | S r   r   r   r   r   r   r,   l  s    zAsyncExitStack.__aenter__c                    s   |d d u}t  d   fdd}d}d}| jr| j \}}z0|rP|| }n|| I d H }|rnd}d}d}W q,   t  }	||	d |d  d}|	}Y q,0 q,|rz|d j}
|d W n ty   |
|d _ Y n0 |o|S )Nr   r   c                    s4   | j }|d u s||u rd S | u r$q*|} q || _ d S r   r   r   r   r   r   r   u  s    z8AsyncExitStack.__aexit__.<locals>._fix_exception_contextFTr   r   )r   r   r   r   r   r   r   r   cb_suppressr   r   r   r   r   r-   o  s8    


zAsyncExitStack.__aexit__N)r%   r&   r'   r   r   r   r   r   r   r   r   r,   r-   r   r   r   r   r      s   

r   c                   @   s&   e Zd ZdddZdd Zdd ZdS )	r
   Nc                 C   s
   || _ d S r   enter_result)r   r   r   r   r   r<     s    znullcontext.__init__c                 C   s   | j S r   r   r   r   r   r   r     s    znullcontext.__enter__c                 G   s   d S r   r   )r   excinfor   r   r   r     s    znullcontext.__exit__)NrW   r   r   r   r   r
     s   
r
   )r*   r]   r   collectionsr   	functoolsr   typesr   r   __all__ABCr   r   objectr   r6   r>   rL   r   r   r	   rX   r   r   r   rk   r   r   r
   r   r   r   r   <module>   s<   
A>!!PE 