o
    ]"<f1                     @   sV   d dl Z d dlZd dlmZ d dlmZmZmZ G dd dZde j	ddfd	d
Z
dS )    N)TracebackType)AnyOptionalTypec                	   @   s   e Zd ZdZddd ee deej ddfddZddd	Z	d
e
e dededee fddZdddZd
e
e dededdfddZedefddZedee fddZdddZd
e
e ddfddZdddZdS )timeouta  timeout 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:

    >>> with timeout(0.001):
    ...     async with aiohttp.get('https://github.com') as r:
    ...         await r.text()


    timeout - value in seconds or None to disable timeout logic
    loop - asyncio compatible event loop
    Nloopr   returnc                C   s8   || _ |d u rt }|| _d | _d| _d | _d | _d S )NF)_timeoutasyncioget_event_loop_loop_task
_cancelled_cancel_handler
_cancel_at)selfr   r    r   F/var/www/html/kck/venv/lib/python3.10/site-packages/asgiref/timeout.py__init__   s   
ztimeout.__init__c                 C   s   |   S N	_do_enterr   r   r   r   	__enter__-   s   ztimeout.__enter__exc_typeexc_valexc_tbc                 C   s   |  | d S r   _do_exitr   r   r   r   r   r   r   __exit__0   s   
ztimeout.__exit__c                    s
   |   S r   r   r   r   r   r   
__aenter__9   s   ztimeout.__aenter__c                    s   |  | d S r   r   r    r   r   r   	__aexit__<   s   ztimeout.__aexit__c                 C   s   | j S r   )r   r   r   r   r   expiredD   s   ztimeout.expiredc                 C   s$   | j d urt| j | j  dS d S )Ng        )r   maxr   timer   r   r   r   	remainingH   s   
ztimeout.remainingc                 C   sr   | j d u r| S t| j| _| jd u rtd| j dkr$| j| j | S | j | j  | _| j	| j| j| _
| S )Nz4Timeout context manager should be used inside a taskr   )r
   current_taskr   r   RuntimeError	call_soon_cancel_taskr&   r   call_atr   r   r   r   r   r   O   s   


ztimeout._do_enterc                 C   sP   |t ju r| jrd | _d | _t j| jd ur#| jd ur#| j  d | _d | _d S r   )r   CancelledErrorr   r   r   TimeoutErrorr
   cancel)r   r   r   r   r   r   c   s   
ztimeout._do_exitc                 C   s"   | j d ur| j   d| _d S d S )NT)r   r/   r   r   r   r   r   r+   n   s   


ztimeout._cancel_task)r	   r   )r	   N)__name__
__module____qualname____doc__r   floatr   AbstractEventLoopr   r   r   BaseExceptionr   boolr!   r"   r#   propertyr$   r'   r   r   r+   r   r   r   r   r      sJ    



	

r   r   r	   zOptional[asyncio.Task[Any]]c                 C   sL   t jdkrtj| d}ntjj| d}|d u r$t| dd }|d ur$| }|S )N)      r   r(   )sysversion_infor   r(   Taskgetattr)r   taskfnr   r   r   r(   t   s   
r(   )r   r;   typesr   typingr   r   r   r   r5   r(   r   r   r   r   <module>   s   e