o
    tPfQ                     @   s8  d Z ddlmZmZ ddlZddlZddlZddlm	Z	 ddl
mZ zddlmZ dd ZW n) eyW   e	jrMddlZG d	d
 d
eZdd Zdd Zndd Zdd ZY nw 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 eekZg Zze W n ey   G dd de ZY nw G dd de!Z"e"dZ"dd Z#dd Z$dd Z%G d d! d!e&Z'G d"d# d#eZ(e'd#efe)e(j*Z(G d$d% d%eZ+G d&d' d'eZ,G d(d) d)eZ-G d*d+ d+eZ.G d,d- d-eZ/G d.d/ d/eZ0G d0d1 d1eZ1G d2d3 d3eZ2G d4d5 d5eZ3G d6d7 d7eZ4dS )8zb
Useful base classes for watchers. The available
watchers will depend on the specific event loop.
    )absolute_importprint_functionN)config)LazyOnClass)get_object_tracebackc                 C      | S N initr	   r	   T/var/www/html/humari/django-venv/lib/python3.10/site-packages/gevent/_ffi/watcher.pytracemalloc      r   c                   @   s    e Zd ZdZdd Zdd ZdS )_TBlinesc                 C   s   dd |D | _ d S )Nc                 S   s   g | ]}|  qS r	   )rstrip).0xr	   r	   r   
<listcomp>    s    z _TB.__init__.<locals>.<listcomp>r   )selfr   r	   r	   r   __init__   s   z_TB.__init__c                 C      | j S r   r   r   r	   r	   r   format"      z
_TB.formatN)__name__
__module____qualname__	__slots__r   r   r	   r	   r	   r   r      s    r   c                       t   fdd}|S )Nc                    s(    | g|R i | t t | _d S r   )r   	tracebackformat_stack_captured_mallocr   argskwargsr
   r	   r   traces&   s   ztracemalloc.<locals>.traces	functoolswraps)r   r'   r	   r
   r   r   %   s   c                 C   r   r   )r#   )objr	   r	   r   r   ,   r   r   c                 C      d S r   r	   )_objr	   r	   r   r   0      c                 C   r   r   r	   r
   r	   r	   r   r   3   r.   fsencode)_dbg)GEVENT_DEBUG_LEVEL)DEBUG)GEVENT_CORE_EVENTS)_NOARGSc                   @      e Zd ZdZdS )ResourceWarningzPython 2 fallbackN)r   r   r   __doc__r	   r	   r	   r   r7   G       r7   c                   @   s   e Zd Zdd ZdS )_NoWatcherResultc                 C      dS )Nz<NoWatcher>r	   r   r	   r	   r   __repr__L   r.   z_NoWatcherResult.__repr__N)r   r   r   r<   r	   r	   r	   r   r:   J   s    r:   c                 C   sV   g }|D ]\}}|}| |@ r| | | | M } | s nq| r&| t|  d|S )N|)appendhexjoin)event_field
all_eventsresultflagstringc_flagr	   r	   r   events_to_strQ   s   


rG   c                    r    )Nc                    s(   | j rtd | g|R i | d S )Nznot while active)active
ValueErrorr$   funcr	   r   nw`   s   znot_while_active.<locals>.nwr(   )rK   rL   r	   rJ   r   not_while_active_      rM   c                    r    )Nc                    s   | j r | S tS r   )_watcherr:   r   rJ   r	   r   if_wh   s   zonly_if_watcher.<locals>.if_wr(   )rK   rP   r	   rJ   r   only_if_watcherg   rN   rQ   c                   @   s<   e Zd ZdZdZdZdd Zedd Zdd Z	d	d
 Z
dS )AbstractWatcherTypez
    Base metaclass for watchers.

    To use, you will:

    - subclass the watcher class defined from this type.
    - optionally subclass this type
    Nc                 C   sD   |dkr| ds| ||| d|v rtstdt| |||S )Nwatcher_watcher_skip_ffi__del__z-CFFI watchers are not allowed to have __del__)get_fill_watcherALLOW_WATCHER_DEL	TypeErrortype__new__)clsnamebasescls_dictr	   r	   r   r[   ~   s
   zAbstractWatcherType.__new__c                    sL  | dr|d d }ddd}|dp|d|}d|vr)|d | }||d< n|d |s<|d |d  }||d< |d   fd	d
}t|| |dse|dp_|d|dp_d}||f fdd}	t||	 |dp}|d|dp}dfdd}
t||
 dd }dD ]}d| }||vrt|||| qd S )N_Tc              	   S   s<   |D ]}zt || W   S  ty   Y qw |rt| d S r   )getattrAttributeError)attrr^   errorbr	   r	   r   _mro_get   s   z3AbstractWatcherType._fill_watcher.<locals>._mro_get_watcher_prefix_watcher_type
_is_activec                    s   t | j S r   )rb   _LIBr   )active_namer	   r   _watcher_is_active      z=AbstractWatcherType._fill_watcher.<locals>._watcher_is_active_watcher_struct_name_watcher_struct_patternFz	struct %sc                    s   | j  d S )Nz *)_FFItypeofr   )watcher_struct_namer	   r   _watcher_struct_pointer_type      zGAbstractWatcherType._fill_watcher.<locals>._watcher_struct_pointer_type_watcher_callback_name_gevent_generic_callbackc                    s   | j | j S r   )rq   	addressofrk   r   )callback_namer	   r   _watcher_callback   ru   z<AbstractWatcherType._fill_watcher.<locals>._watcher_callbackc                    s    fdd}||_ |S )Nc                    s   | j d   }t| j|S )Nr`   )ri   rb   rk   )r   lib_namer]   r	   r   meth   s   zCAbstractWatcherType._fill_watcher.<locals>._make_meth.<locals>.meth)r   )r]   watcher_namer}   r	   r|   r   
_make_meth   s   z5AbstractWatcherType._fill_watcher.<locals>._make_meth)startstopr   	_watcher_)T)endswithrV   
startswithr   lazy)r\   r]   r^   r_   rg   rh   watcher_typerm   watcher_struct_patternrt   rz   r   	meth_namer~   r	   )rl   ry   rs   r   rW      sJ   








z!AbstractWatcherType._fill_watcherc                 C      | j |S r   )rq   
new_handle)r\   r+   r	   r	   r   r      rn   zAbstractWatcherType.new_handlec                 C   r   r   )rq   new)r\   kindr	   r	   r   r      rn   zAbstractWatcherType.new)r   r   r   r8   rq   rk   r[   classmethodrW   r   r   r	   r	   r	   r   rR   p   s    

ArR   c                   @   s  e Zd ZdZdZdZdZeddefddZ	dd Z
e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ZdZdZdZdZdZdZdd  Zd!d" Z d#d$ Z!d%d& Z"e#rpd'd( Z$d)Z%d*d+ Z&e'd,d- Z(d.d/ Z)e'd0d1 Z*d2d3 Z+d4d5 Z,e'e+e,Z-d6d7 Z.d8d9 Z/e'e.e/Z0d:d; Z1d<d= Z2d>d? Z3e4d@dA Z5e'e3e5Z6e'dBdC Z7e'dDdE Z8dS )FrS   NTc                 C   s$   || _ || _|| _|| _|   d S r   )loop_watcher__init_priority_watcher__init_args_watcher__init_ref_watcher_full_init)r   _looprefpriorityr%   r	   r	   r   r      s
   zwatcher.__init__c                 C   sZ   | j }| j}| j}| | |d ur| | z| | W n   d | _ | | d S r   )r   r   r   _watcher_create_watcher_ffi_set_priority_watcher_ffi_initrO   _watcher_ffi_set_init_ref)r   r   r   r%   r	   r	   r   r      s   

zwatcher._watcher_full_initc                 C   r,   r   r	   )r\   ffi_watcherr	   r	   r   _watcher_ffi_close   r   zwatcher._watcher_ffi_closec                 C   s   |   | _d S r   )_watcher_newrO   r   r   r	   r	   r   r      s   zwatcher._watcher_createc                 C   s   t | | jS r   )rZ   r   rt   r   r	   r	   r   r      ru   zwatcher._watcher_newc                 C   r,   r   r	   r   r	   r	   r   r     r.   z!watcher._watcher_ffi_set_init_refc                 C   r,   r   r	   r   r   r	   r	   r   r     r.   z!watcher._watcher_ffi_set_priorityc                 C      t  r   NotImplementedErrorr   r%   r	   r	   r   r     r   zwatcher._watcher_ffi_initc                 C   r   r   r   r   r	   r	   r   _watcher_ffi_start  r   zwatcher._watcher_ffi_startc                 C   s   |  | jj| j d S r   )_watcher_stopr   ptrrO   r   r	   r	   r   _watcher_ffi_stop  s   zwatcher._watcher_ffi_stopc                 C   r   r   r   r   r	   r	   r   _watcher_ffi_ref  r   zwatcher._watcher_ffi_refc                 C   r   r   r   r   r	   r	   r   _watcher_ffi_unref  r   zwatcher._watcher_ffi_unrefc                 C      |    d S r   )r   r   r	   r	   r   _watcher_ffi_start_unref  s   z watcher._watcher_ffi_start_unrefc                 C   r   r   )r   r   r	   r	   r   _watcher_ffi_stop_ref  rn   zwatcher._watcher_ffi_stop_refc                 C   sF   | j d u rd S |   | j }d | _ | || jj | | d | _d S r   )rO   r   _watcher_set_datarq   NULLr   r   )r   rO   r	   r	   r   close6  s   


zwatcher.closec                 C   s
   ||_ |S r   )data)r   the_watcherr   r	   r	   r   r   A  s   zwatcher._watcher_set_datac                 C   r   r   r	   r   r	   r	   r   	__enter__H  r.   zwatcher.__enter__c                 C   r   r   )r   )r   tvtbr	   r	   r   __exit__K  rn   zwatcher.__exit__c                 C   sT   | j r(t| }d}|d urd| }d| }td| |f t |   d S d S )N 
z
Traceback:
zFailed to close watcher %r%s)rO   r   r@   r   warningswarnr7   r   )r   r   tb_msgr	   r	   r   rU   O  s   zwatcher.__del__Fc                 C   s  d| j jt| f }| jr|d S d| _zhd||  f }| jr$|d7 }| jd urEt| jdd }|| u r=|d| jj 7 }n|d| jf 7 }| jd urR|d	| jf 7 }| jd u r`| jd u r`|d
7 }|d| j	 7 }|d| j
 7 }|d| j 7 }|d W d| _S d| _w )Nz<%s at 0x%x>Tz%s%sz pending__self__z# callback=<bound method %s of self>z callback=%rz args=%rz stoppedz watcher=%sz
 handle=%sz ref=%sF)	__class__r   id_watcher__in_repr_formatpendingcallbackrb   r%   rO   _watcher_handler   )r   basicrC   fselfr	   r	   r   r<   ^  s,   

zwatcher.__repr__c                 C   s   | j r| j jS d S r   )rO   r   r   r	   r	   r   r   {  s   zwatcher._watcher_handlec                 C   r;   )Nr   r	   r   r	   r	   r   r     r.   zwatcher._formatc                 C   r   r   r   r   r	   r	   r   r        zwatcher.refc                 C   s   d| j v r| jS d S )N	_callback)__dict__r   r   r	   r	   r   _get_callback  s   zwatcher._get_callbackc                 C   sF   t |s|d urtd|f |d u rd| jv r| `d S d S || _d S )NzExpected callable, not %rr   )callablerY   r   r   )r   cbr	   r	   r   _set_callback  s   

zwatcher._set_callbackc                 C   r   r   )_argsr   r	   r	   r   	_get_args  r   zwatcher._get_argsc                 C   sB   t |ts|d urtd|d u rd| jv r| `d S d S || _d S )Nzargs must be a tuple or Noner   )
isinstancetuplerY   r   r   r   r	   r	   r   	_set_args  s   

zwatcher._set_argsc                 G   s\   |d u rt d|| _|pt| _| jj|  | | jt	| 
| | _|   |   d S )Nz#callback must be callable, not None)rY   r   r5   r%   r   _keepalivesetaddr   rO   rZ   r   _handler   r   )r   r   r%   r	   r	   r   r     s   
zwatcher.startc                 C   sn   | j d u r| jd u s| | jjvsJ d S d | _ |   |   | jj|  d | _| | j| j	j
 d | _d S r   )r   r   r   r   r   discardr   r   rO   rq   r   r%   r   r	   r	   r   r     s   

zwatcher.stopc                 C   r,   r   r	   r   r	   r	   r   _get_priority  r.   zwatcher._get_priorityc                 C   r,   r   r	   r   r	   r	   r   _set_priority  r   zwatcher._set_priorityc                 C   s   | j d ur| | j rdS dS )NTF)rO   rm   r   r	   r	   r   rH     s   zwatcher.activec                 C   r;   )NFr	   r   r	   r	   r   r     r   zwatcher.pending)9r   r   r   r   r   rO   r   r   r5   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   ri   _watcher_init_watcher_startr   rt   rz   rm   r   r   r   r   rX   rU   r   r<   propertyr   r   r   r   r   r   r   r   r%   r   r   r   rM   r   r   rH   r   r	   r	   r	   r   rS      sr    




	



rS   c                       s6   e Zd ZdZd
 fdd	Z fddZdd	 Z  ZS )IoMixinr   TNc                    sV   |dk r
t d| || j @ rt d| || _tt| j||||p&||fd d S )Nr   zfd must be non-negative: %rzillegal event mask: %rr   r   r%   )rI   
EVENT_MASK_fdsuperr   r   )r   r   fdeventsr   r   r   r   r	   r   r     s   

zIoMixin.__init__c                    s8   |pt }|drtf| }tt| j|g|R   d S )Npass_events)r5   rV   r4   r   r   r   )r   r   r%   r&   r   r	   r   r     s   

zIoMixin.startc                 C   s
   d| j  S )Nz fd=%d)r   r   r	   r	   r   r     s   
zIoMixin._format)TNN)r   r   r   r   r   r   r   __classcell__r	   r	   r   r   r     s
    r   c                       s6   e Zd ZdZd fdd	Z fddZd	d
 Z  ZS )
TimerMixintimer        TNc                    s@   |dk r
t d| || _|| _tt| j|||||fd d S )Nr   z#repeat must be positive or zero: %rr   )rI   _after_repeatr   r   r   )r   r   afterrepeatr   r   r   r	   r   r     s
    zTimerMixin.__init__c                    s:   | d| jj}|r| j  tt| j|g|R   d S )Nupdate)rV   r   #starting_timer_may_update_loop_time
update_nowr   r   r   )r   r   r%   kwr   r   r	   r   r     s   
zTimerMixin.startc                 O   r   r   r   )r   r   r%   r   r	   r	   r   again  r   zTimerMixin.again)r   r   TN)r   r   r   ri   r   r   r   r   r	   r	   r   r   r     s
    r   c                       s"   e Zd ZdZd fdd	Z  ZS )SignalMixinsignalTNc                    sB   |dk s	|t jkrtd| || _tt| j||||fd d S )N   zillegal signal number: %rr   )signalmoduleNSIGrI   
_signalnumr   r   r   )r   r   	signalnumr   r   r   r	   r   r     s   zSignalMixin.__init__)TN)r   r   r   ri   r   r   r	   r	   r   r   r     s    r   c                   @   r6   )	IdleMixinidleNr   r   r   ri   r	   r	   r	   r   r     r9   r   c                   @   r6   )PrepareMixinprepareNr   r	   r	   r	   r   r     r9   r   c                   @   r6   )
CheckMixincheckNr   r	   r	   r	   r   r     r9   r   c                   @   r6   )	ForkMixinforkNr   r	   r	   r	   r   r   #  r9   r   c                   @   s,   e Zd ZdZdd Zdd Zedd ZdS )	
AsyncMixinasyncc                 C   r   r   r   r   r	   r	   r   send*  r   zAsyncMixin.sendc                 C   s   |   S )z
        Calling compatibility with ``greenlet.switch(arg)``
        as used by waiters that have ``rawlink``.

        This is an advanced method, not usually needed.
        )r  )r   _ignoredr	   r	   r   send_ignoring_arg-  s   zAsyncMixin.send_ignoring_argc                 C   r   r   r   r   r	   r	   r   r   6  r   zAsyncMixin.pendingN)r   r   r   ri   r  r  r   r   r	   r	   r	   r   r  '  s    	r  c                       sV   e Zd ZdZd fdd	Zdd Zedd Zed	d
 ZdZ	dZ
edd Z  ZS )
ChildMixinTr   c                    sD   |j std|  || _| jr tt| j||||fd d S d S )Nz5child watchers are only available on the default loop)r   r%   )defaultrY   install_sigchld_pid_CALL_SUPER_INITr   r  r   )r   r   pidtracer   r   r	   r   r   @  s   zChildMixin.__init__c                 C   s   d| j | jf S )Nz pid=%r rstatus=%r)r  rstatusr   r	   r	   r   r   H  ru   zChildMixin._formatc                 C   r   r   )r	  r   r	   r	   r   r  K  r   zChildMixin.pidc                 C   r   r   )_rpidr   r	   r	   r   rpidO  s   zChildMixin.rpidNc                 C   r   r   )_rstatusr   r	   r	   r   r  W  r   zChildMixin.rstatus)r   T)r   r   r   r
  r   r   r   r  r  r  r  r  r   r	   r	   r   r   r  ;  s    

r  c                       sZ   e Zd Zedd Zd fdd	Zedd	 Zed
d Zedd Z	edd Z
  ZS )	StatMixinc                 C   s   t | S r   r/   )pathr	   r	   r   _encode_path]  s   zStatMixin._encode_pathr   TNc                    sL   |  || _|| _| jd| j| _|| _tt| j	|||| j|fd d S )Nzchar[]r   )
r  _paths_pathrq   r   _cpath	_intervalr   r  r   )r   r   r  intervalr   r   r   r	   r   r   a  s   	
zStatMixin.__init__c                 C   r   r   )r  r   r	   r	   r   r  v  r   zStatMixin.pathc                 C      t r   r   r   r	   r	   r   rd   z  r   zStatMixin.attrc                 C   r  r   r   r   r	   r	   r   prev~  r   zStatMixin.prevc                 C   r   r   )r  r   r	   r	   r   r    r   zStatMixin.interval)r   TN)r   r   r   staticmethodr  r   r   r  rd   r  r  r   r	   r	   r   r   r  [  s    



r  )5r8   
__future__r   r   r   r   r)   r   gevent._configr   gevent._utilr   r   r   ImportErrortrace_mallocr!   objectr   gevent._compatr0   gevent._ffir1   r2   r3   gevent._ffi.loopr4   r5   rX   __all__r7   	NameErrorWarningintr:   rG   rM   rQ   rZ   rR   rS   dictr   r   r   r   r   r   r   r   r  r  r  r	   r	   r	   r   <module>   sj    

!	]   