
    Χa                     l    d dl Z d dlmZ d dlZd dlZd dlZd dlmZmZ d dl	m
Z
 d	dZd Zd Zd Zd Zy)
    N)contextmanagersuppress)heappopc                    | xs t        j                         } t        | t         j                        st	        dt        |       z        t        | dd      ryt                t        |        t                t                y)z/Patch asyncio to make its event loop reentrant.zCan't patch loop of type %s_nest_patchedN)asyncioget_event_loop
isinstanceBaseEventLoop
ValueErrortypegetattr_patch_asyncio_patch_loop_patch_task_patch_tornadoloops    ./usr/lib/python3/dist-packages/nest_asyncio.pyapplyr   
   sb    +7))+DdG1127$t*DEEt_d+M    c                     ddd} t         j                  dk\  rt        j                  j                  xt        _        xt        j                  _        t        j                  _        t        j                  j                  xt        _	        xt        j                  _
        t        j                  _	        t         j                  dk  rdt        j                  j
                  j                  t        j                  _        t        j                  j
                  j                  t        _        t        t        d      s&t        t        dd      t        _        | t        _        yy)	z
    Patch asyncio module to use pure Python tasks and futures,
    use module level _current_tasks, all_tasks and patch run method.
    F)debugc                   t        j                         }|s4t        j                         }t        j                  |       t	        |       |j                  |       t        j                  |       }	 |j                  |      |j                         sD|j                          t        t        j                        5  |j                  |       d d d        S S # 1 sw Y   S xY w# |j                         sP|j                          t        t        j                        5  |j                  |       d d d        w # 1 sw Y   w xY ww xY wN)events_get_running_loopnew_event_loopset_event_loopr   	set_debugr   ensure_futurerun_until_completedonecancelr   CancelledError)mainr   r   tasks       r   runz_patch_asyncio.<locals>.run   s    '')((*D!!$'u$$T*	2**4099;g445 2++D12 2 2 2 99;g445 2++D12 2 2 s0   2C# ;CC 	#:ED8/	E8E=E)      r   r)      r   	_run_origr(   N)sysversion_infor   tasks_PyTaskTask_CTaskfutures	_PyFutureFuture_CFuture_current_tasks	all_taskshasattrr   r-   r(   )r(   s    r   r   r      s    
 ! 2  9$MM!!	" 	"w}}+gmm.@ OO%%	& 	&1GOO4J
)#'.}}'9'9'H'H$#MM..887K(#GUD9 )r   c                    fd}fd}d }t         d        t         d        d }| j                  }||_        ||_        ||_        ||_        ||_        d|_        d|_        t        j                  d	k(  xr t        |t        j                        |_        t        j                   d
k  r|j"                  |_        yy)z Patch loop to make it reentrant.c                      |       5   |       5  	 | j                          | j                  rn	 d d d        d d d        d| _        y # 1 sw Y   xY w# 1 sw Y   d| _        y xY w)NF)	_run_once	_stopping)selfmanage_asyncgens
manage_runs    r   run_foreverz _patch_loop.<locals>.run_forever=   sk     	/5 	 >>  		 	
 	 	 	
 s!   	A AAA	AA(c                 V    |       5  t        j                  ||       }||urd|_        |j                         s.| j	                          | j
                  rn|j                         s.|j                         st        d      |j                         cd d d        S # 1 sw Y   y xY w)Nr   Fz+Event loop stopped before Future completed.)r   r!   _log_destroy_pendingr#   r=   r>   RuntimeErrorresult)r?   futurefrA   s      r   r"   z'_patch_loop.<locals>.run_until_completeE   s     	%%f48A).&ffh >> ffh 668"AC C88:	 	 	s   A B+*BB(c                    | j                   }| j                  }|r,|d   j                  rt        |       |r|d   j                  r|s| j                  rdn7|r4t        t        |d   j                  | j                         z
  d      d      nd}| j                  j                  |      }| j                  |       | j                         | j                  z   }|rC|d   j                  |k  r1t        |      }|j                  |       |r|d   j                  |k  r1t        t        |            D ]5  }|s d}y|j!                         }|j                  r&|j#                          7 d}y)zu
        Simplified re-implementation of asyncio's _run_once that
        runs handles as they become ready.
        r   iQ N)_ready
_scheduled
_cancelledr   r>   minmax_whentime	_selectorselect_process_events_clock_resolutionappendrangelenpopleft_run)r?   ready	scheduledtimeout
event_listend_timehandle_s           r   r=   z_patch_loop.<locals>._run_onceS   sK   
 OO	IaL33I IaL33 $..A@I S!""TYY[0!56;=	 	
 ^^**73
Z(99;!7!77IaL..9Y'FLL  IaL..9 s5z" 	A  ]]_F$$	 r   c              3   (  K   | j                          | j                  }t        j                         }	 t	        j
                         | _        t        j                  |        | xj                  dz  c_        | j                  r'| j                  | j                  | j                         d || _        t        j                  |       | xj                  dz  c_        | j                  rr| j                  dk(  rb| j                  U| j                  j                  }| j                  j                          || j                  j                  |       d| _	        yyyy# || _        t        j                  |       | xj                  dz  c_        | j                  rr| j                  dk(  rb| j                  U| j                  j                  }| j                  j                          || j                  j                  |       d| _	        w w w w xY ww)zSet up the loop for running.   Nr   )_check_closed
_thread_idr   r   	threading	get_ident_set_running_loop_num_runs_pending_is_proactorloop_self_reading_future	call_soon_loop_self_reading_ovr$   	_proactor_unregister)r?   old_thread_idold_running_loopovs       r   rA   z_patch_loop.<locals>.manage_runr   s     	!335	5'113DO$$T*""a'"$$,,4NN4#:#:;+DO$$%56""a'"$$**a/ 55A2266B--446~222604D- B 0 % ,DO$$%56""a'"$$**a/ 55A2266B--446~222604D- B 0 %s   1HA:E .B0HB1HHc              3     K   t        t        d      sy t        j                         }	 | j                  | j                         | j
                  +t        j                  | j                  | j                         d  | j                  d       | j
                  t        j                  |  y y # | j                  d       | j
                  t        j                  |  w w xY ww)Nget_asyncgen_hooks)	firstiter	finalizerF)	r:   r.   rt   _set_coroutine_origin_tracking_debug
_asyncgensset_asyncgen_hooks_asyncgen_firstiter_hook_asyncgen_finalizer_hook)r?   old_agen_hookss     r   r@   z%_patch_loop.<locals>.manage_asyncgens   s     s01//1
	8//<*&&";;";;= //6*&&7 + //6*&&7 +s   &C%AB0 ?1C%02C""C%c                      y)z2Do not throw exception if loop is already running.N )r?   s    r   _check_runningz#_patch_loop.<locals>._check_running   s    r   Tr   ntr+   N)r   	__class__rB   r"   r=   r   _check_runnungr   rh   osname
issubclassr   ProactorEventLoopri   r.   r/   _set_coroutine_wrapperrw   )r   rB   r"   r=   r   clsr@   rA   s         @@r   r   r   :   s    > 5 54 8 8" ..C!CO/CCM'C'CCCGGtOJ
30I0I J 
)#-0-G-G* $r   c                  ^   dfd	} t         j                  }t        j                  dk\  rbfd}fd}|t         j                  _        |t         j                  _        t         j                  j                  |j                  | |_        y|j                  |j                  | |_	        y)zCPatch the Task's step and enter/leave methods to make it reentrant.Nc                    j                  | j                        }	  | |       |j                  | j                  d        y || j                  <   y # |j                  | j                  d        w || j                  <   w xY wr   )get_looppop)r'   exc	curr_task
curr_tasks	step_origs      r   stepz_patch_task.<locals>.step   st    NN4::.		3dC  tzz40)2
4::&  tzz40)2
4::&s   	A 0Br+   c                     || <   y r   r   r   r'   r   s     r   
enter_taskz_patch_task.<locals>.enter_task   s    #Jtr   c                 *    j                  | d        y r   )r   r   s     r   
leave_taskz_patch_task.<locals>.leave_task   s    NN4&r   r   )
r   r2   r.   r/   r0   _enter_task_leave_taskr8   _Task__step_step)r   r2   r   r   r   r   s       @@r   r   r      s    3 <<D
9$	$	' %/!$.!]]11
$$	((
JJ	
r   c                      dt         j                  v r]ddlm}  t        j
                  | _        t        j
                  | j                  vr%| xj                  t        j
                  fz  c_        yyy)z{
        If tornado is imported before nest_asyncio, make tornado aware of
        the pure-Python asyncio Future.
        tornador   N)r.   modulestornado.concurrent
concurrentr   r6   FUTURES)tcs    r   r   r      sM    
 #+BI~~RZZ/

w~~//
 0 $r   r   )r   asyncio.eventsr   r   r.   re   
contextlibr   r   heapqr   r   r   r   r   r   r   r   r   <module>r      s8      	 
  / DtHnB	0r   