o
    .j                     @  sf   d dl mZ d dlZd dlZd dlmZ ejr#d dlmZ ddl	m
Z G dd dZG d	d
 d
ZdS )    )annotationsN)timeout)socket   )AsyncSocketc                   @  2   e Zd ZdZdZdddZdddZdddZdS )
SubTimeouta{  Context manager that temporarily lowers a socket's timeout to honor
    a secondary deadline (e.g. a QUIC state-machine timer).

    When the secondary deadline expires before the primary timeout, the
    *on_timer* callback is invoked and the ``SocketTimeout`` exception is
    suppressed.  The caller can inspect :attr:`timer_fired` after the
    ``with`` block to decide whether to retry the I/O operation.

    When *next_timer* is ``None`` or falls after the primary timeout the
    context manager is a lightweight no-op, the original socket timeout
    is left untouched and any ``SocketTimeout`` raised inside the block
    propagates as a regular user timeout.

    Usage::

        sub = SubTimeout(sock, protocol.next_timer(), send_pending)
        with sub:
            data = sock.recv(blocksize)
        if sub.timer_fired:
            continue  # timer was handled, retry
    sock
next_timeron_timertimer_fired_original_timeout	_adjustedr
   _sync_socketr   float | Noner   typing.Callable[[], None]returnNonec                 C  (   || _ || _|| _d| _d | _d| _d S NFr	   selfr
   r   r    r   T/home/thesage/.local/lib/python3.10/site-packages/urllib3_future/util/sub_timeout.py__init__-      
zSubTimeout.__init__c                 C  sX   | j  | _| jd ur*| jt  }t|d}| jd u s!|| jk r*| j | d| _| S Ngư>Tr
   
gettimeoutr   r   timemax
settimeoutr   r   time_to_timerr   r   r   	__enter__:   s   

zSubTimeout.__enter__exc_typetype[BaseException] | Noneexc_valBaseException | Noneexc_tb
typing.Anyboolc                 C  s8   | j r
| j| j |tu r| j rd| _|   dS dS NTFr   r
   r"   r   SocketTimeoutr   r   r   r&   r(   r*   r   r   r   __exit__O   s   zSubTimeout.__exit__N)r
   r   r   r   r   r   r   r   )r   r   r&   r'   r(   r)   r*   r+   r   r,   )__name__
__module____qualname____doc__	__slots__r   r%   r1   r   r   r   r   r      s    
	
r   c                   @  r   )AsyncSubTimeoutz)Async counterpart of :class:`SubTimeout`.r	   r
   _async_socketr   r   r   +typing.Callable[[], typing.Awaitable[None]]r   r   c                 C  r   r   r	   r   r   r   r   r   l   r   zAsyncSubTimeout.__init__c                   sZ   | j  | _| jd ur+| jt  }t|d}| jd u s"|| jk r+| j | d| _| S r   r   r#   r   r   r   
__aenter__y   s   

zAsyncSubTimeout.__aenter__r&   r'   r(   r)   r*   r+   r,   c                   s@   | j r| j| j |tu r| j rd| _|  I d H  dS dS r-   r.   r0   r   r   r   	__aexit__   s   zAsyncSubTimeout.__aexit__N)r
   r9   r   r   r   r:   r   r   )r   r8   r2   )r3   r4   r5   r6   r7   r   r;   r<   r   r   r   r   r8   `   s    
	
r8   )
__future__r   r    typingr   r   r/   TYPE_CHECKINGr   contrib.ssar   r9   r   r8   r   r   r   r   <module>   s    S