Your IP : 216.73.216.236
�
e fw-�@s�dZddlZddlmZddlmZddddd d
gZGdd�de�ZGdd
�d
e�Z dd�Z
Gdd�de�ZGdd �d �ZGdd
�d
�Z
Gdd�de�ZdS)z4Utilities for with-statement contexts. See PEP 343.�N)�deque)�wraps�contextmanager�closing�ContextDecorator� ExitStack�redirect_stdout�suppressc@s.eZdZdZdd�Zdd�ZdS)rzJA base class or mixin that enables context managers to work as decorators.cCs|S)a6Return 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.
�)�selfr
r
�//opt/alt/python34/lib64/python3.4/contextlib.py�_recreate_cms
zContextDecorator._recreate_cmcs%t����fdd��}|S)Nc
s$�j���||�SWdQXdS)N)r
)�args�kwds)�funcrr
r�inners
z(ContextDecorator.__call__.<locals>.inner)r)rrrr
)rrr�__call__s!zContextDecorator.__call__N)�__name__�
__module__�__qualname__�__doc__r
rr
r
r
rrsc@sFeZdZdZdd�Zdd�Zdd�Zdd �Zd
S)�_GeneratorContextManagerz%Helper for @contextmanager decorator.cCsl|||�|_||||_|_|_t|dd�}|dkr_t|�j}n||_dS)Nr)�genrrr�getattr�typer)rrrr�docr
r
r�__init__%sz!_GeneratorContextManager.__init__cCs|j|j|j|j�S)N)� __class__rrr)rr
r
rr
3sz%_GeneratorContextManager._recreate_cmcCs9yt|j�SWn!tk
r4td�d�YnXdS)Nzgenerator didn't yield)�nextr�
StopIteration�RuntimeError)rr
r
r� __enter__9s
z"_GeneratorContextManager.__enter__cCs�|dkrEyt|j�Wntk
r5dSYq�Xtd��n�|dkr]|�}ny&|jj|||�td��WnRtk
r�}z||k SWYdd}~Xn$tj�d|k r��nYnXdS)Nzgenerator didn't stopz#generator didn't stop after throw()�)rrrr �throw�sys�exc_info)rr�value� traceback�excr
r
r�__exit__?s
z!_GeneratorContextManager.__exit__N)rrrrrr
r!r)r
r
r
rr"s
rcs"t���fdd��}|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>
cst�||�S)N)r)rr)rr
r�helper|szcontextmanager.<locals>.helper)r)rr*r
)rrr`sc@s:eZdZdZdd�Zdd�Zdd�ZdS) ra2Context 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()
cCs
||_dS)N)�thing)rr+r
r
rr�szclosing.__init__cCs|jS)N)r+)rr
r
rr!�szclosing.__enter__cGs|jj�dS)N)r+�close)rr%r
r
rr)�szclosing.__exit__N)rrrrrr!r)r
r
r
rr�sc@s:eZdZdZdd�Zdd�Zdd�ZdS) ra@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)
cCs||_g|_dS)N)�_new_target�_old_targets)r�
new_targetr
r
rr�s zredirect_stdout.__init__cCs&|jjtj�|jt_|jS)N)r.�appendr$�stdoutr-)rr
r
rr!�szredirect_stdout.__enter__cCs|jj�t_dS)N)r.�popr$r1)r�exctype�excinst�exctbr
r
rr)�szredirect_stdout.__exit__N)rrrrrr!r)r
r
r
rr�sc@s:eZdZdZdd�Zdd�Zdd�ZdS) 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
cGs
||_dS)N)�_exceptions)r�
exceptionsr
r
rr�szsuppress.__init__cCsdS)Nr
)rr
r
rr!�szsuppress.__enter__cCs|dk ot||j�S)N)�
issubclassr6)rr3r4r5r
r
rr)�s
zsuppress.__exit__N)rrrrrr!r)r
r
r
rr �s c@s�eZdZdZdd�Zdd�Zdd�Zdd �Zd
d�Zdd
�Z dd�Z
dd�Zdd�ZdS)ra�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
cCst�|_dS)N)r�_exit_callbacks)rr
r
rr�szExitStack.__init__cCs+t|��}|j|_t�|_|S)z?Preserve the context stack by transferring it to a new instance)rr9r)r� new_stackr
r
r�pop_all�szExitStack.pop_allcs/��fdd�}�|_|j|�dS)z:Helper to correctly register callbacks to __exit__ methodscs
��|�S)Nr
)�exc_details)�cm�cm_exitr
r�
_exit_wrapper�sz.ExitStack._push_cm_exit.<locals>._exit_wrapperN)�__self__�push)rr=r>r?r
)r=r>r�
_push_cm_exit�s zExitStack._push_cm_exitcCsRt|�}y
|j}Wn"tk
r=|jj|�YnX|j||�|S)aRegisters 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)
)rr)�AttributeErrorr9r0rB)r�exit�_cb_type�exit_methodr
r
rrA�s
zExitStack.pushcs2���fdd�}�|_|j|��S)z\Registers an arbitrary callback and arguments.
Cannot suppress exceptions.
cs����dS)Nr
)�exc_typer(�tb)r�callbackrr
rr?
sz)ExitStack.callback.<locals>._exit_wrapper)�__wrapped__rA)rrIrrr?r
)rrIrrrIs
zExitStack.callbackcCs8t|�}|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.
)rr)r!rB)rr=�_cm_type�_exit�resultr
r
r�
enter_contexts
zExitStack.enter_contextcCs|jddd�dS)z$Immediately unwind the context stackN)r))rr
r
rr,szExitStack.closecCs|S)Nr
)rr
r
rr!#szExitStack.__enter__c s
|ddk }tj�d��fdd�}d}d}xy|jr�|jj�}y%||�r}d}d}d}nWqAtj�}||d|d�d}|}YqAXqAW|ry|dj}|d�Wqtk
r�||d_�YqXn|o|S)Nrr"csOx?|j}||krdS|dks4|�kr8Pn|}qW||_dS)N)�__context__)�new_exc�old_exc�exc_context)� frame_excr
r�_fix_exception_context,s
z2ExitStack.__exit__.<locals>._fix_exception_contextFT)NNN)r$r%r9r2rO�
BaseException) rr<�received_excrT�suppressed_exc�
pending_raise�cb�new_exc_details� fixed_ctxr
)rSrr)&s2
zExitStack.__exit__N)
rrrrrr;rBrArIrNr,r!r)r
r
r
rr�s
)rr$�collectionsr� functoolsr�__all__�objectrrrrrr rr
r
r
r�<module>s>"
?>