o
    ^i)                     @  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Z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activeexpiringexpiredfinishedN)__name__
__module____qualname__CREATEDENTEREDEXPIRINGEXPIREDEXITED r   r   W/var/www/html/stock_analysis/be/venv/lib/python3.10/site-packages/taskgroup/timeouts.pyr      s    r   c                   @  s`   e Zd ZdZd!ddZd"dd	Zd!d
dZd#ddZd$ddZd%ddZ	d&ddZ
d'ddZd S )(_TimeoutzAsynchronous context manager for cancelling overdue coroutines.

    Use `timeout()` or `timeout_at()` rather than instantiating this class directly.
    whenOptional[float]returnNonec                 C  s   t j| _d| _d| _|| _dS )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!   r   r   r   __init__)   s   
z_Timeout.__init__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ur| j tju rtdtd| j j d|| _| jdur'| j  |du r0d| _dS t	
 }|| krC|| j| _dS ||| 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time	call_soon_on_timeoutcall_at)r)   r!   loopr   r   r   
reschedule:   s   


z_Timeout.rescheduleboolc                 C  s   | j tjtjfv 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u r| jd u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!   info_strr   r   r   __repr__U   s   
z_Timeout.__repr__r   c                   sX   | j tjurtdt }|d u 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	   current_taskr   r'   
cancelling_cancellingr5   r(   )r)   taskr   r   r   
__aenter__]   s   z_Timeout.__aenter__exc_typeOptional[Type[BaseException]]exc_valOptional[BaseException]exc_tbOptional[TracebackType]Optional[bool]c                   s   | j tjtjfv sJ | jd ur| j  d | _| j tju r5tj| _ | j | j	kr3|t
ju r3t|d S | j tju r?tj| _ d S N)r%   r   r   r   r&   r.   r   r'   uncancelrE   r   CancelledErrorTimeoutErrorr   r)   rH   rJ   rL   r   r   r   	__aexit__i   s    

z_Timeout.__aexit__c                 C  s:   | j d usJ | jtju sJ | j   tj| _d | _d S rO   )r'   r%   r   r   r.   r   r&   r+   r   r   r   r2      s
   

z_Timeout._on_timeoutN)r!   r"   r#   r$   )r#   r"   )r#   r6   )r#   r7   r#   r   rH   rI   rJ   rK   rL   rM   r#   rN   )r#   r$   )r   r   r   __doc__r*   r!   r5   r   rB   rG   rT   r2   r   r   r   r   r    #   s    






r    c                      s2   e Zd ZU ded< d fddZdddZ  ZS )r   zcontextlib.AsyncExitStack_Timeout__stackr#   r   c              	     s   t  4 I d H *}|t I d H  t  I d H  |t j |	 | _
W d   I d H  | S 1 I d H s9w   Y  | S rO   )
contextlibAsyncExitStackenter_async_context_installinstall_uncancelsuperrG   push_async_exitrT   pop_allrX   )r)   stack	__class__r   r   rG      s   zTimeout.__aenter__rH   rI   rJ   rK   rL   rM   rN   c                   s   | j |||I d H S rO   )rX   rT   rS   r   r   r   rT      s   zTimeout.__aexit__rU   rV   )r   r   r   __annotations__rG   rT   __classcell__r   r   rb   r   r      s   
 r   delayr"   r#   c                 C  s&   t  }t| dur| |  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   r/   r   r0   )rf   r4   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   )rf   r"   r#   r   )r!   r"   r#   r   )
__future__r   rY   enumtypesr   typingr   r   r   asyncior   r   r	   r8   r   r\   typing_extensionsr   __all__Enumr   r    r   r   r   r   r   r   r   <module>   s"   j
