o
    €¨Êh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r0e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r˜d#d$„ Zd%d&„ Znd'd$„ Zd(d&„ Zzdd)lmZ W n ey³   eZ Y nw 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   ú=/var/www/html/env/lib/python3.10/site-packages/contextlib2.pyr      s   

r   c                 G   st   z| j }W n ty   tt| g ƒƒ}Y nw |D ]}|D ]}||jv r2|j| d u r0t    S  nqt  S qd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 )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                   @   ó(   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   r&   )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   r;   ]   s   
zContextDecorator._recreate_cmc                    s   t ˆ ƒ‡ ‡fdd„ƒ}|S )Nc                     s:   ˆ  ¡ � ˆ | i |¤ŽW  d   ƒ S 1 sw   Y  d S r   )r;   ©ÚargsÚkwds©Úfuncr(   r   r   Úinnerj   s   
$ÿz(ContextDecorator.__call__.<locals>.innerr   )r(   rA   rB   r   r@   r   Ú__call__i   s   zContextDecorator.__call__N)r0   r1   r2   r3   r<   r;   rC   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 r"t| ƒj}|| _d S )Nr3   )ÚgenrA   r>   r?   ÚgetattrÚtyper3   )r(   rA   r>   r?   Údocr   r   r   Ú__init__t   s   

z!_GeneratorContextManager.__init__c                 C   s   |   | j| j| j¡S r   )Ú	__class__rA   r>   r?   r'   r   r   r   r;   ‚   s   z%_GeneratorContextManager._recreate_cmc                 C   s$   zt | jƒW S  ty   tdƒ‚w )Nzgenerator didn't yield)ÚnextrE   ÚStopIterationÚRuntimeErrorr'   r   r   r   r)   ˆ   s
   ÿz"_GeneratorContextManager.__enter__c              
   C   sì   |d u rz
t | jƒ W tdƒ‚ ty   Y d S w |d u r |ƒ }z| j |||¡ tdƒ‚ tyB } z
||uW  Y d }~S d }~w tyg } z||u rTW Y d }~dS trb|j|u rbW Y d }~dS ‚ d }~w   t ¡ d |urs‚ Y d S )Nzgenerator didn't stopz#generator didn't stop after throw()Fé   )	rK   rE   rL   rM   ÚthrowÚ_HAVE_EXCEPTION_CHAININGÚ	__cause__ÚsysÚexc_info)r(   rG   Úvaluer,   Úexcr   r   r   r-   Ž   s4   ýÿ€€ÿz!_GeneratorContextManager.__exit__N)r0   r1   r2   r3   rI   r;   r)   r-   r   r   r   r   rD   q   s    rD   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   )rD   r=   ©rA   r   r   ÚhelperÕ   s   zcontextmanager.<locals>.helperr   )rA   rW   r   rV   r   r   ¹   s   r   c                   @   r7   )	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   ó
   || _ d S r   ©Úthing)r(   rZ   r   r   r   rI   ì   ó   
zclosing.__init__c                 C   ó   | j S r   rY   r'   r   r   r   r)   ï   ó   zclosing.__enter__c                 G   s   | j  ¡  d S r   )rZ   Úclose)r(   rS   r   r   r   r-   ò   s   zclosing.__exit__N©r0   r1   r2   r3   rI   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   rI   ú   s   
z_RedirectStream.__init__c                 C   s*   | j  tt| jƒ¡ tt| j| jƒ | jS r   )rb   r   rF   rR   Ú_streamÚsetattrra   r'   r   r   r   r)   ÿ   s   z_RedirectStream.__enter__c                 C   s   t t| j| j ¡ ƒ d S r   )re   rR   rd   rb   Úpop©r(   ÚexctypeÚexcinstÚexctbr   r   r   r-     ó   z_RedirectStream.__exit__)r0   r1   r2   rd   rI   r)   r-   r   r   r   r   r`   ö   s
    r`   c                   @   ó   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©r0   r1   r2   r3   rd   r   r   r   r   r     s    r   c                   @   rl   )r   zCContext manager for temporarily redirecting stderr to another file.ÚstderrNrn   r   r   r   r   r     s    r   c                   @   r7   )	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   rX   r   )Ú_exceptions)r(   Ú
exceptionsr   r   r   rI   )  r[   zsuppress.__init__c                 C   ó   d S r   r   r'   r   r   r   r)   ,  ó   zsuppress.__enter__c                 C   s   |d uo	t || jƒS r   )Ú
issubclassrp   rg   r   r   r   r-   /  s   
zsuppress.__exit__Nr_   r   r   r   r   r     s
    
r   r   c                    s   ‡ fdd„}|S )Nc                    s6   	 | j }||u r
d S |d u s|ˆ u rn|} 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   )rz   r{   r   ry   r   Ú_make_context_fixer?  s   r|   c                 C   s.   z	| d j }| d ‚ ty   || d _ ‚ w )NrN   )ru   ÚBaseException)Úexc_detailsÚ	fixed_ctxr   r   r   Ú_reraise_with_existing_contextO  s   

þr€   c                 C   s   dd„ S )Nc                 S   rr   r   r   )rv   rw   r   r   r   Ú<lambda>[  s    z%_make_context_fixer.<locals>.<lambda>r   ry   r   r   r   r|   Z  ó   c                 C   s   | \}}}t dƒ d S )Nz!raise exc_type, exc_value, exc_tb)Úexec)r~   r*   r+   Úexc_tbr   r   r   r€   _  s   
)ÚInstanceTypec                 C   s   t | ƒ}|tu r| jS |S r   )rG   r…   rJ   )ÚobjÚ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   rI     s   zExitStack.__init__c                 C   s   t | ƒƒ }| j|_tƒ | _|S )z?Preserve the context stack by transferring it to a new instance)rG   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   )r~   ©Ú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   sD   t |ƒ}z|j}W n ty   | j |¡ Y |S w |  ||¡ |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*   rU   Ú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   r^   ¾  s   zExitStack.closec                 C   r&   r   r   r'   r   r   r   r)   Â  rs   zExitStack.__enter__c           	      G   s–   |d d u}t  ¡ d }t|ƒ}d}d}| jrA| j ¡ }z||Ž r'd}d}d}W n   t  ¡ }||d |d ƒ d}|}Y | js|rGt|ƒ |oJ|S )Nr   rN   FT)NNN)rR   rS   r|   r‰   rf   r€   )	r(   r~   Úreceived_excrz   r{   Úsuppressed_excÚpending_raiseÚcbÚnew_exc_detailsr   r   r   r-   Å  s,   
€ôzExitStack.__exit__N)r0   r1   r2   r3   rI   r‹   r’   r‘   r˜   rœ   r^   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)r8   r9   r:   Úsuperr   rI   r'   ©rJ   r   r   rI   ç  s   ÿzContextStack.__init__c                 C   s
   |   |¡S r   )r‘   )r(   r˜   r   r   r   Úregister_exitì  r[   zContextStack.register_exitc                 O   s   | j |g|¢R i |¤ŽS r   )r˜   )r(   r˜   r>   r?   r   r   r   Úregisterï  rk   zContextStack.registerc                 C   s   |   ¡ S r   )r‹   r'   r   r   r   Úpreserveò  r‚   zContextStack.preserve)	r0   r1   r2   r3   rI   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   rX   r   ©Úenter_result)r(   r©   r   r   r   rI   ÿ  r[   znullcontext.__init__c                 C   r\   r   r¨   r'   r   r   r   r)     r]   znullcontext.__enter__c                 G   rr   r   r   )r(   Úexcinfor   r   r   r-     rs   znullcontext.__exit__r   r_   r   r   r   r   r   ö  s
    
r   )$r3   r4   rR   r8   Úcollectionsr   Ú	functoolsr   Ú__all__Úversion_infor   Ú_abc_ABCÚABCMetaÚobjectr   r%   r   r	   rD   r   r   r`   r   r   r   rP   r|   r€   Útypesr…   ÚImportErrorrG   rˆ   r
   r   r   r   r   r   r   Ú<module>   sJ    

(H"
þq