U
    ddG                     @   s6  U d dl Z d dlZd dlmZ d dlmZmZmZmZm	Z	m
Z
mZ d dlmZ ddlmZ ejdkrrd dlmZ n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edddZe Zi Zeedf 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 )    N)	dataclass)AnyDictGenericSetTypeVarUnionoverload)WeakKeyDictionary   )get_asynclib)      )LiteralTD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-yaxr6kle/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   c                   C   s
   t   S )zVReturn 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   _TokenWrapper_token_wrappersT)frozenc                   @   s   e Zd ZU dZeed< dS )r   )_token__weakref__r   N)__name__
__module____qualname__	__slots__object__annotations__r   r   r   r   r   E   s   
c                   @   s   e Zd Ze ZdS )_NoValueSetN)r   r    r!   enumautoNO_VALUE_SETr   r   r   r   r%   K   s   r%   c                   @   s.   e Zd ZdZdeeeej f dddZ	dS )RunvarToken_var_value	_redeemedz	RunVar[T])varvaluec                 C   s   || _ || _d| _d S )NFr*   )selfr.   r/   r   r   r   __init__R   s    zRunvarToken.__init__N)
r   r    r!   r"   r   r   r   r%   r(   r1   r   r   r   r   r)   O   s    r)   c                   @   s  e Zd ZU dZdZejZeej ed< e	 Z
ee ed< efeeeeej f dddZeeeef dd	d
Zeeeeef dddZeedddZefeeeej f eeef dddZeee dddZ	ee ddddZedddZdS )RunVarzQLike a :class:`~contextvars.ContextVar`, expect scoped to the running event loop._name_defaultr(   r   )namedefaultc                 C   s   || _ || _d S Nr3   )r0   r6   r7   r   r   r   r1   c   s    zRunVar.__init__r   c                 C   sb   t  }z
t| W S  tk
r8   t|}| j| Y q tk
rZ   i  }t|< | Y S X qd S r8   )r   	_run_vars	TypeErrorr   r   addKeyError)r0   tokenZrun_varsr   r   r   _current_varsk   s    
zRunVar._current_vars)r7   r   c                 C   s   d S r8   r   r0   r7   r   r   r   get{   s    z
RunVar.getc                 C   s   d S r8   r   r0   r   r   r   r@      s    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>   r4   r<   r2   r(   r5   LookupErrorr?   r   r   r   r@      s    
)r/   r   c                 C   s*   | j }t| || jtj}||| j< |S r8   )r>   r)   r@   r4   r2   r(   )r0   r/   Zcurrent_varsr=   r   r   r   set   s    
z
RunVar.setN)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>   r4   r<   )r0   r=   r   r   r   reset   s    
zRunVar.resetc                 C   s   d| j dS )Nz<RunVar name=>)r4   rA   r   r   r   __repr__   s    zRunVar.__repr__)r   r    r!   __doc__r"   r%   r(   r   r$   rC   r   r   r   strr   r   r1   propertyr   r>   r	   r   r@   r)   rE   rG   r   r   r   r   r2   Z   s.   

r2   )"r&   sysZdataclassesr   typingr   r   r   r   r   r   r	   weakrefr
   Z_core._eventloopr   version_infor   Ztyping_extensionsr   r   r   r   r   r#   r   r9   r   r$   r   Enumr%   r)   r2   r   r   r   r   <module>   s,    $
