o
    i                     @   sP   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 edZG dd dZ	dS )    N)NestedEventLoopsTc                   @   s:   e Zd ZdZdddZdd Zdeje defdd	Z	d
S )RunnerzSimplified backport of asyncio.Runner from Python 3.11

    Like asyncio.run() but allows multiple calls to the same event loop
    before teardown, and is converts sigints into graceful cancellations
    similar to asyncio.run on Python 3.11+.
    returnc                 C   s2   zt   W t  ty   Y nw t  | _| S N)asyncioget_running_loopRuntimeErrorr   new_event_loop_loop)self r   M/home/ubuntu/.local/lib/python3.10/site-packages/synchronicity/async_utils.py	__enter__   s   

zRunner.__enter__c                 C   sD   | j | j   tjd d dkr| j | j   | j   dS )N   )   	   F)r   run_until_completeshutdown_asyncgenssysversion_infoshutdown_default_executorclose)r   exc_type	exc_value	tracebackr   r   r   __exit__   s
   
zRunner.__exit__coroc              
      s   t  t  k}d_tj|jd  fdd} fdd}d }z|o-ttj	tj
k}|r7ttj	|}W n tyG   |tj	d  Y nw z%zj| W W |r\ttj	| S S  tjym   jdkrlt  w |rxttj	| w w )Nr   )loopc                      s    I d H S r   r   r   )	coro_taskr   r   wrapper_coro.   s   
z Runner.run.<locals>.wrapper_coroc                    s0    j d7  _ j dkrj j d S t )N   )_num_sigintsr   call_soon_threadsafecancelKeyboardInterrupt)signumframer   r   r   r   _sigint_handler5   s
   
z#Runner.run.<locals>._sigint_handler)	threadingcurrent_threadmain_threadr"   r   ensure_futurer   signal	getsignalSIGINTdefault_int_handlerr%   r   CancelledError)r   r   is_main_threadr    r)   original_sigint_handlerhandle_sigintr   r(   r   run(   s6   
z
Runner.runN)r   r   )
__name__
__module____qualname____doc__r   r   typing	Awaitabler   r6   r   r   r   r   r      s
    
	r   )
r   r.   r   r*   r;   synchronicity.exceptionsr   TypeVarr   r   r   r   r   r   <module>   s    
