� !A?h����dZddlZddlZddlZddlZddlZddlZddlmZddlm Z ddlm Z ddlm Z Gd�d ej ��Z Gd �d ��Zdd �d �Zd�ZdS))�Runner�run�N�)� coroutines)�events)� exceptions)�tasksc��eZdZdZdZdZdS)�_State�created� initialized�closedN)�__name__� __module__� __qualname__�CREATED� INITIALIZED�CLOSED������n�� ��� �)����� ��� � !���$����rc�.�|���|S�N)� _lazy_init�r#s r� __enter__zRunner.__enter__:s�� ������� rc�.�|���dSr&)�close)r#�exc_type�exc_val�exc_tbs r�__exit__zRunner.__exit__>s�� � � � � � � � rc� �|jtjurdS |j}t |��|�|�����|�|�����|jrtj d��|� ��d|_tj |_dS#|jrtj d��|� ��d|_tj |_wxYw)zShutdown and close event loop.N) rr rr�_cancel_all_tasks�run_until_complete�shutdown_asyncgens�shutdown_default_executorr"r�set_event_loopr+r)r#�loops rr+z Runner.closeAs��� �;�f�0� 0� 0� �F� (��:�D� �d� #� #� #� � #� #�D�$;�$;�$=�$=� >� >� >� � #� #�D�$B�$B�$D�$D� E� E� E��#� ,��%�d�+�+�+� �J�J�L�L�L��D�J� �-�D�K�K�K�� �#� ,��%�d�+�+�+� �J�J�L�L�L��D�J� �-�D�K� '� '� '� 's �A$C�A D c�8�|���|jS)zReturn embedded event loop.)r'rr(s r�get_loopzRunner.get_loopQs�� �������z�r��contextc��tj|��s"td�|�����t j���t d���|���|�|j}|j � ||���}tj ��tj ��urxtjtj��tjurNt%j|j|���} tjtj|��n#t$rd}YnwxYwd}d|_ |j �|��|�Jtjtj��|ur+tjtjtj��SSS#t.j$r<|jdkr/t3|dd��}|�|��dkrt5����wxYw#|�Jtjtj��|ur+tjtjtj��wwwxYw)z/Run a coroutine inside the embedded event loop.z"a coroutine was expected, got {!r}Nz7Runner.run() cannot be called from a running event loopr9)� main_taskr�uncancel)r� iscoroutine� ValueError�formatr�_get_running_loop� RuntimeErrorr'r r� create_task� threading�current_thread� main_thread�signal� getsignal�SIGINT�default_int_handler� functools�partial� _on_sigintr!r2r�CancelledError�getattr�KeyboardInterrupt)r#�coror:�task�sigint_handlerr=s rrz Runner.runVs5���%�d�+�+� P��A�H�H��N�N�O�O� O� � #� %� %� 1��I�K�K� K� ������ �?��m�G��z�%�%�d�G�%�<�<�� � $� &� &�)�*?�*A�*A� A� A�� ���/�/�6�3M�M�M�&�.�t��$�O�O�O�N� &�� �f�m�^�<�<�<�<��� &� &� &�"&����  &���� "�N� !��� I��:�0�0��6�6��*��$�V�]�3�3�~�E�E�� �f�m�V�-G�H�H�H�H�+�E���(� � � ��$�q�(�(�"�4��T�:�:���'�H�H�J�J�!�O�O�+�-�-�-� �  ������*��$�V�]�3�3�~�E�E�� �f�m�V�-G�H�H�H�H�+�E���s,�>D� D-�,D-�:F!�!A G,�,G/�/AH>c���|jtjurtd���|jtjurdS|j�@t j��|_|j s t j |j��d|_ n|���|_|j �|j� |j ��tj��|_tj|_dS)NzRunner is closedT)rr rrBrrr�new_event_looprr"r5r� set_debug� contextvars� copy_contextr r(s rr'zRunner._lazy_init�s��� �;�&�-� '� '��1�2�2� 2� �;�&�,� ,� ,� �F� � � %��.�0�0�D�J��'� ,��%�d�j�1�1�1�'+��$���+�+�-�-�D�J� �;� "� �J� � ��� -� -� -�#�0�2�2�� ��(�� � � rc���|xjdz c_|jdkrE|���s1|���|j�d���dSt ���)Nrc��dSr&rrrr�z#Runner._on_sigint..�s��D�r)r!�done�cancelr�call_soon_threadsaferP)r#�signum�framer<s rrMzRunner._on_sigint�sn�� ����"��� � �A� %� %�i�n�n�.>�.>� %� � � � � � � �J� +� +�L�L� 9� 9� 9� �F��!�!�!r) rrr�__doc__r$r)r/r+r8rr'rMrrrrrs���������6!%�4�%�%�%�%�%�������(�(�(� ��� $(�+I�+I�+I�+I�+I�Z)�)�)�&"�"�"�"�"rr�rc���tj���td���t|���5}|�|��cddd��S#1swxYwYdS)a�Execute the coroutine and return the result. This function runs the passed coroutine, taking care of managing the asyncio event loop and finalizing asynchronous generators. This function cannot be called when another asyncio event loop is running in the same thread. If debug is True, the event loop will be run in debug mode. This function always creates a new event loop and closes it at the end. It should be used as a main entry point for asyncio programs, and should ideally only be called once. Example: async def main(): await asyncio.sleep(1) print('hello') asyncio.run(main()) Nz8asyncio.run() cannot be called from a running event looprb)rrArBrr)�mainr�runners rrr�s���0��!�!�-�� F�H�H� H� �e� � � � ���z�z�$��� � � � � � � � � � � � ���� � � � � � s�A�A�Ac�b�tj|��}|sdS|D]}|����|�tj|ddi���|D]V}|���r�|����+|�d|���|d����WdS)N�return_exceptionsTz1unhandled exception during asyncio.run() shutdown)�message� exceptionrR)r � all_tasksr]r2�gather� cancelledri�call_exception_handler)r6� to_cancelrRs rr1r1�s������%�%�I� �������� � � � � � � ����E�L�)�L�t�L�L�M�M�M����� �>�>� � � � � �>�>� � � '� � '� '�N�!�^�^�-�-��)�)� � � �� �r)�__all__rW�enumrKrDrG�sys�rrrr �Enumr rrr1rrr�rts&�� ������ � � � ��������� � � � � � � � ������������������������������T�Y���� H"�H"�H"�H"�H"�H"�H"�H"�V� � � � � �B����r