
    iKj5                     4   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a G d d      Z	 dddd	d
edefdZej                   dk  rd Znej                   dk  rd Znd Zej                   dk  rdddZnddddZdddefdZd Zd Zd Zy)z*Patch asyncio to allow nested event loops.    N)contextmanagersuppress)heappopTc                       e Zd ZdZy)_NestAsyncio2zInternal class of `nest_asyncio2`.
     
    Mainly for holding the original properties to support unapply() and nest_asyncio2.run().
    N)__name__
__module____qualname____doc__     ^/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/nest_asyncio2.pyr   r      s     	r   r   F)run_close_looperror_on_mispatchedr   r   c                    t        |       t                t                | xs
 t               } | t	        |        t
        |z  ay)a|  Patch asyncio to make its event loop reentrant.
    
    - `run_close_loop`: Close the event loop created by `asyncio.run()`, if any.
      See README for details.
    - `error_on_mispatched`:
      - `False` (default): Warn if asyncio is already patched by `nest_asyncio` on Python 3.12+.
      - `True`: Raise `RuntimeError` if asyncio is already patched by `nest_asyncio`.
    r   N)_patch_asyncio_patch_policy_patch_tornado_get_event_loop_patch_loop_run_close_loop)loopr   r   s      r   applyr      s=      ':;O$?$DD~%Or         r   c                  *    t        j                         S N)asyncioget_event_loopr   r   r   r   r   /   s    %%''r   r      r   c                      t        j                         } | *t        j                         }|j                  j                  } | S r   )events_get_running_loopget_event_loop_policy_local_loop)r   policys     r   r   r   2   s9     '')<113F==&&Dr   c                  J    	 t        j                         S # t        $ r Y y w xY wr   )r    r!   RuntimeErrorr   r   r   r   r   ;   s(    	))++ 		s    	"")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r   )	r    r!   	set_debugensure_futurerun_until_completedonecancelr   CancelledError)mainr-   r   tasks       r   runr7   C   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/   B- B!!B*	-:D'D9	DDD)r-   loop_factoryc                   d}d }	 t        j                         }t        |       |j                  |       t        j                  | |      }	 |j                  |      |j                         sC|j                          t        t         j                        5  |j                  |       d d d        |rt        j                  |       |r|j                          S S # t        $ r t        s;t	               }|n|t         j
                  } |       }t        j                  |       n?|4t        j
                         }t	               }t        j                  |       n |       }d}Y Nw xY w# 1 sw Y   xY w# |j                         sP|j                          t        t         j                        5  |j                  |       d d d        n# 1 sw Y   nxY w|rt        j                  |       |r|j                          w w xY w)NFTr   )r    get_running_loopr,   r   r   new_event_loopset_event_loopr   r/   r0   r1   r2   r3   r   r4   close)r5   r-   r8   r<   r=   r   r6   s          r   r7   r7   O   s   	&++-D6 	Du$$T5	**4099;g445++D1 6&&~6

 Q  	& #&(<#+'.'='='>D**40'"113D%4%6N**40'>D!%3	&F 65 99;g445++D1 655&&~6

 sB   C E8 E,B	E)(E),E5	8:H2G	HG2Hr   c                    dd}t        t        d      rFt        t        d      s5| rt        d      t        j                  dk\  rddl}|j                  d       yt        j                  d	k\  rt        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\  r4t        j                  d
k  r!|xt"        _        xt"        _        t        _        t(        t        _        dt        _        t-               t        _        y)z:Patch asyncio module to use pure Python tasks and futures.c                 v    t        j                         }|"t        j                         j                         }|S r   )r%   r&   r'   r!   )
stacklevelr   s     r   r   z'_patch_asyncio.<locals>._get_event_loop   s1    '')<//1@@BDr   _nest_patched_nest_asyncio2z*asyncio is already patched by nest_asyncior   r   NzUasyncio is already patched by nest_asyncio. You may encounter bugs related to asyncio)r      r   r"   r      r   )r   	   r   T)r   )hasattrr    r,   sysversion_infowarningswarntasks_PyTaskTask_CTaskfutures	_PyFutureFuture_CFuture_current_tasks	all_tasksr%   r   r!   r7   rB   r   rC   )r   r   rK   s      r   r   r      sZ    w(w 01""#OPP!!Z/uv 9$)9)9J)FMM!!	" 	"w}}+gmm.@ OO%%	& 	&1GOO4J
)#'.}}'9'9'H'H$#MM..88
9$)9)9J)F%4	5 	5!6"GK G*_Gr   c                      t         j                  dk\  ryd } t         j                  dk  rt        j                         }nt        j                         }| |j
                  _        y)z1Patch the policy to always return a patched loop.r"   Nc                     | j                   j                  ,| j                         }t        |       | j	                  |       | j                   j                  S r   )r(   r)   r<   r   r=   )selfr   s     r   r!   z%_patch_policy.<locals>.get_event_loop   sF    ;;$&&(D%{{   r   )rI   rJ   r%   r'   _get_event_loop_policy	__class__r!   )r!   r*   s     r   r   r      sT     :%! *$--/..0&4F#r   c                    fd}fd}fd}t         d        t         d        d }t        | d      ryt        | t        j                        st        d	t        |       z        | j                  }||_        ||_	        ||_
        ||_        ||_        | j                         rd
nd|_        t        j                   dk(  xr t#        |t        j$                        |_        t(        j*                  dk  r|j,                  |_        t(        j*                  dk\  rt        j0                  j2                  nt        j4                  j2                  d|_        t9               |_        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)rY   manage_asyncgens
manage_runs    r   run_foreverz _patch_loop.<locals>.run_forever   s[    /5 >>  	  6
   65
 s!   	A AAA	AA(c                     |       5  t        j                  ||       }||urd|_        |j                         s.| j	                          | j
                  rn|j                         s.|j                         st        d      | j                  r| j	                          |j                         cd d d        S # 1 sw Y   y xY w)Nr:   Fz+Event loop stopped before Future completed.)	r    r0   _log_destroy_pendingr2   r^   r_   r,   _readyresult)rY   futurefra   s      r   r1   z'_patch_loop.<locals>.run_until_complete   s    %%f48A).&ffh >> ffh 668"AC C {{ 88:/ s   A B;+AB;;C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 ]  }|s d}y|j!                         }|j                  r&t"        j$                  dk  r	j'                  | d      }n!	 t(        j*                  j-                  | d      }	 |j1                          |t"        j$                  dk  r|	| <   t(        j*                  j-                  | |        d}y# t.        $ r d}Y _w xY w# |:t"        j$                  dk  r|	| <   w t(        j*                  j-                  | |       w w xY w)zu
        Simplified re-implementation of asyncio's _run_once that
        runs handles as they become ready.
        r   iQ Nr"   )re   
_scheduled
_cancelledr   r_   minmax_whentime	_selectorselect_process_events_clock_resolutionappendrangelenpopleftrI   rJ   popr    rM   _swap_current_taskKeyError_run)
rY   ready	scheduledtimeout
event_listend_timehandle_	curr_task
curr_taskss
            r   r^   z_patch_loop.<locals>._run_once   s   
 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2 1 ]]_F$$ ##j0 *tT :I)$+MM$D$DT4$P		NKKM !,++j8/8Jt, $MM<<T9M3 #6  $ )$(	) !,++j8/8Jt, $MM<<T9M -s   ? G1 H1G?>G?>I 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_ovr3   	_proactor_unregister)rY   old_thread_idold_running_loopovs       r   ra   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)	rH   rI   r   _set_coroutine_origin_tracking_debug
_asyncgensset_asyncgen_hooks_asyncgen_firstiter_hook_asyncgen_finalizer_hook)rY   old_agen_hookss     r   r`   z%_patch_loop.<locals>.manage_asyncgens7  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.Nr   )rY   s    r   _check_runningz#_patch_loop.<locals>._check_runningI  s    r   rB   NzCan't patch loop of type %sr   r   ntrE   T)r   rH   
isinstancer    BaseEventLoop
ValueErrortyper[   rb   r1   r^   r   _check_runnung
is_runningr   osname
issubclassProactorEventLoopr   rI   rJ   _set_coroutine_wrapperr   rM   rU   rO   rB   r   rC   )	r   rb   r1   r^   r   clsr   r`   ra   s	         @@@r   r   r      s8   42h 5 54 8 8" t_%dG1127$t*DEE
..C!CO/CCM'C'C!%!2AC
4FJsG,E,EF 
)#-0-G-G*y( --.5ll.I.I C&C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)rI   modulestornado.concurrent
concurrentr    rS   FUTURES)tcs    r   r   r   b  sM    
 CKK'NN	>>+JJ7>>++J ,  r   r   )r   r    asyncio.eventsr%   r   rI   r   
contextlibr   r   heapqr   r   r   boolr   rJ   r   r7   r   r   r   r   r   r   r   <module>r      s    0   	 
  / 	 	 
& ! %	& & 	&4 j (
" j   
2 !t /b 38 &-4 &-R54\)~	,r   