U
    gI                     @  s  U d dl mZ d dlZd dlmZ d dlmZmZmZm	Z	m
Z
 d dlmZ ddlmZ e	dZe	d	Zd
dddZd
dddZd
dddZddddZe Zded< i Zded< eddG dd dZG dd dejZG dd  d ee ZG d!d" d"ee ZdS )#    )annotationsN)	dataclass)AnyGenericLiteralTypeVaroverload)WeakKeyDictionary   )get_async_backendTDNonereturnc                     s   t   I dH  dS )z
    Check for cancellation and allow the scheduler to switch to another task.

    Equivalent to (but more efficient than)::

        await checkpoint_if_cancelled()
        await cancel_shielded_checkpoint()


    .. versionadded:: 3.0

    N)r   
checkpoint r   r   2/tmp/pip-unpacked-wheel-3u0cc9gf/anyio/lowlevel.pyr      s    r   c                     s   t   I dH  dS )z
    Enter a checkpoint if the enclosing cancel scope has been cancelled.

    This does not allow the scheduler to switch to a different task.

    .. versionadded:: 3.0

    N)r   checkpoint_if_cancelledr   r   r   r   r      s    	r   c                     s   t   I dH  dS )z
    Allow the scheduler to switch to another task but without checking for cancellation.

    Equivalent to (but potentially more efficient than)::

        with CancelScope(shield=True):
            await checkpoint()


    .. versionadded:: 3.0

    N)r   cancel_shielded_checkpointr   r   r   r   r   *   s    r   objectc                   C  s
   t   S )ze
    Return a backend specific token object that can be used to get back to the event
    loop.

    )r   current_tokenr   r   r   r   r   :   s    r   z&WeakKeyDictionary[Any, dict[str, Any]]	_run_varszdict[Any, _TokenWrapper]_token_wrappersT)frozenc                   @  s   e Zd ZU dZded< dS )_TokenWrapper)_token__weakref__r   r   N)__name__
__module____qualname__	__slots____annotations__r   r   r   r   r   G   s   
r   c                   @  s   e Zd Ze ZdS )_NoValueSetN)r   r   r    enumautoNO_VALUE_SETr   r   r   r   r#   M   s   r#   c                   @  s    e Zd ZdZdddddZdS )RunvarToken_var_value	_redeemedz	RunVar[T]%T | Literal[_NoValueSet.NO_VALUE_SET])varvaluec                 C  s   || _ || _d| _d S )NFr(   )selfr-   r.   r   r   r   __init__T   s    zRunvarToken.__init__N)r   r   r    r!   r0   r   r   r   r   r'   Q   s   r'   c                   @  s   e Zd ZU dZdZejZded< e Z	ded< efddd	d
dZ
eddddZedddddZeddddZefdddddZdddddZddddd Zddd!d"Zd#S )$RunVarz[
    Like a :class:`~contextvars.ContextVar`, except scoped to the running event loop.
    _name_defaultz!Literal[_NoValueSet.NO_VALUE_SET]r&   zset[_TokenWrapper]r   strr,   )namedefaultc                 C  s   || _ || _d S Nr2   )r/   r6   r7   r   r   r   r0   e   s    zRunVar.__init__zdict[str, T]r   c                 C  s:   t  }z
t| W S  tk
r4   i  }t|< | Y S X d S r8   )r   r   KeyError)r/   tokenZrun_varsr   r   r   _current_varsk   s    
zRunVar._current_varsr   zT | D)r7   r   c                 C  s   d S r8   r   r/   r7   r   r   r   gett   s    z
RunVar.getr   c                 C  s   d S r8   r   r/   r   r   r   r=   w   s    z%D | Literal[_NoValueSet.NO_VALUE_SET]c                 C  sb   z| j | j W S  tk
rJ   |tjk	r0| Y S | jtjk	rF| j Y S Y nX td| j dd S )NzRun variable "z!" has no value and no default set)r;   r3   r9   r1   r&   r4   LookupErrorr<   r   r   r   r=   z   s    
zRunvarToken[T])r.   r   c                 C  s*   | j }t| || jtj}||| j< |S r8   )r;   r'   r=   r3   r1   r&   )r/   r.   Zcurrent_varsr:   r   r   r   set   s    
z
RunVar.setr   )r:   r   c                 C  sj   |j | k	rtd|jr td|jtjkrRz| j| j= W q` tk
rN   Y q`X n|j| j| j< d|_d S )Nz)This token does not belong to this RunVarz This token has already been usedT)	r)   
ValueErrorr+   r*   r#   r&   r;   r3   r9   )r/   r:   r   r   r   reset   s    
zRunVar.resetc                 C  s   d| j dS )Nz<RunVar name=>)r3   r>   r   r   r   __repr__   s    zRunVar.__repr__N)r   r   r    __doc__r!   r#   r&   r"   r@   r   r0   propertyr;   r   r=   rB   rD   r   r   r   r   r1   Z   s"   
r1   )
__future__r   r$   Zdataclassesr   typingr   r   r   r   r   weakrefr	   Z_core._eventloopr   r   r   r   r   r   r   r   r"   r   r   Enumr#   r'   r1   r   r   r   r   <module>   s&    		