
Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­
<!DOCTYPE html>
<html>
3

  \j3  ã               @   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gZG dd„ dej	ƒZ
G dd„ deƒZG dd„ de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G dd	„ d	e
ƒZdS )z4Utilities for with-statement contexts.  See PEP 343.é    N)Údeque)ÚwrapsÚcontextmanagerÚclosingÚAbstractContextManagerÚ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/lib64/python3.6/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j|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               @   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   ˆj ƒ � ˆ | |ŽS Q R X d S )N)r!   )ÚargsÚkwds)Úfuncr   r   r   Úinner1   s    
z(ContextDecorator.__call__.<locals>.inner)r   )r   r$   r%   r   )r$   r   r   Ú__call__0   s    zContextDecorator.__call__N)r   r   r   r   r!   r&   r   r   r   r   r   !   s   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   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__;   s    
z!_GeneratorContextManager.__init__c             C   s   | j | j| j| jƒS )N)Ú	__class__r$   r"   r#   )r   r   r   r   r!   I   s    z%_GeneratorContextManager._recreate_cmc             C   s.   y
t | jƒS  tk
r(   tdƒd ‚Y nX d S )Nzgenerator didn't yield)Únextr(   ÚStopIterationÚRuntimeError)r   r   r   r   r   O   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j|||ƒ W nˆ tk
r| } z||k	S d }~X nf tk
rÀ } z*||kr˜dS |tkr®|j|kr®dS ‚ W Y d d }~X n"   tjƒ d |krÚdS ‚ Y nX tdƒ‚d S )NFzgenerator didn't stopé   z#generator didn't stop after throw())r.   r(   r/   r0   Ú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   r'   8   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)r'   )r"   r#   )r$   r   r   Úhelper�   s    zcontextmanager.<locals>.helper)r   )r$   r8   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   r9   r   r   r   r,   ´   s    zclosing.__init__c             C   s   | j S )N)r9   )r   r   r   r   r   ¶   s    zclosing.__enter__c             G   s   | j jƒ  d S )N)r9   Úclose)r   r5   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 )N)Ú_new_targetÚ_old_targets)r   Ú
new_targetr   r   r   r,   À   s    z_RedirectStream.__init__c             C   s*   | j jtt| jƒƒ tt| j| jƒ | jS )N)r=   Úappendr)   r4   Ú_streamÚsetattrr<   )r   r   r   r   r   Å   s    z_RedirectStream.__enter__c             C   s   t t| j| jjƒ ƒ d S )N)rA   r4   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   r;   ¼   s   r;   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.ÚstderrN)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 )	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   Ú
exceptionsr   r   r   r,   ï   s    zsuppress.__init__c             C   s   d S )Nr   )r   r   r   r   r   ò   s    zsuppress.__enter__c             C   s   |d k	ot || jƒS )N)Ú
issubclassrH   )r   rC   rD   rE   r   r   r   r   õ   s    
zsuppress.__exit__N)r   r   r   r   r,   r   r   r   r   r   r   r   ä   s   	c               @   sP   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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 )N)r   Ú_exit_callbacks)r   r   r   r   r,     s    zExitStack.__init__c             C   s   t | ƒƒ }| j|_tƒ | _|S )z?Preserve the context stack by transferring it to a new instance)r*   rK   r   )r   Ú	new_stackr   r   r   Úpop_all  s    
zExitStack.pop_allc                s"   ‡ ‡fdd„}ˆ |_ | j|ƒ dS )z:Helper to correctly register callbacks to __exit__ methodsc                 s   ˆˆ f| žŽ S )Nr   )Úexc_details)ÚcmÚcm_exitr   r   Ú_exit_wrapper  s    z.ExitStack._push_cm_exit.<locals>._exit_wrapperN)Ú__self__Úpush)r   rO   rP   rQ   r   )rO   rP   r   Ú_push_cm_exit  s    zExitStack._push_cm_exitc             C   sD   t |ƒ}y
|j}W n  tk
r2   | jj|ƒ Y nX | j||ƒ |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   ÚAttributeErrorrK   r?   rT   )r   ÚexitÚ_cb_typeÚexit_methodr   r   r   rS      s    

zExitStack.pushc                s$   ‡ ‡‡fdd„}ˆ|_ | j|ƒ ˆS )z\Registers an arbitrary callback and arguments.

        Cannot suppress exceptions.
        c                s   ˆˆ ˆŽ d S )Nr   )r   r7   Útb)r"   Úcallbackr#   r   r   rQ   9  s    z)ExitStack.callback.<locals>._exit_wrapper)Ú__wrapped__rS   )r   rZ   r"   r#   rQ   r   )r"   rZ   r#   r   rZ   4  s    
zExitStack.callbackc             C   s(   t |ƒ}|j}|j|ƒ}| 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   rT   )r   rO   Ú_cm_typeÚ_exitÚresultr   r   r   Úenter_contextA  s
    
zExitStack.enter_contextc             C   s   | j dddƒ dS )z$Immediately unwind the context stackN)r   )r   r   r   r   r:   N  s    zExitStack.closec       	         sÎ   |d d k	}t jƒ d ‰ ‡ fdd„}d}d}x\| jrˆ| jjƒ }y||Ž rTd}d}d}W q.   t jƒ }||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   r1   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_contextX  s    z2ExitStack.__exit__.<locals>._fix_exception_contextFT)NNN)r4   r5   rK   rB   r`   ÚBaseException)	r   rN   Úreceived_excre   Úsuppressed_excÚpending_raiseÚcbÚnew_exc_detailsÚ	fixed_ctxr   )rd   r   r   R  s2    


zExitStack.__exit__N)r   r   r   r   r,   rM   rT   rS   rZ   r_   r:   r   r   r   r   r   r     s   )r   r   r4   r   Úcollectionsr   Ú	functoolsr   Ú__all__ÚABCr   Úobjectr   r'   r   r   r;   r	   r
   r   r   r   r   r   r   Ú<module>   s$   I"