o
    tPfX                     @   s&  d Z ddlmZ ddlmZ ddlm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lmZ ddlmZ edje d< dd e d< dgZeejZeejZG dd deZdd Z G dd deZ!dd Z"e"  ddl#m$Z$ e$e% d dS )zO
Internal module, support for the linkable protocol for "event" like objects.

    )absolute_import)division)print_functionN)get_objects)greenlet)error)thread_mod_name)get_hub_noargs)get_hub_if_exists)InvalidSwitchError)InvalidThreadUseError)Timeoutr   
getcurrentc                   C      d S N r   r   r   Z/var/www/html/humari/django-venv/lib/python3.10/site-packages/gevent/_abstract_linkable.py<lambda>   s    r   greenlet_initAbstractLinkablec                   @   s   e Zd ZdZdd ZdS )_FakeNotifierpendingc                 C   s
   d| _ d S )NFr   selfr   r   r   __init__*      
z_FakeNotifier.__init__N)__name__
__module____qualname__	__slots__r   r   r   r   r   r   %   s    r   c                      s    ddl m   fddt D S )Nr   Hubc                    s(   i | ]}t | r|jd ur|j|qS r   )
isinstanceloopparent).0xr!   r   r   
<dictcomp>/   s    
z&get_roots_and_hubs.<locals>.<dictcomp>)
gevent.hubr"   r   r   r   r!   r   get_roots_and_hubs-   s   
r*   c                   @   s   e Zd ZdZd1d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dd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zefd)d*Zd+d, Zd1d-d.Zd/d0 ZdS )2r   )hub_links	_notifier_notify_all__weakref__Nc                 C   s   g | _ d | _d| _|| _d S )NT)r,   r-   r.   r+   r   r+   r   r   r   r   X   s   
	zAbstractLinkable.__init__c                 C   s
   t | jS r   )lenr,   r   r   r   r   	linkcount   s   
zAbstractLinkable.linkcountc                 C   s   t r   )NotImplementedErrorr   r   r   r   ready   s   zAbstractLinkable.readyc                 C   s.   t |std|f | j| |   dS )a   
        Register a callback to call when this object is ready.

        *callback* will be called in the :class:`Hub
        <gevent.hub.Hub>`, so it must not use blocking gevent API.
        *callback* will be passed one argument: this instance.
        zExpected callable: %rN)callable	TypeErrorr,   append_check_and_notifyr   callbackr   r   r   rawlink   s   zAbstractLinkable.rawlinkc                 C   sV   z| j | W n	 ty   Y nw | j s%| jdur'| jjr)| j  dS dS dS dS )z*Remove the callback set by :meth:`rawlink`N)r,   remove
ValueErrorr-   r   stopr9   r   r   r   unlink   s   zAbstractLinkable.unlinkc                 C      t  S r   )_allocate_thread_lockr   r   r   r   _allocate_lock      zAbstractLinkable._allocate_lockc                 C   r@   r   )r   r   r   r   r   _getcurrent   rC   zAbstractLinkable._getcurrentc                 C   r@   r   )_get_thread_identr   r   r   r   rE      rC   z"AbstractLinkable._get_thread_identc                 C   s   	 | j }|d u r	n|jr| j |u rd | _ d }nnnq| j d u r/|r$t nt }| j d u r/|| _ | j d urD| j jt krDt| j t t | j S r   )r+   deadget_hubr
   thread_identrE   r   r   )r   createmy_hubcurrent_hubr   r   r   _capture_hub   s.   


zAbstractLinkable._capture_hubc                 C   s   |   r?| jrA| jsCd }z| d}W n	 ty   Y nw |d ur,|j| jg | _d S d| _z| g  W d | _d S d | _w d S d S d S )NF   )r4   r,   r-   rL   r   r$   run_callback_notify_linksr0   r   r   r   r8      s   z"AbstractLinkable._check_and_notifyc           
      C   s  |sg S | j  }|d }t }| jd ur| jnt }g }|r|r'|  s'	 |S |d}t|}||vr|| z'|   zz||  W n t	yS   |
| Y nw W |   n|   w W n   |d urv|j|| fgt R   ndd l}	|	  Y ||u r	 |S |s|S )Nr   )r.   setr+   r
   r4   popidadd_drop_lock_for_switch_outgreenlet_errorr7   _acquire_lock_for_switch_inhandle_errorsysexc_info	traceback	print_exc)
r   linksonly_while_ready
final_linkdoner+   
unswitchedlinkid_linkr[   r   r   r   _notify_link_list   sF   

!z"AbstractLinkable._notify_link_listc              	   C   s   | j }|d u r|   d S z)| | j}|r&| |}|| | j| W | j |u s3J | j |fd | _ n| j |u sCJ | j |fd | _ w |   |rT| | d S d S r   )r-   r8   rd   r,   extend _handle_unswitched_notifications)r   arrived_while_waitingnotifierra   un2r   r   r   rO     s&   

zAbstractLinkable._notify_linksc           	      C   s  d }d}| j  }|r|r|  s| || d S |d}d }t|dd dkrjtt|dd trj|j}|j}|d urJt	|drC|j}n|j}|d us:|d u rZ|d u rUt
 }||}|d urj|jd urj|j||  |d u ss|jd u r{| || d}|s
d S d S )NFr   r   switch__self__r$   T)r.   r4   +_AbstractLinkable__print_unswitched_warningrR   getattrr#   r   rk   r%   hasattrr*   getr$   run_callback_threadsafe)	r   ra   root_greenlets
printed_tbr^   rb   r+   gletr%   r   r   r   rf   ^  s<   


z1AbstractLinkable._handle_unswitched_notificationsc                 C   sF   t d|d| dtjd |s!d}t ddtjd dd l}|  d S d S )	Nz+gevent: error: Unable to switch to greenletfromz,; crossing thread boundaries is not allowed.)fileTzNgevent: error: This is a result of using gevent objects from multiple threads,z!and is a bug in the calling code.r   )printrY   stderrr[   print_stack)r   rb   rr   r[   r   r   r   __print_unswitched_warning  s   z+AbstractLinkable.__print_unswitched_warningc                 C   s^   |d u rd S |  | | jd ur+| jjr-z| jjd | W d S  ty*   Y d S w d S d S Nr   )r?   r-   argsr<   r=   )r   objr   r   r   _quiet_unlink_all  s   
z"AbstractLinkable._quiet_unlink_allc              	   C   sX   t  j}|r| | n	| jjd | z| | j d }W | | d S | | w rz   )	r   rj   r;   r-   r{   r7   _switch_to_hubr+   r}   )r   r;   resume_this_greenletr   r   r   __wait_to_be_notified  s   z&AbstractLinkable.__wait_to_be_notifiedc                 C   sH   |    z
| }W |   n|   w || ur"td| jj|f d S )Nz!Invalid switch into %s.wait(): %r)rU   rj   rW   r   	__class__r   )r   the_hubresultr   r   r   r~     s   
zAbstractLinkable._switch_to_hubc                 C   r   r   r   r   r   r   r   rW        z,AbstractLinkable._acquire_lock_for_switch_inc                 C   r   r   r   r   r   r   r   rU     r   z*AbstractLinkable._drop_lock_for_switch_outc                 C   sz   t |.}z| d W W d   dS  |y2 } z||ur! W Y d}~W d   dS d}~ww 1 s6w   Y  dS )a  
        The core of the wait implementation, handling switching and
        linking.

        This method is NOT safe to call from multiple threads.

        ``self.hub`` must be initialized before entering this method.
        The hub that is set is considered the owner and cannot be changed
        while this method is running. It must only be called from the thread
        where ``self.hub`` is the current hub.

        If *catch* is set to ``()``, a timeout that elapses will be
        allowed to be raised.

        :return: A true value if the wait succeeded without timing out.
          That is, a true return value means we were notified and control
          resumed in this greenlet.
        TNF)r   _start_new_or_dummy&_AbstractLinkable__wait_to_be_notified)r   timeoutcatchtimerexr   r   r   
_wait_core  s   
zAbstractLinkable._wait_corec                 C   r   r   r   )r   waitedwait_successr   r   r   _wait_return_value  s   z#AbstractLinkable._wait_return_valuec                 C   sH   |  d |  r| dd}| jr| d |S | |}| d|S )NTF)rL   r4   r   r-   r   r   )r   r   r   gotitr   r   r   _wait  s   


zAbstractLinkable._waitc                 C   s   d| _ d| _dS )a  
        This method was added in Python 3.9 and is called by logging.py
        ``_after_at_fork_child_reinit_locks`` on Lock objects.

        It is also called from threading.py, ``_after_fork`` in
        ``_reset_internal_locks``, and that can hit ``Event`` objects.

        Subclasses should reset themselves to an initial state. This
        includes unlocking/releasing, if possible. This method detaches from the
        previous hub and drops any existing notifier.
        N)r+   r-   r   r   r   r   _at_fork_reinit  s   
z AbstractLinkable._at_fork_reinitr   )r   r   r   r    r   r2   r4   r;   r?   rB   rD   rE   rL   r8   rd   rO   rf   rl   r}   r   r~   rW   rU   r   r   r   r   r   r   r   r   r   r   :   s2    
)%3?:!
c                   C   s
   t   d S r   )r   r   r   r   r   _init  r   r   )import_c_accelzgevent.__abstract_linkable)&__doc__
__future__r   r   r   rY   gcr   r   r   rV   gevent._compatr   gevent._hub_localr	   rG   r
   gevent.exceptionsr   r   gevent.timeoutr   
__import__r   locals__all__	get_identrE   allocate_lockrA   objectr   r*   r   r   gevent._utilr   globalsr   r   r   r   <module>   s<   

   d