� !A?hk��|�dZddlZddlZddlZddlZddlmZddlmZddl m Z m Z gd�Z Gd�dej ��ZGd �d ej ��ZGd �d e��ZGd �de��ZGd�d��ZGd�deee��ZGd�deee��Zd�Zd�ZGd�de��ZGd�de��ZGd�de��ZGd�de��ZGd�d e��ZGd!�d"e��ZGd#�d$��ZGd%�d&ee��ZGd'�d(ee��Z Gd)�d*ee��Z!Gd+�d,e��Z"dS)-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�suppress�aclosing�chdirc�d�eZdZdZee��Zd�Zej d���Z ed���Z dS)r z,An abstract base class for context managers.c��|S�z0Return `self` upon entering the runtime context.���selfs �7/opt/alt/python-internal/lib64/python3.11/contextlib.py� __enter__z AbstractContextManager.__enter__s��� �c��dS�z9Raise any exception triggered within the runtime context.Nr�r�exc_type� exc_value� tracebacks r�__exit__zAbstractContextManager.__exit__s ���trc�N�|turtj|dd��StS)Nrr%)r �_collections_abc�_check_methods�NotImplemented��cls�Cs r�__subclasshook__z'AbstractContextManager.__subclasshook__ s)�� �(� (� (�#�2�1�k�:�N�N� N��rN) �__name__� __module__� __qualname__�__doc__� classmethodr�__class_getitem__r�abc�abstractmethodr%r-rrrr r ss������6�6�#� �L�1�1����� �����������[���rr c�d�eZdZdZee��Zd�Zej d���Z ed���Z dS)r z9An abstract base class for asynchronous context managers.c�� K�|Srrrs r� __aenter__z&AbstractAsyncContextManager.__aenter__-s ����� rc�� K�dSr rr!s r� __aexit__z%AbstractAsyncContextManager.__aexit__1s �����trc�N�|turtj|dd��StS)Nr8r:)r r'r(r)r*s rr-z,AbstractAsyncContextManager.__subclasshook__6s3�� �-� -� -�#�2�1�l�3>�@�@� @��rN) r.r/r0r1r2rr3r8r4r5r:r-rrrr r 'ss������C�C�#� �L�1�1����� �����������[���rr c��eZdZdZd�Zd�ZdS)rzJA base class or mixin that enables context managers to work as decorators.c��|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. rrs r� _recreate_cmzContextDecorator._recreate_cmAs ��� rc�@���t�����fd���}|S)Nc�p������5�|i|��cddd��S#1swxYwYdS�N�r>��args�kwds�funcrs ��r�innerz(ContextDecorator.__call__..innerNs�����"�"�$�$� +� +��t�T�*�T�*�*� +� +� +� +� +� +� +� +� +� +� +� +���� +� +� +� +� +� +s �+�/�/r�rrFrGs`` r�__call__zContextDecorator.__call__Ms9���� �t��� +� +� +� +� +� �� +�� rN�r.r/r0r1r>rIrrrrr>s8������P�P� � � �����rrc��eZdZdZd�Zd�ZdS)�AsyncContextDecoratorzPA base class or mixin that enables async context managers to work as decorators.c��|S)z-Return a recreated instance of self. rrs rr>z"AsyncContextDecorator._recreate_cmXs ��� rc�@���t�����fd���}|S)Nc���K�����4�d{V���|i|���d{V��cddd���d{V��S#1�d{V��swxYwYdSrArBrCs ��rrGz-AsyncContextDecorator.__call__..inner^s�������(�(�*�*� 1� 1� 1� 1� 1� 1� 1� 1�!�T�4�0�4�0�0�0�0�0�0�0�0� 1� 1� 1� 1� 1� 1� 1� 1� 1� 1� 1� 1� 1� 1� 1� 1� 1� 1� 1� 1� 1� 1� 1� 1���� 1� 1� 1� 1� 1� 1s�?� A � A rrHs`` rrIzAsyncContextDecorator.__call__]s9���� �t��� 1� 1� 1� 1� 1� �� 1�� rNrJrrrrLrLUs8������V�V���� ����rrLc��eZdZdZd�Zd�ZdS)�_GeneratorContextManagerBasezBShared functionality for @contextmanager and @asynccontextmanager.c��||i|��|_|||c|_|_|_t |dd��}|�t |��j}||_dS)Nr1)�genrFrDrE�getattr�typer1)rrFrDrE�docs r�__init__z%_GeneratorContextManagerBase.__init__hs]���4��&��&�&���*.��d�'�� �4�9�d�i��d�I�t�,�,�� �;��t�*�*�$�C��� � � rc�N�|�|j|j|j��SrA)� __class__rFrDrErs rr>z)_GeneratorContextManagerBase._recreate_cmvs ���~�~�d�i���D�I�>�>�>rN)r.r/r0r1rWr>rrrrQrQes8������L�L����?�?�?�?�?rrQc��eZdZdZd�Zd�ZdS)�_GeneratorContextManagerz%Helper for @contextmanager decorator.c�t�|`|`|` t|j��S#t $rt d��d�wxYw�Nzgenerator didn't yield)rDrErF�nextrS� StopIteration� RuntimeErrorrs rrz"_GeneratorContextManager.__enter__�sU�� �I�t�y�$�)� C����>�>� !��� C� C� C��7�8�8�d� B� C���s��7c�f�|�T t|j�� td���#|j���wxYw#t$rYdSwxYw|� |��} |j�|||��n�#t$r}||ucYd}~Sd}~wt$rB}||ur ||_Yd}~dSt|t��r|j|ur ||_Yd}~dS�d}~wt$r}||ur�||_Yd}~dSd}~wwxYw td���#|j���wxYw)N�generator didn't stopFz#generator didn't stop after throw()) r^rSr`�closer_�throw� __traceback__� isinstance� __cause__� BaseException�r�typ�valuer$�excs rr%z!_GeneratorContextManager.__exit__�s��� �;� %��T�X����%�&�'>�?�?�?���H�N�N�$�$�$�$����� !� � � ��u�u� �����}������# �����s�E�9�5�5�5�5�� � (� (� (��%�'�'�'�'�'�'�'������ � � ��%�<�<�(1�C�%� �5�5�5�5�5��u�m�4�4�!�� ��.�.�*3�E�'� �5�5�5�5�5������ � � � ��e�#�#��$-��!��u�u�u�u�u����� ���� !�"�#H�I�I�I������ � � � ���sh�A�(�A� A�A�$B� D� B�D� D�! C�2%C�C� D�+ C=�=D�D�D0N)r.r/r0r1rr%rrrr[r[}s=������ 0�/�C�C�C�7!�7!�7!�7!�7!rr[c��eZdZdZd�Zd�ZdS)�_AsyncGeneratorContextManagerz*Helper for @asynccontextmanager decorator.c��K�|`|`|` t|j���d{V��S#t $rt d��d�wxYwr])rDrErF�anextrS�StopAsyncIterationr`rs rr8z(_AsyncGeneratorContextManager.__aenter__�sk���� �I�t�y�$�)� C��t�x���(�(�(�(�(�(� (��!� C� C� C��7�8�8�d� B� C���s�$�?c��K�|�` t|j���d{V�� td���#|j����d{V��wxYw#t$rYdSwxYw|� |��} |j�|||���d{V��n�#t$r}||ucYd}~Sd}~wt$rI}||ur ||_Yd}~dSt|ttf��r|j |ur ||_Yd}~dS�d}~wt$r}||ur�||_Yd}~dSd}~wwxYw td���#|j����d{V��wxYw)NrbFz$generator didn't stop after athrow()) rprSr`�acloserq�athrowrerfr_rgrhris rr:z'_AsyncGeneratorContextManager.__aexit__�s���� �;� ,��D�H�o�o�%�%�%�%�%�%�%�,�&�'>�?�?�?���(�/�/�+�+�+�+�+�+�+�+�+�+����� &� � � ��u�u� �����}������# ��h�o�o�c�5�)�<�<�<�<�<�<�<�<�<�<��%� (� (� (��%�'�'�'�'�'�'�'������ � � ��%�<�<�(1�C�%� �5�5�5�5�5��u�}�6H�&I�J�J�!�� ��.�.�*3�E�'� �5�5�5�5�5������ � � � ��e�#�#��$-��!��u�u�u�u�u����� ���� (�"�#I�J�J�J���h�o�o�'�'�'�'�'�'�'�'�'�'���sh�A�0�!A� A"�!A"�2"B� D�B(�"D�( D�5 C9�,C9�8C9�9 D� D�D�!D0�0!EN)r.r/r0r1r8r:rrrrnrn�s=������ 5�4�C�C�C�7(�7(�7(�7(�7(rrnc�<��t����fd���}|S)a�@contextmanager decorator. Typical usage: @contextmanager def some_generator(): try: yield finally: This makes this: with some_generator() as : equivalent to this: try: = finally: c�&��t�||��SrA)r[�rDrErFs �r�helperzcontextmanager..helper+s���'��d�D�9�9�9rr�rFrxs` rr r s3���6 �4�[�[�:�:�:�:��[�:� �Mrc�<��t����fd���}|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: c�&��t�||��SrA)rnrws �rrxz#asynccontextmanager..helperLs���,�T�4��>�>�>rrrys` rrr1s3���6 �4�[�[�?�?�?�?��[�?� �Mrc�$�eZdZdZd�Zd�Zd�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() c��||_dSrA��thing�rrs rrWzclosing.__init__c� ���� � � rc��|jSrAr~rs rrzclosing.__enter__es ���z�rc�8�|j���dSrA)rrc�r�exc_infos rr%zclosing.__exit__gs�� � �������rN�r.r/r0r1rWrr%rrrr r RsK�������� ����������rr c�$�eZdZdZd�Zd�Zd�ZdS)ra�Async context manager for safely finalizing an asynchronously cleaned-up resource such as an async generator, calling its ``aclose()`` method. Code like this: async with aclosing(.fetch()) as agen: is equivalent to this: agen = .fetch() try: finally: await agen.aclose() c��||_dSrAr~r�s rrWzaclosing.__init__}r�rc��K�|jSrAr~rs rr8zaclosing.__aenter__s�����z�rc��HK�|j����d{V��dSrA)rrsr�s rr:zaclosing.__aexit__�s2�����j���!�!�!�!�!�!�!�!�!�!�!rN)r.r/r0r1rWr8r:rrrrrksK��������"������"�"�"�"�"rrc�$�eZdZdZd�Zd�Zd�ZdS)�_RedirectStreamNc�"�||_g|_dSrA)� _new_target� _old_targets)r� new_targets rrWz_RedirectStream.__init__�s��%�������rc��|j�tt|j����t t|j|j��|jSrA)r��appendrT�sys�_stream�setattrr�rs rrz_RedirectStream.__enter__�sE�� �� � ���d�l�!;�!;�<�<�<���T�\�4�#3�4�4�4���rc�j�tt|j|j�����dSrA)r�r�r�r��pop�r�exctype�excinst�exctbs rr%z_RedirectStream.__exit__�s+����T�\�4�#4�#8�#8�#:�#:�;�;�;�;�;r)r.r/r0r�rWrr%rrrr�r��sF�������G����  � � � <�<�<�<�._exit_wrapper�s��� �H�d� #�d� #� #� #� #� #rr�r�rDrEr�s``` r�_create_cb_wrapperz!_BaseExitStack._create_cb_wrapper�s.����� $� $� $� $� $� $� $��rc�,�t��|_dSrA)r�_exit_callbacksrs rrWz_BaseExitStack.__init__�s��$�w�w����rc�r�t|����}|j|_t��|_|S)z@Preserve the context stack by transferring it to a new instance.)rUr�r)r� new_stacks r�pop_allz_BaseExitStack.pop_all�s0���D��J�J�L�L� �$(�$8� �!�$�w�w����rc��t|��} |j}|�||��n%#t$r|�|��YnwxYw|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). )rUr%� _push_cm_exit�AttributeError�_push_exit_callback�r�exit�_cb_type� exit_methods r�pushz_BaseExitStack.push�su����:�:�� 2�"�+�K� � � �t�[� 1� 1� 1� 1�� � +� +� +� � $� $�T� *� *� *� *� *� +���� � s�/�A�Ac���t|��} |j}|j}n/#t$r"t d|j�d|j�d���d�wxYw||��}|�||��|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. �'�.z6' object does not support the context manager protocolN)rUrr%r�� TypeErrorr/r0r��rr�r+�_enter�_exit�results r� enter_contextz_BaseExitStack.enter_context�s����2�h�h�� S��]�F��L�E�E��� S� S� S��H���H�H��1A�H�H�H�I�I�NR� S� S���������� ���2�u�%�%�%�� s � �,A c�`�|j|g|�Ri|��}||_|�|��|S)z\Registers an arbitrary callback and arguments. Cannot suppress exceptions. )r�� __wrapped__r��rr�rDrEr�s rr�z_BaseExitStack.callback sI�� 0��/��H�4�H�H�H�4�H�H� �%-� �!� � � ��/�/�/��rc�^�|�||��}|�|d��dS)z;Helper to correctly register callbacks to __exit__ methods.TN)r�r��rr�r�r�s rr�z_BaseExitStack._push_cm_exits3���1�1�"�g�>�>� � � � ���5�5�5�5�5rTc�>�|j�||f��dSrA)r�r�)rr��is_syncs rr�z"_BaseExitStack._push_exit_callbacks$�� ��#�#�W�h�$7�8�8�8�8�8rN)T)r.r/r0r1� staticmethodr�r�rWr�r�r�r�r�r�rrrr�r��s�������8�8��'�'��\�'�����\�� '�'�'�������(���& � � �6�6�6� 9�9�9�9�9�9rr�c�$�eZdZdZd�Zd�Zd�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. c��|SrArrs rrzExitStack.__enter__+s��� rc�� �|ddu}tj��d� � fd�}d}d}|jrj|j���\}}|sJ� ||�rd}d}d}n6#tj��}||d|d��d}|}YnxYw|j�j|r2 |dj} |d�#t $r| |d_�wxYw|o|S)Nr�c�F�� |j}|�||urdS|�urn|}�||_dSrA�� __context__��new_exc�old_exc� exc_context� frame_excs �r�_fix_exception_contextz2ExitStack.__exit__.._fix_exception_context4�M��� &�%�1� ��&�+��*@�*@��F��)�+�+��%�� &�#*�G� � � rFT�NNN�r�r�r�r�r�rh) r� exc_details� received_excr��suppressed_exc� pending_raiser��cb�new_exc_details� fixed_ctxr�s @rr%zExitStack.__exit__.sA���"�1�~�T�1� ��L�N�N�1�%� � *� *� *� *� *� ��� ��"� .��.�2�2�4�4�K�G�R��N�N�7� .��2�{�#�5�%)�N�$)�M�"4�K��� .�"%�,�.�.��&�&��q�'9�;�q�>�J�J�J� $� �-� � � �����"� .� � � �(��N�6� �!�!�n�$�� � � � �-6� �A��*�� �����.��.s� A"�"1B�"B7�7Cc�4�|�ddd��dS�z%Immediately unwind the context stack.N)r%rs rrczExitStack.close_s�� � � �d�D�$�'�'�'�'�'rN)r.r/r0r1rr%rcrrrrr sL�����������//�//�//�b(�(�(�(�(rrc�h�eZdZdZed���Zed���Zd�Zd�Zd�Z d�Z d�Z d �Z d �Z d S) ra�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. c�"�t||��SrAr�r�s r�_create_async_exit_wrapperz)AsyncExitStack._create_async_exit_wrapperrr�rc��������fd�}|S)Nc��(�K���i����d{V��dSrArr�s ���rr�z>AsyncExitStack._create_async_cb_wrapper.._exit_wrapperxs4������(�D�)�D�)�)� )� )� )� )� )� )� )� )� )rrr�s``` r�_create_async_cb_wrapperz'AsyncExitStack._create_async_cb_wrappervs.����� *� *� *� *� *� *� *��rc���K�t|��} |j}|j}n/#t$r"t d|j�d|j�d���d�wxYw||���d{V��}|�||��|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. r�r�zC' object does not support the asynchronous context manager protocolN)rUr8r:r�r�r/r0�_push_async_cm_exitr�s r�enter_async_contextz"AsyncExitStack.enter_async_context|s����� �2�h�h�� '��^�F��M�E�E��� '� '� '��U���U�U��1A�U�U�U���"&� '� '�����v�b�z�z�!�!�!�!�!�!�� � � ��U�+�+�+�� s �"�,Ac��t|��} |j}|�||��n&#t$r|�|d��YnwxYw|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)rUr:r�r�r�r�s r�push_async_exitzAsyncExitStack.push_async_exit�sw����:�:�� 8�"�,�K� � $� $�T�;� 7� 7� 7� 7�� � 2� 2� 2� � $� $�T�5� 1� 1� 1� 1� 1� 2���� � s�/� A�Ac�b�|j|g|�Ri|��}||_|�|d��|S)zfRegisters an arbitrary coroutine function and arguments. Cannot suppress exceptions. F)r�r�r�r�s r�push_async_callbackz"AsyncExitStack.push_async_callback�sK�� 6��5�h�N��N�N�N��N�N� �%-� �!� � � ���6�6�6��rc��DK�|�ddd���d{V��dSr�)r:rs rrszAsyncExitStack.aclose�s4�����n�n�T�4��.�.�.�.�.�.�.�.�.�.�.rc�^�|�||��}|�|d��dS)zLHelper to correctly register coroutine function to __aexit__ method.FN)r�r�r�s rr�z"AsyncExitStack._push_async_cm_exit�s5���7�7��G�D�D� � � � ���6�6�6�6�6rc�� K�|SrArrs rr8zAsyncExitStack.__aenter__�s ����� rc���� K�|ddu}tj��d� � fd�}d}d}|jrv|j���\}} |r||�}n ||��d{V��}|rd}d}d}n6#tj��} || d|d��d}| }YnxYw|j�v|r2 |dj} |d�#t $r| |d_�wxYw|o|S)Nrr�c�F�� |j}|�||urdS|�urn|}�||_dSrAr�r�s �rr�z8AsyncExitStack.__aexit__.._fix_exception_context�r�rFTr�r�) rr�r�r�r�r�r�r�� cb_suppressr�r�r�s @rr:zAsyncExitStack.__aexit__�si�����"�1�~�T�1� ��L�N�N�1�%� � *� *� *� *� *� ��� ��"� .��.�2�2�4�4�K�G�R� .��9�"$�"�k�"2�K�K�(*��K�(8�"8�"8�"8�"8�"8�"8�K��5�%)�N�$)�M�"4�K��� .�"%�,�.�.��&�&��q�'9�;�q�>�J�J�J� $� �-� � � ����#�"� .�$ � � �(��N�6� �!�!�n�$�� � � � �-6� �A��*�� �����.��.s�A0�01B#�0C�CN)r.r/r0r1r�r�r�r�r�r�rsr�r8r:rrrrres������� � ��'�'��\�'�����\�� ���$���$ � � �/�/�/�7�7�7� ���3/�3/�3/�3/�3/rrc�2�eZdZdZdd�Zd�Zd�Zd�Zd�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 Nc��||_dSrA�� enter_result)rrs rrWznullcontext.__init__�s��(����rc��|jSrArrs rrznullcontext.__enter__�s ��� � rc��dSrAr�r�excinfos rr%znullcontext.__exit__r�rc��K�|jSrArrs rr8znullcontext.__aenter__s����� � rc�� K�dSrArr s rr:znullcontext.__aexit__s ���� �rrA) r.r/r0r1rWrr%r8r:rrrr r �sn��������)�)�)�)�!�!�!� � � �!�!�!� � � � � rr c�$�eZdZdZd�Zd�Zd�ZdS)rzHNon thread-safe context manager to change the current working directory.c�"�||_g|_dSrA)�path�_old_cwd)rrs rrWzchdir.__init__s���� ��� � � rc��|j�tj����tj|j��dSrA)rr��os�getcwdrrrs rrzchdir.__enter__s6�� � ���R�Y�[�[�)�)�)� ��������rc�\�tj|j�����dSrA)rrrr�r s rr%zchdir.__exit__s&�� ����"�"�$�$�%�%�%�%�%rNr�rrrrr sG������R�R�������&�&�&�&�&rr)#r1r4rr�r'� collectionsr� functoolsr�typesrr�__all__�ABCr r �objectrrLrQr[rnr rr rr�rrrr�rrr rrrr�rs���:�:� � � � � � � � � � � � �����������������*�*�*�*�*�*�*�*� � � �������S�W����,�����#�'����.�����v����. � � � � �F� � � � ?�?�?�?�?�?�?�?�0G!�G!�G!�G!�G!� ���G!�G!�G!�RG(�G(�G(�G(�G(� ���G(�G(�G(�T���B���B�����$����2"�"�"�"�"�*�"�"�"�4<�<�<�<�<�,�<�<�<�$ � � � � �o� � � � �����o���� M�M�M�M�M�%�M�M�M�