B
    Áì2a»`  ã               @   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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Ú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   ú6/var/www/html/prediksi/Python-3.7.12/Lib/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 )z0Return `self` upon entering the runtime context.r   )r   r   r   r   Ú
__aenter__&   s    z&AbstractAsyncContextManager.__aenter__c             Ã   s   dS )z9Raise any exception triggered within the runtime context.Nr   )r   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   r   /   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   ˆ  ¡ � ˆ | |ŽS Q R X d S )N)r'   )ÚargsÚkwds)Úfuncr   r   r   ÚinnerG   s    
z(ContextDecorator.__call__.<locals>.inner)r   )r   r*   r+   r   )r*   r   r   Ú__call__F   s    zContextDecorator.__call__N)r   r   r    r!   r'   r,   r   r   r   r   r   7   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!   )Úgenr*   r(   r)   ÚgetattrÚtyper!   )r   r*   r(   r)   Údocr   r   r   Ú__init__Q   s    
z%_GeneratorContextManagerBase.__init__N)r   r   r    r!   r2   r   r   r   r   r-   N   s   r-   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 )N)Ú	__class__r*   r(   r)   )r   r   r   r   r'   e   s    z%_GeneratorContextManager._recreate_cmc             C   s:   | ` | `| `y
t| jƒS  tk
r4   tdƒd ‚Y nX d S )Nzgenerator didn't yield)r(   r)   r*   Únextr.   ÚStopIterationÚRuntimeError)r   r   r   r   r   k   s
    
z"_GeneratorContextManager.__enter__c          
   C   sð   |d kr6yt | jƒ W n tk
r*   dS X tdƒ‚n¶|d krD|ƒ }y| j |||¡ W nŠ tk
r~ } z||k	S d }~X Y nf tk
rÂ } z(||kršdS |tkr°|j|kr°dS ‚ W d d }~X Y n"   t ¡ d |krÜdS ‚ Y nX tdƒ‚d S )NFzgenerator didn't stopé   z#generator didn't stop after throw())r5   r.   r6   r7   ÚthrowÚ	__cause__ÚsysÚexc_info)r   r0   Úvaluer   Úexcr   r   r   r   t   s.    
z!_GeneratorContextManager.__exit__N)r   r   r    r!   r'   r   r   r   r   r   r   r3   `   s   	r3   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú_AsyncGeneratorContextManagerz Helper for @asynccontextmanager.c             Ã   s4   y| j  ¡ I d H S  tk
r.   tdƒd ‚Y nX d S )Nzgenerator didn't yield)r.   Ú	__anext__ÚStopAsyncIterationr7   )r   r   r   r   r%   ¨   s    z(_AsyncGeneratorContextManager.__aenter__c          
   Ã   s  |d kr<y| j  ¡ I d H  W n tk
r0   d S X tdƒ‚nÐ|d krJ|ƒ }y"| j  |||¡I d H  tdƒ‚W nž tk
r’ } z||k	S d }~X Y nz tk
rÜ } z.||kr®dS t|ttfƒrÊ|j|krÊdS ‚ W d d }~X Y n0 tk
�r
 } z||k	� rú‚ W d d }~X Y nX d S )Nzgenerator didn't stopz$generator didn't stop after athrow()F)	r.   r@   rA   r7   ZathrowÚ
isinstancer6   r:   ÚBaseException)r   Ztypr=   r   r>   r   r   r   r&   ®   s.    


z'_AsyncGeneratorContextManager.__aexit__N)r   r   r    r!   r%   r&   r   r   r   r   r?   ¤   s   r?   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 )N)r3   )r(   r)   )r*   r   r   Úhelperí   s    zcontextmanager.<locals>.helper)r   )r*   rD   r   )r*   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 )N)r?   )r(   r)   )r*   r   r   rD     s    z#asynccontextmanager.<locals>.helper)r   )r*   rD   r   )r*   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 )N)Úthing)r   rE   r   r   r   r2   %  s    zclosing.__init__c             C   s   | j S )N)rE   )r   r   r   r   r   '  s    zclosing.__enter__c             G   s   | j  ¡  d S )N)rE   Úclose)r   r<   r   r   r   r   )  s    zclosing.__exit__N)r   r   r    r!   r2   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 )N)Ú_new_targetÚ_old_targets)r   Z
new_targetr   r   r   r2   1  s    z_RedirectStream.__init__c             C   s*   | j  tt| jƒ¡ tt| j| jƒ | jS )N)rI   Úappendr/   r;   Ú_streamÚsetattrrH   )r   r   r   r   r   6  s    z_RedirectStream.__enter__c             C   s   t t| j| j ¡ ƒ d S )N)rL   r;   rK   rI   Úpop)r   ÚexctypeÚexcinstÚexctbr   r   r   r   ;  s    z_RedirectStream.__exit__)r   r   r    rK   r2   r   r   r   r   r   r   rG   -  s   rG   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!   rK   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.ÚstderrN)r   r   r    r!   rK   r   r   r   r   r   O  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 )N)Ú_exceptions)r   Z
exceptionsr   r   r   r2   `  s    zsuppress.__init__c             C   s   d S )Nr   )r   r   r   r   r   c  s    zsuppress.__enter__c             C   s   |d k	ot || jƒS )N)Ú
issubclassrS   )r   rN   rO   rP   r   r   r   r   f  s    
zsuppress.__exit__N)r   r   r    r!   r2   r   r   r   r   r   r   r   U  s   	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                s   ‡ ‡fdd„}|S )Nc                s   ˆˆ | ||ƒS )Nr   )r   r>   Útb)ÚcmÚcm_exitr   r   Ú_exit_wrapperx  s    z:_BaseExitStack._create_exit_wrapper.<locals>._exit_wrapperr   )rW   rX   rY   r   )rW   rX   r   Ú_create_exit_wrapperv  s    z#_BaseExitStack._create_exit_wrapperc                 s   ˆ ^‰‰ ‡ ‡‡fdd„}|S )Nc                s   ˆˆ ˆŽ d S )Nr   )r   r>   rV   )r(   Úcallbackr)   r   r   rY     s    z8_BaseExitStack._create_cb_wrapper.<locals>._exit_wrapperr   )r(   r)   rY   r   )r(   r[   r)   r   Ú_create_cb_wrapper|  s    z!_BaseExitStack._create_cb_wrapperc             C   s   t ƒ | _d S )N)r   Ú_exit_callbacks)r   r   r   r   r2   ƒ  s    z_BaseExitStack.__init__c             C   s   t | ƒƒ }| j|_tƒ | _|S )z@Preserve the context stack by transferring it to a new instance.)r0   r]   r   )r   Z	new_stackr   r   r   Úpop_all†  s    
z_BaseExitStack.pop_allc             C   sB   t |ƒ}y
|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).
        )r0   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 )z«Enters the supplied context manager.

        If successful, also pushes its __exit__ method as a callback and
        returns the result of the __enter__ method.
        )r0   r   r   ra   )r   rW   Ú_cm_typeÚ_exitÚresultr   r   r   Úenter_context¡  s
    
z_BaseExitStack.enter_contextc              O   s|   t | ƒdkr| ^}}} n>| s&tdƒ‚n0d|krB| 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 argumentr[   z8callback expected at least 1 positional argument, got %dr8   )ÚlenÚ	TypeErrorrM   r\   Ú__wrapped__r`   )r(   r)   r   r[   rY   r   r   r   r[   ¯  s    



z_BaseExitStack.callbackc             C   s"   |   ||¡}||_|  |d¡ dS )z;Helper to correctly register callbacks to __exit__ methods.TN)rZ   Ú__self__r`   )r   rW   rX   rY   r   r   r   ra   È  s    z_BaseExitStack._push_cm_exitTc             C   s   | j  ||f¡ d S )N)r]   rJ   )r   r[   Úis_syncr   r   r   r`   Î  s    z"_BaseExitStack._push_exit_callbackN)T)r   r   r    r!   ÚstaticmethodrZ   r\   r2   r^   re   ri   r[   ra   r`   r   r   r   r   rU   s  s   rU   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 )Nr   )r   r   r   r   r   Þ  s    zExitStack.__enter__c       
         sÚ   |d d k	}t  ¡ d ‰ ‡ fdd„}d}d}xh| jr”| j ¡ \}}|sJt‚y||Ž r`d}d}d}W q.   t  ¡ }||d |d ƒ d}|}Y q.X q.W |rÒy|d j}	|d ‚W n  tk
rÐ   |	|d _‚ Y nX |oØ|S )Nr   r8   c                s8   x,| j }||krd S |d ks$|ˆ kr&P |} qW || _ d S )N)Ú__context__)Únew_excÚold_excÚexc_context)Ú	frame_excr   r   Ú_fix_exception_contextç  s    z2ExitStack.__exit__.<locals>._fix_exception_contextFT)NNN)r;   r<   r]   rM   ÚAssertionErrorrq   rC   )
r   Úexc_detailsÚreceived_excrv   Úsuppressed_excÚpending_raisero   ÚcbÚnew_exc_detailsÚ	fixed_ctxr   )ru   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   rF     s    zExitStack.closeN)r   r   r    r!   r   r   rF   r   r   r   r   r   Ó  s   	1c               @   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                s   ‡ ‡fdd„}|S )Nc             “   s   ˆˆ | ||ƒI d H S )Nr   )r   r>   rV   )rW   rX   r   r   rY   '  s    z@AsyncExitStack._create_async_exit_wrapper.<locals>._exit_wrapperr   )rW   rX   rY   r   )rW   rX   r   Ú_create_async_exit_wrapper%  s    z)AsyncExitStack._create_async_exit_wrapperc                 s   ˆ ^‰‰ ‡ ‡‡fdd„}|S )Nc             “   s   ˆˆ ˆŽI d H  d S )Nr   )r   r>   rV   )r(   r[   r)   r   r   rY   .  s    z>AsyncExitStack._create_async_cb_wrapper.<locals>._exit_wrapperr   )r(   r)   rY   r   )r(   r[   r)   r   Ú_create_async_cb_wrapper+  s    z'AsyncExitStack._create_async_cb_wrapperc             Ã   s.   t |ƒ}|j}| |¡I dH }|  ||¡ |S )z³Enters the supplied async context manager.

        If successful, also pushes its __aexit__ method as a callback and
        returns the result of the __aenter__ method.
        N)r0   r&   r%   Ú_push_async_cm_exit)r   rW   rf   rg   rh   r   r   r   Úenter_async_context2  s
    z"AsyncExitStack.enter_async_contextc             C   sD   t |ƒ}y
|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)r0   r&   r_   r`   r�   )r   rb   rc   rd   r   r   r   Úpush_async_exit>  s    
zAsyncExitStack.push_async_exitc              O   s~   t | ƒdkr| ^}}} n>| s&tdƒ‚n0d|krB| d¡}| ^}} ntdt | ƒd  ƒ‚|j|f| ž|Ž}||_| |d¡ |S )zfRegisters an arbitrary coroutine function and arguments.

        Cannot suppress exceptions.
        rj   zMdescriptor 'push_async_callback' of 'AsyncExitStack' object needs an argumentr[   zCpush_async_callback expected at least 1 positional argument, got %dr8   F)rk   rl   rM   r€   rm   r`   )r(   r)   r   r[   rY   r   r   r   Úpush_async_callbackP  s    


z"AsyncExitStack.push_async_callbackc             Ã   s   |   ddd¡I dH  dS )z%Immediately unwind the context stack.N)r&   )r   r   r   r   Úaclosei  s    zAsyncExitStack.aclosec             C   s"   |   ||¡}||_|  |d¡ dS )zLHelper to correctly register coroutine function to __aexit__
        method.FN)r   rn   r`   )r   rW   rX   rY   r   r   r   r�   m  s    z"AsyncExitStack._push_async_cm_exitc             Ã   s   | S )Nr   )r   r   r   r   r%   t  s    zAsyncExitStack.__aenter__c             ‡   sê   |d d k	}t  ¡ d ‰ ‡ fdd„}d}d}xx| jr¤| j ¡ \}}y0|rR||Ž }n||Ž I d H }|rpd}d}d}W q.   t  ¡ }	||	d |d ƒ d}|	}Y q.X q.W |rây|d j}
|d ‚W n  tk
rà   |
|d _‚ Y nX |oè|S )Nr   r8   c                s8   x,| j }||krd S |d ks$|ˆ kr&P |} qW || _ d S )N)rq   )rr   rs   rt   )ru   r   r   rv   }  s    z8AsyncExitStack.__aexit__.<locals>._fix_exception_contextFT)NNN)r;   r<   r]   rM   rq   rC   )r   rx   ry   rv   rz   r{   ro   r|   Zcb_suppressr}   r~   r   )ru   r   r&   w  s8    


zAsyncExitStack.__aexit__N)r   r   r    r!   rp   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 )N)Úenter_result)r   r†   r   r   r   r2   ¸  s    znullcontext.__init__c             C   s   | j S )N)r†   )r   r   r   r   r   »  s    znullcontext.__enter__c             G   s   d S )Nr   )r   Zexcinfor   r   r   r   ¾  s    znullcontext.__exit__)N)r   r   r    r!   r2   r   r   r   r   r   r   r   ­  s   	
)r!   r"   r;   r   Zcollectionsr   Ú	functoolsr   Ú__all__ÚABCr   r	   Úobjectr   r-   r3   r?   r   r   r   rG   r   r   r   rU   r   r
   r   r   r   r   r   Ú<module>   s<   
B-!!`E 