
    2\h                         d Z ddlZddlZddlZddlZddlZddlZddlmZ ddlm	Z	 ddlm
Z
 ddlmZ  G d d	ej                  Z G d
 d          ZdddZd ZdS ))Runnerrun    N   )
coroutines)events)
exceptions)tasksc                       e Zd ZdZdZdZdS )_StatecreatedinitializedclosedN)__name__
__module____qualname__CREATEDINITIALIZEDCLOSED     9/opt/python-3.11.14/usr/lib/python3.11/asyncio/runners.pyr   r      s        GKFFFr   r   c                   N    e Zd Z	 ddddZd Zd Zd Zd Zddd	Zd
 Z	d Z
dS )r   N)debugloop_factoryc                |    t           j        | _        || _        || _        d | _        d | _        d| _        d| _        d S )Nr   F)	r   r   _state_debug_loop_factory_loop_context_interrupt_count_set_event_loop)selfr   r   s      r   __init__zRunner.__init__1   s>    n)
 !$r   c                 .    |                                   | S N)
_lazy_initr#   s    r   	__enter__zRunner.__enter__:   s    r   c                 .    |                                   d S r&   )close)r#   exc_typeexc_valexc_tbs       r   __exit__zRunner.__exit__>   s    

r   c                 "   	 | j         t          j        urd S 	 | j        }t	          |           |                    |                                           |                    |                                           | j        rt          j
        d            |                                 d | _        t          j        | _         d S # | j        rt          j
        d            |                                 d | _        t          j        | _         w xY wr&   )r   r   r   r   _cancel_all_tasksrun_until_completeshutdown_asyncgensshutdown_default_executorr"   r   set_event_loopr+   r   )r#   loops     r   r+   zRunner.closeA   s    ,;f000F
	(:Dd#####D$;$;$=$=>>>##D$B$B$D$DEEE# ,%d+++JJLLLDJ -DKKK	 # ,%d+++JJLLLDJ -DK''''s   A$C A	Dc                 :    	 |                                   | j        S r&   )r'   r   r(   s    r   get_loopzRunner.get_loopQ   s    )zr   contextc                   	 t          j        |          s"t          d                    |                    t	          j                    t          d          |                                  || j        }| j	        
                    ||          }t          j                    t          j                    u rxt          j        t          j                  t          j        u rNt%          j        | j        |          }	 t          j        t          j        |           n# t          $ r d }Y nw xY wd }d| _        	 | j	                            |          |Jt          j        t          j                  |u r+t          j        t          j        t          j                   S S S # t.          j        $ r< | j        dk    r/t3          |dd           }| |            dk    rt5                       w xY w# |Jt          j        t          j                  |u r+t          j        t          j        t          j                   w w w xY w)Nz"a coroutine was expected, got {!r}z7Runner.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!   r2   r   CancelledErrorgetattrKeyboardInterrupt)r#   coror:   tasksigint_handlerr=   s         r   r   z
Runner.runV   s8   =%d++ 	PAHHNNOOO#%%1IK K K 	?mGz%%dG%<<$&&)*?*A*AAA //63MMM&.t$OOON&fm^<<<< & & & "&	& "N !	I:0066 *$V]33~EEfmV-GHHHH +E ( 	 	 	$q(("4T::'HHJJ!OO+---	 *$V]33~EEfmV-GHHHH +Es,   ?D D.-D.;F" "AG--G0 0AH?c                    | j         t          j        u rt          d          | j         t          j        u rd S | j        @t          j                    | _        | j	        s t          j
        | j                   d| _	        n|                                 | _        | j        | j                            | j                   t          j                    | _        t          j        | _         d S )NzRunner is closedT)r   r   r   rB   r   r   r   new_event_loopr   r"   r5   r   	set_debugcontextvarscopy_contextr    r(   s    r   r'   zRunner._lazy_init   s    ;&-''1222;&,,,F%.00DJ' , %dj111'+$++--DJ;"J  ---#022(r   c                     | xj         dz  c_         | j         dk    rE|                                s1|                                 | j                            d            d S t                      )Nr   c                      d S r&   r   r   r   r   <lambda>z#Runner._on_sigint.<locals>.<lambda>   s    D r   )r!   donecancelr   call_soon_threadsaferP   )r#   signumframer<   s       r   rM   zRunner._on_sigint   sn    " A%%inn.>.>%J++LL999F!!!r   )r   r   r   r$   r)   r/   r+   r8   r   r'   rM   r   r   r   r   r      s        6 !%4 % % % % %    ( ( (   
 $( +I +I +I +I +IZ) ) )&" " " " "r   r   r   c                    	 t          j                    t          d          t          |          5 }|                    |           cd d d            S # 1 swxY w Y   d S )Nz8asyncio.run() cannot be called from a running event loopra   )r   rA   rB   r   r   )mainr   runners      r   r   r      s    . !!-FH H 	H 
e			  zz$                                   s   AAAc                 b   t          j        |           }|sd S |D ]}|                                 |                     t          j        |ddi           |D ]V}|                                r|                                +|                     d|                                |d           Wd S )Nreturn_exceptionsTz1unhandled exception during asyncio.run() shutdown)message	exceptionrR   )r	   	all_tasksr]   r2   gather	cancelledrh   call_exception_handler)r6   	to_cancelrR   s      r   r1   r1      s    %%I   EL)LtLLMMM  >> 	>>'''N!^^--) )   	 r   )__all__rW   enumrK   rD   rG   sys r   r   r   r	   Enumr   r   r   r1   r   r   r   <module>rs      s&  
               



                            TY   H" H" H" H" H" H" H" H"V           B    r   