
    Rh                         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Z G d	 d
e j                        Ze G d d             Zdee   defdZdee   defdZy)    N)TracebackType)finalOptionalType   )events)
exceptions)tasks)Timeouttimeout
timeout_atc                        e Zd ZdZdZdZdZdZy)_StatecreatedactiveexpiringexpiredfinishedN)__name__
__module____qualname__CREATEDENTEREDEXPIRINGEXPIREDEXITED     '/usr/lib/python3.12/asyncio/timeouts.pyr   r      s    GGHGFr   r   c            	           e Zd ZdZdee   ddfdZdee   fdZdee   ddfdZde	fdZ
defd	Zdd
Zdeee      dee   dee   dee	   fdZddZy)r   zAsynchronous context manager for cancelling overdue coroutines.

    Use `timeout()` or `timeout_at()` rather than instantiating this class directly.
    whenreturnNc                 X    t         j                  | _        d| _        d| _        || _        y)zSchedule 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!   s     r   __init__zTimeout.__init__!   s%     nn>B+/

r   c                     | j                   S )zReturn the current deadline.)r'   r(   s    r   r!   zTimeout.when.   s    zzr   c                    | j                   t        j                  urJ| j                   t        j                  u rt	        d      t	        d| j                   j
                   d      || _        | j                  | j                  j                          |d| _        yt        j                         }||j                         k  r!|j                  | j                        | _        y|j                  || j                        | _        y)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time	call_soon_on_timeoutcall_at)r(   r!   loops      r   
reschedulezTimeout.reschedule2   s    ;;fnn,{{fnn,"#ABB)$++*;*;)<HE  
  ,!!((*<$(D!**,Dtyy{"(,t7G7G(H%(,T4;K;K(L%r   c                 Z    | j                   t        j                  t        j                  fv S )z$Is timeout expired during execution?)r$   r   r   r   r+   s    r   r   zTimeout.expiredI   s    {{v???r   c                    dg}| j                   t        j                  u r8| j                  t	        | j                  d      nd }|j                  d|        dj                  |      }d| j                   j                   d| dS )N    zwhen= z
<Timeout []>)r$   r   r   r'   roundappendjoinr.   )r(   infor!   info_strs       r   __repr__zTimeout.__repr__M   ss    t;;&..(+/::+A5Q'tDKK%v'88D>DKK--.az;;r   c                 J  K   | j                   t        j                  urt        d      t	        j
                         }|t        d      t        j                  | _         || _        | j                  j                         | _	        | j                  | j                         | S w)Nz Timeout has already been enteredz$Timeout should be used inside a task)r$   r   r   r-   r
   current_taskr   r&   
cancelling_cancellingr6   r'   )r(   tasks     r   
__aenter__zTimeout.__aenter__U   s}     ;;fnn,ABB!!#<EFFnn
::002

#s   B!B#exc_typeexc_valexc_tbc                   K   | j                   t        j                  t        j                  fv sJ | j                  !| j                  j                          d | _        | j                   t        j                  u rVt        j                  | _         | j                  j                         | j                  k  r|t        j                  u rt        |y | j                   t        j                  u rt        j                  | _         y wN)r$   r   r   r   r%   r/   r   r&   uncancelrG   r	   CancelledErrorTimeoutErrorr   )r(   rJ   rK   rL   s       r   	__aexit__zTimeout.__aexit__a   s      {{v~~v????  ,!!((*$(D!;;&//) ..DKzz""$(8(88XIbIb=b #/  [[FNN* --DKs   D Dc                     | j                   t        j                  u sJ | j                  j	                          t        j
                  | _         d | _        y rN   )r$   r   r   r&   r/   r   r%   r+   s    r   r3   zTimeout._on_timeouty   s;    {{fnn,,,

oo $r   )r"   r   )r"   N)r   r   r   __doc__r   floatr)   r!   r6   boolr   strrC   rI   r   BaseExceptionr   rR   r3   r   r   r   r   r      s    
Xe_  huo Mx M4 M.@ @<# <
4./ -( '	
 
$0%r   r   delayr"   c                 r    t        j                         }t        | |j                         | z         S 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   )rY   r5   s     r   r   r      s5      ""$D%*;499;&FFFFr   r!   c                     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!   s    r   r   r      s    & 4=r   )enumtypesr   typingr   r   r   r9   r   r	   r
   __all__Enumr   r   rU   r   r   r   r   r   <module>ra      s      ( (   TYY  c% c% c%LG8E? Gw G(Xe_  r   