U
    RhN                     @  s  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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eZG dd dZG d d! d!eZG d"d# d#ZG d$d% d%Z G d&d' d'e Z!G d(d) d)Z"G d*d+ d+e"Z#G d,d- d-Z$dS ).    )annotationsN)deque)	dataclass)TracebackType)AsyncLibraryNotFoundError   )
checkpoint   )get_async_backend)BusyResourceError)CancelScope)TaskInfoget_current_taskT)frozenc                   @  s   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   B./venv/lib/python3.8/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, ...]	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%   :   s   
r%   c                   @  s   e Zd ZU dZded< dS )SemaphoreStatisticszZ
    :ivar int tasks_waiting: number of tasks waiting on :meth:`~.Semaphore.acquire`

    r   r   Nr   r   r   r   r   r&   F   s   
r&   c                   @  sR   e Zd Zd dddZddddZdddd	Zddd
dZddddZdS )Eventreturnc                 C  s,   zt   W S  tk
r&   t  Y S X d S N)r
   create_eventr   EventAdapterclsr   r   r   __new__Q   s    zEvent.__new__Nonec                 C  s   t dS )z&Set the flag, notifying all listeners.NNotImplementedErrorselfr   r   r   setW   s    z	Event.setr!   c                 C  s   t dS )z5Return ``True`` if the flag is set, ``False`` if not.Nr1   r3   r   r   r   is_set[   s    zEvent.is_setc                   s   t dS )z
        Wait until the flag has been set.

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

        Nr1   r3   r   r   r   wait_   s    z
Event.waitr   c                 C  s   t dS )z8Return statistics about the current state of this event.Nr1   r3   r   r   r   
statisticsi   s    zEvent.statisticsN)r   r   r   r/   r5   r6   r7   r8   r   r   r   r   r'   P   s
   
r'   c                   @  sr   e Zd ZU dZded< d dddZedddd	Zd
dddZddddZ	d
dddZ
ddddZdS )r,   NzEvent | None_internal_eventr(   c                 C  s
   t | S r*   objectr/   r-   r   r   r   r/   q   s    zEventAdapter.__new__r'   c                 C  s   | j d krt  | _ | j S r*   )r9   r
   r+   r3   r   r   r   _eventt   s    
zEventAdapter._eventr0   c                 C  s   | j   d S r*   )r<   r5   r3   r   r   r   r5   {   s    zEventAdapter.setr!   c                 C  s   | j d k	o| j  S r*   )r9   r6   r3   r   r   r   r6   ~   s    zEventAdapter.is_setc                   s   | j  I d H  d S r*   )r<   r7   r3   r   r   r   r7      s    zEventAdapter.waitr   c                 C  s   | j d krtddS | j  S Nr   )r   )r9   r   r8   r3   r   r   r   r8      s    

zEventAdapter.statistics)r   r   r   r9   r   r/   propertyr<   r5   r6   r7   r8   r   r   r   r   r,   n   s   
r,   c                   @  s   e Zd Zdddd dddZddd	d
ZdddddddZddddZddddZddddZddddZ	ddddZ
dS )LockFfast_acquirer!   rA   r)   c                C  s4   zt  j|dW S  tk
r.   t|d Y S X d S Nr@   )r
   create_lockr   LockAdapterr.   rA   r   r   r   r/      s    zLock.__new__r0   r(   c                   s   |   I d H  d S r*   acquirer3   r   r   r   
__aenter__   s    zLock.__aenter__type[BaseException] | NoneBaseException | NoneTracebackType | Noneexc_typeexc_valexc_tbr)   c                   s   |    d S r*   releaser4   rN   rO   rP   r   r   r   	__aexit__   s    zLock.__aexit__c                   s   t dS zAcquire the lock.Nr1   r3   r   r   r   rH      s    zLock.acquirec                 C  s   t dS zw
        Acquire the lock, without blocking.

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

        Nr1   r3   r   r   r   acquire_nowait   s    zLock.acquire_nowaitc                 C  s   t dS zRelease the lock.Nr1   r3   r   r   r   rR      s    zLock.releasec                 C  s   t dS )*Return True if the lock is currently held.Nr1   r3   r   r   r   r"      s    zLock.lockedr    c                 C  s   t dS )zh
        Return statistics about the current state of this lock.

        .. versionadded:: 3.0
        Nr1   r3   r   r   r   r8      s    zLock.statisticsN)r   r   r   r/   rI   rT   rH   rW   rR   r"   r8   r   r   r   r   r?      s   	r?   c                   @  s   e Zd ZU dZded< dddd ddd	Zddddd
dZeddddZddddZ	dddddddZ
ddddZddddZddddZdddd Zd!dd"d#ZdS )$rE   NLock | None_internal_lockFr@   r!   rB   c                C  s
   t | S r*   r:   rF   r   r   r   r/      s    zLockAdapter.__new__c                C  s
   || _ d S r*   )_fast_acquire)r4   rA   r   r   r   __init__   s    zLockAdapter.__init__r?   r(   c                 C  s"   | j d krt j| jd| _ | j S rC   )r[   r
   rD   r\   r3   r   r   r   _lock   s
    
zLockAdapter._lockr0   c                   s   | j  I d H  d S r*   r^   rH   r3   r   r   r   rI      s    zLockAdapter.__aenter__rJ   rK   rL   rM   c                   s   | j d k	r| j   d S r*   )r[   rR   rS   r   r   r   rT      s    
zLockAdapter.__aexit__c                   s   | j  I dH  dS rU   r_   r3   r   r   r   rH      s    zLockAdapter.acquirec                 C  s   | j   dS rV   )r^   rW   r3   r   r   r   rW      s    zLockAdapter.acquire_nowaitc                 C  s   | j   dS rX   r^   rR   r3   r   r   r   rR      s    zLockAdapter.releasec                 C  s
   | j  S )rY   r^   r"   r3   r   r   r   r"      s    zLockAdapter.lockedr    c                 C  s    | j dkrtdddS | j  S )zi
        Return statistics about the current state of this lock.

        .. versionadded:: 3.0

        NFr   )r[   r    r8   r3   r   r   r   r8      s    
zLockAdapter.statistics)r   r   r   r[   r   r/   r]   r>   r^   rI   rT   rH   rW   rR   r"   r8   r   r   r   r   rE      s   
		rE   c                   @  s   e Zd ZU dZded< d)ddddZdd	d
dZdddddddZdd	ddZdd	ddZ	dd	ddZ
dd	ddZdd	ddZd*dddd d!Zdd	d"d#Zdd	d$d%Zd&d	d'd(ZdS )+	ConditionNr#   _owner_taskrZ   )lockc                 C  s   |pt  | _t | _d S r*   )r?   r^   r   _waiters)r4   rd   r   r   r   r]      s    zCondition.__init__r0   r(   c                   s   |   I d H  d S r*   rG   r3   r   r   r   rI     s    zCondition.__aenter__rJ   rK   rL   rM   c                   s   |    d S r*   rQ   rS   r   r   r   rT     s    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   RuntimeErrorr3   r   r   r   _check_acquired  s    zCondition._check_acquiredc                   s   | j  I dH  t | _dS )zAcquire the underlying lock.N)r^   rH   r   rc   r3   r   r   r   rH     s    zCondition.acquirec                 C  s   | j   t | _dS z
        Acquire the underlying lock, without blocking.

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

        N)r^   rW   r   rc   r3   r   r   r   rW     s    
zCondition.acquire_nowaitc                 C  s   | j   dS )zRelease the underlying lock.Nr`   r3   r   r   r   rR      s    zCondition.releaser!   c                 C  s
   | j  S )zReturn True if the lock is set.ra   r3   r   r   r   r"   $  s    zCondition.lockedr	   r   )nr)   c              	   C  sL   |    t|D ]6}z| j }W n tk
r<   Y  qHY nX |  qdS )zNotify exactly n listeners.N)rg   rangere   popleft
IndexErrorr5   )r4   ri   _eventr   r   r   notify(  s    zCondition.notifyc                 C  s*   |    | jD ]}|  q| j  dS )zNotify all the listeners.N)rg   re   r5   clearr4   rn   r   r   r   
notify_all3  s    

zCondition.notify_allc                   s   t  I dH  t }| j| |   zBz| I dH  W n* tk
rd   |	 s^| j
|  Y nX W 5 tdd |  I dH  W 5 Q R X X dS )zWait for a notification.NT)Zshield)r   r'   re   appendrR   r   rH   r7   BaseExceptionr6   removerq   r   r   r   r7   ;  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%   lenre   r^   r8   r3   r   r   r   r8   L  s    zCondition.statistics)N)r	   )r   r   r   rc   r   r]   rI   rT   rg   rH   rW   rR   r"   ro   rr   r7   r8   r   r   r   r   rb      s   

rb   c                   @  s   e Zd Zddddddd ddd	Zddddddd
ddZd dddZdddddddZddddZddddZddddZ	e
ddddZe
dddd Zd!dd"d#ZdS )$	SemaphoreNF	max_valuerA   r   
int | Noner!   initial_valuery   rA   r)   c                C  s:   zt  j|||dW S  tk
r4   t||d Y S X d S )Nrx   ry   )r
   create_semaphorer   SemaphoreAdapterr.   r|   ry   rA   r   r   r   r/   V  s      zSemaphore.__new__)r|   ry   rA   c                C  sV   t |tstd|dk r"td|d k	rLt |ts<td||k rLtd|| _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
ValueErrorr\   r4   r|   ry   rA   r   r   r   r]   d  s    

zSemaphore.__init__r(   c                   s   |   I d H  | S r*   rG   r3   r   r   r   rI   y  s    zSemaphore.__aenter__rJ   rK   rL   r0   rM   c                   s   |    d S r*   rQ   rS   r   r   r   rT   }  s    zSemaphore.__aexit__c                   s   t dS )z5Decrement the semaphore value, blocking if necessary.Nr1   r3   r   r   r   rH     s    zSemaphore.acquirec                 C  s   t dS rh   r1   r3   r   r   r   rW     s    zSemaphore.acquire_nowaitc                 C  s   t dS )zIncrement the semaphore value.Nr1   r3   r   r   r   rR     s    zSemaphore.releasec                 C  s   t dS )z#The current value of the semaphore.Nr1   r3   r   r   r   value  s    zSemaphore.valuec                 C  s   t dS )z#The maximum value of the semaphore.Nr1   r3   r   r   r   ry     s    zSemaphore.max_valuer&   c                 C  s   t dS )zm
        Return statistics about the current state of this semaphore.

        .. versionadded:: 3.0
        Nr1   r3   r   r   r   r8     s    zSemaphore.statistics)r   r   r   r/   r]   rI   rT   rH   rW   rR   r>   r   ry   r8   r   r   r   r   rw   U  s    	rw   c                      s   e Zd ZU dZded< ddddddd d	d
dZdddddddd	 fddZeddddZddddZ	ddddZ
ddddZeddddZeddddZddddZ  ZS ) r   NzSemaphore | None_internal_semaphoreFrx   r   rz   r!   r{   c                C  s
   t | S r*   r:   r   r   r   r   r/     s    zSemaphoreAdapter.__new__r0   c                  s"   t  j|||d || _|| _d S )Nrx   )superr]   _initial_value
_max_valuer   	__class__r   r   r]     s    zSemaphoreAdapter.__init__rw   r(   c                 C  s&   | j d kr t j| j| jd| _ | j S )Nr}   )r   r
   r~   r   r   r3   r   r   r   
_semaphore  s    
 zSemaphoreAdapter._semaphorec                   s   | j  I d H  d S r*   )r   rH   r3   r   r   r   rH     s    zSemaphoreAdapter.acquirec                 C  s   | j   d S r*   )r   rW   r3   r   r   r   rW     s    zSemaphoreAdapter.acquire_nowaitc                 C  s   | j   d S r*   )r   rR   r3   r   r   r   rR     s    zSemaphoreAdapter.releasec                 C  s   | j d kr| jS | jjS r*   )r   r   r   r   r3   r   r   r   r     s    
zSemaphoreAdapter.valuec                 C  s   | j S r*   )r   r3   r   r   r   ry     s    zSemaphoreAdapter.max_valuer&   c                 C  s   | j d krtddS | j S r=   )r   r&   r   r8   r3   r   r   r   r8     s    

zSemaphoreAdapter.statistics)r   r   r   r   r   r/   r]   r>   r   rH   rW   rR   r   ry   r8   __classcell__r   r   r   r   r     s"   
r   c                   @  s   e Zd Zdd dddZddddZd	d
dddddZeddddZejdddddZeddddZ	eddddZ
ddddZdddddZdddd Zdddd!d"Zddd#d$Zdddd%d&Zd'dd(d)Zd*S )+CapacityLimiterr   r   r)   c                 C  s0   zt  |W S  tk
r*   t| Y S X d S r*   )r
   create_capacity_limiterr   CapacityLimiterAdapterr.   r   r   r   r   r/     s    zCapacityLimiter.__new__r0   r(   c                   s   t d S r*   r1   r3   r   r   r   rI     s    zCapacityLimiter.__aenter__rJ   rK   rL   bool | NonerM   c                   s   t d S r*   r1   rS   r   r   r   rT     s    zCapacityLimiter.__aexit__c                 C  s   t dS )aM  
        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.

        Nr1   r3   r   r   r   r     s    zCapacityLimiter.total_tokensr   r)   c                 C  s   t d S r*   r1   r4   r   r   r   r   r     s    r   c                 C  s   t dS )z7The number of tokens that have currently been borrowed.Nr1   r3   r   r   r   r   	  s    zCapacityLimiter.borrowed_tokensc                 C  s   t dS )z7The number of tokens currently available to be borrowedNr1   r3   r   r   r   available_tokens  s    z CapacityLimiter.available_tokensc                 C  s   t dS )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

        Nr1   r3   r   r   r   rW     s    zCapacityLimiter.acquire_nowaitr;   borrowerr)   c                 C  s   t dS )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

        Nr1   r4   r   r   r   r   acquire_on_behalf_of_nowait  s    z+CapacityLimiter.acquire_on_behalf_of_nowaitc                   s   t dS )zr
        Acquire a token for the current task, waiting if necessary for one to become
        available.

        Nr1   r3   r   r   r   rH   '  s    zCapacityLimiter.acquirec                   s   t dS )z
        Acquire a token, waiting if necessary for one to become available.

        :param borrower: the entity borrowing a token

        Nr1   r   r   r   r   acquire_on_behalf_of/  s    z$CapacityLimiter.acquire_on_behalf_ofc                 C  s   t dS )z
        Release the token held by the current task.

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

        Nr1   r3   r   r   r   rR   8  s    zCapacityLimiter.releasec                 C  s   t dS )z
        Release the token held by the given borrower.

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

        Nr1   r   r   r   r   release_on_behalf_ofB  s    z$CapacityLimiter.release_on_behalf_ofr   c                 C  s   t dS )zl
        Return statistics about the current state of this limiter.

        .. versionadded:: 3.0

        Nr1   r3   r   r   r   r8   L  s    zCapacityLimiter.statisticsN)r   r   r   r/   rI   rT   r>   r   setterr   r   rW   r   rH   r   rR   r   r8   r   r   r   r   r     s$   

	

r   c                   @  s"  e Zd ZU dZded< dd dddZdddd	d
ZeddddZddddZ	dddddddZ
eddddZejdddddZeddddZedddd Zddd!d"Zd#dd$d%d&Zddd'd(Zd#dd$d)d*Zddd+d,Zd#dd$d-d.Zd/dd0d1ZdS )2r   NzCapacityLimiter | None_internal_limiterr   r   c                 C  s
   t | S r*   r:   r   r   r   r   r/   Y  s    zCapacityLimiterAdapter.__new__r0   c                 C  s
   || _ d S r*   )r   )r4   r   r   r   r   r]   \  s    zCapacityLimiterAdapter.__init__r   r(   c                 C  s    | j d krt | j| _ | j S r*   )r   r
   r   _total_tokensr3   r   r   r   _limiter_  s
    
zCapacityLimiterAdapter._limiterc                   s   | j  I d H  d S r*   )r   rI   r3   r   r   r   rI   h  s    z!CapacityLimiterAdapter.__aenter__rJ   rK   rL   r   rM   c                   s   | j |||I d H S r*   )r   rT   rS   r   r   r   rT   k  s    z CapacityLimiterAdapter.__aexit__c                 C  s   | j d kr| jS | j jS r*   )r   r   r   r3   r   r   r   r   s  s    
z#CapacityLimiterAdapter.total_tokensr   c                 C  sN   t |ts|tjk	rtdn|dk r.td| jd krB|| _d S || j_	d S )Nz'total_tokens must be an int or math.infr	   ztotal_tokens must be >= 1)
r   r   mathinfr   r   r   r   r   r   r   r   r   r   r   z  s    

r   c                 C  s   | j d krdS | j jS )Nr   )r   r   r3   r   r   r   r     s    
z&CapacityLimiterAdapter.borrowed_tokensc                 C  s   | j d kr| jS | j jS r*   )r   r   r   r3   r   r   r   r     s    
z'CapacityLimiterAdapter.available_tokensc                 C  s   | j   d S r*   )r   rW   r3   r   r   r   rW     s    z%CapacityLimiterAdapter.acquire_nowaitr;   r   c                 C  s   | j | d S r*   )r   r   r   r   r   r   r     s    z2CapacityLimiterAdapter.acquire_on_behalf_of_nowaitc                   s   | j  I d H  d S r*   )r   rH   r3   r   r   r   rH     s    zCapacityLimiterAdapter.acquirec                   s   | j |I d H  d S r*   )r   r   r   r   r   r   r     s    z+CapacityLimiterAdapter.acquire_on_behalf_ofc                 C  s   | j   d S r*   )r   rR   r3   r   r   r   rR     s    zCapacityLimiterAdapter.releasec                 C  s   | j | d S r*   )r   r   r   r   r   r   r     s    z+CapacityLimiterAdapter.release_on_behalf_ofr   c                 C  s&   | j d krtd| jdddS | j  S )Nr   r   )r   r   r   r   )r   r   r   r8   r3   r   r   r   r8     s    
z!CapacityLimiterAdapter.statistics)r   r   r   r   r   r/   r]   r>   r   rI   rT   r   r   r   r   rW   r   rH   r   rR   r   r8   r   r   r   r   r   V  s,   
r   c                   @  sF   e Zd ZdZdZdddddZdd	d
dZdddddddZdS )ResourceGuarda  
    A context manager for ensuring that a resource is only used by a single task at a
    time.

    Entering this context manager while the previous has not exited it yet will trigger
    :exc:`BusyResourceError`.

    :param action: the action to guard against (visible in the :exc:`BusyResourceError`
        when triggered, e.g. "Another task is already {action} this resource")

    .. versionadded:: 4.1
    action_guardedusingstr)r   c                 C  s   || _ d| _d S NFr   )r4   r   r   r   r   r]     s    zResourceGuard.__init__r0   r(   c                 C  s   | j rt| jd| _ d S )NT)r   r   r   r3   r   r   r   	__enter__  s    
zResourceGuard.__enter__rJ   rK   rL   r   rM   c                 C  s
   d| _ d S r   )r   rS   r   r   r   __exit__  s    zResourceGuard.__exit__N)r   )r   r   r   r   	__slots__r]   r   r   r   r   r   r   r     s
   r   )%Z
__future__r   r   collectionsr   Zdataclassesr   typesr   Zsniffior   Zlowlevelr   Z
_eventloopr
   _exceptionsr   Z_tasksr   Z_testingr   r   r   r   r    r%   r&   r'   r,   r?   rE   rb   rw   r   r   r   r   r   r   r   r   <module>   s<   	0@ZT;r]