
    jB8                     $   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dS )z*Patch asyncio to allow nested event loops.    N)contextmanagersuppress)heappopTc                       e Zd ZdZdS )_NestAsyncio2zInternal class of `nest_asyncio2`.

    Mainly for holding the original properties to support unapply() and nest_asyncio2.run().
    N)__name__
__module____qualname____doc__     _/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/polars/_utils/nest_asyncio.pyr   r   1   s         
 	Dr   r   F)run_close_looperror_on_mispatchedr   r   c                    t          |           t                       t                       | pt                      } | t	          |            t
          |z  adS )ax  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]     ':;;;;OOO$?$$DD~%OOOr         r   c                  (    t          j                    S N)asyncioget_event_loopr   r   r   r   r   T   s    %'''r   r      r   c                  n    t          j                    } | t          j                    }|j        j        } | S r   )events_get_running_loopget_event_loop_policy_local_loop)r   policys     r   r   r   X   s4     '))<133F=&Dr   c                  L    	 t          j                    S # t          $ r Y d S w xY wr   )r    r!   RuntimeErrorr   r   r   r   r   b   s8    	)+++ 	 	 	44	s    
##)debugc                l   t          j                    }|                    |           t          j        |           }	 |                    |          |                                s[|                                 t          t           j                  5  |                    |           d d d            S # 1 swxY w Y   S S # |                                s[|                                 t          t           j                  5  |                    |           d d d            w # 1 swxY w Y   w w xY wr   )	r    r!   	set_debugensure_futurerun_until_completedonecancelr   CancelledError)mainr-   r   tasks       r   runr7   l   s   %''u$T**	2**40099;; 2g455 2 2++D1112 2 2 2 2 2 2 2 2 2 2 2 2 2 2 22499;; 2g455 2 2++D1112 2 2 2 2 2 2 2 2 2 2 2 2 2 2 22sB   C B55B9	<B9	AD3D&D3&D**D3-D*.D3)r-   loop_factoryc                z   d}d }	 t          j                    }n# t          $ r t          s=t	                      }|,|t           j        } |            }t          j        |           nD|6t          j                    }t	                      }t          j        |           n
 |            }d}Y nw xY wt          |           |                    |           t          j	        | |          }	 |
                    |          |                                sZ|                                 t          t           j                  5  |
                    |           d d d            n# 1 swxY w Y   |rt          j        |           |r|                                 S S # |                                sZ|                                 t          t           j                  5  |
                    |           d d d            n# 1 swxY w Y   |rt          j        |           |r|                                 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   y   s   	&+--DD 	& 	& 	& # &&((<#+'.'='<>>D*4000'"133D%4%6%6N*40000'<>>D!%3	&4 	Du$T555	**40099;; 2g455 2 2++D1112 2 2 2 2 2 2 2 2 2 2 2 2 2 2 7&~666 

 99;; 2g455 2 2++D1112 2 2 2 2 2 2 2 2 2 2 2 2 2 2 7&~666 

sS    BB/.B/-F E$$E(	+E(	AH:H5H:HH:H	1H:r   c                    dd}t          t          d          rQt          t          d          s:| rt          d          t          j        dk    rddl}|                    d	           dS t          j        d
k    rt          j        dk     rzt          j        j        xt          _	        xt          j        _
        t          j        _	        t          j        j        xt          _        xt          j        _        t          j        _        t          j        dk     rEt          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          _        dS )z:Patch asyncio module to use pure Python tasks and futures.r   c                 z    t          j                    }|%t          j                                                    }|S r   )r%   r&   r'   r!   )
stacklevelr   s     r   r   z'_patch_asyncio.<locals>._get_event_loop   s4    '))</11@@B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      sx       w(( 
w 011 	" "#OPPP!Z//k   	 9$$)9J)F)FCJ=CXXXw}+gm.@O%	
 	
1GO4J )##'.}'9'H$#M.8
9$$)9J)F)F	
 	
!69O GK G*__Gr   c                      t           j        dk    rdS d } t           j        dk     rt          j                    }nt          j                    }| |j        _        dS )z1Patch the policy to always return a patched loop.r"   Nc                     | j         j        8|                                 }t          |           |                     |           | j         j        S r   )r(   r)   r<   r   r=   )selfr   s     r   r!   z%_patch_policy.<locals>.get_event_loop   sK    ;$&&((D%%%{  r   )rI   rJ   r%   r'   _get_event_loop_policy	__class__r!   )r!   r*   s     r   r   r      se     :%%! ! ! *$$-//.00&4F###r   c                    fd}fd}fd}t           d             t           d             d }t          | d          rdS t          | t          j                  st          d	t          |           z            | j        }||_        ||_	        ||_
        ||_        ||_        |                                 rd
nd|_        t          j        dk    ot#          |t          j                  |_        t(          j        dk     r|j        |_        t(          j        dk    rt          j        j        nt          j        j        d|_        t9                      |_        dS )z Patch loop to make it reentrant.c                      |           5   |           5  	 |                                   | j        rn	 d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   d| _        d S )NTF)	_run_once	_stopping)rY   manage_asyncgens
manage_runs    r   run_foreverz _patch_loop.<locals>.run_forever   s   Z 	 	//55 	 	   >  		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	
 s3   AAAA			AA		AA #A c                     |           5  t          j        ||           }||urd|_        |                                s0|                                  | j        rn|                                0|                                st          d          |                                cd d d            S # 1 swxY w Y   d S )Nr:   Fz+Event loop stopped before Future completed.)r    r0   _log_destroy_pendingr2   r^   r_   r,   result)rY   futurefra   s      r   r1   z'_patch_loop.<locals>.run_until_complete   s	   Z 
	 
	%f4888A).&ffhh    >  ffhh  6688 R"#PQQQ88::
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	s   BB66B:=B:c                    | j         }| j        }|r+|d         j        rt          |           |r|d         j        |s| j        rdnA|r>t          t          |d         j        |                                 z
  d          d          nd}| j	        
                    |          }|                     |           |                                 | j        z   }|rH|d         j        |k     r7t          |          }|                    |           |r|d         j        |k     7t          t          |                    D ]
}|s n|                                }|j        st"          j        dk     r	                    | d          }n4	 t(          j                            | d          }n# t.          $ r d}Y nw xY w	 |                                 |6t"          j        dk     r|	| <   t(          j                            | |           # |7t"          j        dk     r|	| <   w t(          j                            | |           w w xY wd}dS )zu
        Simplified re-implementation of asyncio's _run_once that
        runs handles as they become ready.
        r   iQ Nr"   )_ready
_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   
 O	 	IaL3 	I  	IaL3 	
 AA S1+diikk91==uEEE 	 ^**733
Z(((99;;!77 	!IaL.99Y''FLL     	!IaL.99 s5zz"" 	N 	NA ]]__F$ N #j00 *tT : :II)$+M$D$DT4$P$P		# ) ) )$(			)	NKKMMM !,+j88/8Jt,, $M<<T9MMM !,+j88/8Jt,, $M<<T9MMMM -!N. s    F''F65F6:H;Ic              3   ~  K   |                                   | j        }t          j                    }	 t	          j                    | _        t          j        |            | xj        dz  c_        | j        r!| j	        | 
                    | j                   dV  || _        t          j        |           | xj        dz  c_        | j        r\| j        dk    rS| j	        N| j	        j        }| j	                                         || j                            |           d| _	        dS dS dS dS # || _        t          j        |           | xj        dz  c_        | j        rZ| j        dk    rO| j	        H| j	        j        }| j	                                         || j                            |           d| _	        w xY 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55	5'133DO$T***""a'""$ <,4NN4#:;;;EEE+DO$%5666""a'""$ 	5*a//1=26B-44666~22266604D---	5 	5//== ,DO$%5666""a'""$ 	5*a//1=26B-44666~22266604D-4444s   A(D. .BF<c              3     K   t          t          d          sd S t          j                    }	 |                     | j                   | j         t          j        | j        | j                   d V  |                     d           | j        t          j        |  d S d S # |                     d           | j        t          j        |  w w xY 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_asyncgens]  s      s011 	F/11	8//<<<*&";";    EEE//666*&7777 +* //666*&777 +s   AB! !-Cc                     dS )z2Do not throw exception if loop is already running.Nr   )rY   s    r   _check_runningz#_patch_loop.<locals>._check_runningp  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      s            4 4 4 4 4l 5 5 ^58 8 8 ^8$   t_%% dG122 E6dCDDD
.C!CO/CCM'C'C!%!2!29AAC7d? zW&0 0C )##-0-G* y(( 	$$\( 
 C&Cr   c                      dt           j        v rGddlm}  t          j        | _        t          j        | j        vr| xj        t          j        fz  c_        dS dS dS )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     sd    
 CK''''''N	>++JJ7>++JJJJ   ,+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  H 1 0        				 



     / / / / / / / /      	 	 	 	 	 	 	 	 
&).E& & &"&&EI& & & &0 j  ( ( ( (
""      j    
2 
2 
2 
2 
2 
2 !t / / / / /d 38 *- *- *-4 *- *- *- *-Z5 5 54X) X) X)v
, 
, 
, 
, 
,r   