
    <OOfmh                        U d Z ddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlmZ ddlmZmZ ddlmZmZmZmZmZmZmZmZmZmZ  G d de      Zeeef   Z ed      Z e        Z!ed	   e"d
<   ddZ# ejH                  e#        G d de      Z% G d de%      Z& G d de%      Z'dejP                  dejP                  fdZ)dejP                  dejP                  fdZ*e	jV                  dk(  r e,ed      rejZ                  Z.nej^                  Z. G d de.      Z0 G d d	      Z1 G d dejd                        Z3y)ak  Bridges between the `asyncio` module and Tornado IOLoop.

.. versionadded:: 3.2

This module integrates Tornado with the ``asyncio`` module introduced
in Python 3.4. This makes it possible to combine the two libraries on
the same event loop.

.. deprecated:: 5.0

   While the code in this module is still used, it is now enabled
   automatically when `asyncio` is available, so applications should
   no longer need to refer to this module directly.

.. note::

   Tornado is designed to use a selector-based event loop. On Windows,
   where a proactor-based event loop has been the default since Python 3.8,
   a selector event loop is emulated by running ``select`` on a separate thread.
   Configuring ``asyncio`` to use a selector event loop may improve performance
   of Tornado (but may reduce performance of other ``asyncio``-based libraries
   in the same process).
    Nconvert_yielded)IOLoop_Selectable)
AnyCallableDictListOptionalProtocolSetTupleTypeVarUnionc                       e Zd ZdefdZy)
_HasFilenoreturnc                      y N selfs    Y/var/www/premiumrankchecker/venv/lib/python3.12/site-packages/tornado/platform/asyncio.pyfilenoz_HasFileno.fileno6   s        N)__name__
__module____qualname__intr   r   r   r   r   r   5   s     r   r   _TSelectorThread_selector_loopsr   c                  j   t         D ]{  } | j                  5  d| _        | j                  j                          d d d        	 | j                  j                  d       | j                  b| j                  j                          } t         j                          y # 1 sw Y   cxY w# t        $ r Y Uw xY w)NT   a)
r"   _select_cond_closing_selectornotify_waker_wsendBlockingIOError_threadjoinclear)loops    r   _atexit_callbackr/   C   s       	'%)D"$$&	'	MMt$ <<#
 LL  	' 	'
  		s   "BB&B#	&	B21B2c            
           e Zd Zdej                  deddf fdZd deddfdZde	e
ef   d	ed
   de
ddfdZde	e
ef   de
ddfdZde	e
ef   ddfdZde
de
ddfdZd!dZd!dZdededededef
dZdeddfdZdedededdfdZdedededdfdZdeej6                  j8                     dedef   deddfdZdej6                  j8                  ddfdZ xZ S )"BaseAsyncIOLoopasyncio_loopkwargsr   Nc                     || _         || _        t        t        d      r*t	        |t        j
                        rt        |      | _        i | _        t               | _	        t               | _
        d| _        t        j                  j                         D ]%  }|j                         s	 t        j                  |= ' t        j                  j#                  ||       }|| urt%        d| d|       t'        | P  di | y # t         $ r Y xw xY w)NProactorEventLoopFzIOLoop z& already associated with asyncio loop r   )r2   selector_loophasattrasyncio
isinstancer5   AddThreadSelectorEventLoophandlerssetreaderswritersclosingr   _ioloop_for_asynciocopy	is_closedKeyError
setdefaultRuntimeErrorsuper
initialize)r   r2   r3   r.   existing_loop	__class__s        r   rG   zBaseAsyncIOLoop.initializeY   s   
 ) *7/0Z'336

 "<L!IDMO!$!$ ..335 	D~~2248	 22==lDQ$-(N|n]  	$V$   s   )D	DDall_fdsc                    d| _         t        | j                        D ]9  }| j                  |   \  }}| j                  |       |s)| j	                  |       ; t
        j                  | j                  = | j                  | j                  ur| j                  j                          | j                  j                          y NT)
r?   listr;   remove_handlerclose_fdr   r@   r2   r6   close)r   rJ   fdfileobjhandler_funcs        r   rP   zBaseAsyncIOLoop.close   s    t}}% 	'B$(MM"$5!G\#g&		' &&t'8'89T%6%66$$&!r   rQ   handler.Neventsc                    | j                  |      \  }}|| j                  v rt        d|z        ||f| j                  |<   |t        j                  z  rQ| j
                  j                  || j                  |t        j                         | j                  j                  |       |t        j                  z  rR| j
                  j                  || j                  |t        j                         | j                  j                  |       y y )Nzfd %s added twice)split_fdr;   
ValueErrorr   READr6   
add_reader_handle_eventsr=   addWRITE
add_writerr>   )r   rQ   rT   rV   rR   s        r   add_handlerzBaseAsyncIOLoop.add_handler   s     mmB'G02566$g.bFKK))"d.A.A2v{{SLLR FLL ))"d.A.A2v||TLLR  !r   c                    | j                  |      \  }}|t        j                  z  r`|| j                  vr| j                  j                  || j                  |t        j                         | j                  j                  |       nD|| j                  v r6| j                  j                  |       | j                  j                  |       |t        j                  z  ra|| j                  vrR| j                  j                  || j                  |t        j                         | j                  j                  |       y y || j                  v r7| j                  j                  |       | j                  j                  |       y y r   )rX   r   rZ   r=   r6   r[   r\   r]   remove_readerremover^   r>   r_   remove_writer)r   rQ   rV   rR   s       r   update_handlerzBaseAsyncIOLoop.update_handler   s    mmB'GFKK%""--b$2E2Er6;;W  $T\\!""004##B'FLL %""--b$2E2Er6<<X  $ & T\\!""004##B' "r   c                 t   | j                  |      \  }}|| j                  vry || j                  v r6| j                  j	                  |       | j                  j                  |       || j                  v r6| j                  j                  |       | j                  j                  |       | j                  |= y r   )rX   r;   r=   r6   rb   rc   r>   rd   )r   rQ   rR   s      r   rN   zBaseAsyncIOLoop.remove_handler   s    mmB'GT]]",,R0LL#,,R0LL#MM"r   c                 :    | j                   |   \  }} |||       y r   )r;   )r   rQ   rV   rR   rS   s        r   r\   zBaseAsyncIOLoop._handle_events   s     $b 1Wf%r   c                 8    | j                   j                          y r   )r2   run_foreverr   s    r   startzBaseAsyncIOLoop.start   s    %%'r   c                 8    | j                   j                          y r   )r2   stopr   s    r   rl   zBaseAsyncIOLoop.stop   s     r   whencallbackargsc           	          | j                   j                  t        d|| j                         z
        | j                  t        j                  |g|i |      S )Nr   )r2   
call_latermaxtime_run_callback	functoolspartial)r   rm   rn   ro   r3   s        r   call_atzBaseAsyncIOLoop.call_at   sS       ++4$))+%&h888
 	
r   timeoutc                 $    |j                          y r   )cancel)r   rx   s     r   remove_timeoutzBaseAsyncIOLoop.remove_timeout   s    r   c                 r   	 t        j                         | j                  u r| j                  j                  }n| j                  j                  }	  || j                  t        j                  |g|i |       y # t
        $ r | j                  j                  }Y Mw xY w# t
        $ r Y y t        $ r Y y w xY wr   )
r8   get_running_loopr2   	call_sooncall_soon_threadsaferE   rt   ru   rv   AttributeError)r   rn   ro   r3   r~   s        r   add_callbackzBaseAsyncIOLoop.add_callback   s    	?'')T->->> --77	 --BB		d(()*;*;H*Vt*Vv*VW	  	?))>>I	?
  	  	 		s*   AA; *B  ;BB 	B6+B65B6c                     t        j                  dt               	 | j                  j	                  | j
                  t        j                  |g|i |       y # t        $ r Y y w xY w)Nz&add_callback_from_signal is deprecated)	warningswarnDeprecationWarningr2   r   rt   ru   rv   rE   )r   rn   ro   r3   s       r   add_callback_from_signalz(BaseAsyncIOLoop.add_callback_from_signal   sa     	>@RS	22""I$5$5h$P$P$P  		s   =A 	A&%A&executorfunc.zasyncio.Future[_T]c                 >     | j                   j                  ||g| S r   )r2   run_in_executor)r   r   r   ro   s       r   r   zBaseAsyncIOLoop.run_in_executor  s$     1t  004G$GGr   c                 8    | j                   j                  |      S r   )r2   set_default_executor)r   r   s     r   r   z$BaseAsyncIOLoop.set_default_executor  s      55h??r   Fr   N)!r   r   r   r8   AbstractEventLoopr   rG   boolrP   r   r   r   r   r`   re   rN   r\   rj   rl   floatobjectrw   r{   r   r   r   
concurrentfuturesExecutorr    r   r   __classcell__rI   s   @r   r1   r1   X   s   /%#55/%AD/%	/%b"T "d ""![()!4<Y4G!QT!	!(sK'7!8 (# ($ (&
sK'7!8 
T 
& &c &d &(!



%-

69

EH

	

f  X c S T 0	 	),	8;			H:--667H sBwH 	H
 
H@Z-?-?-H-H @T @r   r1   c                   4     e Zd ZdZdeddf fdZddZ xZS )AsyncIOMainLoopa  ``AsyncIOMainLoop`` creates an `.IOLoop` that corresponds to the
    current ``asyncio`` event loop (i.e. the one returned by
    ``asyncio.get_event_loop()``).

    .. deprecated:: 5.0

       Now used automatically when appropriate; it is no longer necessary
       to refer to this class directly.

    .. versionchanged:: 5.0

       Closing an `AsyncIOMainLoop` now closes the underlying asyncio loop.
    r3   r   Nc                 J    t        |   t        j                         fi | y r   )rF   rG   r8   get_event_loop)r   r3   rI   s     r   rG   zAsyncIOMainLoop.initialize  s    7113>v>r   c                      y r   r   r   s    r   _make_currentzAsyncIOMainLoop._make_current"  s     	r   r   )r   r   r   __doc__r   rG   r   r   r   s   @r   r   r     s     ?3 ?4 ?r   r   c                   R     e Zd ZdZdeddf fdZd
deddf fdZddZdd	Z	 xZ
S )AsyncIOLoopaa  ``AsyncIOLoop`` is an `.IOLoop` that runs on an ``asyncio`` event loop.
    This class follows the usual Tornado semantics for creating new
    ``IOLoops``; these loops are not necessarily related to the
    ``asyncio`` default event loop.

    Each ``AsyncIOLoop`` creates a new ``asyncio.EventLoop``; this object
    can be accessed with the ``asyncio_loop`` attribute.

    .. versionchanged:: 6.2

       Support explicit ``asyncio_loop`` argument
       for specifying the asyncio loop to attach to,
       rather than always creating a new one with the default policy.

    .. versionchanged:: 5.0

       When an ``AsyncIOLoop`` becomes the current `.IOLoop`, it also sets
       the current `asyncio` event loop.

    .. deprecated:: 5.0

       Now used automatically when appropriate; it is no longer necessary
       to refer to this class directly.
    r3   r   Nc                     d| _         d }d|vrt        j                         x|d<   }	 t        |   di | y # t
        $ r ||j                           w xY w)NFr2   r   )
is_currentr8   new_event_looprF   rG   	ExceptionrP   )r   r3   r.   rI   s      r   rG   zAsyncIOLoop.initializeB  sg    ',3,B,B,DDF>"T	G(( 	 

	s	   9 ArJ   c                 ^    | j                   r| j                          t        |   |       y )N)rJ   )r   _clear_currentrF   rP   )r   rJ   rI   s     r   rP   zAsyncIOLoop.closeP  s$    ??!g&r   c                     | j                   s!	 t        j                         | _        d| _         t        j                  | j                         y # t        t
        f$ r
 d | _        Y ?w xY wrL   )r   r8   r   old_asynciorE   AssertionErrorset_event_loopr2   r   s    r   r   zAsyncIOLoop._make_currentU  sZ    (#*#9#9#;  #DOt001 !.1 (#' (s   A A'&A'c                 j    | j                   r't        j                  | j                         d| _         y y )NF)r   r8   r   r   r   s    r   _clear_current_hookzAsyncIOLoop._clear_current_hook^  s)    ??""4#3#34#DO r   r   r   )r   r   r   r   r   rG   r   rP   r   r   r   r   s   @r   r   r   (  s8    23 4 'T 'd '
2$r   r   asyncio_futurec                     | S )zConvert an `asyncio.Future` to a `tornado.concurrent.Future`.

    .. versionadded:: 4.1

    .. deprecated:: 5.0
       Tornado ``Futures`` have been merged with `asyncio.Future`,
       so this method is now a no-op.
    r   )r   s    r   to_tornado_futurer   d  s
     r   tornado_futurec                     t        |       S )as  Convert a Tornado yieldable object to an `asyncio.Future`.

    .. versionadded:: 4.1

    .. versionchanged:: 4.3
       Now accepts any yieldable object, not just
       `tornado.concurrent.Future`.

    .. deprecated:: 5.0
       Tornado ``Futures`` have been merged with `asyncio.Future`,
       so this method is now equivalent to `tornado.gen.convert_yielded`.
    r   )r   s    r   to_asyncio_futurer   p  s     >**r   win32WindowsSelectorEventLoopPolicyc                   H     e Zd ZdZd fdZdej                  f fdZ xZS )AnyThreadEventLoopPolicya  Event loop policy that allows loop creation on any thread.

    The default `asyncio` event loop policy only automatically creates
    event loops in the main threads. Other threads must create event
    loops explicitly or `asyncio.get_event_loop` (and therefore
    `.IOLoop.current`) will fail. Installing this policy allows event
    loops to be created automatically on any thread, matching the
    behavior of Tornado versions prior to 5.0 (or 5.0 on Python 2).

    Usage::

        asyncio.set_event_loop_policy(AnyThreadEventLoopPolicy())

    .. versionadded:: 5.0

    .. deprecated:: 6.2

        ``AnyThreadEventLoopPolicy`` affects the implicit creation
        of an event loop, which is deprecated in Python 3.10 and
        will be removed in a future version of Python. At that time
        ``AnyThreadEventLoopPolicy`` will no longer be useful.
        If you are relying on it, use `asyncio.new_event_loop`
        or `asyncio.run` explicitly in any non-main threads that
        need event loops.
    r   c                 Z    t         |           t        j                  dt        d       y )NzYAnyThreadEventLoopPolicy is deprecated, use asyncio.run or asyncio.new_event_loop instead   )
stacklevel)rF   __init__r   r   r   )r   rI   s    r   r   z!AnyThreadEventLoopPolicy.__init__  s%    0		
r   c                     	 t         |          S # t        $ r& | j                         }| j	                  |       |cY S w xY wr   )rF   r   rE   r   r   )r   r.   rI   s     r   r   z'AnyThreadEventLoopPolicy.get_event_loop  sF    	7)++ 	&&(D%K		s    ,A A r   )	r   r   r   r   r   r8   r   r   r   r   s   @r   r   r     s$    4
 9 9  r   r   c                       e Zd ZdZdZdej                  ddfdZddZde	j                  d   fd	Zdd
ZddZddZddZdee   dee   ddfdZdedeeef   ddfdZdeded   deddfdZdeded   deddfdZdedefdZdedefdZy)r!   aq  Define ``add_reader`` methods to be called in a background select thread.

    Instances of this class start a second thread to run a selector.
    This thread is completely hidden from the user;
    all callbacks are run on the wrapped event loop's thread.

    Typically used via ``AddThreadSelectorEventLoop``,
    but can be attached to a running asyncio loop.
    F	real_loopr   Nc                 &    | _         t        j                          _        d  _        d _        d  _         j                          _        d fd j                   j                   fd       i  _
        i  _        t        j                         \   _         _         j                  j!                  d        j                  j!                  d       t"        j%                           j'                   j                   j(                         y )NFc                  V   K    j                   j                          d {    y 7 wr   )_thread_manager_handle	__anext__r   s   r   thread_manager_anextz5SelectorThread.__init__.<locals>.thread_manager_anext  s!      --77999s   )')c                  D     j                   j                                S r   )
_real_loopcreate_task)r   r   s   r   <lambda>z)SelectorThread.__init__.<locals>.<lambda>  s    DOO//0D0FG r   r   )r   	threading	Conditionr%   _select_argsr&   r+   _thread_managerr   r~   _readers_writerssocket
socketpair_waker_rr(   setblockingr"   r]   r[   _consume_waker)r   r   r   s   ` @r   r   zSelectorThread.__init__  s    #%//1  	 "'37&*&:&:&<#	: 	!!G	
 >@=? (.'8'8':$t}!!%(!!%(D!t':':;r   c                    | j                   ry | j                  5  d| _        | j                  j                          d d d        | j	                          | j
                  | j
                  j                          t        j                  |        | j                  | j                         | j                  j                          | j                  j                          d| _         y # 1 sw Y   xY wrL   )_closedr%   r&   r'   _wake_selectorr+   r,   r"   discardrb   r   rP   r(   r   s    r   rP   zSelectorThread.close  s    << 	'%)D"$$&	' 	<<#LL%4==)	' 	's   "C&&C/)NNc                   K   t        j                  dd| j                        | _        | j                  j	                          | j                          	 d  y # t        $ r | j                           w xY ww)NzTornado selectorT)namedaemontarget)r   Thread_run_selectr+   rj   _start_selectGeneratorExitrP   r   s    r   r   zSelectorThread._thread_manager  sk      !''###

 	
	  	JJL	s   AA9A A9A66A9c                 t    | j                   ry 	 | j                  j                  d       y # t        $ r Y y w xY w)Nr$   )r   r(   r)   r*   r   s    r   r   zSelectorThread._wake_selector  s5    <<	MMt$ 		s   + 	77c                 Z    	 | j                   j                  d       y # t        $ r Y y w xY w)Ni   )r   recvr*   r   s    r   r   zSelectorThread._consume_waker  s+    	MMt$ 		s    	**c                 ,   | j                   5  | j                  J t        | j                  j	                               t        | j
                  j	                               f| _        | j                   j                          d d d        y # 1 sw Y   y xY wr   )r%   r   rM   r   keysr   r'   r   s    r   r   zSelectorThread._start_select  sw      	'$$,,,!%dmm&8&8&:!;T$--BTBTBV=W XD$$&	' 	' 	's   A4B

Bc                    	 | j                   5  | j                  ?| j                  s3| j                   j                          | j                  | j                  s3| j                  r
	 d d d        y | j                  J | j                  \  }}d | _        d d d        	 t	        j                  |      \  }}}||z   }	 | j                  j                  | j                  ||       # 1 sw Y   SxY w# t
        $ rr}|j                  t        t        dt        j                        k(  r;t	        j                  | j                  j                         gg g d      \  }}}|rg }n  Y d }~d }~ww xY w# t        $ r Y t        $ r Y w xY w)NWSAENOTSOCKr   )r%   r   r&   waitselectOSErrorerrnogetattrEBADFr   r   r   r   _handle_selectrE   r   )r   to_readto_writerswsxse_s           r   r   zSelectorThread._run_select$  s{   "" )''/8N8N%%**, ''/8N8N))	) )
 ((444$($5$5!$(!)  $]]7HhG
B"W,44T5H5H"bQc ) )4   77ge]EKKHH%}}dmm.B.B.D-Er2qQHB1	 .    !  	sI   AC&C&0$C& C2 >'E0 &C/2	E-;A(E((E-0	F;FFr   r   c                     |D ]  }| j                  || j                           |D ]  }| j                  || j                           | j                          y r   )_handle_eventr   r   r   )r   r   r   rws        r   r   zSelectorThread._handle_selectd  sV      	1Aq$--0	1 	1Aq$--0	1r   rQ   cb_mapc                 <    	 ||   } |        y # t         $ r Y y w xY wr   )rC   )r   rQ   r   rn   s       r   r   zSelectorThread._handle_eventm  s+    
	bzH 	
  		s    	rn   rU   ro   c                 j    t        j                  |g| | j                  |<   | j                          y r   )ru   rv   r   r   r   rQ   rn   ro   s       r   r[   zSelectorThread.add_readerx  .     &--h>>br   c                 j    t        j                  |g| | j                  |<   | j                          y r   )ru   rv   r   r   r   s       r   r_   zSelectorThread.add_writer~  r   r   c                 ^    	 | j                   |= | j                          y# t        $ r Y yw xY wNFT)r   rC   r   r   rQ   s     r   rb   zSelectorThread.remove_reader  8    	b! 	  		     	,,c                 ^    	 | j                   |= | j                          y# t        $ r Y yw xY wr   )r   rC   r   r   s     r   rd   zSelectorThread.remove_writer  r   r  r   )r   r   r   r   r   r8   r   r   rP   typingAsyncGeneratorr   r   r   r   r   r
   _FileDescriptorLiker   r	   r   r   r   r[   r_   r   rb   rd   r   r   r   r!   r!     s*    G <'";";  <  <Dv'<'<Z'H 2'>@*+156I1J			 ((23	 
		%19)1DMP	%19)1DMP	 3   3  r   c                        e Zd ZdZh dZdedef fdZdej                  ddfdZ
dd	Zd
dded   deddfdZd
dded   deddfdZd
ddefdZd
ddefdZ xZS )r:   a  Wrap an event loop to add implementations of the ``add_reader`` method family.

    Instances of this class start a second thread to run a selector.
    This thread is completely hidden from the user; all callbacks are
    run on the wrapped event loop's thread.

    This class is used automatically by Tornado; applications should not need
    to refer to it directly.

    It is safe to wrap any event loop with this class, although it only makes sense
    for event loops that do not implement the ``add_reader`` family of methods
    themselves (i.e. ``WindowsProactorEventLoop``)

    Closing the ``AddThreadSelectorEventLoop`` also closes the wrapped event loop.

    >   rP   	_selectorr   r[   r_   rb   rd   r   r   c                 r    |t         j                  v rt        |   |      S t	        | j
                  |      S r   )r:   MY_ATTRIBUTESrF   __getattribute__r   r   )r   r   rI   s     r   r
  z+AddThreadSelectorEventLoop.__getattribute__  s3    -;;;7+D11t--r   r   Nc                 2    || _         t        |      | _        y r   )r   r!   r  )r   r   s     r   r   z#AddThreadSelectorEventLoop.__init__  s    #'	2r   c                 l    | j                   j                          | j                  j                          y r   )r  rP   r   r   s    r   rP   z AddThreadSelectorEventLoop.close  s"    r   rQ   r  rn   rU   ro   c                 >     | j                   j                  ||g| S r   )r  r[   r   s       r   r[   z%AddThreadSelectorEventLoop.add_reader  "     )t~~((X===r   c                 >     | j                   j                  ||g| S r   )r  r_   r   s       r   r_   z%AddThreadSelectorEventLoop.add_writer  r  r   c                 8    | j                   j                  |      S r   )r  rb   r   s     r   rb   z(AddThreadSelectorEventLoop.remove_reader      ~~++B//r   c                 8    | j                   j                  |      S r   )r  rd   r   s     r   rd   z(AddThreadSelectorEventLoop.remove_writer  r  r   r   )r   r   r   r   r	  strr   r
  r8   r   r   rP   r   r[   r_   r   rb   rd   r   r   s   @r   r:   r:     s    &M.S .S .
3'";"; 3 3 >'>3;I3F>OR>	>
>'>3;I3F>OR>	>
0 5 0$ 00 5 0$ 0r   r:   r   )4r   r8   atexitconcurrent.futuresr   r   ru   r   r   sysr   r  r   tornado.genr   tornado.ioloopr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r  r    r<   r"   __annotations__r/   registerr1   r   r   Futurer   r   platformr7   r   _BasePolicyDefaultEventLoopPolicyr   r!   r   r:   r   r   r   <module>r     sN  0        
    ' .   
 CO, T] *-%& .$   !u@f u@po 09$/ 9$x	gnn 	 	+gnn + +  <<7ww0PQ 88K00K+{ +\\ \~90!:!: 90r   