Current File : //opt/alt/python35/lib64/python3.5/__pycache__/contextlib.cpython-35.pyc
�q�\�0�@sdZddlZddlmZddlmZddddd d
dgZGdd�de�ZGd
d�de�Z dd�Z
Gdd�de�ZGdd�d�ZGdd �d e�Z
Gdd
�d
e�ZGdd�d�ZGdd�de�ZdS)z4Utilities for with-statement contexts. See PEP 343.�N)�deque)�wraps�contextmanager�closing�ContextDecorator� ExitStack�redirect_stdout�redirect_stderr�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.
�)�selfrr�//opt/alt/python35/lib64/python3.5/contextlib.py�_recreate_cms
zContextDecorator._recreate_cmcs%t����fdd��}|S)Nc
s%�j���||�SWdQRXdS)N)r)�args�kwds)�funcrrr
�inners
z(ContextDecorator.__call__.<locals>.inner)r)rrrr)rrr
�__call__s!zContextDecorator.__call__N)�__name__�
__module__�__qualname__�__doc__rrrrrr
rsc@sFeZdZdZdd�Zdd�Zdd�Zdd �Zd
S)�_GeneratorContextManagerz%Helper for @contextmanager decorator.cCsi|||�|_||||_|_|_t|dd�}|dkr\t|�j}||_dS)Nr)�genrrr�getattr�typer)rrrr�docrrr
�__init__%sz!_GeneratorContextManager.__init__cCs|j|j|j|j�S)N)� __class__rrr)rrrr
r3sz%_GeneratorContextManager._recreate_cmcCs9yt|j�SWn!tk
r4td�d�YnXdS)Nzgenerator didn't yield)�nextr�
StopIteration�RuntimeError)rrrr
� __enter__9s
z"_GeneratorContextManager.__enter__cCs0|dkrEyt|j�Wntk
r5dSYq,Xtd��n�|dkrZ|�}y|jj|||�Wn�tk
r�}z||k SWYdd}~Xn{tk
r�}z7||kr�dS|tkr�|j|kr�dS�WYdd}~Xn%tj�d|krdS�YnXtd��dS)NFzgenerator didn't stop�z#generator didn't stop after throw())rrr r!�throw� __cause__�sys�exc_info)rr�value� traceback�excrrr
�__exit__?s.
z!_GeneratorContextManager.__exit__N)rrrrrrr"r+rrrr
r"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)rrr
�helper�szcontextmanager.<locals>.helper)r)rr,r)rr
rksc@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-rrr
r�szclosing.__init__cCs|jS)N)r-)rrrr
r"�szclosing.__enter__cGs|jj�dS)N)r-�close)rr'rrr
r+�szclosing.__exit__N)rrrrrr"r+rrrr
r�sc@s:eZdZdZdd�Zdd�Zdd�ZdS)�_RedirectStreamNcCs||_g|_dS)N)�_new_target�_old_targets)r�
new_targetrrr
r�s z_RedirectStream.__init__cCs9|jjtt|j��tt|j|j�|jS)N)r1�appendrr&�_stream�setattrr0)rrrr
r"�sz_RedirectStream.__enter__cCs tt|j|jj��dS)N)r5r&r4r1�pop)r�exctype�excinst�exctbrrr
r+�sz_RedirectStream.__exit__)rrrr4rr"r+rrrr
r/�sr/c@seZdZdZdZdS)raAContext 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)rrrrr4rrrr
r�sc@seZdZdZdZdS)r zCContext manager for temporarily redirecting stderr to another file.�stderrN)rrrrr4rrrr
r �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�
exceptionsrrr
r�szsuppress.__init__cCsdS)Nr)rrrr
r"�szsuppress.__enter__cCs|dk ot||j�S)N)�
issubclassr<)rr7r8r9rrr
r+�s
zsuppress.__exit__N)rrrrrr"r+rrrr
r
�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)rrrr
r�szExitStack.__init__cCs+t|��}|j|_t�|_|S)z?Preserve the context stack by transferring it to a new instance)rr?r)r� new_stackrrr
�pop_all�szExitStack.pop_allcs/��fdd�}�|_|j|�dS)z:Helper to correctly register callbacks to __exit__ methodscs
��|�S)Nr)�exc_details)�cm�cm_exitrr
�
_exit_wrappersz.ExitStack._push_cm_exit.<locals>._exit_wrapperN)�__self__�push)rrCrDrEr)rCrDr
�
_push_cm_exits 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+�AttributeErrorr?r3rH)r�exit�_cb_type�exit_methodrrr
rG
s
zExitStack.pushcs2���fdd�}�|_|j|��S)z\Registers an arbitrary callback and arguments.
Cannot suppress exceptions.
cs����dS)Nr)�exc_typer*�tb)r�callbackrrr
rE#sz)ExitStack.callback.<locals>._exit_wrapper)�__wrapped__rG)rrOrrrEr)rrOrr
rOs
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"rH)rrC�_cm_type�_exit�resultrrr
�
enter_context+s
zExitStack.enter_contextcCs|jddd�dS)z$Immediately unwind the context stackN)r+)rrrr
r.8szExitStack.closecCs|S)Nr)rrrr
r"<szExitStack.__enter__c s|ddk }tj�d��fdd�}d}d}xv|jr�|jj�}y"||�rzd}d}d}WqAtj�}||d|d�d}|}YqAXqAW|r�y|dj}|d�Wn"tk
r�||d_�YnX|o|S)Nrr#csLx<|j}||krdS|dks4|�kr5P|}qW||_dS)N)�__context__)�new_exc�old_exc�exc_context)� frame_excrr
�_fix_exception_contextEs
z2ExitStack.__exit__.<locals>._fix_exception_contextFT)NNN)r&r'r?r6rU�
BaseException) rrB�received_excrZ�suppressed_exc�
pending_raise�cb�new_exc_details� fixed_ctxr)rYr
r+?s2
zExitStack.__exit__N)
rrrrrrArHrGrOrTr.r"r+rrrr
r�s
)rr&�collectionsr� functoolsr�__all__�objectrrrrr/rr r
rrrrr
�<module>sI"