o
    oi3                     @   sb   d 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
 Z
G dd dZdS )z^
The implementation of thread patching for Python versions
prior to 3.13.

Internal use only.
    N)LoopExit   )is_object_patched)_notify_patch)_patch_module)_queue_warningc                 C   s  t t|  dkrd S |  | _z|  }W n ty$   |  }Y nw t| 	 }zdd l
}W n tyB   G dd dt}Y nw |jj}td}| D ]5}t||rwdD ]}t||rnt||d urlt|||  nqXtdt|qOt||r|jd ur||_qOd S )Nr   r   c                   @   s   e Zd ZdS )z*_patch_existing_locks.<locals>._ModuleLockN)__name__
__module____qualname__ r   r   a/var/www/html/pca-backend/venv/lib/python3.10/site-packages/gevent/monkey/_patch_thread_common.py_ModuleLock'   s    r   gc)_owner_RLock__ownerzEUnsupported Python implementation; Found unknown lock implementation.)lenlist	enumerate_allocate_lock_active_limbo_lock	get_identAttributeError
_get_identtypeRLockimportlib._bootstrapImportErrorobject
_bootstrapr   
__import__get_objects
isinstancehasattrgetattrsetattrAssertionErrorvarsowner)	threadingtid
rlock_type	importlibr   r   o
owner_namer   r   r   _patch_existing_locks   sD   





r.   c                   @   s   e Zd ZdZdZdZdZdZdZ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edd ZdS )BasePatcherNTc                 C   s(   || _ || _|| _|| _|| _|| _d S N)r(   threading_localEventloggingexisting_lockswarnings)selfr(   _threading_localr2   r3   r4   	_warningsr   r   r   __init__m   s   
zBasePatcher.__init__c                 C   s   t d| _| j | _| j | _| jj| _td| jdd\}}| j	r'| 
  | jr.|   | j	r5|   tdd}| j| j krM|sM|   |   n|sS|   ddlm} t|d|| | jd urtt|d| j| j d S d S )Nr(   threadFr8   _notify_did_subscribers	_shutdownr   )events)r   threading_modcurrent_threadorig_current_threadmain_threadr=   orig_shutdownr   r5   r(   ,patch_threading_event_logging_existing_locksr1   patch__threading_localpatch_active_threadsr   ;patch_threading_shutdown_on_main_thread_not_already_patchedpatch_main_thread_cleanup!patch_shutdown_not_on_main_threadgeventr>   r   GeventDidPatchModuleEventgevent_threading_mod)r6   gevent_thread_mod
thread_modalready_patchedr>   r   r   r   __call__y   s>   







zBasePatcher.__call__c                 C   sf   t d| jdd\| _}|| ju sJ | jr|   | jr!t| j | jr/dt	j
v r1|   d S d S d S )Nr(   Fr;   r3   )r   r5   rL   r?   r2   patch_eventr4   r.   r3   sysmodulespatch_logging)r6   patched_modr   r   r   rD      s   
z8BasePatcher.patch_threading_event_logging_existing_locksc                 C   sH   ddl m} ddlm} || jd| t| jdr"|| jd| d S d S )Nr   )r2   r   
patch_itemr2   _Event)gevent.eventr2   apirW   r?   r"   )r6   r2   rW   r   r   r   rQ      s   zBasePatcher.patch_eventc                 C   st   ddl m} td}||d| j  |jD ]!}t|r| n|}|d u r&qt|ds1td| | j |_	qd S )Nr   rV   r3   _locklockzUnknown/unsupported handler %r)
rZ   rW   r   r?   r   _handlerListcallabler"   	TypeErrorr\   )r6   rW   r3   wrhandlerr   r   r   rT      s   

zBasePatcher.patch_loggingc                 C   s0   t d}ddlm} ddlm} ||d| d S )Nr7   r   )localr   rV   rb   )r   gevent.localrb   rZ   rW   )r6   r7   rb   rW   r   r   r   rE      s   z"BasePatcher.patch__threading_localc                 C      t r0   NotImplementedErrorr6   r   r   r   rF         z BasePatcher.patch_active_threadsc                 C   rd   r0   re   rg   r   r   r   rG      rh   zGBasePatcher.patch_threading_shutdown_on_main_thread_not_already_patchedc                 C   sd   | j }| jj| |j}| j |_|| jjv r$| jj| | jj|j< ||jkr0| jj|= d S d S r0   )rB   r?   	_danglingremoveidentr   _ident_active)r6   rB   oldidr   r   r   rH      s   
z%BasePatcher.patch_main_thread_cleanupc                    sV   t d| j ddlm | j| j| jj | j fdd}d| d S )Nz`Monkey-patching not on the main thread; threading.main_thread().join() will hang from a greenletr   rV   c                      s6     _ z  W n	 ty   Y nw d d S )Nr=   )rl   r   r   r   rB   rC   rW   r?   r   r   r=     s   
z@BasePatcher.patch_shutdown_not_on_main_thread.<locals>._shutdownr=   )r   r5   rZ   rW   rB   r?   r   rC   )r6   r=   r   ro   r   rI      s   z-BasePatcher.patch_shutdown_not_on_main_threadc                    s2   ddl m  ddlm  d fdd	}|S )Nr   )time)sleepc                    sz   d }  u rtdd urjrd S  sd S | r" |  } r;|d ur1 |kr1d S  d  s&d S d S )NzCannot join current threadg{Gz?)r@   RuntimeErrordeadis_alive)timeoutendrq   r:   thread_greenletr?   rp   r   r   join   s   
zBBasePatcher._make_existing_non_main_thread_join_func.<locals>.joinr0   )rp   
gevent.hubrq   )r:   rx   r?   ry   r   rw   r   (_make_existing_non_main_thread_join_func  s    z4BasePatcher._make_existing_non_main_thread_join_func)TTTTTN)r   r	   r
   rL   rM   rN   r?   rA   rB   rC   r9   rP   rD   rQ   rT   rE   rF   rG   rH   rI   staticmethodr{   r   r   r   r   r/   O   s.    
8r/   )__doc__rR   gevent.exceptionsr   _stater   _utilr   r   r   r.   r/   r   r   r   r   <module>   s   =