o
    v&i\%                     @   sN  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ZddlZddlm	Z	m
Z
mZmZmZ ddlmZ ddlmZmZ ddlmZmZmZmZmZmZmZmZ ddlmZmZ dd	lmZ dd
lm Z  ernddlm!Z! dZ"edZ#G dd dej$Z%eG dd dZ&deddfddZ'deddfddZ(dedej)j*ddfddZ+dS )zTBackported implementation of asyncio.Runner from 3.11 compatible down to Python 3.8.    N)
coroutineseventstasks
exceptionsAbstractEventLoop)Context)TracebackType	FrameType)Callable	CoroutineTypeVarAnyOptionalTypefinalTYPE_CHECKING   )_orig_int_to_enum_patched_int_to_enum)_patch_object)Task)_HANDLER)Runner_Tc                   @   s   e Zd ZdZdZdZdS )_StatecreatedinitializedclosedN)__name__
__module____qualname__CREATEDINITIALIZEDCLOSED r$   r$   e/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/backports/asyncio/runner/runner.pyr   '   s    r   c                   @   s   e Zd ZdZddddee deeg ef  fddZd d	d
Z	de
e dededdfddZd!ddZdefddZdddeeeef dee defddZd!ddZdedee dejddfddZdS )"r   a-  A context manager that controls event loop life cycle.

    The context manager always creates a new event loop,
    allows to run async functions inside it,
    and properly finalizes the loop at the context manager exit.

    If debug is True, the event loop will be run in debug mode.
    If loop_factory is passed, it is used for new event loop creation.

    asyncio.run(main(), debug=True)

    is a shortcut for

    with Runner(debug=True) as runner:
        runner.run(main())

    The run() method can be called multiple times within the runner's context.

    This can be useful for interactive console (e.g. IPython),
    unittest runners, console tools, -- everywhere when async code
    is called from existing sync framework and where the preferred single
    asyncio.run() call doesn't work.

    N)debugloop_factoryr&   r'   c                C   s0   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'   r$   r$   r%   __init__J   s   
zRunner.__init__returnc                 C   s   |    | S N)
_lazy_initr/   r$   r$   r%   	__enter__X   s   zRunner.__enter__exc_typeexc_valexc_tbc                 C   s   |    d S r2   )close)r/   r6   r7   r8   r$   r$   r%   __exit__\   s   zRunner.__exit__c              	   C   s   | j tjurdS z;| j}t| ||  tjdkr$||	  n|t
| W | jr4td |  d| _tj| _ tt_dS | jrLtd |  d| _tj| _ tt_w )zShutdown and close event loop.N)   	   )r(   r   r"   r+   _cancel_all_tasksrun_until_completeshutdown_asyncgenssysversion_infoshutdown_default_executor_shutdown_default_executorr.   r   set_event_loopr9   r#   r   signal_int_to_enum)r/   loopr$   r$   r%   r9   d   s,   



zRunner.closec                 C   s   |    | jS )zReturn embedded event loop.)r3   r+   r4   r$   r$   r%   get_loop|   s   zRunner.get_loopcontextcororJ   c             
      s  t |std|t durtd|    du r!| j t	t
jt
jjjt* t	ttjj fdd | j|}W d   n1 sIw   Y  W d   n1 sXw   Y  t t u rttjtju rtj| j|d}z	ttj| W n ty   d}Y nw d}d| _z?z| j|W W |durttj|u rttjtj S S S  tjy   | jdkrt |dd}|dur| dkrt!  w |durttj|u rttjtj w w w )	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 loopc                      s    S r2   r$   r$   rI   r$   r%   <lambda>       zRunner.run.<locals>.<lambda>)	main_taskr   uncancel)"r   iscoroutine
ValueErrorformatr   _get_running_loopRuntimeErrorr3   r,   r   asyncior   r   r   contextvarscopy_contextr+   create_task	threadingcurrent_threadmain_threadrE   	getsignalSIGINTdefault_int_handler	functoolspartial
_on_sigintr-   r>   r   CancelledErrorgetattrKeyboardInterrupt)r/   rK   rJ   tasksigint_handlerrO   r$   rI   r%   run   s`   
	
z
Runner.runc                 C   s   | j tju r
td| j tju rd S tt_| jd u r,t	
 | _| js+t	| j d| _n|  | _| jd ur=| j| j t | _tj| _ d S )NzRunner is closedT)r(   r   r#   rT   r"   r   rE   rF   r*   r   new_event_loopr+   r.   rD   r)   	set_debugrV   rW   r,   r4   r$   r$   r%   r3      s    




zRunner._lazy_initsignumframerN   c                 C   sB   |  j d7  _ | j dkr| s|  | jdd  d S t )Nr   c                   S   s   d S r2   r$   r$   r$   r$   r%   rL      rM   z#Runner._on_sigint.<locals>.<lambda>)r-   donecancelr+   call_soon_threadsaferd   )r/   rj   rk   rN   r$   r$   r%   ra      s   zRunner._on_sigint)r1   r   )r1   N)r   r   r    __doc__r   boolr
   r   r0   r5   r   BaseExceptionr   r:   r9   rH   r   r   r   r   rg   r3   intr	   rU   r   ra   r$   r$   r$   r%   r   -   sN    





;r   rG   r1   c                 C   st   t | }|s	d S |D ]}|  q| t j|ddi |D ]}| r&q| d ur7| d| |d qd S )Nreturn_exceptionsTz1unhandled exception during asyncio.run() shutdown)message	exceptionre   )r   	all_tasksrm   r>   gather	cancelledru   call_exception_handler)rG   	to_cancelre   r$   r$   r%   r=      s$   

r=   c                    s\   d| _ | jdu rdS |  }tjt| |fd}|  z|I dH  W |  dS |  w )z.Schedule the shutdown of the default executor.TN)targetargs)_executor_shutdown_called_default_executorcreate_futurerY   Thread_do_shutdownstartjoin)rG   futurethreadr$   r$   r%   rC      s   
rC   r   c              
   C   s|   z| j jdd |  s| |jd  W d S W d S  ty= } z|  s2| |j| W Y d }~d S W Y d }~d S d }~ww )NT)wait)r~   shutdown	is_closedrn   
set_result	Exceptionset_exception)rG   r   exr$   r$   r%   r     s   r   ),ro   rU   asyncio.tasksrV   enumr_   rE   r@   rY   r   r   r   r   r   r   typesr   r	   typingr
   r   r   r   r   r   r   r   rF   r   r   _patchr   r   r   __all__r   Enumr   r   r=   rC   futuresFuturer   r$   r$   r$   r%   <module>   s6    ( 5