a
    û"©bB  ã                   @   s¨  d Z ddlZddlZddlZddlmZ ddlmZ g d¢Zedg7 Zej	dd… dkr`ej
Zne d	efd
di¡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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ej	d d"kZe�r2d#d$„ Zd%d&„ Znd'd$„ Zd(d&„ Zzdd)lmZ W n e�yj   eZ Y n
0 d*d+„ Z G d,d-„ d-eƒZ!G d.d„ de!ƒZ"G d/d0„ d0eƒZ#dS )1zAcontextlib2 - backports and enhancements to the contextlib moduleé    N)Údeque©Úwraps)	ÚcontextmanagerÚclosingÚnullcontextÚAbstractContextManagerÚContextDecoratorÚ	ExitStackÚredirect_stdoutÚredirect_stderrÚsuppressÚContextStacké   )é   é   ÚABCÚ	__slots__© c                 C   s0   | |v rd S |  | ¡ | jD ]}t||ƒ q|S ©N)ÚappendÚ	__bases__Ú_classic_mro)ÚCÚresultÚBr   r   ú`/home/yuhw/wc/wire-cell-python-pr-11/pr11/lib/python3.9/site-packages/pip/_vendor/contextlib2.pyr      s    

r   c                 G   st   z
| j }W n  ty*   tt| g ƒƒ}Y n0 |D ]>}|D ],}||jv r8|j| d u r`t    S  q0q8t  S q0dS )NT)Ú__mro__ÚAttributeErrorÚtupler   Ú__dict__ÚNotImplemented)r   ÚmethodsÚmroÚmethodr   r   r   r   Ú_check_methods$   s    


r%   c                   @   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.r   ©Úselfr   r   r   Ú	__enter__8   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 u rt|ddƒS tS )z<Check whether subclass is considered a subclass of this ABC.r(   r,   )r   r%   r!   )Úclsr   r   r   r   Ú__subclasshook__A   s    z'AbstractContextManager.__subclasshook__N)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r(   ÚabcÚabstractmethodr,   Úclassmethodr.   r   r   r   r   r   5   s   
r   c                   @   s(   e Zd ZdZd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   t  dt¡ |  ¡ S )aÁ  Returns the context manager used to actually wrap the call to the
        decorated function.

        The default implementation just returns *self*.

        Overriding this method allows otherwise one-shot context managers
        like _GeneratorContextManager to support use as decorators via
        implicit recreation.

        DEPRECATED: refresh_cm was never added to the standard library's
                    ContextDecorator API
        z2refresh_cm was never added to the standard library)ÚwarningsÚwarnÚDeprecationWarningÚ_recreate_cmr&   r   r   r   Ú
refresh_cmL   s    ÿzContextDecorator.refresh_cmc                 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   r9   ]   s    
zContextDecorator._recreate_cmc                    s   t ˆ ƒ‡ ‡fdd„ƒ}|S )Nc                     s:   ˆ  ¡ � ˆ | i |¤ŽW  d   ƒ S 1 s,0    Y  d S r   )r9   ©ÚargsÚkwds©Úfuncr'   r   r   Úinnerj   s    
z(ContextDecorator.__call__.<locals>.innerr   )r'   r?   r@   r   r>   r   Ú__call__i   s    zContextDecorator.__call__N)r/   r0   r1   r2   r:   r9   rA   r   r   r   r   r	   I   s   r	   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )Ú_GeneratorContextManagerz%Helper for @contextmanager decorator.c                 C   sN   ||i |¤Ž| _ |||  | _| _| _t|dd ƒ}|d u rDt| ƒj}|| _d S )Nr2   )Úgenr?   r<   r=   ÚgetattrÚtyper2   )r'   r?   r<   r=   Údocr   r   r   Ú__init__t   s    
z!_GeneratorContextManager.__init__c                 C   s   |   | j| j| j¡S r   )Ú	__class__r?   r<   r=   r&   r   r   r   r9   ‚   s    z%_GeneratorContextManager._recreate_cmc                 C   s,   zt | jƒW S  ty&   tdƒ‚Y n0 d S )Nzgenerator didn't yield)ÚnextrC   ÚStopIterationÚRuntimeErrorr&   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 |||¡ tdƒ‚W n  tyŒ } z||uW  Y d }~S d }~0  tyâ } z@||u r°W Y d }~dS trÌ|j|u rÌW Y d }~dS ‚ W Y d }~n(d }~0    t ¡ d |u� rü‚ Y n0 d S )Nzgenerator didn't stopz#generator didn't stop after throw()Fé   )	rI   rC   rJ   rK   ÚthrowÚ_HAVE_EXCEPTION_CHAININGÚ	__cause__ÚsysÚexc_info)r'   rE   Úvaluer+   Úexcr   r   r   r,   Ž   s,    
z!_GeneratorContextManager.__exit__N)r/   r0   r1   r2   rG   r9   r(   r,   r   r   r   r   rB   q   s
   rB   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 r   )rB   r;   ©r?   r   r   ÚhelperÕ   s    zcontextmanager.<locals>.helperr   )r?   rU   r   rT   r   r   ¹   s    r   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 r   ©Úthing)r'   rW   r   r   r   rG   ì   s    zclosing.__init__c                 C   s   | j S r   rV   r&   r   r   r   r(   ï   s    zclosing.__enter__c                 G   s   | j  ¡  d S r   )rW   Úclose)r'   rQ   r   r   r   r,   ò   s    zclosing.__exit__N©r/   r0   r1   r2   rG   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   rG   ú   s    z_RedirectStream.__init__c                 C   s*   | j  tt| jƒ¡ tt| j| jƒ | jS r   )r\   r   rD   rP   Ú_streamÚsetattrr[   r&   r   r   r   r(   ÿ   s    z_RedirectStream.__enter__c                 C   s   t t| j| j ¡ ƒ d S r   )r_   rP   r^   r\   Úpop©r'   ÚexctypeÚexcinstÚexctbr   r   r   r,     s    z_RedirectStream.__exit__)r/   r0   r1   r^   rG   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/   r0   r1   r2   r^   r   r   r   r   r     s   r   c                   @   s   e Zd ZdZdZdS )r   zCContext manager for temporarily redirecting stderr to another file.ÚstderrNrf   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 )	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 r   )Ú_exceptions)r'   Ú
exceptionsr   r   r   rG   )  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__NrY   r   r   r   r   r     s   
r   r   c                    s   ‡ fdd„}|S )Nc                    s4   | j }||u rd S |d u s*|ˆ u r$q*|} q || _ d S r   )Ú__context__)Únew_excÚold_excÚexc_context©Ú	frame_excr   r   Ú_fix_exception_context@  s    z3_make_context_fixer.<locals>._fix_exception_contextr   )rp   rq   r   ro   r   Ú_make_context_fixer?  s    rr   c                 C   s:   z| d j }| d ‚W n ty4   || d _ ‚ Y n0 d S )NrL   )rk   ÚBaseException)Úexc_detailsÚ	fixed_ctxr   r   r   Ú_reraise_with_existing_contextO  s    

rv   c                 C   s   dd„ S )Nc                 S   s   d S r   r   )rl   rm   r   r   r   Ú<lambda>[  ó    z%_make_context_fixer.<locals>.<lambda>r   ro   r   r   r   rr   Z  s    c                 C   s   | \}}}t dƒ d S )Nz!raise exc_type, exc_value, exc_tb)Úexec)rt   r)   r*   Úexc_tbr   r   r   rv   _  s    
)ÚInstanceTypec                 C   s   t | ƒ}|tu r| jS |S r   )rE   r{   rH   )ÚobjZobj_typer   r   r   Ú	_get_typek  s    r}   c                   @   sX   e Zd Z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 )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   t ƒ | _d S r   )r   Ú_exit_callbacksr&   r   r   r   rG     s    zExitStack.__init__c                 C   s   t | ƒƒ }| j|_tƒ | _|S )z?Preserve the context stack by transferring it to a new instance)rE   r~   r   )r'   Ú	new_stackr   r   r   Úpop_all‚  s    
zExitStack.pop_allc                    s"   ‡ ‡fdd„}ˆ |_ |  |¡ dS )z:Helper to correctly register callbacks to __exit__ methodsc                     s   ˆˆ g| ¢R Ž S r   r   )rt   ©ÚcmÚcm_exitr   r   Ú_exit_wrapper‹  s    z.ExitStack._push_cm_exit.<locals>._exit_wrapperN)Ú__self__Úpush)r'   r‚   rƒ   r„   r   r�   r   Ú_push_cm_exit‰  s    zExitStack._push_cm_exitc                 C   sB   t |ƒ}z
|j}W n ty0   | j |¡ Y n0 |  ||¡ |S )a  Registers a callback with the standard __exit__ method signature

        Can suppress exceptions the same way __exit__ methods can.

        Also accepts any object with an __exit__ method (registering a call
        to the method instead of the object itself)
        )r}   r,   r   r~   r   r‡   )r'   ÚexitÚ_cb_typeÚexit_methodr   r   r   r†   �  s    

zExitStack.pushc                    s$   ‡ ‡‡fdd„}ˆ|_ |  |¡ ˆS )z\Registers an arbitrary callback and arguments.

        Cannot suppress exceptions.
        c                    s   ˆˆ i ˆ¤Ž d S r   r   )r)   rS   Útb©r<   Úcallbackr=   r   r   r„   ©  s    z)ExitStack.callback.<locals>._exit_wrapper)Ú__wrapped__r†   )r'   r�   r<   r=   r„   r   rŒ   r   r�   ¤  s    
zExitStack.callbackc                 C   s(   t |ƒ}|j}| |¡}|  ||¡ |S )zªEnters 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'   r‚   Ú_cm_typeÚ_exitr   r   r   r   Úenter_context±  s
    
zExitStack.enter_contextc                 C   s   |   ddd¡ dS )z$Immediately unwind the context stackN)r,   r&   r   r   r   rX   ¾  s    zExitStack.closec                 C   s   | S r   r   r&   r   r   r   r(   Â  s    zExitStack.__enter__c           	      G   s–   |d d u}t  ¡ d }t|ƒ}d}d}| jr‚| j ¡ }z||Ž rNd}d}d}W q(   t  ¡ }||d |d ƒ d}|}Y q(0 q(|rŽt|ƒ |o”|S )Nr   rL   FT)NNN)rP   rQ   rr   r~   r`   rv   )	r'   rt   Úreceived_excrp   rq   Úsuppressed_excÚpending_raiseÚcbÚnew_exc_detailsr   r   r   r,   Å  s(    
zExitStack.__exit__N)r/   r0   r1   r2   rG   r€   r‡   r†   r�   r‘   rX   r(   r,   r   r   r   r   r
   s  s   r
   c                       s8   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Z‡  ZS )
r   z+Backwards compatibility alias for ExitStackc                    s   t  dt¡ tt| ƒ ¡  d S )Nz*ContextStack has been renamed to ExitStack)r6   r7   r8   Úsuperr   rG   r&   ©rH   r   r   rG   ç  s    ÿzContextStack.__init__c                 C   s
   |   |¡S r   )r†   )r'   r�   r   r   r   Úregister_exitì  s    zContextStack.register_exitc                 O   s   | j |g|¢R i |¤ŽS r   )r�   )r'   r�   r<   r=   r   r   r   Úregisterï  s    zContextStack.registerc                 C   s   |   ¡ S r   )r€   r&   r   r   r   Úpreserveò  s    zContextStack.preserve)	r/   r0   r1   r2   rG   r™   rš   r›   Ú__classcell__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   aM  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 r   ©Úenter_result)r'   rž   r   r   r   rG   ÿ  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__)NrY   r   r   r   r   r   ö  s   
r   )$r2   r3   rP   r6   Úcollectionsr   Ú	functoolsr   Ú__all__Úversion_infor   Ú_abc_ABCÚABCMetaÚobjectr   r%   r   r	   rB   r   r   rZ   r   r   r   rN   rr   rv   Útypesr{   ÚImportErrorrE   r}   r
   r   r   r   r   r   r   Ú<module>   sF   

(H"

q