
    yhB                     r    S r SSKrSSKJr  SSKrSSKrSSKrSSKJrJ	r	  SSK
Jr  S
S jrS rS rS rS	 rg)z*Patch asyncio to allow nested event loops.    N)contextmanagersuppress)heappopc                     [        5         [        5         [        5         U =(       d    [        R                  " 5       n [        U 5        g)z/Patch asyncio to make its event loop reentrant.N)_patch_asyncio_patch_policy_patch_tornadoasyncioget_event_loop_patch_looploops    dc:\Users\julio\OneDrive\Documentos\Trabajo\IdeasFrscas\Cabanna\env\Lib\site-packages\nest_asyncio.pyapplyr      s.    O+7))+D    c                  *   SS.S jn SS jn[        [        S5      (       a  g[        R                  S:  a  [        R                  R
                  =[        l        =[        R                  l        [        R                  l        [        R                  R                  =[        l
        =[        R                  l        [        R                  l
        [        R                  S:  ad  [        R                  R                  R                  [        R                  l        [        R                  R                  R                  [        l        [        R                  S	:  a!  U=[        l        =[        l        [        l        U [        l        S
[        l        g)z:Patch asyncio module to use pure Python tasks and futures.F)debugc                P   [         R                  " 5       nUR                  U5        [         R                  " U 5      n UR	                  U5      UR                  5       (       dD  UR                  5         [        [         R                  5         UR	                  U5        S S S 5        $ $ ! , (       d  f       $ = f! UR                  5       (       dU  UR                  5         [        [         R                  5         UR	                  U5        S S S 5        f ! , (       d  f       f = ff = fN)	r
   r   	set_debugensure_futurerun_until_completedonecancelr   CancelledError)mainr   r   tasks       r   run_patch_asyncio.<locals>.run   s    %%'u$$T*	2**4099;;g445++D1 65 55 99;;g445++D1 655 s/   B9 B((
B6	9?D%8D
	D%
D!D%c                 |    [         R                  " 5       nUc#  [         R                  " 5       R                  5       nU$ r   )events_get_running_loopget_event_loop_policyr   )
stacklevelr   s     r   _get_event_loop'_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    S n [         R                  " 5       nXR                  l        g)z1Patch the policy to always return a patched loop.c                     U R                   R                  c,  U R                  5       n[        U5        U R	                  U5        U R                   R                  $ r   )_local_loopnew_event_loopr   set_event_loop)selfr   s     r   r   %_patch_policy.<locals>.get_event_loop@   sF    ;;$&&(D%{{   r   N)r!   r#   	__class__r   )r   policys     r   r   r   =   s#    ! ))+F&4#r   c                   ^^^ UU4S jnU4S jnU4S jn[         S 5       m[         S 5       mS n[        U S5      (       a  g[        U [        R                  5      (       d  [        S	[        U 5      -  5      eU R                  nXl        X%l	        X5l
        XEl        XEl        U R                  5       (       a  S
OSUl        [        R                   S:H  =(       a    [#        U[        R$                  5      Ul        [(        R*                  S:  a  UR,                  Ul        [(        R*                  S:  a  [        R0                  R2                  O[        R4                  R2                  mSUl        g)z Patch loop to make it reentrant.c                    > T" U 5         T" U 5          U R                  5         U R                  (       a  OM$  S S S 5        S S S 5        SU l        g ! , (       d  f       N= f! , (       d  f       N'= f)NF)	_run_once	_stopping)r@   manage_asyncgens
manage_runs    r   run_forever _patch_loop.<locals>.run_foreverN   sP    /5 >>   6
   65s!   	A"&AA"
A	A""
A0c                   > T" U 5         [         R                  " XS9nX!La  SUl        UR                  5       (       d9  U R	                  5         U R
                  (       a  OUR                  5       (       d  M9  UR                  5       (       d  [        S5      eUR                  5       sS S S 5        $ ! , (       d  f       g = f)Nr   Fz+Event loop stopped before Future completed.)r
   r   _log_destroy_pendingr   rF   rG   RuntimeErrorresult)r@   futurefrI   s      r   r   '_patch_loop.<locals>.run_until_completeV   s    %%f8A).&ffhh >> ffhh 6688"AC C88: s   A,B18/B11
B?c                   > U R                   nU R                  nU(       a<  US   R                  (       a(  [        U5        U(       a  US   R                  (       a  M(  U(       d  U R                  (       a  SO<U(       a4  [        [        US   R                  U R                  5       -
  S5      S5      OSnU R                  R                  U5      nU R                  U5        U R                  5       U R                  -   nU(       aK  US   R                  U:  a8  [        U5      nUR                  U5        U(       a  US   R                  U:  a  M8  [        [        U5      5       H^  nU(       d    SngUR!                  5       nUR                  (       a  M1  T	R#                  U S5      n UR%                  5         Ub  UT	U '   M^  M`     Sng! Ub  UT	U '   f f = f)z]
Simplified re-implementation of asyncio's _run_once that
runs handles as they become ready.
r   iQ N)_ready
_scheduled
_cancelledr   rG   minmax_whentime	_selectorselect_process_events_clock_resolutionappendrangelenpopleftpop_run)
r@   ready	scheduledtimeout
event_listend_timehandle_	curr_task
curr_taskss
            r   rF   _patch_loop.<locals>._run_onced   s   
 OO	IaL33I IaL3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$$$ 'NN46	5KKM !,+4
4( - #   !,+4
4( -s   'GGc              3   Z  #    U R                  5         U R                  n[        R                  " 5       n [        R
                  " 5       U l        [        R                  " U 5        U =R                  S-  sl        U R                  (       a(  U R                  c  U R                  U R                  5        Sv   Xl        [        R                  " U5        U =R                  S-  sl        U R                  (       au  U R                  S:X  ad  U R                  bV  U R                  R                  nU R                  R                  5         Ub  U R                  R                  U5        SU l	        gggg! Xl        [        R                  " U5        U =R                  S-  sl        U R                  (       au  U R                  S:X  ad  U R                  bV  U R                  R                  nU R                  R                  5         Ub  U R                  R                  U5        SU l	        f f f f = f7f)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   rI   _patch_loop.<locals>.manage_run   s     	!335	5'113DO$$T*""a'"$$,,4NN4#:#:;+O$$%56""a'"$$**a/ 55A2266B--446~222604D- B 0 % ,O$$%56""a'"$$**a/ 55A2266B--446~222604D- B 0 %s   2H+BE/ 7B8H+/B9H((H+c              3     #    [        [        S5      (       d  g [        R                  " 5       n U R                  U R                  5        U R
                  b)  [        R                  " U R                  U R                  S9  S v   U R                  S5        U R
                  b  [        R                  " U6   g g ! U R                  S5        U R
                  b  [        R                  " U6   f f = f7f)Nget_asyncgen_hooks)	firstiter	finalizerF)	r-   r.   r   _set_coroutine_origin_tracking_debug
_asyncgensset_asyncgen_hooks_asyncgen_firstiter_hook_asyncgen_finalizer_hook)r@   old_agen_hookss     r   rH   %_patch_loop.<locals>.manage_asyncgens   s     s011//1
	8//<*&&";;";;= //6*&&7 + //6*&&7 +s   ,C.AB7 3C.74C++C.c                     g)z2Do not throw exception if loop is already running.N )r@   s    r   _check_running#_patch_loop.<locals>._check_running   s    r   r'   NzCan't patch loop of type %srp   r   ntr*   T)r   r-   
isinstancer
   BaseEventLoop
ValueErrortyperB   rJ   r   rF   r   _check_runnung
is_runningrv   osname
issubclassProactorEventLooprw   r.   r/   _set_coroutine_wrapperr   r0   r8   r2   r'   )	r   rJ   r   rF   r   clsrm   rH   rI   s	         @@@r   r   r   K   s-   'R 5 54 8 8" t_%%dG11227$t*DEE
..C!O/M''!%!2!2AC
4FJsG,E,EF 
)#-0-G-G*y( --.5ll.I.I Cr   c                      S[         R                  ;   a_  SSKJn   [        R
                  U l        [        R
                  U R                  ;  a%  U =R                  [        R
                  4-  sl        ggg)zc
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    
 CKK'NN	>>+JJ7>>++J ,  r   r   )__doc__r
   asyncio.eventsr!   r   r.   rs   
contextlibr   r   heapqr   r   r   r   r   r	   r   r   r   <module>r      s9    0   	 
  / $!N5DN	,r   