
0c@_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 d d d d d	 d
 d d d g	 Z e d g 7Z e j	 d d  d/ k r e j
 Z n e j d e f d f  i  Z d d   Z d d   Z Gd d   d e  Z Gd d	   d	 e  Z Gd d   d e  Z d d   Z Gd d   d e  Z Gd d   d e  Z Gd  d   d e  Z Gd! d   d e  Z Gd" 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 y d d) l m Z Wn e k
 r	e Z  Yn Xd* d+   Z  Gd, d
   d
 e  Z! Gd- d   d e!  Z" Gd. d   d e  Z# d S)0zAcontextlib2 - 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   sB   |  | k r d  S| j  |   x |  j D] } t | |  q' W| S)N)append	__bases___classic_mro)CresultB r   6/tmp/pip-build-jynh7p1z/pip/pip/_vendor/contextlib2.pyr      s    r   c             G   s   y |  j  } Wn' t k
 r6 t t |  g    } Yn XxJ | D]B } x9 | D]- } | | j k rK | j | d  k rw t SPqK Wt Sq> Wd S)NT)__mro__AttributeErrortupler   __dict__NotImplemented)r   methodsmromethodr   r   r   r   _check_methods$   s    r#   c               @   sI   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  k r t | d d  St 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   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  j d t  |  j   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_cm)r$   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   r6   ]   s    
zContextDecorator._recreate_cmc                s%   t        f d d    } | S)Nc           
      s%    j       |  |   SWd  QRXd  S)N)r6   )argskwds)funcr$   r   r   innerj   s    z(ContextDecorator.__call__.<locals>.inner)r   )r$   r:   r;   r   )r:   r$   r   __call__i   s    !zContextDecorator.__call__N)r,   r-   r.   r/   r7   r6   r<   r   r   r   r   r   I   s   c               @   sF   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   si   | | |   |  _  | | | |  _ |  _ |  _ t | d d   } | d  k r\ t |   j } | |  _ d  S)Nr/   )genr:   r8   r9   getattrtyper/   )r$   r:   r8   r9   docr   r   r   __init__t   s    z!_GeneratorContextManager.__init__c             C   s   |  j  |  j |  j |  j  S)N)	__class__r:   r8   r9   )r$   r   r   r   r6      s    z%_GeneratorContextManager._recreate_cmc             C   s6   y t  |  j  SWn t k
 r1 t d   Yn Xd  S)Nzgenerator didn't yield)nextr>   StopIterationRuntimeError)r$   r   r   r   r%      s    z"_GeneratorContextManager.__enter__c             C   s&  | d  k rE y t  |  j  Wn t k
 r5 d  SYq"Xt d   n | d  k rZ |   } y& |  j j | | |  t d   Wn t k
 r } z | | k	 SWYd  d  } ~ Xnq t k
 r} z1 | | k r d St r | j | k r d S  WYd  d  } ~ Xn! t j   d | k	 r  Yn Xd  S)Nzgenerator didn't stopz#generator didn't stop after throw()F   )	rD   r>   rE   rF   throw_HAVE_EXCEPTION_CHAINING	__cause__sysexc_info)r$   r@   valuer(   excr   r   r   r)      s,    		z!_GeneratorContextManager.__exit__N)r,   r-   r.   r/   rB   r6   r%   r)   r   r   r   r   r=   q   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=   )r8   r9   )r:   r   r   helper   s    zcontextmanager.<locals>.helper)r   )r:   rO   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$   rP   r   r   r   rB      s    zclosing.__init__c             C   s   |  j  S)N)rP   )r$   r   r   r   r%      s    zclosing.__enter__c             G   s   |  j  j   d  S)N)rP   close)r$   rL   r   r   r   r)      s    zclosing.__exit__N)r,   r-   r.   r/   rB   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   rB      s    	z_RedirectStream.__init__c             C   s9   |  j  j t t |  j   t t |  j |  j  |  j S)N)rT   r   r?   rK   _streamsetattrrS   )r$   r   r   r   r%      s    z_RedirectStream.__enter__c             C   s    t  t |  j |  j j    d  S)N)rW   rK   rV   rT   pop)r$   exctypeexcinstexctbr   r   r   r)     s    z_RedirectStream.__exit__)r,   r-   r.   rV   rB   r%   r)   r   r   r   r   rR      s   rR   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/   rV   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/   rV   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   rB   )  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)
issubclassr^   )r$   rY   rZ   r[   r   r   r   r)   /  s    
zsuppress.__exit__N)r,   r-   r.   r/   rB   r%   r)   r   r   r   r   r     s   	c                s     f d d   } | S)Nc                sL   x< |  j  } | | k r d  S| d  k s4 |   k r5 P| }  q W| |  _  d  S)N)__context__)new_excold_excexc_context)	frame_excr   r   _fix_exception_context@  s    	
z3_make_context_fixer.<locals>._fix_exception_contextr   )re   rf   r   )re   r   _make_context_fixer?  s    rg   c             C   sD   y |  d j  } |  d  Wn" t k
 r? | |  d _    Yn Xd  S)NrG   )ra   BaseException)exc_details	fixed_ctxr   r   r   _reraise_with_existing_contextO  s    rk   c             C   s
   d d   S)Nc             S   s   d  S)Nr   )rb   rc   r   r   r   <lambda>[  s    z%_make_context_fixer.<locals>.<lambda>r   )re   r   r   r   rg   Z  s    c             C   s   |  \ } } } t  d  d  S)Nz!raise exc_type, exc_value, exc_tb)exec)ri   r&   r'   exc_tbr   r   r   rk   _  s    )InstanceTypec             C   s#   t  |   } | t k r |  j S| S)N)r@   ro   rC   )objZobj_typer   r   r   	_get_typek  s    rq   c               @   s   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)N)r   _exit_callbacks)r$   r   r   r   rB     s    zExitStack.__init__c             C   s+   t  |     } |  j | _ t   |  _ | S)z?Preserve the context stack by transferring it to a new instance)r@   rr   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      |   S)Nr   )ri   )cmcm_exitr   r   _exit_wrapper  s    z.ExitStack._push_cm_exit.<locals>._exit_wrapperN)__self__push)r$   ru   rv   rw   r   )ru   rv   r   _push_cm_exit  s    	zExitStack._push_cm_exitc             C   sR   t  |  } y | j } Wn" t k
 r= |  j j |  Yn 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)
        )rq   r)   r   rr   r   rz   )r$   exit_cb_typeexit_methodr   r   r   ry     s    
zExitStack.pushc                s2       f d d   }  | _  |  j |   S)z\Registers an arbitrary callback and arguments.

        Cannot suppress exceptions.
        c                s         d  S)Nr   )r&   rN   tb)r8   callbackr9   r   r   rw     s    z)ExitStack.callback.<locals>._exit_wrapper)__wrapped__ry   )r$   r   r8   r9   rw   r   )r8   r   r9   r   r     s    	zExitStack.callbackc             C   s8   t  |  } | j } | j |  } |  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.
        )rq   r)   r%   rz   )r$   ru   _cm_type_exitr   r   r   r   enter_context  s
    	zExitStack.enter_contextc             C   s   |  j  d d d  d S)z$Immediately unwind the context stackN)r)   )r$   r   r   r   rQ     s    zExitStack.closec             C   s   |  S)Nr   )r$   r   r   r   r%     s    zExitStack.__enter__c       	   
   G   s   | d d  k	 } t  j   d } t |  } d } d } xv |  j r |  j j   } y" | |   rt d } d } d } Wq; t  j   } | | d | d  d } | } Yq; Xq; W| r t |  | o | S)Nr   rG   FT)NNN)rK   rL   rg   rr   rX   rk   )	r$   ri   received_excre   rf   suppressed_excpending_raisecbnew_exc_detailsr   r   r   r)     s(    

zExitStack.__exit__N)r,   r-   r.   r/   rB   rt   rz   ry   r   r   rQ   r%   r)   r   r   r   r   r	   s  s   c                   sL   e  Z d  Z d Z   f d d   Z d d   Z d d   Z d d	   Z   S)
r   z+Backwards compatibility alias for ExitStackc                s'   t  j d t  t t |   j   d  S)Nz*ContextStack has been renamed to ExitStack)r3   r4   r5   superr   rB   )r$   )rC   r   r   rB     s    	zContextStack.__init__c             C   s   |  j  |  S)N)ry   )r$   r   r   r   r   register_exit  s    zContextStack.register_exitc             O   s   |  j  | | |  S)N)r   )r$   r   r8   r9   r   r   r   register  s    zContextStack.registerc             C   s
   |  j    S)N)rt   )r$   r   r   r   preserve  s    zContextStack.preserve)r,   r-   r.   r/   rB   r   r   r   r   r   )rC   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)N)enter_result)r$   r   r   r   r   rB     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__)r,   r-   r.   r/   rB   r%   r)   r   r   r   r   r     s   )r   r   )$r/   r0   rK   r3   collectionsr   	functoolsr   __all__version_infor   Z_abc_ABCABCMetaobjectr   r#   r   r   r=   r   r   rR   r
   r   r   rI   rg   rk   typesro   ImportErrorr@   rq   r	   r   r   r   r   r   r   <module>   sL   	
(H"q