o
    tPf|                     @   s(  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 deZe  ZZG dd deZG dd deZdddZejd dkr{ee fZ!e"fZ#nddl$Z$e$j!fZ!e"e$j%fZ#dZ&G dd deZ'dS )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                   @   s   e Zd Zdd ZdS )_EVENTSTypec                 C      dS )Nzgevent.core.EVENTS selfr   r   Q/var/www/html/humari/django-venv/lib/python3.10/site-packages/gevent/_ffi/loop.py__repr__      z_EVENTSType.__repr__N)__name__
__module____qualname__r   r   r   r   r   r      s    r   c                   @   s   e Zd ZdZdd ZdS )_DiscardedSetr   c                 C   r   )zDoes nothing.Nr   )r   or   r   r   discard%       z_DiscardedSet.discardN)r   r   r   	__slots__r   r   r   r   r   r   "   s    r   c                   @   sr   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Ze	s#dd Z
ndd Z
dd Zdd Zdd Zdd ZdS )AbstractCallbacksc                 C   s$   || _ g | _ttk r|j| _d S d S N)ffi	callbacksr   r   from_handle)r   r!   r   r   r   __init__M   s
   zAbstractCallbacks.__init__c                 C   s   | j |}|S r    )r!   r#   )r   handlexr   r   r   r#   S   s   zAbstractCallbacks.from_handlec                 C   sb  d}d}z5|st d W dS | |}|j}|j}|j}|du r"t}|r3|d tkr3|f|dd  }|j|  W nc   z| W n tyO   | |}Y nw t	
 |_|jdu s_|jdu rd}|j}|`z|durx|j|g|R   W Y dS | j|  W Y dS    tdt	jd t  Y Y dS |jj| Y dS |jdur||jjv r|j|u rdS dS )	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_watcherr.   r3   r   r   r   python_callbackW   s\   







	z!AbstractCallbacks.python_callbackc                 C   s   t d| |s	d S z/| |}|j}|`|jjd g|R   W z|  W d S    |jj|gt R   Y d S z|  W n   |jj|gt R   Y    Y d S )NzHandling error for handle)r   r#   r4   r-   r6   stopr2   r3   )r   r%   _reventswatcherr3   r   r   r   python_handle_error   s&   


z%AbstractCallbacks.python_handle_errorc                 C   sj   d }|d ur|j jdnd }|r| |}|d ur%|jd ||| dS tdtjd t	
||| d S )Nr%   r'   z0WARNING: gevent: Unhandled error with no watcherr(   )tb_framef_localsgetr#   r-   r6   r8   r2   r9   r:   print_exception)r   tvtbrE   r%   r   r   r   r7      s   
z#AbstractCallbacks.unhandled_onerrorc                 C   s,   |st dtjd d S | |}|  d S )NzWARNING: gevent: Unable to dereference handle; not stopping watcher. Native resources may leak. This is most likely a bug in gevent.r(   )r8   r2   r9   r#   rC   )r   r%   rE   r   r   r   python_stop   s   
zAbstractCallbacks.python_stopc                 C      d S r    r   r   watcher_ptrr   r   r   python_check_callback  s   z'AbstractCallbacks.python_check_callbackc                 C   s   zt   t y   Y d S w r    )MemoryErrorrP   r   r   r   rR     s
   c                 C   s,   |  |}|d u rtd| d S |  d S )NzDWARNING: gevent: running prepare callbacks from a destroyed handle: )_find_loop_from_c_watcherr8   _run_callbacks)r   rQ   r-   r   r   r   python_prepare_callback(  s   
z)AbstractCallbacks.python_prepare_callbackc                 C   s8   |  |}|r| |}|d ur|d ||| d S |r    )_find_watcher_ptr_in_tracebackrT   r6   )r   rK   rL   rM   rQ   r-   r   r   r   check_callback_onerror0  s   

z(AbstractCallbacks.check_callback_onerrorc                 C      t  r    NotImplementedErrorrP   r   r   r   rT   ;     z+AbstractCallbacks._find_loop_from_c_watcherc                 C   s   |d ur
|j jd S d S )NrQ   )rG   rH   )r   rM   r   r   r   rW   >  s   z0AbstractCallbacks._find_watcher_ptr_in_tracebackN)r   r   r   r$   r#   rB   rF   r7   rN   r
   rR   rV   rX   rT   rW   r   r   r   r   r   J   s    m 
	r   r   c                    s   ||  dd |D }t  fdd|D } jdf jdf jdf j jf j jff| D ]\}}|p8 j}| j|d|} j	
| q0 S )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.
    c                 S   s$   g | ]}t |d kr|n|dfqS )r*   N)len).0extrar   r   r   
<listcomp>P  s   $ z-assign_standard_callbacks.<locals>.<listcomp>c                 3   s"    | ]\}}t  ||fV  qd S r    )getattr)r^   nameerrorr"   r   r   	<genexpr>Q  s     z,assign_standard_callbacks.<locals>.<genexpr>N)onerror)tuplerB   rF   rN   rR   rX   rV   r7   
def_externr"   append)r!   libcallbacks_classextrasfunc
error_funcr	   r   rd   r   r   B  s   


   c                   @   s^  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dZd{ddZdd	 Zd
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zed d! Zed"d# Zed$d% Zed&d' Z d(d) Z!d*d+ Z"d,d- Z#d|d.d/Z$d0d1 Z%d2d3 Z&d4d5 Z'd}d6d7Z(d8d9 Z)d:d; Z*d<d= Z+d>d? Z,d@dA Z-edBdC Z.edDdE Z/edFdG Z0edHdI Z1edJdK Z2edLdM Z3d~dOdPZ4dQdR Z5ddTdUZ6d~dVdWZ7d~dXdYZ8d~dZd[Z9d~d\d]Z:d~d^d_Z;d~d`daZ<e<e= db< e>j?dckrddedfZ@dgdh ZAddidjZBd}dkdlZCdmdn ZDdodp ZEdqdr ZFdsdt ZGdudv ZHdwdx ZIedydz ZJdS )r   2   NFc                 C   sL   || _ || _d | _| j | | _|| _d| _t | _t	 | _
| || d S NF)_ffi_lib_ptr
new_handle_handle_to_self	_watchers_in_callbackr   
_callbackssetr<   _init_loop_and_aux_watchers)r   r!   rj   watchersflagsdefaultr   r   r   r$     s   zAbstractLoop.__init__c                 C   s   |  ||| _| j| j| _| j| j_|   | j| j	| _
| j| j
_|   | j| j| _| j| j_|   | jdd| _| jdd  d S )NF)refc                   S   rO   r    r   r   r   r   r   <lambda>  r   z:AbstractLoop._init_loop_and_aux_watchers.<locals>.<lambda>)
_init_looprt   rr   new_CHECK_POINTER_checkrv   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~   r   r   r   r{     s   

	
z(AbstractLoop._init_loop_and_aux_watchersc                 C   rY   )zs
        Called by __init__ to create or find the loop. The return value
        is assigned to self._ptr.
        rZ   r   r   r   r   r     s   zAbstractLoop._init_loopc                 C   rY   r    rZ   r   r   r   r   r     r\   z"AbstractLoop._init_and_start_checkc                 C   rY   r    rZ   r   r   r   r   r     r\   z$AbstractLoop._init_and_start_preparec                 C   rY   r    rZ   r   r   r   r   r     r\   z!AbstractLoop._init_callback_timerc                 C   rY   r    rZ   r   r   r   r   _stop_callback_timer  r\   z!AbstractLoop._stop_callback_timerc                 C   rY   r    rZ   r   r   r   r   _start_callback_timer  r\   z"AbstractLoop._start_callback_timerc                 C   s   |  d ||| d S r    )r6   )r   rK   rL   rM   r   r   r   _check_callback_handle_error     z)AbstractLoop._check_callback_handle_errorc                 C   sP  d| _ z| j}|  }|t  }|   | jr| j }|d8 }|   |j}d |_|j	}|d u s5|d u r6qz4z||  W n(   z| j
|gt R   W n   ztdtjd t  W n   Y Y Y W d |_	nd |_	w |dkr| jr| j}|   |  |krd}n| js|dkr|   | jr|   W d| _ d S W d| _ d S d| _ w )NTr'   z&Exception while handling another errorr(   r   F)#starting_timer_may_update_loop_timeCALLBACK_CHECK_COUNTnowr   r   ry   popleftunrefr	   r.   r6   r2   r3   r8   r9   r:   r;   
update_nowr   )r   countr   
expirationcbr	   r.   r   r   r   rU     sT   

7

zAbstractLoop._run_callbacksc                 C   s"   | j d ur| j   d | _ d S d S r    )r   closer   r   r   r   _stop_aux_watchers*  s   


zAbstractLoop._stop_aux_watchersc                 C   sp   | j }|r6z&| |sW d | _| `| `| `dS |   | | W d | _| `| `| `dS d | _| `| `| `w d S )NFT)ptr_can_destroy_looprt   rv   ry   r<   r   _destroy_loopr   r   r   r   r   destroy/  s*   
zAbstractLoop.destroyc                 C   rY   r    rZ   r   r   r   r   r   B  r\   zAbstractLoop._can_destroy_loopc                 C   rY   r    rZ   r   r   r   r   r   E  r\   zAbstractLoop._destroy_loopc                 C   s   | j S r    )rt   r   r   r   r   r   H  s   zAbstractLoop.ptrc                 C   s   | j jS r    )rw   rE   r   r   r   r   WatcherTypeM  s   zAbstractLoop.WatcherTypec                 C   r   Nr'   r   r   r   r   r   MAXPRIQ     zAbstractLoop.MAXPRIc                 C   r   r   r   r   r   r   r   MINPRIU  r   zAbstractLoop.MINPRIc                 C   sZ   zt |}W n   t  Y zd||f }W n   t  Y | d tt|d  d S )Nz%s: %s)osstrerrorr:   r;   r6   SystemError)r   messageerrnor   r   r   _handle_syserrY  s   

zAbstractLoop._handle_syserrc                 C   sb   |t u r| j  |   d S d }| j}|d ur't|d|}||||| d S | |||| d S )Nr6   )r   ry   clearbreak_error_handlerra   _default_handle_error)r   contexttypevaluerM   r6   r   r   r   r   r6   d  s   
zAbstractLoop.handle_errorc                 C   s   t ||| d S r    )r:   rJ   )r   r   r   r   rM   r   r   r   r   s  s   z"AbstractLoop._default_handle_errorc                 C   rY   r    rZ   )r   nowaitoncer   r   r   runz  r\   zAbstractLoop.runc                 C   rY   r    rZ   r   r   r   r   reinit}  r\   zAbstractLoop.reinitc                 C   rY   r    rZ   r   r   r   r   r     s   zAbstractLoop.refc                 C   rY   r    rZ   r   r   r   r   r     r\   zAbstractLoop.unrefc                 C   rY   r    rZ   )r   howr   r   r   r     r\   zAbstractLoop.break_c                 C   rO   r    r   r   r   r   r   verify  r   zAbstractLoop.verifyc                 C   rY   r    rZ   r   r   r   r   r     r\   zAbstractLoop.nowc                 C   rY   r    rZ   r   r   r   r   r     r\   zAbstractLoop.update_nowc                 C   s$   dd l }|jdtdd |   d S )Nr   z('update' is deprecated; use 'update_now'r*   )
stacklevel)warningswarnDeprecationWarningr   )r   r   r   r   r   update  s   zAbstractLoop.updatec                 C   s    d| j j| j jt| |  f S )Nz<%s.%s at 0x%x %s>)	__class__r   r   id_formatr   r   r   r   r     s   zAbstractLoop.__repr__c                 C   s   | j r| jS dS rq   )r   _defaultr   r   r   r   r~     s   zAbstractLoop.defaultc                 C   r   Nr+   r   r   r   r   r   	iteration  r   zAbstractLoop.iterationc                 C   r   r   r   r   r   r   r   depth  r   zAbstractLoop.depthc                 C   r   Nr   r   r   r   r   r   backend_int  r   zAbstractLoop.backend_intc                 C   r   )Nr~   r   r   r   r   r   backend  r   zAbstractLoop.backendc                 C   r   r   r   r   r   r   r   
pendingcnt  r   zAbstractLoop.pendingcntTc                 C      | j | ||||S r    )rw   io)r   fdeventsr   priorityr   r   r   r     r   zAbstractLoop.ioc                 C   r   rq   r   )r   r   r   r   r   
closing_fd  r   zAbstractLoop.closing_fd        c                 C   r   r    )rw   timer)r   afterrepeatr   r   r   r   r   r     r   zAbstractLoop.timerc                 C      | j | |||S r    )rw   signal)r   signumr   r   r   r   r   r        zAbstractLoop.signalc                 C      | j | ||S r    )rw   idler   r   r   r   r   r   r        zAbstractLoop.idlec                 C   r   r    )rw   preparer   r   r   r   r     r   zAbstractLoop.preparec                 C   r   r    )rw   checkr   r   r   r   r     r   zAbstractLoop.checkc                 C   r   r    )rw   forkr   r   r   r   r     r   zAbstractLoop.forkc                 C   r   r    )rw   r   r   r   r   r   r     r   zAbstractLoop.async_asyncwin32r   c                 C   r   r    )rw   child)r   pidtracer   r   r   r   r     r   zAbstractLoop.childc                 C   rO   r    r   r   r   r   r   install_sigchld  r   zAbstractLoop.install_sigchldc                 C   r   r    )rw   stat)r   pathintervalr   r   r   r   r   r     r   zAbstractLoop.statc                 C   s
   t | |S r    r   )r   r   r   r   r   r	     s   
zAbstractLoop.callbackc                 C   rY   r    rZ   r   r   r   r   _setup_for_run_callback  r\   z$AbstractLoop._setup_for_run_callbackc                 G   s"   t ||}| j| |   |S r    )r	   ry   ri   r   r   rm   r.   r   r   r   r   run_callback  s   
zAbstractLoop.run_callbackc                 G   s    | j |g|R  }| j  |S r    )r   r   sendr   r   r   r   run_callback_threadsafe  s   
z$AbstractLoop.run_callback_threadsafec                 C   sT   | j }|sdS d| j }|dt| 7 }| jr|d7 }|d| j 7 }||  7 }|S )N	destroyedzbackend=z ptr=z defaultz pending=%s)r   r   strr~   r   _format_details)r   r   msgr   r   r   r     s   
zAbstractLoop._formatc                 C   sx   d}|   }z| j}W n ty   d }Y nw |d ur#|dt| 7 }|d ur/|dt| 7 }|dtt| j 7 }|S )N z ref=z fileno=z callbacks=)fileno	activecntAttributeErrorreprr   r]   ry   )r   r   r   r   r   r   r   r     s   
zAbstractLoop._format_detailsc                 C   rO   r    r   r   r   r   r   r     r   zAbstractLoop.filenoc                 C   s   | j stddS )Nzoperation on destroyed loopr   )r   
ValueErrorr   r   r   r   r     s   zAbstractLoop.activecnt)NN)FFr    )TN)r   TN)r   T)Kr   r   r   r   r   r   r   _TIMER_CALLBACK_SIGr   r   r   r   r<   r   r$   r{   r   r   r   r   r   r   r   rU   r   r   r   r   propertyr   r   r   r   r   r6   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   localsr2   platformr   r   r   r	   r   r   r   r   r   r   r   r   r   r   r   r   v  s    

&P























	)r   )(__doc__
__future__r   r   collectionsr   r2   r   r:   gevent._ffir   r   r   gevent._ffi.callbackr	   gevent._compatr
   gevent.exceptionsr   geventr   __all__objectr   EVENTSr0   	frozensetr   r   r   version_infobytesr   
basestringintinteger_types__builtin__longr/   r   r   r   r   r   <module>   s<    
( 
y&
