o
    :ήcA                     @   s  d dl mZ d dlmZ d dlmZ d dlmZmZm	Z	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$e#fd%d&Z'd7d(ee# d$e$fd)d*Z(d$e"fd+d,Z)d'd-d.e*d/ee* d$e%fd0d1Z+d2e,d$e&fd3d4Z-G d5d6 d6Z.d'S )8    )deque)	dataclass)TracebackType)DequeOptionalTupleType)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eed< dS )EventStatisticszR
    :ivar int tasks_waiting: number of tasks waiting on :meth:`~.Event.wait`
    tasks_waitingN__name__
__module____qualname____doc__int__annotations__ r!   r!   C/tmp/pip-target-vg8gfxp4/lib/python/anyio/_core/_synchronization.pyr      s   
 r   c                   @   s:   e Zd ZU dZeed< eed< eedf ed< e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`
    borrowed_tokenstotal_tokens.	borrowersr   N)	r   r   r   r   r   r    floatr   objectr!   r!   r!   r"   r#      s   
 	r#   c                   @   s.   e Zd ZU dZeed< ee ed< e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`
    lockedownerr   N)	r   r   r   r   boolr    r   r   r   r!   r!   r!   r"   r)   )   s
   
 r)   c                   @   s"   e Zd ZU dZeed< e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   lock_statisticsN)r   r   r   r   r   r    r)   r!   r!   r!   r"   r-   7   s   
 r-   c                   @   r   )SemaphoreStatisticszZ
    :ivar int tasks_waiting: number of tasks waiting on :meth:`~.Semaphore.acquire`

    r   Nr   r!   r!   r!   r"   r/   B   s   
 r/   c                   @   sJ   e Zd ZdddZdefddZdefddZdd	d
Zde	fddZ
dS )Eventreturnc                 C   s
   t   S N)r   r0   )clsr!   r!   r"   __new__M   s   
zEvent.__new__c                 C      t )z&Set the flag, notifying all listeners.NotImplementedErrorselfr!   r!   r"   setP      z	Event.setc                 C   r5   )z5Return ``True`` if the flag is set, ``False`` if not.r6   r8   r!   r!   r"   is_setT   r;   zEvent.is_setNc                       t )z
        Wait until the flag has been set.

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

        r6   r8   r!   r!   r"   waitX      z
Event.waitc                 C   r5   )z8Return statistics about the current state of this event.r6   r8   r!   r!   r"   
statisticsa   r;   zEvent.statistics)r1   r0   r1   N)r   r   r   r4   r   r:   r,   r<   r>   r   r@   r!   r!   r!   r"   r0   L   s    

	r0   c                   @   s   e Zd ZU dZee ed< dddZdddZdee	e
  d	ee
 d
ee ddfddZdddZdddZdefddZdefddZdefddZdS )LockN_owner_taskr1   c                 C   s   t  | _d S r2   )r   _waitersr8   r!   r!   r"   __init__i      zLock.__init__c                       |   I d H  d S r2   acquirer8   r!   r!   r"   
__aenter__l      zLock.__aenter__exc_typeexc_valexc_tbc                       |    d S r2   releaser9   rL   rM   rN   r!   r!   r"   	__aexit__o      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   r0   rD   appendr>   BaseExceptionr<   removerC   rQ   r   )r9   taskeventtokenr!   r!   r"   rI   w   s6   
zLock.acquirec                 C   s0   t  }| j|krtd| jdurt|| _dS )zq
        Acquire the lock, without blocking.

        :raises ~WouldBlock: if the operation would block

        z)Attempted to acquire an already held LockN)r   rC   RuntimeErrorr   )r9   rY   r!   r!   r"   rU      s   


zLock.acquire_nowaitc                 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)rC   r   r\   rD   popleftr:   r   rQ   r9   rZ   r!   r!   r"   rQ      s   

zLock.releasec                 C   s
   | j duS )z*Return True if the lock is currently held.N)rC   r8   r!   r!   r"   r*         
zLock.lockedc                 C   s   t |  | jt| jS )zh
        Return statistics about the current state of this lock.

        .. versionadded:: 3.0
        )r)   r*   rC   lenrD   r8   r!   r!   r"   r@      s   zLock.statisticsrA   )r   r   r   rC   r   r   r    rE   rJ   r   rW   r   rS   rI   rU   r   rQ   r,   r*   r)   r@   r!   r!   r!   r"   rB   f   s$   
 





rB   c                   @   s   e Zd ZU dZee ed< d"dee fddZd#ddZ	d	ee
e  d
ee dee ddfddZd#ddZd#ddZd#ddZdefddZdefddZd$deddfddZd#ddZd#ddZdefd d!ZdS )%	ConditionNrC   lockc                 C   s   |pt  | _t | _d S r2   )rB   _lockr   rD   )r9   rb   r!   r!   r"   rE      s   zCondition.__init__r1   c                    rG   r2   rH   r8   r!   r!   r"   rJ      rK   zCondition.__aenter__rL   rM   rN   c                    rO   r2   rP   rR   r!   r!   r"   rS      rT   zCondition.__aexit__c                 C   s   | j t kr
tdd S )Nz3The current task is not holding the underlying lock)rC   r   r\   r8   r!   r!   r"   _check_acquired   s   zCondition._check_acquiredc                    s   | j  I dH  t | _dS )zAcquire the underlying lock.N)rc   rI   r   rC   r8   r!   r!   r"   rI      s   zCondition.acquirec                 C   s   | j   t | _dS )|
        Acquire the underlying lock, without blocking.

        :raises ~WouldBlock: if the operation would block

        N)rc   rU   r   rC   r8   r!   r!   r"   rU      s   
zCondition.acquire_nowaitc                 C   s   | j   t| jS )zRelease the underlying lock.)rc   rQ   r   r8   r!   r!   r"   rQ      s   

zCondition.releasec                 C   s
   | j  S )zReturn True if the lock is set.)rc   r*   r8   r!   r!   r"   r*      r_   zCondition.lockedr   nc              	   C   sH   |    t|D ]}z| j }W n ty   Y  dS w |  qdS )zNotify exactly n listeners.N)rd   rangerD   r]   
IndexErrorr:   )r9   rf   _rZ   r!   r!   r"   notify   s   
zCondition.notifyc                 C   s*   |    | jD ]}|  q| j  dS )zNotify all the listeners.N)rd   rD   r:   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   r0   rD   rV   rQ   r>   rW   r<   rX   r   rI   r^   r!   r!   r"   r>      s$   .zCondition.waitc                 C   s   t t| j| j S )zm
        Return statistics about the current state of this condition.

        .. versionadded:: 3.0
        )r-   r`   rD   rc   r@   r8   r!   r!   r"   r@     s   zCondition.statisticsr2   rA   )r   )r   r   r   rC   r   r   r    rB   rE   rJ   r   rW   r   rS   rd   rI   rU   r   rQ   r,   r*   r   rj   rl   r>   r-   r@   r!   r!   r!   r"   ra      s,   
 








ra   c                   @   s   e Zd Zdddedee fddZddd	Zd
eee  dee dee	 ddfddZ
dddZdddZdefddZedefddZedee fddZdefddZdS )	SemaphoreN	max_valueinitial_valuerp   c                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   rD   )r9   rq   rp   r!   r!   r"   rE     s   

zSemaphore.__init__r1   c                    s   |   I d H  | S r2   rH   r8   r!   r!   r"   rJ   *  s   zSemaphore.__aenter__rL   rM   rN   c                    rO   r2   rP   rR   r!   r!   r"   rS   .  rT   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   rU   r   r0   rD   rV   r>   rW   r<   rX   rQ   r   r^   r!   r!   r"   rI   6  s.   zSemaphore.acquirec                 C   s    | j dkrt|  j d8  _ dS )re   r   r   N)ru   r   r8   r!   r!   r"   rU   N  s   
zSemaphore.acquire_nowaitc                 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   )rv   ru   rt   rD   r]   r:   r   rQ   r8   r!   r!   r"   rQ   Z  s   
zSemaphore.releasec                 C      | j S )z#The current value of the semaphore.)ru   r8   r!   r!   r"   valuef     zSemaphore.valuec                 C   rw   )z#The maximum value of the semaphore.)rv   r8   r!   r!   r"   rp   k  ry   zSemaphore.max_valuec                 C   s   t t| jS )zm
        Return statistics about the current state of this semaphore.

        .. versionadded:: 3.0
        )r/   r`   rD   r8   r!   r!   r"   r@   p  s   zSemaphore.statistics)r1   rn   rA   )r   r   r   r   r   rE   rJ   r   rW   r   rS   rI   rU   r   rQ   propertyrx   rp   r/   r@   r!   r!   r!   r"   rn     s(    




rn   c                	   @   s  e Zd Zdedd fddZd&ddZdeee  d	ee d
ee	 dee
 fddZedefddZejdeddfddZdeddfddZedefddZedefddZdefddZdedefddZd&ddZdeddfddZd&d d!Zdeddfd"d#Zdefd$d%ZdS )'CapacityLimiterr%   r1   c                 C   s   t  |S r2   )r   r{   )r3   r%   r!   r!   r"   r4   z  rF   zCapacityLimiter.__new__Nc                    r=   r2   r6   r8   r!   r!   r"   rJ   }  s   zCapacityLimiter.__aenter__rL   rM   rN   c                    r=   r2   r6   rR   r!   r!   r"   rS     s   zCapacityLimiter.__aexit__c                 C   r5   )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.

        r6   r8   r!   r!   r"   r%     s   zCapacityLimiter.total_tokensrx   c                 C   r5   r2   r6   r9   rx   r!   r!   r"   r%     r;   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_tokensc                 C   r5   )z7The number of tokens that have currently been borrowed.r6   r8   r!   r!   r"   r$        zCapacityLimiter.borrowed_tokensc                 C   r5   )z7The number of tokens currently available to be borrowedr6   r8   r!   r!   r"   available_tokens  r   z CapacityLimiter.available_tokensc                 C   r5   )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

        r6   r8   r!   r!   r"   rU        zCapacityLimiter.acquire_nowaitborrowerc                 C   r5   )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

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

        r6   r8   r!   r!   r"   rI     s   zCapacityLimiter.acquirec                    r=   )z
        Acquire a token, waiting if necessary for one to become available.

        :param borrower: the entity borrowing a token

        r6   r   r!   r!   r"   acquire_on_behalf_of  r?   z$CapacityLimiter.acquire_on_behalf_ofc                 C   r5   )z
        Release the token held by the current task.
        :raises RuntimeError: if the current task has not borrowed a token from this limiter.

        r6   r8   r!   r!   r"   rQ     s   zCapacityLimiter.releasec                 C   r5   )z
        Release the token held by the given borrower.

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

        r6   r   r!   r!   r"   release_on_behalf_of  r   z$CapacityLimiter.release_on_behalf_ofc                 C   r5   )zl
        Return statistics about the current state of this limiter.

        .. versionadded:: 3.0

        r6   r8   r!   r!   r"   r@     r   zCapacityLimiter.statisticsrA   )r   r   r   r'   r4   rJ   r   r   rW   r   r,   rS   rz   r%   setterr~   r   r$   r   r   rU   r(   r   rI   r   rQ   r   r#   r@   r!   r!   r!   r"   r{   y  s8    


	


		r{   r1   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}   rB   r!   r!   r!   r"   create_lock  s   

r   Nrb   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rb   )r	   r}   ra   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   r0   r!   r!   r!   r"   create_event
  s   


r   ro   rx   rp   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() directlyro   )r	   r}   rn   )rx   rp   r!   r!   r"   create_semaphore  s
   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                	   @   sV   e Zd ZdZdefddZdddZd	eee	  d
ee	 dee
 dee fddZdS )ResourceGuardaction_guardedr   c                 C   s   || _ d| _d S NFr   )r9   r   r!   r!   r"   rE   B  s   
zResourceGuard.__init__r1   Nc                 C   s   | j rt| jd| _ d S )NT)r   r   r   r8   r!   r!   r"   	__enter__F  s   

zResourceGuard.__enter__rL   rM   rN   c                 C   s
   d| _ d S r   )r   rR   r!   r!   r"   __exit__L  s   zResourceGuard.__exit__rA   )r   r   r   	__slots__strrE   r   r   r   rW   r   r,   r   r!   r!   r!   r"   r   ?  s    

r   r2   )/collectionsr   dataclassesr   typesr   typingr   r   r   r   warningsr	   lowlevelr   r   r   _compatr   
_eventloopr   _exceptionsr   r   _tasksr   _testingr   r   r   r#   r)   r-   r/   r0   rB   ra   rn   r{   r   r   r   r   r   r'   r   r   r!   r!   r!   r"   <module>   s@    
	W[aq 