
    IVfF                     v    d Z 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d
 Zy)z*Patch asyncio to allow nested event loops.    N)contextmanagersuppress)heappopc                     t                t                t                t                | xs t	        j
                         } t        |        y)z/Patch asyncio to make its event loop reentrant.N)_patch_asyncio_patch_policy_patch_task_patch_tornadoasyncioget_event_loop_patch_looploops    U/var/www/html/software/conda/envs/catlas/lib/python3.12/site-packages/nest_asyncio.pyapplyr      s3    OM+7))+D    c                     ddd} dd}t        t        d      ry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        j                  d	k\  r!|xt        _        xt        _        t        _        | t        _        d
t        _        y)z:Patch asyncio module to use pure Python tasks and futures.F)debugc                $   t        j                         }|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)	r   r   	set_debugensure_futurerun_until_completedonecancelr   CancelledError)mainr   r   tasks       r   runz_patch_asyncio.<locals>.run   s    %%'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 s/   B- B!!B*	-:D'D9	DDDc                 v    t        j                         }|"t        j                         j                         }|S r   )events_get_running_loopget_event_loop_policyr   )
stacklevelr   s     r   _get_event_loopz'_patch_asyncio.<locals>._get_event_loop&   s1    '')<//1@@BDr   _nest_patchedN)      r   r'      r   )r'   	   r   T)r'   )hasattrr   sysversion_infotasks_PyTaskTask_CTaskfutures	_PyFutureFuture_CFuture_current_tasks	all_tasksr!   r%   r   r   r&   )r   r%   s     r   r   r      s    ! 
2 w(
9$MM!!	" 	"w}}+gmm.@ OO%%	& 	&1GOO4J
)#'.}}'9'9'H'H$#MM..88
9$%4	5 	5!6"GK Gr   c                  T    d } t        j                         }| |j                  _        y)z1Patch the policy to always return a patched loop.c                     | j                   j                  ,| j                         }t        |       | j	                  |       | j                   j                  S r   )_local_loopnew_event_loopr   set_event_loop)selfr   s     r   r   z%_patch_policy.<locals>.get_event_loopA   sF    ;;$&&(D%{{   r   N)r!   r#   	__class__r   )r   policys     r   r   r   >   s%    ! ))+F&4F#r   c                     fd}fd}d }t         d        t         d        d }t        | d      ryt        | t        j                        st        d	t        |       z        | j                  }||_        ||_	        ||_
        ||_        ||_        d
|_        t        j                  dk(  xr t!        |t        j"                        |_        t&        j(                  dk  r|j*                  |_        d|_        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)r?   manage_asyncgens
manage_runs    r   run_foreverz _patch_loop.<locals>.run_foreverO   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   rD   rE   RuntimeErrorresult)r?   futurefrG   s      r   r   z'_patch_loop.<locals>.run_until_completeW   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   rE   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   rD   z_patch_loop.<locals>._run_oncee   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   rG   z_patch_loop.<locals>.manage_run   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-   rz   _set_coroutine_origin_tracking_debug
_asyncgensset_asyncgen_hooks_asyncgen_firstiter_hook_asyncgen_finalizer_hook)r?   old_agen_hookss     r   rF   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   r&   NzCan't patch loop of type %sr   ntr)   T)r   r,   
isinstancer   BaseEventLoop
ValueErrortyper@   rH   r   rD   r   _check_runnungrn   osname
issubclassProactorEventLoopro   r-   r.   _set_coroutine_wrapperr}   r&   )r   rH   r   rD   r   clsrF   rG   s         @@r   r   r   L   s    > 5 54 8 8" t_%dG1127$t*DEE
..C!CO/CCM'C'CC
4FJsG,E,EF 
)#-0-G-G*Cr   c                     dfd	} t         j                  }t        |d      ryt        j                  dk\  rifd}fd}|t         j
                  _        |t         j
                  _        t         j
                  j                  |j                  | |_	        d|_        y|j                  |j                  | |_
        d|_        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   )getr<   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&   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   Tr   )r   r1   r,   r-   r.   r/   _enter_task_leave_taskr7   _Task__step_stepr&   )r   r1   r   r   r   r   s       @@r   r	   r	      s    3 <<Dt_%
9$	$	' %/!$.!]]11
$$	
 D ((
JJ	
D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)zo
    If tornado is imported before nest_asyncio, make tornado aware of
    the pure-Python asyncio Future.
    tornador   N)r-   modulestornado.concurrent
concurrentr   r5   FUTURES)tcs    r   r
   r
      sM    
 CKK'NN	>>+JJ7>>++J ,  r   r   )__doc__r   asyncio.eventsr!   r   r-   rk   
contextlibr   r   heapqr   r   r   r   r   r	   r
   r   r   r   <module>r      s?    0   	 
  / $!N5xv!H	,r   