U
    pda                     @   sj  d Z 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	 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 )(z4Utilities for with-statement contexts.  See PEP 343.    N)dequewraps
MethodTypeasynccontextmanagercontextmanagerclosingnullcontextAbstractContextManagerAbstractAsyncContextManagerAsyncExitStackContextDecorator	ExitStackredirect_stdoutredirect_stderrsuppressc                   @   s2   e Zd ZdZdd Zejdd Zedd Z	dS )	r   z,An abstract base class for context managers.c                 C   s   | S z0Return `self` upon entering the runtime context. selfr   r    /usr/lib/python3.8/contextlib.py	__enter__   s    z AbstractContextManager.__enter__c                 C   s   dS z9Raise any exception triggered within the runtime context.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____doc__r   abcabstractmethodr   classmethodr%   r   r   r   r   r      s   
c                   @   s2   e Zd ZdZdd Zejdd Zedd Z	dS )	r   z9An abstract base class for asynchronous context managers.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 krt|ddS tS )Nr-   r.   )r   r   r    r!   r"   r   r   r   r%   0   s
    z,AbstractAsyncContextManager.__subclasshook__N)
r&   r'   r(   r)   r-   r*   r+   r.   r,   r%   r   r   r   r   r   #   s   
c                   @   s    e Zd ZdZdd Zdd ZdS )r   zJA base class or mixin that enables context managers to work as decorators.c                 C   s   | S )a6  Return a recreated instance of self.

        Allows an otherwise one-shot context manager like
        _GeneratorContextManager to support use as
        a decorator via implicit recreation.

        This is a private interface just for _GeneratorContextManager.
        See issue #11647 for details.
        r   r   r   r   r   _recreate_cm;   s    
zContextDecorator._recreate_cmc                    s   t   fdd}|S )Nc               
      s*       | |W  5 Q R  S Q R X d S N)r/   argskwdsfuncr   r   r   innerH   s    
z(ContextDecorator.__call__.<locals>.innerr   )r   r5   r6   r   r4   r   __call__G   s    zContextDecorator.__call__N)r&   r'   r(   r)   r/   r7   r   r   r   r   r   8   s   c                   @   s   e Zd ZdZdd ZdS )_GeneratorContextManagerBasezBShared functionality for @contextmanager and @asynccontextmanager.c                 C   sJ   |||| _ |||  | _| _| _t|dd }|d kr@t| j}|| _d S )Nr)   )genr5   r2   r3   getattrtyper)   )r   r5   r2   r3   docr   r   r   __init__R   s    
z%_GeneratorContextManagerBase.__init__N)r&   r'   r(   r)   r=   r   r   r   r   r8   O   s   r8   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	_GeneratorContextManagerz%Helper for @contextmanager decorator.c                 C   s   |  | j| j| jS r0   )	__class__r5   r2   r3   r   r   r   r   r/   f   s    z%_GeneratorContextManager._recreate_cmc                 C   s<   | ` | `| `zt| jW S  tk
r6   tdd Y nX d S Nzgenerator didn't yield)r2   r3   r5   nextr9   StopIterationRuntimeErrorr   r   r   r   r   l   s
    z"_GeneratorContextManager.__enter__c              
   C   s  |d kr8zt | j W n tk
r,   Y dS X tdn|d krF| }z| j||| W n tk
r } z||k	 W Y S d }~X Y nt tk
r } z4||krW Y &dS |tkr|j|krW Y 
dS  W 5 d }~X Y n$   t d |krY dS  Y nX tdd S )NFgenerator didn't stop   z#generator didn't stop after throw())rA   r9   rB   rC   throw	__cause__sysexc_info)r   r;   valuer   excr   r   r   r   u   s.    


z!_GeneratorContextManager.__exit__N)r&   r'   r(   r)   r/   r   r   r   r   r   r   r>   a   s   	r>   c                   @   s    e Zd ZdZdd Zdd ZdS )_AsyncGeneratorContextManagerz Helper for @asynccontextmanager.c                    s6   z| j  I d H W S  tk
r0   tdd Y nX d S r@   )r9   	__anext__StopAsyncIterationrC   r   r   r   r   r-      s    z(_AsyncGeneratorContextManager.__aenter__c              
      s&  |d kr>z| j  I d H  W n tk
r2   Y d S X tdn|d krL| }z"| j |||I d H  tdW n tk
r } z||k	 W Y S d }~X Y n tk
r } z:||krW Y ,dS t|ttfr|j|krW Y 
dS  W 5 d }~X Y n0 tk
r  } z||k	r W 5 d }~X Y nX d S )NrD   z$generator didn't stop after athrow()F)	r9   rM   rN   rC   athrow
isinstancerB   rG   BaseException)r   typrJ   r   rK   r   r   r   r.      s.    




z'_AsyncGeneratorContextManager.__aexit__N)r&   r'   r(   r)   r-   r.   r   r   r   r   rL      s   rL   c                    s   t   fdd}|S )a  @contextmanager decorator.

    Typical usage:

        @contextmanager
        def some_generator(<arguments>):
            <setup>
            try:
                yield <value>
            finally:
                <cleanup>

    This makes this:

        with some_generator(<arguments>) as <variable>:
            <body>

    equivalent to this:

        <setup>
        try:
            <variable> = <value>
            <body>
        finally:
            <cleanup>
    c                     s   t  | |S r0   )r>   r1   r5   r   r   helper   s    zcontextmanager.<locals>.helperr   r5   rT   r   rS   r   r      s    c                    s   t   fdd}|S )a  @asynccontextmanager decorator.

    Typical usage:

        @asynccontextmanager
        async def some_async_generator(<arguments>):
            <setup>
            try:
                yield <value>
            finally:
                <cleanup>

    This makes this:

        async with some_async_generator(<arguments>) as <variable>:
            <body>

    equivalent to this:

        <setup>
        try:
            <variable> = <value>
            <body>
        finally:
            <cleanup>
    c                     s   t  | |S r0   )rL   r1   rS   r   r   rT     s    z#asynccontextmanager.<locals>.helperr   rU   r   rS   r   r      s    c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	r	   a2  Context to automatically close something at the end of a block.

    Code like this:

        with closing(<module>.open(<arguments>)) as f:
            <block>

    is equivalent to this:

        f = <module>.open(<arguments>)
        try:
            <block>
        finally:
            f.close()

    c                 C   s
   || _ d S r0   thing)r   rW   r   r   r   r=   &  s    zclosing.__init__c                 C   s   | j S r0   rV   r   r   r   r   r   (  s    zclosing.__enter__c                 G   s   | j   d S r0   )rW   close)r   rI   r   r   r   r   *  s    zclosing.__exit__Nr&   r'   r(   r)   r=   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 r0   )_new_target_old_targets)r   
new_targetr   r   r   r=   2  s    z_RedirectStream.__init__c                 C   s*   | j tt| j tt| j| j | jS r0   )r\   appendr:   rH   _streamsetattrr[   r   r   r   r   r   7  s    z_RedirectStream.__enter__c                 C   s   t t| j| j  d S r0   )r`   rH   r_   r\   popr   exctypeexcinstexctbr   r   r   r   <  s    z_RedirectStream.__exit__)r&   r'   r(   r_   r=   r   r   r   r   r   r   rZ   .  s   rZ   c                   @   s   e Zd ZdZdZdS )r   aA  Context manager for temporarily redirecting stdout to another file.

        # How to send help() to stderr
        with redirect_stdout(sys.stderr):
            help(dir)

        # How to write help() to a file
        with open('help.txt', 'w') as f:
            with redirect_stdout(f):
                help(pow)
    stdoutNr&   r'   r(   r)   r_   r   r   r   r   r   @  s   c                   @   s   e Zd ZdZdZdS )r   zCContext manager for temporarily redirecting stderr to another file.stderrNrg   r   r   r   r   r   P  s   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	r   a?  Context manager to suppress specified exceptions

    After the exception is suppressed, execution proceeds with the next
    statement following the with statement.

         with suppress(FileNotFoundError):
             os.remove(somefile)
         # Execution still resumes here if the file was already removed
    c                 G   s
   || _ d S r0   )_exceptions)r   
exceptionsr   r   r   r=   a  s    zsuppress.__init__c                 C   s   d S r0   r   r   r   r   r   r   d  s    zsuppress.__enter__c                 C   s   |d k	ot || jS r0   )
issubclassri   rb   r   r   r   r   g  s    
zsuppress.__exit__NrY   r   r   r   r   r   V  s   
c                   @   sh   e Zd 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e_dd ZdddZdS )_BaseExitStackz.A base class for ExitStack and AsyncExitStack.c                 C   s
   t || S r0   r   cmcm_exitr   r   r   _create_exit_wrapperw  s    z#_BaseExitStack._create_exit_wrapperc                   s    fdd}|S )Nc                    s     d S r0   r   r   rK   tbr2   callbackr3   r   r   _exit_wrapper}  s    z8_BaseExitStack._create_cb_wrapper.<locals>._exit_wrapperr   rt   r2   r3   ru   r   rs   r   _create_cb_wrapper{  s    z!_BaseExitStack._create_cb_wrapperc                 C   s   t  | _d S r0   )r   _exit_callbacksr   r   r   r   r=     s    z_BaseExitStack.__init__c                 C   s   t |  }| j|_t | _|S )z@Preserve the context stack by transferring it to a new instance.)r;   rx   r   )r   	new_stackr   r   r   pop_all  s    
z_BaseExitStack.pop_allc                 C   sB   t |}z
|j}W n tk
r0   | | Y nX | || |S )a  Registers a callback with the standard __exit__ method signature.

        Can suppress exceptions the same way __exit__ method can.
        Also accepts any object with an __exit__ method (registering a call
        to the method instead of the object itself).
        )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 )zEnters the supplied context manager.

        If successful, also pushes its __exit__ method as a callback and
        returns the result of the __enter__ method.
        )r;   r   r   r}   r   rn   _cm_type_exitresultr   r   r   enter_context  s
    
z_BaseExitStack.enter_contextc                  O   s   t | dkr| ^}}} nV| s&tdnHd|krZ|d}| ^}} ddl}|jdtdd ntdt | d	  |j|f| |}||_|| |S )
z\Registers an arbitrary callback and arguments.

        Cannot suppress exceptions.
           zBdescriptor 'callback' of '_BaseExitStack' object needs an argumentrt   r   N4Passing 'callback' as keyword argument is deprecated
stacklevelz8callback expected at least 1 positional argument, got %drE   )	len	TypeErrorra   warningswarnDeprecationWarningrw   __wrapped__r|   r2   r3   r   rt   r   ru   r   r   r   rt     s&    

 

z_BaseExitStack.callback#($self, callback, /, *args, **kwds)c                 C   s   |  ||}| |d dS )z;Helper to correctly register callbacks to __exit__ methods.TN)rp   r|   r   rn   ro   ru   r   r   r   r}     s    z_BaseExitStack._push_cm_exitTc                 C   s   | j ||f d S r0   )rx   r^   )r   rt   is_syncr   r   r   r|     s    z"_BaseExitStack._push_exit_callbackN)T)r&   r'   r(   r)   staticmethodrp   rw   r=   rz   r   r   rt   __text_signature__r}   r|   r   r   r   r   rl   t  s   

rl   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	r   a  Context manager for dynamic management of a stack of exit callbacks.

    For example:
        with ExitStack() as stack:
            files = [stack.enter_context(open(fname)) for fname in filenames]
            # All opened files will automatically be closed at the end of
            # the with statement, even if attempts to open files later
            # in the list raise an exception.
    c                 C   s   | S r0   r   r   r   r   r   r     s    zExitStack.__enter__c           
         s   |d d k	}t  d   fdd}d}d}| jr| j \}}|sHtz|| r^d}d}d}W q,   t  }||d |d  d}|}Y q,X q,|rz|d j}	|d W n  tk
r   |	|d _ Y nX |o|S )Nr   rE   c                    s4   | j }||krd S |d ks*| kr$q*|} q || _ d S r0   __context__new_excold_excexc_context	frame_excr   r   _fix_exception_context  s    z2ExitStack.__exit__.<locals>._fix_exception_contextFTNNN)rH   rI   rx   ra   AssertionErrorr   rQ   )
r   exc_detailsreceived_excr   suppressed_excpending_raiser   cbnew_exc_details	fixed_ctxr   r   r   r     s4    

zExitStack.__exit__c                 C   s   |  ddd dS z%Immediately unwind the context stack.N)r   r   r   r   r   rX     s    zExitStack.closeN)r&   r'   r(   r)   r   r   rX   r   r   r   r   r     s   
1c                   @   sf   e Zd ZdZedd Zedd Zdd Zdd	 Zd
d Z	de	_
dd Zdd Zdd Zdd ZdS )r   a  Async context manager for dynamic management of a stack of exit
    callbacks.

    For example:
        async with AsyncExitStack() as stack:
            connections = [await stack.enter_async_context(get_connection())
                for i in range(5)]
            # All opened connections will automatically be released at the
            # end of the async with statement, even if attempts to open a
            # connection later in the list raise an exception.
    c                 C   s
   t || S r0   r   rm   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 r0   r   rq   rs   r   r   ru   ,  s    z>AsyncExitStack._create_async_cb_wrapper.<locals>._exit_wrapperr   rv   r   rs   r   _create_async_cb_wrapper*  s    z'AsyncExitStack._create_async_cb_wrapperc                    s.   t |}|j}||I dH }| || |S )zEnters the supplied async context manager.

        If successful, also pushes its __aexit__ method as a callback and
        returns the result of the __aenter__ method.
        N)r;   r.   r-   _push_async_cm_exitr   r   r   r   enter_async_context0  s
    z"AsyncExitStack.enter_async_contextc                 C   sD   t |}z
|j}W n  tk
r2   | |d Y nX | || |S )a#  Registers a coroutine function with the standard __aexit__ method
        signature.

        Can suppress exceptions the same way __aexit__ method can.
        Also accepts any object with an __aexit__ method (registering a call
        to the method instead of the object itself).
        F)r;   r.   r{   r|   r   r~   r   r   r   push_async_exit<  s    
zAsyncExitStack.push_async_exitc                  O   s   t | dkr| ^}}} nV| s&tdnHd|krZ|d}| ^}} ddl}|jdtdd ntdt | d	  |j|f| |}||_||d
 |S )zfRegisters an arbitrary coroutine function and arguments.

        Cannot suppress exceptions.
        r   zMdescriptor 'push_async_callback' of 'AsyncExitStack' object needs an argumentrt   r   Nr   r   zCpush_async_callback expected at least 1 positional argument, got %drE   F)	r   r   ra   r   r   r   r   r   r|   r   r   r   r   push_async_callbackN  s&    

 
z"AsyncExitStack.push_async_callbackr   c                    s   |  dddI dH  dS r   )r.   r   r   r   r   aclosek  s    zAsyncExitStack.aclosec                 C   s   |  ||}| |d dS )zLHelper to correctly register coroutine function to __aexit__
        method.FN)r   r|   r   r   r   r   r   o  s    z"AsyncExitStack._push_async_cm_exitc                    s   | S r0   r   r   r   r   r   r-   u  s    zAsyncExitStack.__aenter__c                    s   |d d k	}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,X q,|rz|d j}
|d W n  tk
r   |
|d _ Y nX |o|S )Nr   rE   c                    s4   | j }||krd S |d ks*| kr$q*|} q || _ d S r0   r   r   r   r   r   r   ~  s    z8AsyncExitStack.__aexit__.<locals>._fix_exception_contextFTr   )rH   rI   rx   ra   r   rQ   )r   r   r   r   r   r   r   r   cb_suppressr   r   r   r   r   r.   x  s8    


zAsyncExitStack.__aexit__N)r&   r'   r(   r)   r   r   r   r   r   r   r   r   r   r-   r.   r   r   r   r   r     s   

c                   @   s*   e Zd ZdZd	ddZdd Zdd ZdS )
r
   aO  Context manager that does no additional processing.

    Used as a stand-in for a normal context manager, when a particular
    block of code is only sometimes used with a normal context manager:

    cm = optional_cm if condition else nullcontext()
    with cm:
        # Perform operation, using optional_cm if condition is True
    Nc                 C   s
   || _ d S r0   enter_result)r   r   r   r   r   r=     s    znullcontext.__init__c                 C   s   | j S r0   r   r   r   r   r   r     s    znullcontext.__enter__c                 G   s   d S r0   r   )r   excinfor   r   r   r     s    znullcontext.__exit__)NrY   r   r   r   r   r
     s   

)r)   r*   rH   r   collectionsr   	functoolsr   typesr   __all__ABCr   r   objectr   r8   r>   rL   r   r   r	   rZ   r   r   r   rl   r   r   r
   r   r   r   r   <module>   sN        D.!!`E 