o
    #6dkA                     @  s  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mZmZ dd	lmZ dd
lmZ ddlmZmZ ddlmZ ddlmZmZ eddG dd dZeddG dd dZeddG dd dZeddG dd dZeddG dd dZG dd dZG dd dZ G dd dZ!G d d! d!Z"G d"d# d#Z#d;d%d&Z$d<d=d*d+Z%d>d,d-Z&d'd.d?d3d4Z'd@d7d8Z(G d9d: d:Z)d'S )A    )annotations)deque)	dataclass)TracebackType)warn   )cancel_shielded_checkpoint
checkpointcheckpoint_if_cancelled   )DeprecatedAwaitable)get_asynclib)BusyResourceError
WouldBlock)CancelScope)TaskInfoget_current_taskT)frozenc                   @     e Zd ZU dZded< dS )EventStatisticszR
    :ivar int tasks_waiting: number of tasks waiting on :meth:`~.Event.wait`
    inttasks_waitingN__name__
__module____qualname____doc____annotations__ r   r   sC:\Users\jesus\OneDrive\Desktop\erpjis_fastapi\backend\jisbackend\Lib\site-packages\anyio/_core/_synchronization.pyr      s   
 r   c                   @  s2   e Zd ZU dZded< ded< ded< ded< d	S )
CapacityLimiterStatisticsa  
    :ivar int borrowed_tokens: number of tokens currently borrowed by tasks
    :ivar float total_tokens: total number of available tokens
    :ivar tuple borrowers: tasks or other objects currently holding tokens borrowed from this
        limiter
    :ivar int tasks_waiting: number of tasks waiting on :meth:`~.CapacityLimiter.acquire` or
        :meth:`~.CapacityLimiter.acquire_on_behalf_of`
    r   borrowed_tokensfloattotal_tokensztuple[object, ...]Z	borrowersr   Nr   r   r   r   r   r       s   
 	r    c                   @  s*   e Zd ZU dZded< ded< ded< dS )	LockStatisticsa  
    :ivar bool locked: flag indicating if this lock is locked or not
    :ivar ~anyio.TaskInfo owner: task currently holding the lock (or ``None`` if the lock is not
        held by any task)
    :ivar int tasks_waiting: number of tasks waiting on :meth:`~.Lock.acquire`
    boollockedTaskInfo | Noneownerr   r   Nr   r   r   r   r   r$   *   s
   
 r$   c                   @  s"   e Zd ZU dZded< ded< dS )ConditionStatisticsz
    :ivar int tasks_waiting: number of tasks blocked on :meth:`~.Condition.wait`
    :ivar ~anyio.LockStatistics lock_statistics: statistics of the underlying :class:`~.Lock`
    r   r   r$   Zlock_statisticsNr   r   r   r   r   r)   8   s   
 r)   c                   @  r   )SemaphoreStatisticszZ
    :ivar int tasks_waiting: number of tasks waiting on :meth:`~.Semaphore.acquire`

    r   r   Nr   r   r   r   r   r*   C   s   
 r*   c                   @  s>   e Zd ZdddZdddZddd	ZdddZdddZdS )Eventreturnc                 C  s
   t   S N)r   r+   )clsr   r   r   __new__N   s   
zEvent.__new__r   c                 C     t )z&Set the flag, notifying all listeners.NotImplementedErrorselfr   r   r   setQ      z	Event.setr%   c                 C  r0   )z5Return ``True`` if the flag is set, ``False`` if not.r1   r3   r   r   r   is_setU   r6   zEvent.is_setNonec                      t )z
        Wait until the flag has been set.

        If the flag has already been set when this method is called, it returns immediately.

        r1   r3   r   r   r   waitY      z
Event.waitr   c                 C  r0   )z8Return statistics about the current state of this event.r1   r3   r   r   r   
statisticsb   r6   zEvent.statisticsNr,   r+   r,   r   r,   r%   r,   r8   )r,   r   )r   r   r   r/   r5   r7   r:   r<   r   r   r   r   r+   M   s    



	r+   c                   @  sj   e Zd ZU dZded< dddZddd	Zd ddZdddZdddZ	d!ddZ
d"ddZd#ddZdS )$LockNr'   _owner_taskr,   r8   c                 C  s   t  | _d S r-   )r   _waitersr3   r   r   r   __init__j      zLock.__init__c                      |   I d H  d S r-   acquirer3   r   r   r   
__aenter__m      zLock.__aenter__exc_typetype[BaseException] | Noneexc_valBaseException | Noneexc_tbTracebackType | Nonec                      |    d S r-   releaser4   rK   rM   rO   r   r   r   	__aexit__p      zLock.__aexit__c                   s   t  I dH  z|   W nG tyT   t }t }||f}| j| z	| I dH  W n tyI   |	 s?| j
|  | j|krH|    w | j|ksQJ Y dS w z	t I dH  W dS  tyj   |    w )zAcquire the lock.N)r
   acquire_nowaitr   r   r+   rC   appendr:   BaseExceptionr7   removerB   rS   r   )r4   taskeventtokenr   r   r   rH   x   s6   
zLock.acquirec                 C  s0   t  }| j|krtd| jdurt|| _dS )zw
        Acquire the lock, without blocking.

        :raises ~anyio.WouldBlock: if the operation would block

        z)Attempted to acquire an already held LockN)r   rB   RuntimeErrorr   )r4   r[   r   r   r   rW      s   


zLock.acquire_nowaitr   c                 C  sB   | j t kr
td| jr| j \| _ }|  n| ` t| jS )zRelease the lock.z)The current task is not holding this lock)rB   r   r^   rC   popleftr5   r   rS   r4   r\   r   r   r   rS      s   

zLock.releaser%   c                 C  s
   | j duS )z*Return True if the lock is currently held.N)rB   r3   r   r   r   r&         
zLock.lockedr$   c                 C  s   t |  | jt| jS )zh
        Return statistics about the current state of this lock.

        .. versionadded:: 3.0
        )r$   r&   rB   lenrC   r3   r   r   r   r<      s   zLock.statisticsr@   rK   rL   rM   rN   rO   rP   r,   r8   r>   r?   )r,   r$   )r   r   r   rB   r   rD   rI   rU   rH   rW   rS   r&   r<   r   r   r   r   rA   g   s   
 






rA   c                   @  s   e Zd ZU dZded< d,d-ddZd.d
dZd/ddZd.ddZd.ddZ	d.ddZ
d0ddZd1ddZd2d3d#d$Zd.d%d&Zd.d'd(Zd4d*d+ZdS )5	ConditionNr'   rB   lockLock | Nonec                 C  s   |pt  | _t | _d S r-   )rA   _lockr   rC   )r4   re   r   r   r   rD      s   zCondition.__init__r,   r8   c                   rF   r-   rG   r3   r   r   r   rI      rJ   zCondition.__aenter__rK   rL   rM   rN   rO   rP   c                   rQ   r-   rR   rT   r   r   r   rU      rV   zCondition.__aexit__c                 C  s   | j t kr
tdd S )Nz3The current task is not holding the underlying lock)rB   r   r^   r3   r   r   r   _check_acquired   s   zCondition._check_acquiredc                   s   | j  I dH  t | _dS )zAcquire the underlying lock.N)rg   rH   r   rB   r3   r   r   r   rH      s   zCondition.acquirec                 C  s   | j   t | _dS )
        Acquire the underlying lock, without blocking.

        :raises ~anyio.WouldBlock: if the operation would block

        N)rg   rW   r   rB   r3   r   r   r   rW      s   
zCondition.acquire_nowaitr   c                 C  s   | j   t| jS )zRelease the underlying lock.)rg   rS   r   r3   r   r   r   rS      s   

zCondition.releaser%   c                 C  s
   | j  S )zReturn True if the lock is set.)rg   r&   r3   r   r   r   r&      ra   zCondition.lockedr   nr   c              	   C  sH   |    t|D ]}z| j }W n ty   Y  dS w |  qdS )zNotify exactly n listeners.N)rh   rangerC   r_   
IndexErrorr5   )r4   rj   _r\   r   r   r   notify   s   
zCondition.notifyc                 C  s*   |    | jD ]}|  q| j  dS )zNotify all the listeners.N)rh   rC   r5   clearr`   r   r   r   
notify_all   s   

zCondition.notify_allc                   s   t  I dH  t }| j| |   z<z	| I dH  W n ty0   | s/| j|  w W t	dd | 
 I dH  W d   dS 1 sJw   Y  dS t	dd | 
 I dH  W d   w 1 shw   Y  w )zWait for a notification.NT)shield)r	   r+   rC   rX   rS   r:   rY   r7   rZ   r   rH   r`   r   r   r   r:      s$   .zCondition.waitr)   c                 C  s   t t| j| j S )zm
        Return statistics about the current state of this condition.

        .. versionadded:: 3.0
        )r)   rb   rC   rg   r<   r3   r   r   r   r<     s   zCondition.statisticsr-   )re   rf   r@   rc   r>   r?   )r   )rj   r   r,   r8   )r,   r)   )r   r   r   rB   r   rD   rI   rU   rh   rH   rW   rS   r&   rn   rp   r:   r<   r   r   r   r   rd      s   
 









rd   c                   @  st   e Zd Zddd#ddZd$d
dZd%ddZd&ddZd&ddZd'ddZe	d(ddZ
e	d)ddZd*d!d"ZdS )+	SemaphoreN	max_valueinitial_valuer   rt   
int | Nonec                C  sd   t |ts	td|dk rtd|d ur&t |tstd||k r&td|| _|| _t | _d S )Nz initial_value must be an integerr   zinitial_value must be >= 0z$max_value must be an integer or Nonez7max_value must be equal to or higher than initial_value)
isinstancer   	TypeError
ValueError_value
_max_valuer   rC   )r4   ru   rt   r   r   r   rD     s   

zSemaphore.__init__r,   c                   s   |   I d H  | S r-   rG   r3   r   r   r   rI   +  s   zSemaphore.__aenter__rK   rL   rM   rN   rO   rP   r8   c                   rQ   r-   rR   rT   r   r   r   rU   /  rV   zSemaphore.__aexit__c                   s   t  I dH  z|   W n3 ty@   t }| j| z| I dH  W Y dS  ty?   | s:| j	|  | 
   w w z	t I dH  W dS  tyV   | 
   w )z5Decrement the semaphore value, blocking if necessary.N)r
   rW   r   r+   rC   rX   r:   rY   r7   rZ   rS   r   r`   r   r   r   rH   7  s.   zSemaphore.acquirec                 C  s    | j dkrt|  j d8  _ dS )ri   r   r   N)rz   r   r3   r   r   r   rW   O  s   
zSemaphore.acquire_nowaitr   c                 C  sL   | j dur| j| j krtd| jr| j   n|  jd7  _t| jS )zIncrement the semaphore value.Nz!semaphore released too many timesr   )r{   rz   ry   rC   r_   r5   r   rS   r3   r   r   r   rS   [  s   
zSemaphore.releasec                 C     | j S )z#The current value of the semaphore.)rz   r3   r   r   r   valueg     zSemaphore.valuec                 C  r|   )z#The maximum value of the semaphore.)r{   r3   r   r   r   rt   l  r~   zSemaphore.max_valuer*   c                 C  s   t t| jS )zm
        Return statistics about the current state of this semaphore.

        .. versionadded:: 3.0
        )r*   rb   rC   r3   r   r   r   r<   q  s   zSemaphore.statistics)ru   r   rt   rv   )r,   rr   rc   r@   r>   r,   r   )r,   rv   )r,   r*   )r   r   r   rD   rI   rU   rH   rW   rS   propertyr}   rt   r<   r   r   r   r   rr     s    




rr   c                   @  s   e Zd Zd0ddZd1ddZd2ddZed3ddZejd4ddZd4ddZ	ed5ddZ
ed3ddZd6ddZd7d"d#Zd1d$d%Zd8d&d'Zd1d(d)Zd8d*d+Zd9d-d.Zd/S ):CapacityLimiterr#   r"   r,   c                 C  s   t  |S r-   )r   r   )r.   r#   r   r   r   r/   {  rE   zCapacityLimiter.__new__r8   c                   r9   r-   r1   r3   r   r   r   rI   ~  s   zCapacityLimiter.__aenter__rK   rL   rM   rN   rO   rP   bool | Nonec                   r9   r-   r1   rT   r   r   r   rU     s   zCapacityLimiter.__aexit__c                 C  r0   )aE  
        The total number of tokens available for borrowing.

        This is a read-write property. If the total number of tokens is increased, the
        proportionate number of tasks waiting on this limiter will be granted their tokens.

        .. versionchanged:: 3.0
            The property is now writable.

        r1   r3   r   r   r   r#     s   zCapacityLimiter.total_tokensr}   c                 C  r0   r-   r1   r4   r}   r   r   r   r#     r6   c                   s   t dt || _d S )NzlCapacityLimiter.set_total_tokens has been deprecated. Set the value of the"total_tokens" attribute directly.)r   DeprecationWarningr#   r   r   r   r   set_total_tokens  s   
z CapacityLimiter.set_total_tokensr   c                 C  r0   )z7The number of tokens that have currently been borrowed.r1   r3   r   r   r   r!        zCapacityLimiter.borrowed_tokensc                 C  r0   )z7The number of tokens currently available to be borrowedr1   r3   r   r   r   available_tokens  r   z CapacityLimiter.available_tokensr   c                 C  r0   )z
        Acquire a token for the current task without waiting for one to become available.

        :raises ~anyio.WouldBlock: if there are no tokens available for borrowing

        r1   r3   r   r   r   rW        zCapacityLimiter.acquire_nowaitborrowerobjectc                 C  r0   )z
        Acquire a token without waiting for one to become available.

        :param borrower: the entity borrowing a token
        :raises ~anyio.WouldBlock: if there are no tokens available for borrowing

        r1   r4   r   r   r   r   acquire_on_behalf_of_nowait  s   z+CapacityLimiter.acquire_on_behalf_of_nowaitc                   r9   )zj
        Acquire a token for the current task, waiting if necessary for one to become available.

        r1   r3   r   r   r   rH     s   zCapacityLimiter.acquirec                   r9   )z
        Acquire a token, waiting if necessary for one to become available.

        :param borrower: the entity borrowing a token

        r1   r   r   r   r   acquire_on_behalf_of  r;   z$CapacityLimiter.acquire_on_behalf_ofc                 C  r0   )z
        Release the token held by the current task.
        :raises RuntimeError: if the current task has not borrowed a token from this limiter.

        r1   r3   r   r   r   rS     s   zCapacityLimiter.releasec                 C  r0   )z
        Release the token held by the given borrower.

        :raises RuntimeError: if the borrower has not borrowed a token from this limiter.

        r1   r   r   r   r   release_on_behalf_of  r   z$CapacityLimiter.release_on_behalf_ofr    c                 C  r0   )zl
        Return statistics about the current state of this limiter.

        .. versionadded:: 3.0

        r1   r3   r   r   r   r<     r   zCapacityLimiter.statisticsNr#   r"   r,   r   r@   rK   rL   rM   rN   rO   rP   r,   r   )r,   r"   )r}   r"   r,   r8   r   r>   )r   r   r,   r   )r   r   r,   r8   )r,   r    )r   r   r   r/   rI   rU   r   r#   setterr   r!   r   rW   r   rH   r   rS   r   r<   r   r   r   r   r   z  s(    





	



	
	r   r,   c                   C  s   t dt t S )z
    Create an asynchronous lock.

    :return: a lock object

    .. deprecated:: 3.0
       Use :class:`~Lock` directly.

    z2create_lock() is deprecated -- use Lock() directly)r   r   rA   r   r   r   r   create_lock  s   

r   Nre   rf   c                 C  s   t dt t| dS )z
    Create an asynchronous condition.

    :param lock: the lock to base the condition object on
    :return: a condition object

    .. deprecated:: 3.0
       Use :class:`~Condition` directly.

    z<create_condition() is deprecated -- use Condition() directlyre   )r   r   rd   r   r   r   r   create_condition  s
   
r   c                   C  s   t dt t  S )z
    Create an asynchronous event object.

    :return: an event object

    .. deprecated:: 3.0
       Use :class:`~Event` directly.

    z4create_event() is deprecated -- use Event() directly)r   r   r   r+   r   r   r   r   create_event  s   


r   rs   r}   r   rt   rv   c                C  s   t dt t| |dS )aV  
    Create an asynchronous semaphore.

    :param value: the semaphore's initial value
    :param max_value: if set, makes this a "bounded" semaphore that raises :exc:`ValueError` if the
        semaphore's value would exceed this number
    :return: a semaphore object

    .. deprecated:: 3.0
       Use :class:`~Semaphore` directly.

    z<create_semaphore() is deprecated -- use Semaphore() directlyrs   )r   r   rr   )r}   rt   r   r   r   create_semaphore  s
   r   r#   r"   c                 C  s   t dt t | S )a  
    Create a capacity limiter.

    :param total_tokens: the total number of tokens available for borrowing (can be an integer or
        :data:`math.inf`)
    :return: a capacity limiter object

    .. deprecated:: 3.0
       Use :class:`~CapacityLimiter` directly.

    zIcreate_capacity_limiter() is deprecated -- use CapacityLimiter() directly)r   r   r   r   )r#   r   r   r   create_capacity_limiter-  s
   r   c                   @  s.   e Zd ZdZdddZddd	ZdddZdS )ResourceGuardaction_guardedr   strc                 C  s   || _ d| _d S NFr   )r4   r   r   r   r   rD   C  s   
zResourceGuard.__init__r,   r8   c                 C  s   | j rt| jd| _ d S )NT)r   r   r   r3   r   r   r   	__enter__G  s   

zResourceGuard.__enter__rK   rL   rM   rN   rO   rP   r   c                 C  s
   d| _ d S r   )r   rT   r   r   r   __exit__M  s   zResourceGuard.__exit__N)r   r   r@   r   )r   r   r   	__slots__rD   r   r   r   r   r   r   r   @  s
    

r   )r,   rA   r-   )re   rf   r,   rd   r=   )r}   r   rt   rv   r,   rr   r   )*
__future__r   collectionsr   dataclassesr   typesr   warningsr   Zlowlevelr   r	   r
   Z_compatr   Z
_eventloopr   _exceptionsr   r   _tasksr   Z_testingr   r   r   r    r$   r)   r*   r+   rA   rd   rr   r   r   r   r   r   r   r   r   r   r   r   <module>   s@    
	W[a
q

