
    <|h:|                        d Z ddlmZmZ ddlmZ ddlZddlZddlZddl	m
Z
 ddl	mZ ddl	mZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddgZ G d de      Z e       xZZ G d de      Z G d de      ZddZeefZ e!fZ"dZ# G d de      Z$y)z0
Basic loop implementation for ffi-based cores.
    )absolute_importprint_function)dequeN)_dbg)GEVENT_DEBUG_LEVEL)TRACEcallback)PYPY)HubDestroyed)getswitchintervalAbstractLoopassign_standard_callbacksc                       e Zd Zd Zy)_EVENTSTypec                      y)Nzgevent.core.EVENTS selfs    O/var/www/html/test/engine/venv/lib/python3.12/site-packages/gevent/_ffi/loop.py__repr__z_EVENTSType.__repr__   s    #    N)__name__
__module____qualname__r   r   r   r   r   r      s    $r   r   c                       e Zd ZdZd Zy)_DiscardedSetr   c                      y)zDoes nothing.Nr   )r   os     r   discardz_DiscardedSet.discard%       r   N)r   r   r   	__slots__r    r   r   r   r   r   "   s    Ir   r   c                   Z    e Zd Zd Zd Zd Zd Zd Zd Ze	sd Z
nd Z
d	 Zd
 Zd Zd Zy)AbstractCallbacksc                 ^    || _         g | _        t        t        k  r|j                  | _        y y N)ffi	callbacksr   r   from_handle)r   r'   s     r   __init__zAbstractCallbacks.__init__M   s)    %"D &r   c                 <    | j                   j                  |      }|S r&   )r'   r)   )r   handlexs      r   r)   zAbstractCallbacks.from_handleS   s    HH  (r   c                 6   d}d}	 |st        d       y| j                  |      }|j                  }|j                  }|j                  }|t
        }|r|d   t        k(  r	|f|dd z   } |j                  |  |j                  '||j                  j&                  v r|j                  |u ryy#  	  n # t        $ r | j                  |      }Y nw xY wt        j                         |_        |j                  |j                  od}|j                  }|`	 | |j                  |g|  n | j                  |  Y y#  t        dt        j                          t#        j$                          Y Y yxY w|j                  j&                  j)                  |       Y yxY w)	a  
        Returns an integer having one of three values:

        - -1
          An exception occurred during the callback and you must call
          :func:`_python_handle_error` to deal with it. The Python watcher
          object will have the exception tuple saved in ``_exc_info``.
        - 1
          Everything went according to plan. You should check to see if the native
          watcher is still active, and call :func:`python_stop` if it is not. This will
          clean up the memory. Finding the watcher still active at the event loop level,
          but not having stopped itself at the gevent level is a buggy scenario and
          shouldn't happen.
        - 2
          Everything went according to plan, but the watcher has already
          been stopped. Its memory may no longer be valid.

        This function should never return 0, as that's the default value that
        Python exceptions will produce.
        Nzpython_callback got null handle   r   z*WARNING: gevent: Error when handling errorfile   )r   r)   _watcherloopargs_NOARGSGEVENT_CORE_EVENTSr
   UnboundLocalErrorsysexc_info	_exc_info_handlehandle_errorunhandled_onerrorprintstderr	traceback	print_exc_keepalivesetadd)r   r,   reventsorig_ffi_watcher	orig_loopthe_watcherr6   r;   s           r   python_callbackz!AbstractCallbacks.python_callbackW   s   ,  	E	  67**62K*33#((I##D| Q#55{T!"X- K  $'` (;#3#3#A#AA((,<< w-	7$ 7"..v67 %(LLNK!  $$,"**2 &00)* ,.	..{FXF...9 *F"zz+'') **..{;sN   B+ A#B+ +F.B10F1CFCAF%D::1E.+F.(Fc                    t        d|       |sy 	 | j                  |      }|j                  }|` |j                  j                  d g|  	 |j                          y #   |j                  j                  |gt        j                           Y y xY w# 	 j                           Y y #   j                  j                  |gt        j                           Y  Y y xY wxY w)NzHandling error for handle)r   r)   r<   r5   r>   stopr:   r;   )r   r,   _reventswatcherr;   s        r   python_handle_errorz%AbstractCallbacks.python_handle_error   s    (&1	&&v.G((H! &GLL%%d6X6D D)))'CCLLNC	D D)))'CCLLNCs5   <B A 1BC"B*'C"*1CC"C"c                 &   d }|%|j                   j                  j                  d      nd }|r| j                  |      }||j                  j                  d |||       yt        dt        j                         t        j                  |||       y )Nr,   r/   z0WARNING: gevent: Unhandled error with no watcherr0   )tb_framef_localsgetr)   r5   r>   r@   r:   rA   rB   print_exception)r   tvtbrN   r,   s         r   r?   z#AbstractCallbacks.unhandled_onerror   s     79~%%))(34&&v.GLL%%dAq"5 	@::	!!!Q+r   c                     |st        dt        j                         y | j                  |      }|j	                          y )NzWARNING: gevent: Unable to dereference handle; not stopping watcher. Native resources may leak. This is most likely a bug in gevent.r0   )r@   r:   rA   r)   rL   )r   r,   rN   s      r   python_stopzAbstractCallbacks.python_stop   s8    RZZ! ""6*r   c                      y r&   r   r   watcher_ptrs     r   python_check_callbackz'AbstractCallbacks.python_check_callback  s     r   c                 6    	 t               # t         $ r Y y w xY wr&   )MemoryErrorr[   s     r   r]   z'AbstractCallbacks.python_check_callback  s!    !m# s   
 	c                 d    | j                  |      }|t        d|       y |j                          y )NzDWARNING: gevent: running prepare callbacks from a destroyed handle: )_find_loop_from_c_watcherr@   _run_callbacks)r   r\   r5   s      r   python_prepare_callbackz)AbstractCallbacks.python_prepare_callback(  s4    --k:<Xr   c                     d }| j                  |      }|r| j                  |      }||j                  d |||       y |r&   )_find_watcher_ptr_in_tracebackra   r>   )r   rU   rV   rW   r5   r\   s         r   check_callback_onerrorz(AbstractCallbacks.check_callback_onerror0  sN    99"=11+>D dAq"-r   c                     t               r&   NotImplementedErrorr[   s     r   ra   z+AbstractCallbacks._find_loop_from_c_watcher<      !##r   c                 <    ||j                   j                  d   S d S )Nr\   )rQ   rR   )r   rW   s     r   re   z0AbstractCallbacks._find_watcher_ptr_in_traceback?  s     68nr{{##M2N$Nr   N)r   r   r   r*   r)   rJ   rO   r?   rY   r   r]   rc   rf   ra   re   r   r   r   r$   r$   J   sG    /kZ@,6 	"	
$Or   r$   r   c                     ||       |D cg c]  }t        |      dk(  r|n|df }}t        fd|D              }j                  dfj                  dfj                  dfj
                  j                  fj                  j                  ff|z   D ]H  \  }}|xs j                  } | j                  |      |      }j                  j                  |       J S c c}w )a4  
    Given the typical *ffi* and *lib* arguments, and a subclass of :class:`AbstractCallbacks`
    in *callbacks_class*, set up the ``def_extern`` Python callbacks from C
    into an instance of *callbacks_class*.

    :param tuple extras: If given, this is a sequence of ``(name, error_function)``
      additional callbacks to register. Each *name* is an attribute of
      the *callbacks_class* instance. (Each element cas also be just a *name*.)
    :return: The *callbacks_class* instance. This object must be kept alive,
      typically at module scope.
    r2   Nc              3   @   K   | ]  \  }}t        |      |f  y wr&   )getattr).0nameerrorr(   s      r   	<genexpr>z,assign_standard_callbacks.<locals>.<genexpr>R  s!     OuGIt,e4Os   )onerror)lentuplerJ   rO   rY   r]   rf   rc   r?   
def_externr(   append)	r'   libcallbacks_classextrasextrafunc
error_funcr
   r(   s	           @r   r   r   C  s     $IGMNes5zQeUDM9NFNOOOF&&-**D1""D),,i.N.NO..	0P0PQ 	 -z  >9#>#>
53>>*>5d;
 	""8,-& + Os   C&c                   R   e Zd ZdZdZdZdZdZdZdZ	dZ
 e       ZdZd?dZd?dZd Zd Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zed        Zed        Zed        Zed        Z d Z!d Z"d Z#d@dZ$d Z%d Z&d Z'dAdZ(d Z)d Z*d  Z+d! Z,d" Z-ed#        Z.ed$        Z/ed%        Z0ed&        Z1ed'        Z2ed(        Z3dBd)Z4d* Z5dCd+Z6dBd,Z7dBd-Z8dBd.Z9dBd/Z:dBd0Z;dBd1Z<e< e=       d2<   e>j~                  d3k7  rdDd4Z@d5 ZAdCd6ZBdAd7ZCd8 ZDd9 ZEd: ZFd; ZGd< ZHd= ZIed>        ZJy)Er   2   NFc                     || _         || _        d | _        | j                   j                  |       | _        || _        d| _        t               | _        t               | _
        | j                  ||       y NF)_ffi_lib_ptr
new_handle_handle_to_self	_watchers_in_callbackr   
_callbackssetrD   _init_loop_and_aux_watchers)r   r'   rx   watchersflagsdefaults         r   r*   zAbstractLoop.__init__  s`    			#yy33D9!!' U((8r   c                    | j                  ||      | _        | j                  j                  | j                        | _        | j                  | j
                  _        | j                          | j                  j                  | j                        | _
        | j                  | j                  _        | j                          | j                  j                  | j                        | _        | j                  | j                  _        | j                          | j                  d      | _        | j                   j#                  d        y )NF)refc                       y r&   r   r   r   r   <lambda>z:AbstractLoop._init_loop_and_aux_watchers.<locals>.<lambda>  r!   r   )
_init_loopr   r   new_CHECK_POINTER_checkr   data_init_and_start_check_PREPARE_POINTER_prepare_init_and_start_prepare_TIMER_POINTER_timer0_init_callback_timerasync__threadsafe_asyncstartr   r   r   s      r   r   z(AbstractLoop._init_loop_and_aux_watchers  s    OOE73	 iimmD$7$78//""$ 		d&;&;<!11$$& yy}}T%8%89 00!!#!%!7 	$$\2r   c                     t               )zs
        Called by __init__ to create or find the loop. The return value
        is assigned to self._ptr.
        rh   r   s      r   r   zAbstractLoop._init_loop  s    
 "##r   c                     t               r&   rh   r   s    r   r   z"AbstractLoop._init_and_start_check  rj   r   c                     t               r&   rh   r   s    r   r   z$AbstractLoop._init_and_start_prepare  rj   r   c                     t               r&   rh   r   s    r   r   z!AbstractLoop._init_callback_timer  rj   r   c                     t               r&   rh   r   s    r   _stop_callback_timerz!AbstractLoop._stop_callback_timer  rj   r   c                     t               r&   rh   r   s    r   _start_callback_timerz"AbstractLoop._start_callback_timer  rj   r   c                 ,    | j                  d |||       y r&   )r>   )r   rU   rV   rW   s       r   _check_callback_handle_errorz)AbstractLoop._check_callback_handle_error  s    $1b)r   c                 x   d| _         	 | j                  }| j                         }|t               z   }| j	                          | j
                  r| j
                  j                         }|dz  }| j                          |j                  }d |_        |j                  }||_	  ||  d |_	        |dk(  r>| j
                  r2| j                  }| j#                          | j                         |k\  rd}n| j
                  r|dk7  r| j#                          | j
                  r| j%                          d| _         y #  	  | j                  |gt        j                           n?#  	 t        dt        j                         t        j                           n#  Y nxY wY nxY wY xY w# d |_	        w xY w# d| _         w xY w)NTr/   z&Exception while handling another errorr0   r   F)#starting_timer_may_update_loop_timeCALLBACK_CHECK_COUNTnowr   r   r   popleftunrefr
   r6   r>   r:   r;   r@   rA   rB   rC   
update_nowr   )r   countr   
expirationcbr
   r6   s          r   rb   zAbstractLoop._run_callbacks  s    480B	=--E((*C022J%%'//__,,.


;;"ww#t|#dO: #BG
 A:$// 55EOO%xxzZ/g //n ax!**,7<D4a! !)))">s||~>!!!"JQTQ[Q[\%//1! 
 #BG( 8=D4sg   BF0 "D7 'AF0 >1F0 7F!:%E F! F#/FFFFF!F$ $	F--F0 0	F9c                 `    | j                   "| j                   j                          d | _         y y r&   )r   closer   s    r   _stop_aux_watcherszAbstractLoop._stop_aux_watchers%  s-    !!-""((*%)D" .r   c                     | j                   }|rP	 | j                  |      s	 d | _        | `| `| `y| j                          | j                  |       d | _        | `| `| `yy # d | _        | `| `| `w xY w)NFT)ptr_can_destroy_loopr   r   r   rD   r   _destroy_loopr   r   s     r   destroyzAbstractLoop.destroy*  s    hh'--c2  !	(O& '')""3'
 !	(O&  !	(O&s   A  !A   A/c                     t               r&   rh   r   s     r   r   zAbstractLoop._can_destroy_loop=  rj   r   c                     t               r&   rh   r   s     r   r   zAbstractLoop._destroy_loop@  rj   r   c                     | j                   S r&   )r   r   s    r   r   zAbstractLoop.ptrC  s     yyr   c                 .    | j                   j                  S r&   )r   rN   r   s    r   WatcherTypezAbstractLoop.WatcherTypeH  s    ~~%%%r   c                      yNr/   r   r   s    r   MAXPRIzAbstractLoop.MAXPRIL      r   c                      yr   r   r   s    r   MINPRIzAbstractLoop.MINPRIP  r   r   c                     	 t        j                  |      }	 |d|}| j	                  d t
        t        |      d        y #  t        j                          Y BxY w#  t        j                          Y UxY w)Nz: )osstrerrorrB   rC   r>   SystemError)r   messageerrnos      r   _handle_syserrzAbstractLoop._handle_syserrT  sd    	"KK&E	"")51G 	$[-A4H	"!	"!s   A A AA4c                     |t         u r+| j                  j                          | j                          y d }| j                  }|t        |d|      } |||||       y | j                  ||||       y )Nr>   )r   r   clearbreak_error_handlerrn   _default_handle_error)r   contexttypevaluerW   r>   r   s          r   r>   zAbstractLoop.handle_error_  sl    <OO!!#KKM**$"=.-PL$r2&&weR@r   c                 2    t        j                  |||       y r&   )rB   rT   )r   r   r   r   rW   s        r   r   z"AbstractLoop._default_handle_errorn  s     	!!$r2r   c                     t               r&   rh   )r   nowaitonces      r   runzAbstractLoop.runu  rj   r   c                     t               r&   rh   r   s    r   reinitzAbstractLoop.reinitx  rj   r   c                     t               r&   rh   r   s    r   r   zAbstractLoop.ref{  s    !##r   c                     t               r&   rh   r   s    r   r   zAbstractLoop.unref  rj   r   c                     t               r&   rh   )r   hows     r   r   zAbstractLoop.break_  rj   r   c                      y r&   r   r   s    r   verifyzAbstractLoop.verify  s    r   c                     t               r&   rh   r   s    r   r   zAbstractLoop.now  rj   r   c                     t               r&   rh   r   s    r   r   zAbstractLoop.update_now  rj   r   c                 \    dd l }|j                  dt        d       | j                          y )Nr   z('update' is deprecated; use 'update_now'r2   )
stacklevel)warningswarnDeprecationWarningr   )r   r   s     r   updatezAbstractLoop.update  s+    @(!" 	 	$ 	r   c                     d| j                   j                  | j                   j                  t        |       | j	                         fz  S )Nz<%s.%s at 0x%x %s>)	__class__r   r   id_formatr   s    r   r   zAbstractLoop.__repr__  s>    #NN%%NN##tHLLN	'
 
 	
r   c                 6    | j                   r| j                  S dS r   )r   _defaultr   s    r   r   zAbstractLoop.default  s     $t}}3e3r   c                      yNr3   r   r   s    r   	iterationzAbstractLoop.iteration      r   c                      yr   r   r   s    r   depthzAbstractLoop.depth  r   r   c                      yNr   r   r   s    r   backend_intzAbstractLoop.backend_int  r   r   c                      y)Nr   r   r   s    r   backendzAbstractLoop.backend  s    r   c                      yr   r   r   s    r   
pendingcntzAbstractLoop.pendingcnt  r   r   c                 @    | j                   j                  | ||||      S r&   )r   io)r   fdeventsr   prioritys        r   r  zAbstractLoop.io  s    ~~  r63AAr   c                      yr   r   )r   r  s     r   
closing_fdzAbstractLoop.closing_fd  s    r   c                 @    | j                   j                  | ||||      S r&   )r   timer)r   afterrepeatr   r  s        r   r
  zAbstractLoop.timer  s    ~~##D%hGGr   c                 >    | j                   j                  | |||      S r&   )r   signal)r   signumr   r  s       r   r  zAbstractLoop.signal  s    ~~$$T63AAr   c                 <    | j                   j                  | ||      S r&   )r   idler   r   r  s      r   r  zAbstractLoop.idle      ~~""4h77r   c                 <    | j                   j                  | ||      S r&   )r   preparer  s      r   r  zAbstractLoop.prepare  s    ~~%%dC::r   c                 <    | j                   j                  | ||      S r&   )r   checkr  s      r   r  zAbstractLoop.check  s    ~~##D#x88r   c                 <    | j                   j                  | ||      S r&   )r   forkr  s      r   r  zAbstractLoop.fork  r  r   c                 <    | j                   j                  | ||      S r&   )r   r   r  s      r   r   zAbstractLoop.async_  s    ~~$$T399r   asyncwin32c                 >    | j                   j                  | |||      S r&   )r   child)r   pidtracer   s       r   r  zAbstractLoop.child  s    >>''c5#>>r   c                      y r&   r   r   s    r   install_sigchldzAbstractLoop.install_sigchld  s    r   c                 @    | j                   j                  | ||||      S r&   )r   stat)r   pathintervalr   r  s        r   r$  zAbstractLoop.stat  s    ~~""4xhGGr   c                     t        | |      S r&   r	   )r   r  s     r   r
   zAbstractLoop.callback  s    h''r   c                     t               r&   rh   r   s    r   _setup_for_run_callbackz$AbstractLoop._setup_for_run_callback  rj   r   c                 t    t        ||      }| j                  j                  |       | j                          |S r&   )r
   r   rw   r)  r   r|   r6   r   s       r   run_callbackzAbstractLoop.run_callback  s3     dD!r"$$&	r   c                 `     | j                   |g| }| j                  j                          |S r&   )r,  r   sendr+  s       r   run_callback_threadsafez$AbstractLoop.run_callback_threadsafe  s0    Tt+d+##%	r   c                     | j                   }|syd| j                  z   }|dt        |      z   z  }| j                  r|dz  }|d| j                  z  z  }|| j                         z  }|S )N	destroyedzbackend=z ptr=z defaultz pending=%s)r   r   strr   r  _format_details)r   r   msgs      r   r   zAbstractLoop._format  sn    hh4<<'wS!!<<:C}t..t##%%
r   c                     d}| j                         }	 | j                  }||dt        |      z   z  }||dt        |      z   z  }|dt	        t        | j                              z   z  }|S # t        $ r d }Y Yw xY w)N z ref=z fileno=z callbacks=)fileno	activecntAttributeErrorreprr2  rt   r   )r   r4  r7  r8  s       r   r3  zAbstractLoop._format_details  s    	I  7T)_,,C:V,,C 	}s3t#7888
  	I	s   A, ,A:9A:c                      y r&   r   r   s    r   r7  zAbstractLoop.fileno  s    r   c                 2    | j                   st        d      y)Nzoperation on destroyed loopr   )r   
ValueErrorr   s    r   r8  zAbstractLoop.activecnt  s    xx:;;r   )NN)FFr&   )TN)g        TN)r   T)Kr   r   r   r   r   r   r   _TIMER_CALLBACK_SIGr   r   r   r   rD   r   r*   r   r   r   r   r   r   r   r   rb   r   r   r   r   propertyr   r   r   r   r   r>   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r
  r  r  r  r  r  r   localsr:   platformr  r"  r$  r
   r)  r,  r/  r   r3  r7  r8  r   r   r   r   r   q  s$   
 MNN*/' H!OM
9!3L$$$$$$*N=`*
&$$   & &    	IA3$$$$$$$
 4 4          BHB8;98: FHW
||w	?	H($

   r   )r   )%__doc__
__future__r   r   collectionsr   r:   r   rB   gevent._ffir   r   r   gevent._ffi.callbackr
   gevent._compatr   gevent.exceptionsr   geventr   __all__objectr   EVENTSr8   	frozensetr   r$   r   bytesr2  
basestringintinteger_typesr7   r   r   r   r   <module>rR     s    7  
 	   *  )  * $ $& $ *m +	I PvO vOr#N S\
 d6 dr   