
    IgB                     p    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y)z*Patch asyncio to allow nested event loops.    N)contextmanagersuppress)heappopc                     t                t                t                | xs t        j                         } t        |        y)z/Patch asyncio to make its event loop reentrant.N)_patch_asyncio_patch_policy_patch_tornadoasyncioget_event_loop_patch_looploops    A/var/www/openai/venv/lib/python3.12/site-packages/nest_asyncio.pyapplyr      s.    O+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++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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_loop@   sF    ;;$&&(D%{{   r   N)r    r"   	__class__r   )r   policys     r   r   r   =   s%    ! ))+F&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|_        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_foreverN   s[    /5 >>  	  6
   6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   rC   rD   RuntimeErrorresult)r>   futurefrF   s      r   r   z'_patch_loop.<locals>.run_until_completeV   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 ]P  }|s d}y|j!                         }|j                  r&	j#                  | d      }	 |j%                          |L|	| <   R d}y# ||	| <   w w xY w)zu
        Simplified re-implementation of asyncio's _run_once that
        runs handles as they become ready.
        r   iQ N)_ready
_scheduled
_cancelledr   rD   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   rC   z_patch_loop.<locals>._run_onced   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  ]]_F$$ 'NN46	5KKM !,+4
4( #   !,+4
4( -s   +F
F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   rF   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,   r|   _set_coroutine_origin_tracking_debug
_asyncgensset_asyncgen_hooks_asyncgen_firstiter_hook_asyncgen_finalizer_hook)r>   old_agen_hookss     r   rE   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 %srj   r   ntr(   T)r   r+   
isinstancer
   BaseEventLoop
ValueErrortyper?   rG   r   rC   r   _check_runnung
is_runningrp   osname
issubclassProactorEventLooprq   r,   r-   _set_coroutine_wrapperr   r.   r6   r0   r%   )	r   rG   r   rC   r   clsrh   rE   rF   s	         @@@r   r   r   K   s.   'R 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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
   r4   FUTURES)tcs    r   r	   r	      sM    
 CKK'NN	>>+JJ7>>++J ,  r   r   )__doc__r
   asyncio.eventsr    r   r,   rm   
contextlibr   r   heapqr   r   r   r   r   r	   r   r   r   <module>r      s9    0   	 
  / $!N5DN	,r   