U
    (–€h)  ã                   @  sÔ   d dl mZ d dlZd dl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 dd	lmZ d d
lmZ dZG dd„ dejƒZG dd„ dƒZeG dd„ deƒƒZdddœdd„Zdddœdd„ZdS )é    )ÚannotationsN)ÚTracebackType)ÚfinalÚOptionalÚType)Úevents)Ú
exceptions)Útasksé   )Úinstall)ÚSelf)ÚTimeoutÚtimeoutÚ
timeout_atc                   @  s    e Zd ZdZdZdZdZdZdS )Ú_StateÚcreatedÚactiveZexpiringÚexpiredÚfinishedN)Ú__name__Ú
__module__Ú__qualname__ÚCREATEDÚENTEREDÚEXPIRINGÚEXPIREDÚEXITED© r   r   ú6/tmp/pip-unpacked-wheel-rrqp3fat/taskgroup/timeouts.pyr      s
   r   c                   @  sŠ   e Zd Z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„Zddœdd„Z	dddddœdd„Z
ddœdd„ZdS )Ú_TimeoutzšAsynchronous context manager for cancelling overdue coroutines.

    Use `timeout()` or `timeout_at()` rather than instantiating this class directly.
    úOptional[float]ÚNone©ÚwhenÚreturnc                 C  s   t j| _d| _d| _|| _dS )zòSchedule a timeout that will trigger at a given loop time.

        - If `when` is `None`, the timeout will never trigger.
        - If `when < loop.time()`, the timeout will trigger on the next
          iteration of the event loop.
        N)r   r   Ú_stateÚ_timeout_handlerÚ_taskÚ_when)Úselfr#   r   r   r   Ú__init__)   s    z_Timeout.__init__©r$   c                 C  s   | j S )zReturn the current deadline.)r(   ©r)   r   r   r   r#   6   s    z_Timeout.whenc                 C  s–   | j tjk	r4| j tjkr tdƒ‚td| j j› d�ƒ‚|| _| jdk	rN| j ¡  |dkr^d| _n4t	 
¡ }|| ¡ kr‚| | j¡| _n| || j¡| _dS )zReschedule the timeout.zTimeout has not been enteredzCannot change state of z TimeoutN)r%   r   r   r   ÚRuntimeErrorÚvaluer(   r&   Úcancelr   Úget_running_loopÚtimeZ	call_soonÚ_on_timeoutZcall_at)r)   r#   Úloopr   r   r   Ú
reschedule:   s    ÿ

z_Timeout.rescheduleÚboolc                 C  s   | j tjtjfkS )z$Is timeout expired during execution?)r%   r   r   r   r,   r   r   r   r   Q   s    z_Timeout.expiredÚstrc                 C  s\   dg}| j tjkr<| jd k	r(t| jdƒnd }| d|› �¡ d |¡}d| j j› d|› d�S )NÚ é   zwhen=ú z
<Timeout [ú]ú>)r%   r   r   r(   ÚroundÚappendÚjoinr.   )r)   Úinfor#   Zinfo_strr   r   r   Ú__repr__U   s    
z_Timeout.__repr__r   c                 Ã  sV   | j tjk	rtdƒ‚t ¡ }|d kr,tdƒ‚tj| _ || _| j ¡ | _	|  
| j¡ | S )Nz Timeout has already been enteredz$Timeout should be used inside a task)r%   r   r   r-   r	   Zcurrent_taskr   r'   Z
cancellingÚ_cancellingr4   r(   )r)   Ztaskr   r   r   Ú
__aenter__]   s    z_Timeout.__aenter__úOptional[Type[BaseException]]úOptional[BaseException]úOptional[TracebackType]úOptional[bool]©Úexc_typeÚexc_valÚexc_tbr$   c                 Ã  s~   | j tjtjfkst‚| jd k	r0| j ¡  d | _| j tjkrftj| _ | j 	¡ | j
krz|tjkrzt|‚n| j tjkrztj| _ d S ©N)r%   r   r   r   ÚAssertionErrorr&   r/   r   r'   ZuncancelrA   r   ZCancelledErrorÚTimeoutErrorr   ©r)   rH   rI   rJ   r   r   r   Ú	__aexit__i   s    

ÿþz_Timeout.__aexit__c                 C  s:   | j d k	st‚| jtjkst‚| j  ¡  tj| _d | _d S rK   )r'   rL   r%   r   r   r/   r   r&   r,   r   r   r   r2   „   s
    
z_Timeout._on_timeoutN)r   r   r   Ú__doc__r*   r#   r4   r   r@   rB   rO   r2   r   r   r   r   r   #   s   r   c                      s@   e Zd ZU ded< ddœ‡ fdd„Zddd	d
dœdd„Z‡  ZS )r   zcontextlib.AsyncExitStackÚ_Timeout__stackr   r+   c              
   ƒ  sb   t  ¡ 4 I d H šB}| t ¡ ¡I d H  tƒ  ¡ I d H  | tƒ j¡ | 	¡ | _
W 5 Q I d H R X | S rK   )Ú
contextlibÚAsyncExitStackÚenter_async_contextÚ_installZinstall_uncancelÚsuperrB   Úpush_async_exitrO   Úpop_allrQ   )r)   Ústack©Ú	__class__r   r   rB   ‘   s    zTimeout.__aenter__rC   rD   rE   rF   rG   c                 Ã  s   | j  |||¡I d H S rK   )rQ   rO   rN   r   r   r   rO   ™   s    zTimeout.__aexit__)r   r   r   Ú__annotations__rB   rO   Ú__classcell__r   r   rZ   r   r   �   s   
r   r    )Údelayr$   c                 C  s$   t  ¡ }t| dk	r| ¡ |  ndƒS )a	  Timeout async context manager.

    Useful in cases when you want to apply timeout logic around block
    of code or in cases when asyncio.wait_for is not suitable. For example:

    >>> async with asyncio.timeout(10):  # 10 seconds timeout
    ...     await long_running_task()


    delay - value in seconds or None to disable timeout logic

    long_running_task() is interrupted by raising asyncio.CancelledError,
    the top-most affected timeout() context manager converts CancelledError
    into TimeoutError.
    N)r   r0   r   r1   )r^   r3   r   r   r   r   ¢   s    r   r"   c                 C  s   t | ƒS )ab  Schedule the timeout at absolute time.

    Like timeout() but argument gives absolute time in the same clock system
    as loop.time().

    Please note: it is not POSIX time but a time with
    undefined starting base, e.g. the time of the system power on.

    >>> async with asyncio.timeout_at(loop.time() + 10):
    ...     await long_running_task()


    when - a deadline when timeout occurs or None to disable timeout logic

    long_running_task() is interrupted by raising asyncio.CancelledError,
    the top-most affected timeout() context manager converts CancelledError
    into TimeoutError.
    )r   )r#   r   r   r   r   ¶   s    r   )Ú
__future__r   rR   ÚenumÚtypesr   Útypingr   r   r   Zasyncior   r   r	   r7   r   rU   Ztyping_extensionsr   Ú__all__ÚEnumr   r   r   r   r   r   r   r   r   Ú<module>   s    j