
    9                        S r SSKrSSKrSSKJr  SSKJr  / SQr\S/-  r " S S\5      r	 " S	 S
\	5      r
S r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r\R"                  S   S:  r\(       a  S rS rOS rS r SSKJr  S r " S S\5      r " S S\5      rg! \ a    \r N!f = f) zAcontextlib2 - backports and enhancements to the contextlib module    N)dequewraps)contextmanagerclosingContextDecorator	ExitStackredirect_stdoutredirect_stderrsuppressContextStackc                   *    \ rS rSrSrS rS rS rSrg)r      zJA base class or mixin that enables context managers to work as decorators.c                 X    [         R                  " S[        5        U R                  5       $ )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selfs    'lib/third_party/contextlib2/__init__.py
refresh_cmContextDecorator.refresh_cm   s%     	J(	*  ""    c                     U $ )a  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   s    r   r   ContextDecorator._recreate_cm"   s	     r   c                 4   ^ ^ [        T5      UU 4S j5       nU$ )Nc                  j   > TR                  5          T" U 0 UD6sS S S 5        $ ! , (       d  f       g = fN)r   )argskwdsfuncr   s     r   inner(ContextDecorator.__call__.<locals>.inner/   s(    ""$T*T* %$$s   $
2r   )r   r#   r$   s   `` r   __call__ContextDecorator.__call__.   s     	t	+ 
	+ r   r   N)	__name__
__module____qualname____firstlineno____doc__r   r   r&   __static_attributes__r   r   r   r   r      s    P#"
r   r   c                   0    \ rS rSrSrS rS rS rS rSr	g)	_GeneratorContextManager6   z%Helper for @contextmanager decorator.c                     U" U0 UD6U l         XUsU l        U l        U l        [	        USS 5      nUc  [        U 5      R                  nX@l        g )Nr,   )genr#   r!   r"   getattrtyper,   )r   r#   r!   r"   docs        r   __init__!_GeneratorContextManager.__init__9   sN    &&*.d'	49didIt,;t*$$Cr   c                 d    U R                  U R                  U R                  U R                  5      $ r    )	__class__r#   r!   r"   r   s    r   r   %_GeneratorContextManager._recreate_cmG   s#     ~~diiDII>>r   c                 `     [        U R                  5      $ ! [         a    [        S5      ef = f)Nzgenerator didn't yield)nextr2   StopIterationRuntimeErrorr   s    r   	__enter__"_GeneratorContextManager.__enter__M   s0    	9>! 	9788	9s    -c                    Uc!   [        U R                  5        [        S5      eUc  U" 5       n U R                  R	                  XU5        [        S5      e! [         a     g f = f! [         a  nXBLs S nA$ S nAf[         a.  nXBL a   S nAg[
        (       a  UR                  UL a   S nAge S nAf  [        R                  " 5       S   ULa  e  g = f)Nzgenerator didn't stopz#generator didn't stop after throw()F   )	r<   r2   r>   r=   throw_HAVE_EXCEPTION_CHAINING	__cause__sysexc_info)r   r4   value	tracebackexcs        r   __exit__!_GeneratorContextManager.__exit__S   s    <<TXX ##:;;} tI6"#HII !  ! ( '' 	<  ,+0F 	 <<>!$E1 2sF   A 'A' 
A$#A$'
C1A93C9CB/B/.B//!C)r,   r!   r#   r2   r"   N)
r(   r)   r*   r+   r,   r6   r   r?   rK   r-   r   r   r   r/   r/   6   s    /?9(r   r/   c                 0   ^  [        T 5      U 4S j5       nU$ )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X5      $ r    )r/   )r!   r"   r#   s     r   helpercontextmanager.<locals>.helper   s    'd99r   r   )r#   rO   s   ` r   r   r   ~   s     8 4[: :Mr   c                   *    \ rS rSrSrS rS rS rSrg)r      a
  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                     Xl         g r    thing)r   rU   s     r   r6   closing.__init__   s    
r   c                     U R                   $ r    rT   r   s    r   r?   closing.__enter__   s    zzr   c                 8    U R                   R                  5         g r    )rU   close)r   rG   s     r   rK   closing.__exit__   s    

r   rT   N	r(   r)   r*   r+   r,   r6   r?   rK   r-   r   r   r   r   r      s     r   r   c                   *    \ rS rSrSrS rS rS rSrg)_RedirectStream   Nc                     Xl         / U l        g r    _new_target_old_targets)r   
new_targets     r   r6   _RedirectStream.__init__   s    %r   c                     U R                   R                  [        [        U R                  5      5        [        [        U R                  U R                  5        U R                  $ r    )rc   appendr3   rF   _streamsetattrrb   r   s    r   r?   _RedirectStream.__enter__   sC      dll!;<T\\4#3#34r   c                 j    [        [        U R                  U R                  R	                  5       5        g r    )ri   rF   rh   rc   popr   exctypeexcinstexctbs       r   rK   _RedirectStream.__exit__   s!    T\\4#4#4#8#8#:;r   ra   )	r(   r)   r*   r+   rh   r6   r?   rK   r-   r   r   r   r^   r^      s    G
 
<r   r^   c                       \ rS rSrSrSrSrg)r
      a  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)
stdoutr   Nr(   r)   r*   r+   r,   rh   r-   r   r   r   r
   r
      s    
 Gr   r
   c                       \ rS rSrSrSrSrg)r      zCContext manager for temporarily redirecting stderr to another file.stderrr   Nru   r   r   r   r   r      s
    MGr   r   c                   *    \ rS rSrSrS rS rS rSrg)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                     Xl         g r    _exceptions)r   
exceptionss     r   r6   suppress.__init__   s    %r   c                     g r    r   r   s    r   r?   suppress.__enter__   s    r   c                 B    US L=(       a    [        XR                  5      $ r    )
issubclassr}   rm   s       r   rK   suppress.__exit__   s     d"Lz';K;K'LLr   r|   Nr\   r   r   r   r   r      s    &
Mr   r      c                    ^  U 4S jnU$ )Nc                 P   >  U R                   nX!L a  g Ub  UTL a  OUn M  Xl         g r    )__context__)new_excold_excexc_context	frame_excs      r   _fix_exception_context3_make_context_fixer.<locals>._fix_exception_context  s<    %11)&+*B%  #*r   r   )r   r   s   ` r   _make_context_fixerr     s    	* &%r   c                 ^     U S   R                   nU S   e! [         a    WU S   l         e f = f)NrB   )r   BaseException)exc_details	fixed_ctxs     r   _reraise_with_existing_contextr     s@    	 $A22Ia.  	)2KN&	s    ,c                     S $ )Nc                     g r    r   )r   r   s     r   <lambda>%_make_context_fixer.<locals>.<lambda>  s    r   r   )r   s    r   r   r     s    ,,r   c                 $    U u  pn[        S5        g )Nz!raise exc_type, exc_value, exc_tb)exec)r   exc_type	exc_valueexc_tbs       r   r   r   "  s    &1#V12r   )InstanceTypec                 F    [        U 5      nU[        L a  U R                  $ U$ r    )r4   r   r9   )objobj_types     r   	_get_typer   .  s"    9|#== r   c                   N    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS rSrg)r	   i5  ao  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                 "    [        5       U l        g r    )r   _exit_callbacksr   s    r   r6   ExitStack.__init__A  s    $wr   c                 f    [        U 5      " 5       nU R                  Ul        [        5       U l        U$ )z?Preserve the context stack by transferring it to a new instance)r4   r   r   )r   	new_stacks     r   pop_allExitStack.pop_allD  s+    JL	$($8$8	!$wr   c                 F   ^^ UU4S jnTUl         U R                  U5        g)z:Helper to correctly register callbacks to __exit__ methodsc                     > T" T/U Q76 $ r    r   )r   cmcm_exits    r   _exit_wrapper.ExitStack._push_cm_exit.<locals>._exit_wrapperM  s    2,,,r   N)__self__push)r   r   r   r   s    `` r   _push_cm_exitExitStack._push_cm_exitK  s    	-!#		- r   c                     [        U5      n UR                  nU R                  X5        U$ ! [         a    U R                  R                  U5         U$ f = f)zRegisters 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   rK   r   AttributeErrorr   rg   )r   exit_cb_typeexit_methods       r   r   ExitStack.pushR  s^     T?	2"++K
 t1  	.  ''- 	.s   , %AAc                 L   ^^^ UUU4S jnTUl         U R                  U5        T$ )zLRegisters an arbitrary callback and arguments.

Cannot suppress exceptions.
c                    > T" T0 TD6  g r    r   )r   rJ   tbr!   callbackr"   s      r   r   )ExitStack.callback.<locals>._exit_wrapperk  s    d#d#r   )__wrapped__r   )r   r   r!   r"   r   s    ``` r   r   ExitStack.callbackf  s#    
	$ %-!		- r   c                 x    [        U5      nUR                  nUR                  U5      nU R                  X5        U$ )zEnters the supplied context manager

If successful, also pushes its __exit__ method as a callback and
returns the result of the __enter__ method.
)r   rK   r?   r   )r   r   _cm_type_exitresults        r   enter_contextExitStack.enter_contexts  s;     R=!!##B'2%r   c                 *    U R                  SSS5        g)z$Immediately unwind the context stackN)rK   r   s    r   rZ   ExitStack.close  s    dD$'r   c                     U $ r    r   r   s    r   r?   ExitStack.__enter__  s    r   c                    US   S Ln[         R                  " 5       S   n[        U5      nSnSnU R                  (       a>  U R                  R	                  5       n U" U6 (       a  SnSnSnU R                  (       a  M>  U(       a  [        U5        U=(       a    U$ !   [         R                  " 5       nU" US   US   5        SnUn N\= f)Nr   rB   FT)NNN)rF   rG   r   r   rl   r   )	r   r   received_excr   r   suppressed_excpending_raisecbnew_exc_detailss	            r   rK   ExitStack.__exit__  s    "1~T1 LLN1%	!4Y!? ""%%))+B
.{#%)N$)M"4K """ *;7..."%,,.&q'9;q>J $-s   B *C)r   N)r(   r)   r*   r+   r,   r6   r   r   r   r   r   rZ   r?   rK   r-   r   r   r   r	   r	   5  s4    
'!((/r   r	   c                   >   ^  \ rS rSrSrU 4S jrS rS rS rSr	U =r
$ )r   i  z+Backwards compatibility alias for ExitStackc                 `   > [         R                  " S[        5        [        [        U ]  5         g )Nz*ContextStack has been renamed to ExitStack)r   r   r   superr   r6   )r   r9   s    r   r6   ContextStack.__init__  s"    B(	*lD*,r   c                 $    U R                  U5      $ r    )r   )r   r   s     r   register_exitContextStack.register_exit  s    yy""r   c                 .    U R                   " U/UQ70 UD6$ r    )r   )r   r   r!   r"   s       r   registerContextStack.register  s    }}X5555r   c                 "    U R                  5       $ r    )r   r   s    r   preserveContextStack.preserve  s    ||~r   r   )r(   r)   r*   r+   r,   r6   r   r   r   r-   __classcell__)r9   s   @r   r   r     s    5-
#6 r   )r,   rF   r   collectionsr   	functoolsr   __all__objectr   r/   r   r   r^   r
   r   r   version_inforD   r   r   typesr   r   ImportErrorr4   r	   r   r   r   r   <module>r      s    G 
   = N %v %PE/ EPDf 2<f <$o  o Mv M> ++A.!3 & -
3
"m/ m/`9 y  Is   B8 8CC