a �DOg0`�@sZdZddlZddlZddlZddlmZddlmZddlm Z m Z gd�Z Gdd�dej �Z Gd d �d ej �ZGd d �d e�ZGd d�d�ZGdd�dee e�ZGdd�dee�Zdd�Zdd�ZGdd�de �ZGdd�de �ZGdd�de�ZGdd�de�ZGdd �d e �ZGd!d"�d"�ZGd#d$�d$ee �ZGd%d&�d&ee�ZGd'd(�d(e �ZdS))z4Utilities for with-statement contexts. See PEP 343.�N)�deque��wraps)� MethodType� GenericAlias) �asynccontextmanager�contextmanager�closing� nullcontext�AbstractContextManager�AbstractAsyncContextManager�AsyncExitStack�ContextDecorator� ExitStack�redirect_stdout�redirect_stderr�suppressc@s:eZdZdZee�Zdd�Zej dd��Z edd��Z dS) r z,An abstract base class for context managers.cCs|S�z0Return `self` upon entering the runtime context.���selfrr�"/usr/lib64/python3.9/contextlib.py� __enter__sz AbstractContextManager.__enter__cCsdS�z9Raise any exception triggered within the runtime context.Nr�r�exc_type� exc_value� tracebackrrr�__exit__szAbstractContextManager.__exit__cCs|turt�|dd�StS)Nrr)r �_collections_abc�_check_methods�NotImplemented��cls�Crrr�__subclasshook__sz'AbstractContextManager.__subclasshook__N) �__name__� __module__� __qualname__�__doc__� classmethodr�__class_getitem__r�abc�abstractmethodrr%rrrrr s r c@s:eZdZdZee�Zdd�Zej dd��Z edd��Z dS) r z9An abstract base class for asynchronous context managers.c�s|Srrrrrr� __aenter__+sz&AbstractAsyncContextManager.__aenter__c�sdSrrrrrr� __aexit__/sz%AbstractAsyncContextManager.__aexit__cCs|turt�|dd�StS)Nr.r/)r rr r!r"rrrr%4s �z,AbstractAsyncContextManager.__subclasshook__N) r&r'r(r)r*rr+r.r,r-r/r%rrrrr %s r c@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. rrrrr� _recreate_cm?s zContextDecorator._recreate_cmcst����fdd��}|S)Ncs:�����|i|��Wd�S1s,0YdS�N)r0��args�kwds��funcrrr�innerLs z(ContextDecorator.__call__..innerr)rr6r7rr5r�__call__KszContextDecorator.__call__N)r&r'r(r)r0r8rrrrr<s rc@s eZdZdZdd�Zdd�ZdS)�_GeneratorContextManagerBasezBShared functionality for @contextmanager and @asynccontextmanager.cCsN||i|��|_||||_|_|_t|dd�}|durDt|�j}||_dS)Nr))�genr6r3r4�getattr�typer))rr6r3r4�docrrr�__init__Vs   z%_GeneratorContextManagerBase.__init__cCs|�|j|j|j�Sr1)� __class__r6r3r4rrrrr0dsz)_GeneratorContextManagerBase._recreate_cmN)r&r'r(r)r>r0rrrrr9Ssr9c@s eZdZdZdd�Zdd�ZdS)�_GeneratorContextManagerz%Helper for @contextmanager decorator.cCs:|`|`|`z t|j�WSty4td�d�Yn0dS�Nzgenerator didn't yield)r3r4r6�nextr:� StopIteration� RuntimeErrorrrrrrrs    z"_GeneratorContextManager.__enter__c Cs |dur6zt|j�Wnty*YdS0td��n�|durD|�}z|j�|||�Wn�ty�}z||uWYd}~Sd}~0ty�}zF||ur�WYd}~dSt|t�r�|j|ur�WYd}~dS�WYd}~nz|j��IdHWnty0YdS0td���n�|durL|�}z|j�|||�IdHWn�ty�}z||uWYd}~Sd}~0t�y�}zJ||ur�WYd}~dSt|ttf�r�|j|ur�WYd}~dS�WYd}~n): try: yield finally: This makes this: with some_generator() as : equivalent to this: try: = finally: cs t�||�Sr1)r@r2�r6rr�helperszcontextmanager..helperr�r6rSrrRrr�srcst���fdd��}|S)a�@asynccontextmanager decorator. Typical usage: @asynccontextmanager async def some_async_generator(): try: yield finally: This makes this: async with some_async_generator() as : equivalent to this: try: = finally: cs t�||�Sr1)rNr2rRrrrS&sz#asynccontextmanager..helperrrTrrRrr src@s(eZdZdZdd�Zdd�Zdd�ZdS) r a2Context to automatically close something at the end of a block. Code like this: with closing(.open()) as f: is equivalent to this: f = .open() try: finally: f.close() cCs ||_dSr1��thing)rrVrrrr>=szclosing.__init__cCs|jSr1rUrrrrr?szclosing.__enter__cGs|j��dSr1)rV�close)r�exc_inforrrrAszclosing.__exit__N�r&r'r(r)r>rrrrrrr ,sr c@s(eZdZdZdd�Zdd�Zdd�ZdS)�_RedirectStreamNcCs||_g|_dSr1)� _new_target� _old_targets)r� new_targetrrrr>Isz_RedirectStream.__init__cCs*|j�tt|j��tt|j|j�|jSr1)r\�appendr;�sys�_stream�setattrr[rrrrrNsz_RedirectStream.__enter__cCstt|j|j���dSr1)rar_r`r\�pop�r�exctype�excinst�exctbrrrrSsz_RedirectStream.__exit__)r&r'r(r`r>rrrrrrrZEsrZc@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�r&r'r(r)r`rrrrrWs rc@seZdZdZdZdS)rzCContext manager for temporarily redirecting stderr to another file.�stderrNrhrrrrrgsrc@s(eZdZdZdd�Zdd�Zdd�ZdS) ra?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 ||_dSr1)� _exceptions)r� exceptionsrrrr>xszsuppress.__init__cCsdSr1rrrrrr{szsuppress.__enter__cCs|duot||j�Sr1)� issubclassrjrcrrrr~s zsuppress.__exit__NrYrrrrrms rc@sbeZdZdZedd��Zedd��Zdd�Zdd �Zd d �Z d d �Z dd�Z dd�Z ddd�Z dS)�_BaseExitStackz.A base class for ExitStack and AsyncExitStack.cCs t||�Sr1�r��cm�cm_exitrrr�_create_exit_wrapper�sz#_BaseExitStack._create_exit_wrappercs���fdd�}|S)Ncs��i���dSr1r�rrM�tb�r3�callbackr4rr� _exit_wrapper�sz8_BaseExitStack._create_cb_wrapper.._exit_wrapperr�rvr3r4rwrrur�_create_cb_wrapper�sz!_BaseExitStack._create_cb_wrappercCs t�|_dSr1)r�_exit_callbacksrrrrr>�sz_BaseExitStack.__init__cCst|��}|j|_t�|_|S)z@Preserve the context stack by transferring it to a new instance.)r<rzr)r� new_stackrrr�pop_all�s z_BaseExitStack.pop_allcCs@t|�}z |j}Wnty.|�|�Yn0|�||�|S)aRegisters a callback with the standard __exit__ method signature. Can suppress exceptions the same way __exit__ method can. Also accepts any object with an __exit__ method (registering a call to the method instead of the object itself). )r<r�AttributeError�_push_exit_callback� _push_cm_exit�r�exit�_cb_type� exit_methodrrr�push�s    z_BaseExitStack.pushcCs(t|�}|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<rrr�rrp�_cm_type�_exit�resultrrr� enter_context�s   z_BaseExitStack.enter_contextcOs,|j|g|�Ri|��}||_|�|�|S)z\Registers an arbitrary callback and arguments. Cannot suppress exceptions. )ry� __wrapped__r~�rrvr3r4rwrrrrv�s z_BaseExitStack.callbackcCs|�||�}|�|d�dS)z;Helper to correctly register callbacks to __exit__ methods.TN)rrr~�rrprqrwrrrr�s z_BaseExitStack._push_cm_exitTcCs|j�||f�dSr1)rzr^)rrv�is_syncrrrr~�sz"_BaseExitStack._push_exit_callbackN)T)r&r'r(r)� staticmethodrrryr>r|r�r�rvrr~rrrrrm�s   rmc@s(eZdZdZdd�Zdd�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. cCs|Sr1rrrrrr�szExitStack.__enter__c s�|ddu}t��d��fdd�}d}d}|jr�|j��\}}|sHJ�z||�r^d}d}d}Wq,t��}||d|d�d}|}Yq,0q,|r�z|dj} |d�Wnty�| |d_�Yn0|o�|S)Nr�cs4|j}|dus||urdS|�ur$q*|}q||_dSr1�� __context__��new_exc�old_exc� exc_context�� frame_excrr�_fix_exception_context�sz2ExitStack.__exit__.._fix_exception_contextFT�NNN�r_rXrzrbr�rI) r� exc_details� received_excr��suppressed_exc� pending_raiser��cb�new_exc_details� fixed_ctxrr�rr�s4        zExitStack.__exit__cCs|�ddd�dS�z%Immediately unwind the context stack.N)rrrrrrWszExitStack.closeN)r&r'r(r)rrrWrrrrr�s 1rc@s`eZdZdZedd��Zedd��Zdd�Zdd �Zd d �Z d d �Z dd�Z dd�Z dd�Z dS)r a�Async context manager for dynamic management of a stack of exit callbacks. For example: async with AsyncExitStack() as stack: connections = [await stack.enter_async_context(get_connection()) for i in range(5)] # All opened connections will automatically be released at the # end of the async with statement, even if attempts to open a # connection later in the list raise an exception. cCs t||�Sr1rnrorrr�_create_async_exit_wrapper-sz)AsyncExitStack._create_async_exit_wrappercs���fdd�}|S)Nc�s��i���IdHdSr1rrsrurrrw3sz>AsyncExitStack._create_async_cb_wrapper.._exit_wrapperrrxrrur�_create_async_cb_wrapper1sz'AsyncExitStack._create_async_cb_wrapperc�s.t|�}|j}|�|�IdH}|�||�|S)z�Enters the supplied async context manager. If successful, also pushes its __aexit__ method as a callback and returns the result of the __aenter__ method. N)r<r/r.�_push_async_cm_exitr�rrr�enter_async_context7s  z"AsyncExitStack.enter_async_contextcCsBt|�}z |j}Wnty0|�|d�Yn0|�||�|S)a#Registers a coroutine function with the standard __aexit__ method signature. Can suppress exceptions the same way __aexit__ method can. Also accepts any object with an __aexit__ method (registering a call to the method instead of the object itself). F)r<r/r}r~r�r�rrr�push_async_exitCs   zAsyncExitStack.push_async_exitcOs.|j|g|�Ri|��}||_|�|d�|S)zfRegisters an arbitrary coroutine function and arguments. Cannot suppress exceptions. F)r�r�r~r�rrr�push_async_callbackUs z"AsyncExitStack.push_async_callbackc�s|�ddd�IdHdSr�)r/rrrr�aclosebszAsyncExitStack.aclosecCs|�||�}|�|d�dS)zLHelper to correctly register coroutine function to __aexit__ method.FN)r�r~r�rrrr�fs z"AsyncExitStack._push_async_cm_exitc�s|Sr1rrrrrr.lszAsyncExitStack.__aenter__c �s�|ddu}t��d��fdd�}d}d}|jr�|j��\}}z0|rP||�}n||�IdH}|rnd}d}d}Wq,t��} || d|d�d}| }Yq,0q,|r�z|dj} |d�Wnty�| |d_�Yn0|o�|S)Nrr�cs4|j}|dus||urdS|�ur$q*|}q||_dSr1r�r�r�rrr�usz8AsyncExitStack.__aexit__.._fix_exception_contextFTr�r�) rr�r�r�r�r�r�r�� cb_suppressr�r�rr�rr/os8         zAsyncExitStack.__aexit__N)r&r'r(r)r�r�r�r�r�r�r�r�r.r/rrrrr s     r c@s*eZdZdZd dd�Zdd�Zdd�ZdS) r aOContext 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 NcCs ||_dSr1�� enter_result)rr�rrrr>�sznullcontext.__init__cCs|jSr1r�rrrrr�sznullcontext.__enter__cGsdSr1r)r�excinforrrr�sznullcontext.__exit__)NrYrrrrr �s r )r)r,r_r� collectionsr� functoolsr�typesrr�__all__�ABCr r �objectrr9r@rNrrr rZrrrrmrr r rrrr�s>   �A �>!!PE