U
    7ÄT_J  ã                   @   s^   d dl Z d dlZd dlmZ d dlmZmZmZ ejdkZ	G dd„ dƒZ
e jddœd	d
„ZdS )é    N)ÚTracebackType)ÚAnyÚOptionalÚType)é   é   c                   @   sÚ   e Zd ZdZddœee eej ddœdd„Zd dœdd	„Z	e
e eeee d
œdd„Zd dœdd„Ze
e eedd
œdd„Zeedœdd„ƒZeee dœdd„ƒZd dœdd„Ze
e ddœdd„Zd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©Úloop)r   r
   Úreturnc                C   s8   || _ |d krt ¡ }|| _d | _d| _d | _d | _d S )NF)Ú_timeoutÚasyncioZget_event_loopÚ_loopÚ_taskÚ
_cancelledÚ_cancel_handlerÚ
_cancel_at)Úselfr   r
   © r   ú3/tmp/pip-unpacked-wheel-_lltyzrh/asgiref/timeout.pyÚ__init__    s    ztimeout.__init__)r   c                 C   s   |   ¡ S ©N©Ú	_do_enter©r   r   r   r   Ú	__enter__/   s    ztimeout.__enter__)Úexc_typeÚexc_valÚexc_tbr   c                 C   s   |   |¡ d S r   ©Ú_do_exit©r   r   r   r   r   r   r   Ú__exit__2   s    
ztimeout.__exit__c                 Ã   s   |   ¡ S r   r   r   r   r   r   Ú
__aenter__;   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   ÚexpiredF   s    ztimeout.expiredc                 C   s(   | j d k	r t| j | j ¡  dƒS d S d S )Ng        )r   Úmaxr   Útimer   r   r   r   Ú	remainingJ   s    
ztimeout.remainingc                 C   sr   | j d kr| S t| jƒ| _| jd kr,tdƒ‚| j dkrH| j | j¡ | S | j ¡ | j  | _| j 	| j| j¡| _
| S )Nz4Timeout context manager should be used inside a taskr   )r   Úcurrent_taskr   r   ÚRuntimeErrorZ	call_soonÚ_cancel_taskr'   r   Zcall_atr   r   r   r   r   r   Q   s    

ÿ
ztimeout._do_enter)r   r   c                 C   sP   |t jkr"| jr"d | _d | _t j‚| jd k	rF| jd k	rF| j ¡  d | _d | _d S r   )r   ZCancelledErrorr   r   r   ÚTimeoutErrorr   Úcancel)r   r   r   r   r   r    e   s    
ztimeout._do_exitc                 C   s   | j d k	r| j  ¡  d| _d S )NT)r   r-   r   r   r   r   r   r+   p   s    

ztimeout._cancel_task)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úfloatr   ÚAbstractEventLoopr   r   r   ÚBaseExceptionr   Úboolr"   r#   r$   Úpropertyr%   r(   r   r    r+   r   r   r   r   r      s4   üûû	ûr   zasyncio.Task[Any])r
   r   c                 C   s>   t rtj| d�}ntjj| d�}|d kr:t| dƒr:|  ¡ }|S )Nr	   r)   )ÚPY_37r   r)   ZTaskÚhasattr)r
   Ztaskr   r   r   r)   v   s    
r)   )r   ÚsysÚtypesr   Útypingr   r   r   Úversion_infor7   r   r3   r)   r   r   r   r   Ú<module>	   s   
e