o
    K+ge                     @   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 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eZG dd dZG dd deeeZG dd de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$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eZ dS )-z4Utilities for with-statement contexts.  See PEP 343.    N)dequewraps)
MethodTypeGenericAlias)asynccontextmanagercontextmanagerclosingnullcontextAbstractContextManagerAbstractAsyncContextManagerAsyncExitStackContextDecorator	ExitStackredirect_stdoutredirect_stderrsuppressaclosingc                   @   :   e Zd ZdZee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. selfr   r   </var/www/html/squashfs-root/usr/lib/python3.10/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__      zAbstractContextManager.__exit__c                 C      | 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____doc__classmethodr   __class_getitem__r   abcabstractmethodr!   r*   r   r   r   r   r          
r   c                   @   r   )	r   z9An abstract base class for asynchronous context managers.c                       | 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   r#   )Nr5   r6   )r   r$   r%   r&   r'   r   r   r   r*   4   s
   z,AbstractAsyncContextManager.__subclasshook__N)r+   r,   r-   r.   r/   r   r0   r5   r1   r2   r6   r*   r   r   r   r   r   %   r3   r   c                   @       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   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   _recreate_cm?   s   
zContextDecorator._recreate_cmc                       t   fdd}|S )Nc                     s:       | i |W  d    S 1 sw   Y  d S Nr8   argskwdsfuncr   r   r   innerL   s   
$z(ContextDecorator.__call__.<locals>.innerr   r   r@   rA   r   r?   r   __call__K      zContextDecorator.__call__Nr+   r,   r-   r.   r8   rC   r   r   r   r   r   <   s    r   c                   @   r7   )AsyncContextDecoratorzPA base class or mixin that enables async context managers to work as decorators.c                 C   r   )z-Return a recreated instance of self.
        r   r   r   r   r   r8   V   r"   z"AsyncContextDecorator._recreate_cmc                    r9   )Nc               	      sV     4 I d H   | i |I d H W  d   I d H  S 1 I d H s$w   Y  d S r:   r;   r<   r?   r   r   rA   \   s   0z-AsyncContextDecorator.__call__.<locals>.innerr   rB   r   r?   r   rC   [   rD   zAsyncContextDecorator.__call__NrE   r   r   r   r   rF   S   s    rF   c                   @   r7   )_GeneratorContextManagerBasezBShared functionality for @contextmanager and @asynccontextmanager.c                 C   sN   ||i || _ |||| _| _| _t|dd }|d u r"t| j}|| _d S )Nr.   )genr@   r=   r>   getattrtyper.   )r   r@   r=   r>   docr   r   r   __init__f   s   

z%_GeneratorContextManagerBase.__init__c                 C   s   |  | j| j| jS r:   )	__class__r@   r=   r>   r   r   r   r   r8   t   s   z)_GeneratorContextManagerBase._recreate_cmN)r+   r,   r-   r.   rL   r8   r   r   r   r   rG   c   s    rG   c                   @   r7   )_GeneratorContextManagerz%Helper for @contextmanager decorator.c                 C   s2   | ` | `| `zt| jW S  ty   tdd w Nzgenerator didn't yield)r=   r>   r@   nextrH   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||| W td tyC } z
||uW  Y d }~S d }~w tyk } z||u rUW Y d }~dS t|trf|j|u rfW Y d }~dS  d }~w ty } z||urw W Y d }~dS d }~ww )NFgenerator didn't stopz#generator didn't stop after throw())rP   rH   rQ   rR   throw
isinstance	__cause__BaseExceptionr   typvaluer    excr   r   r   r!      s<    
z!_GeneratorContextManager.__exit__N)r+   r,   r-   r.   r   r!   r   r   r   r   rN   {       	rN   c                   @   r7   )_AsyncGeneratorContextManagerz*Helper for @asynccontextmanager decorator.c                    s:   | ` | `| `z	t| jI d H W S  ty   tdd w rO   )r=   r>   r@   anextrH   StopAsyncIterationrR   r   r   r   r   r5      s   
z(_AsyncGeneratorContextManager.__aenter__c              
      s  |d u rzt | jI d H  W td ty   Y dS w |d u r$| }z| j|||I d H  W td tyJ } z
||uW  Y d }~S d }~w tyt } z||u r\W Y d }~dS t|ttfro|j|u roW Y d }~dS  d }~w ty } z||ur W Y d }~dS d }~ww )NFrS   z$generator didn't stop after athrow())	r^   rH   r_   rR   athrowrU   rQ   rV   rW   rX   r   r   r   r6      s>    
z'_AsyncGeneratorContextManager.__aexit__N)r+   r,   r-   r.   r5   r6   r   r   r   r   r]      r\   r]   c                       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                        t  | |S r:   )rN   r<   r@   r   r   helper     zcontextmanager.<locals>.helperr   r@   rd   r   rc   r   r         r   c                    ra   )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                     rb   r:   )r]   r<   rc   r   r   rd   8  re   z#asynccontextmanager.<locals>.helperr   rf   r   rc   r   r     rg   r   c                   @   (   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   
   || _ d S r:   thingr   rk   r   r   r   rL   O     
zclosing.__init__c                 C      | j S r:   rj   r   r   r   r   r   Q     zclosing.__enter__c                 G   s   | j   d S r:   )rk   closer   exc_infor   r   r   r!   S  s   zclosing.__exit__Nr+   r,   r-   r.   rL   r   r!   r   r   r   r   r	   >  s
    r	   c                   @   rh   )	r   a  Async context manager for safely finalizing an asynchronously cleaned-up
    resource such as an async generator, calling its ``aclose()`` method.

    Code like this:

        async with aclosing(<module>.fetch(<arguments>)) as agen:
            <block>

    is equivalent to this:

        agen = <module>.fetch(<arguments>)
        try:
            <block>
        finally:
            await agen.aclose()

    c                 C   ri   r:   rj   rl   r   r   r   rL   i  rm   zaclosing.__init__c                       | j S r:   rj   r   r   r   r   r5   k     zaclosing.__aenter__c                    s   | j  I d H  d S r:   )rk   acloserq   r   r   r   r6   m  s   zaclosing.__aexit__N)r+   r,   r-   r.   rL   r5   r6   r   r   r   r   r   W  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   Z
new_targetr   r   r   rL   u  s   
z_RedirectStream.__init__c                 C   s*   | j tt| j tt| j| j | jS r:   )ry   appendrI   sys_streamsetattrrx   r   r   r   r   r   z  s   z_RedirectStream.__enter__c                 C   s   t t| j| j  d S r:   )r}   r{   r|   ry   popr   ZexctypeZexcinstZexctbr   r   r   r!     s   z_RedirectStream.__exit__)r+   r,   r-   r|   rL   r   r!   r   r   r   r   rw   q  s
    rw   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r+   r,   r-   r.   r|   r   r   r   r   r     s    r   c                   @   r   )r   zCContext manager for temporarily redirecting stderr to another file.stderrNr   r   r   r   r   r     s    r   c                   @   rh   )	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   ri   r:   )_exceptions)r   Z
exceptionsr   r   r   rL     rm   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:   )
issubclassr   r   r   r   r   r!     s   
zsuppress.__exit__Nrs   r   r   r   r   r     s
    
r   c                   @   sb   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d ZdddZdS )_BaseExitStackz.A base class for ExitStack and AsyncExitStack.c                 C   
   t || S r:   r   cmcm_exitr   r   r   _create_exit_wrapper     
z#_BaseExitStack._create_exit_wrapperc                       fdd}|S )Nc                    s    i  d S r:   r   r   r[   tbr=   callbackr>   r   r   _exit_wrapper  s   z8_BaseExitStack._create_cb_wrapper.<locals>._exit_wrapperr   r   r=   r>   r   r   r   r   _create_cb_wrapper     z!_BaseExitStack._create_cb_wrapperc                 C   s   t  | _d S r:   )r   _exit_callbacksr   r   r   r   rL     s   z_BaseExitStack.__init__c                 C   s   t |  }| j|_t | _|S )z@Preserve the context stack by transferring it to a new instance.)rJ   r   r   )r   Z	new_stackr   r   r   pop_all  s   
z_BaseExitStack.pop_allc                 C   sB   t |}z|j}W n ty   | | Y |S w | || |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).
        )rJ   r!   AttributeError_push_exit_callback_push_cm_exitr   exitZ_cb_typeZ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.
        )rJ   r!   r   r   r   r   Z_cm_type_exitresultr   r   r   enter_context  s
   
z_BaseExitStack.enter_contextc                O   s,   | j |g|R i |}||_| | |S )z\Registers an arbitrary callback and arguments.

        Cannot suppress exceptions.
        )r   __wrapped__r   r   r   r=   r>   r   r   r   r   r     s   
z_BaseExitStack.callbackc                 C      |  ||}| |d dS )z;Helper to correctly register callbacks to __exit__ methods.TN)r   r   r   r   r   r   r   r   r   r     s   z_BaseExitStack._push_cm_exitTc                 C   s   | j ||f d S r:   )r   rz   )r   r   is_syncr   r   r   r     s   z"_BaseExitStack._push_exit_callbackN)T)r+   r,   r-   r.   staticmethodr   r   rL   r   r   r   r   r   r   r   r   r   r   r     s    

r   c                   @   rh   )	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   r   r:   r   r   r   r   r   r     r   zExitStack.__enter__c           
         s   |d d u}t  d   fdd}d}d}| jrI| j \}}|s$J z|| r/d}d}d}W n   t  }||d |d  d}|}Y | js|rbz	|d j}	|d  tya   |	|d _ w |oe|S )Nr      c                    6   	 | j }|d u s||u rd S | u rn|} q|| _ d S r:   __context__Znew_excZold_excZexc_contextZ	frame_excr   r   _fix_exception_context     

z2ExitStack.__exit__.<locals>._fix_exception_contextFTNNNr{   rr   r   r~   r   rW   )
r   exc_detailsreceived_excr   suppressed_excpending_raiser   cbnew_exc_details	fixed_ctxr   r   r   r!     s:   

zExitStack.__exit__c                 C   s   |  ddd dS z%Immediately unwind the context stack.N)r!   r   r   r   r   rp   F  s   zExitStack.closeN)r+   r,   r-   r.   r   r!   rp   r   r   r   r   r     s
    
1r   c                   @   s`   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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   r   r:   r   r   r   r   r   _create_async_exit_wrapperY  r   z)AsyncExitStack._create_async_exit_wrapperc                   r   )Nc                    s    i I d H  d S r:   r   r   r   r   r   r   _  s   z>AsyncExitStack._create_async_cb_wrapper.<locals>._exit_wrapperr   r   r   r   r   _create_async_cb_wrapper]  r   z'AsyncExitStack._create_async_cb_wrapperc                    s0   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)rJ   r6   r5   _push_async_cm_exitr   r   r   r   enter_async_contextc  s   z"AsyncExitStack.enter_async_contextc                 C   sD   t |}z|j}W n ty   | |d Y |S w | || |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)rJ   r6   r   r   r   r   r   r   r   push_async_exito  s   
zAsyncExitStack.push_async_exitc                O   s.   | j |g|R i |}||_| |d |S )zfRegisters an arbitrary coroutine function and arguments.

        Cannot suppress exceptions.
        F)r   r   r   r   r   r   r   push_async_callback  s   z"AsyncExitStack.push_async_callbackc                    s   |  dddI dH  dS r   )r6   r   r   r   r   rv     s   zAsyncExitStack.aclosec                 C   r   )zLHelper to correctly register coroutine function to __aexit__
        method.FN)r   r   r   r   r   r   r     s   z"AsyncExitStack._push_async_cm_exitc                    r4   r:   r   r   r   r   r   r5        zAsyncExitStack.__aenter__c                    s   |d d u}t  d   fdd}d}d}| jrR| j \}}z|r)|| }n|| I d H }|r8d}d}d}W n   t  }	||	d |d  d}|	}Y | js|rkz	|d j}
|d  tyj   |
|d _ w |on|S )Nr   r   c                    r   r:   r   r   r   r   r   r     r   z8AsyncExitStack.__aexit__.<locals>._fix_exception_contextFTr   r   )r   r   r   r   r   r   r   r   Zcb_suppressr   r   r   r   r   r6     s@   


zAsyncExitStack.__aexit__N)r+   r,   r-   r.   r   r   r   r   r   r   rv   r   r5   r6   r   r   r   r   r   L  s    

r   c                   @   s:   e Zd ZdZdddZdd Z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   ri   r:   enter_result)r   r   r   r   r   rL     rm   znullcontext.__init__c                 C   rn   r:   r   r   r   r   r   r     ro   znullcontext.__enter__c                 G   r   r:   r   r   Zexcinfor   r   r   r!     r   znullcontext.__exit__c                    rt   r:   r   r   r   r   r   r5     ru   znullcontext.__aenter__c                    s   d S r:   r   r   r   r   r   r6     r   znullcontext.__aexit__r:   )	r+   r,   r-   r.   rL   r   r!   r5   r6   r   r   r   r   r
     s    

r
   )!r.   r1   r{   r$   collectionsr   	functoolsr   typesr   r   __all__ABCr   r   objectr   rF   rG   rN   r]   r   r   r	   r   rw   r   r   r   r   r   r   r
   r   r   r   r   <module>   sH    

@A!!PE 