§
    Uä�aŒ2  ã                   ó¨   — d 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gZ G d	„ d
e¦  «        Z e¦   «         Z G d„ de¦  «        Zd„ ZdS )aã  
Timeouts.

Many functions in :mod:`gevent` have a *timeout* argument that allows
limiting the time the function will block. When that is not available,
the :class:`Timeout` class and :func:`with_timeout` function in this
module add timeouts to arbitrary code.

.. warning::

    Timeouts can only work when the greenlet switches to the hub.
    If a blocking function is called or an intense calculation is ongoing during
    which no switches occur, :class:`Timeout` is powerless.
é    )Úabsolute_importÚprint_functionÚdivision)Ústring_types)Ú_NONE)Ú
getcurrent)Úget_hub_noargsÚTimeoutÚwith_timeoutc                   ón   — e Zd ZdZed„ ¦   «         ZeZed„ ¦   «         ZexZZ	d„ Z
d„ ZeZexZZd„ Zd„ ZdS )	Ú
_FakeTimer© c                 ó   — dS )NFr   ©Úselfs    ú0/usr/lib/python3/dist-packages/gevent/timeout.pyÚpendingz_FakeTimer.pending(   s   € àˆuó    c                 ó   — dS )zAlways returns NoneNr   r   s    r   Úsecondsz_FakeTimer.seconds.   s   € € € r   c                 ó    — t          d¦  «        ‚)Nz$non-expiring timer cannot be started)ÚAssertionError)r   ÚargsÚkwargss      r   Ústartz_FakeTimer.start4   s   € åÐCÑDÔDÐDr   c                 ó   — d S ©Nr   r   s    r   Ústopz_FakeTimer.stop8   ó   € Øˆr   c                 ó   — | S r   r   r   s    r   Ú	__enter__z_FakeTimer.__enter__?   s   € Øˆr   c                 ó   — d S r   r   )r   Ú_tÚ_vÚ_tbs       r   Ú__exit__z_FakeTimer.__exit__B   r   r   N)Ú__name__Ú
__module__Ú__qualname__Ú	__slots__Úpropertyr   Úactiver   ÚtimerÚ	exceptionr   r   ÚcancelÚcloser!   r&   r   r   r   r   r      s°   € € € € € ð €Iàðð ñ „Xðð €Fàðð ñ „Xðð  Ð€EˆIðEð Eð Eðð ð ð €FàÐ€Dˆ5ðð ð ðð ð ð ð r   r   c                   ó”   — e Zd ZdZ	 	 dd„Zd„ Zd„ Zedd	„¦   «         Ze	dd
„¦   «         Z
ed„ ¦   «         Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ ZdS )r
   a„  
    Timeout(seconds=None, exception=None, ref=True, priority=-1)

    Raise *exception* in the current greenlet after *seconds*
    have elapsed::

        timeout = Timeout(seconds, exception)
        timeout.start()
        try:
            ...  # exception will be raised here, after *seconds* passed since start() call
        finally:
            timeout.close()

    .. warning::

        You must **always** call `close` on a ``Timeout`` object you have created,
        whether or not the code that the timeout was protecting finishes
        executing before the timeout elapses (whether or not the
        ``Timeout`` exception is raised)  This ``try/finally``
        construct or a ``with`` statement is a good pattern. (If
        the timeout object will be started again, use `cancel` instead
        of `close`; this is rare. You must still `close` it when you are
        done.)

    When *exception* is omitted or ``None``, the ``Timeout`` instance
    itself is raised::

        >>> import gevent
        >>> gevent.Timeout(0.1).start()
        >>> gevent.sleep(0.2)  #doctest: +IGNORE_EXCEPTION_DETAIL
        Traceback (most recent call last):
         ...
        Timeout: 0.1 seconds

    If the *seconds* argument is not given or is ``None`` (e.g.,
    ``Timeout()``), then the timeout will never expire and never raise
    *exception*. This is convenient for creating functions which take
    an optional timeout parameter of their own. (Note that this is **not**
    the same thing as a *seconds* value of ``0``.)

    ::

       def function(args, timeout=None):
          "A function with an optional timeout."
          timer = Timeout(timeout)
          with timer:
             ...

    .. caution::

        A *seconds* value less than ``0.0`` (e.g., ``-1``) is poorly defined. In the future,
        support for negative values is likely to do the same thing as a value
        of ``None`` or ``0``

    A *seconds* value of ``0`` requests that the event loop spin and poll for I/O;
    it will immediately expire as soon as control returns to the event loop.

    .. rubric:: Use As A Context Manager

    To simplify starting and canceling timeouts, the ``with``
    statement can be used::

        with gevent.Timeout(seconds, exception) as timeout:
            pass  # ... code block ...

    This is equivalent to the try/finally block above with one
    additional feature: if *exception* is the literal ``False``, the
    timeout is still raised, but the context manager suppresses it, so
    the code outside the with-block won't see it.

    This is handy for adding a timeout to the functions that don't
    support a *timeout* parameter themselves::

        data = None
        with gevent.Timeout(5, False):
            data = mysock.makefile().readline()
        if data is None:
            ...  # 5 seconds passed without reading a line
        else:
            ...  # a line was read within 5 seconds

    .. caution::

        If ``readline()`` above catches and doesn't re-raise
        :exc:`BaseException` (for example, with a bare ``except:``), then
        your timeout will fail to function and control won't be returned
        to you when you expect.

    .. rubric:: Catching Timeouts

    When catching timeouts, keep in mind that the one you catch may
    not be the one you have set (a calling function may have set its
    own timeout); if you going to silence a timeout, always check that
    it's the instance you need::

        timeout = Timeout(1)
        timeout.start()
        try:
            ...
        except Timeout as t:
            if t is not timeout:
                raise # not my timeout
        finally:
            timeout.close()


    .. versionchanged:: 1.1b2

        If *seconds* is not given or is ``None``, no longer allocate a
        native timer object that will never be started.

    .. versionchanged:: 1.1

        Add warning about negative *seconds* values.

    .. versionchanged:: 1.3a1

        Timeout objects now have a :meth:`close`
        method that *must* be called when the timeout will no longer be
        used to properly clean up native resources.
        The ``with`` statement does this automatically.

    NTéÿÿÿÿFc                 óä   — t                                | ¦  «         || _        || _        || _        |€t
          | _        d S t          ¦   «         j                             |pd||¬¦  «        | _        d S )Ng        )ÚrefÚpriority)	ÚBaseExceptionÚ__init__r   r.   Ú	_one_shotr   r-   Úget_hubÚloop)r   r   r.   r4   r5   r8   s         r   r7   zTimeout.__init__È   sj   € å×Ò˜tÑ$Ô$Ð$ØˆŒØ"ˆŒØ"ˆŒØˆ?õ $ˆDŒJˆJˆJõ !™œœ×-Ò-¨g¨n¸À#ÐPXÐ-ÑYÔYˆDŒJˆJˆJr   c                 ó  — | j         rt          d| z  ¦  «        ‚| j        €dS | j        �#| j        du st	          | j        t
          ¦  «        r| }n| j        }| j                             | j        t          ¦   «         |d¬¦  «         dS )zSchedule the timeout.z5%r is already started; to restart it, cancel it firstNFT)Úupdate)
r   r   r   r.   Ú
isinstancer   r-   r   Ú_on_expirationr   )r   Úthrowss     r   r   zTimeout.startÜ   s“   € àŒ<ð 	aÝ Ð!XÐ[_Ñ!_Ñ`Ô`Ð`àŒ<ÐàˆFàŒ>Ð! T¤^°uÐ%<Ð%<Å
È4Ì>Õ[gÑ@hÔ@hÐ%<àˆFˆFð ”^ˆFð 	Œ
×Ò˜Ô,­j©l¬l¸FÈ4ÐÑPÔPÐPÐPÐPr   c                 ó0   — |                      |¦  «         d S r   )Úthrow)r   Úprev_greenletÚexs      r   r>   zTimeout._on_expirationð   s   € à×Ò˜BÑÔÐÐÐr   c                 ó°   — t          |t          ¦  «        r|j        s|                     ¦   «          |S  | ||||¬¦  «        }|                     ¦   «          |S )a·  Create a started :class:`Timeout`.

        This is a shortcut, the exact action depends on *timeout*'s type:

        * If *timeout* is a :class:`Timeout`, then call its :meth:`start` method
          if it's not already begun.
        * Otherwise, create a new :class:`Timeout` instance, passing (*timeout*, *exception*) as
          arguments, then call its :meth:`start` method.

        Returns the :class:`Timeout` instance.
        )r4   r8   )r=   r
   r   r   )ÚclsÚtimeoutr.   r4   r8   s        r   Ú	start_newzTimeout.start_newô   s\   € õ �g�wÑ'Ô'ð 	Ø”?ð  Ø—’‘”�ØˆNØ�#�g˜y¨c¸YÐGÑGÔGˆØ�Š‰ŒˆØˆr   c                 óP   — | €t           S t                               | ||d¬¦  «        S )NT©r8   )r   r
   rG   )rF   r.   r4   s      r   Ú_start_new_or_dummyzTimeout._start_new_or_dummy	  s,   € ð" ˆ?ÝÐÝ× Ò  ¨)°SÀDÐ ÑIÔIÐIr   c                 ó2   — | j         j        p| j         j        S )z.True if the timeout is scheduled to be raised.)r-   r   r,   r   s    r   r   zTimeout.pending  s   € ð ŒzÔ!Ð6 T¤ZÔ%6Ð6r   c                 ór   — | j                              ¦   «          | j        r|                      ¦   «          dS dS )zï
        If the timeout is pending, cancel it. Otherwise, do nothing.

        The timeout object can be :meth:`started <start>` again. If
        you will not start the timeout again, you should use
        :meth:`close` instead.
        N)r-   r   r8   r0   r   s    r   r/   zTimeout.cancel#  s;   € ð 	Œ
�ŠÑÔÐØŒ>ð 	Ø�JŠJ‰LŒLˆLˆLˆLð	ð 	r   c                 ó‚   — | j                              ¦   «          | j                              ¦   «          t          | _         dS )z‚
        Close the timeout and free resources. The timer cannot be started again
        after this method has been used.
        N)r-   r   r0   r   r   s    r   r0   zTimeout.close/  s5   € ð
 	Œ
�ŠÑÔÐØŒ
×ÒÑÔÐÝˆŒ
ˆ
ˆ
r   c           	      óÈ   — t          | ¦  «        j        }| j        rd}nd}| j        €d}n
d| j        z  }d|›dt	          t          | ¦  «        ¦  «        ›d| j        ›|›|›d�	S )Nz pendingÚ z exception=%rú<z at z	 seconds=ú>)Útyper'   r   r.   ÚhexÚidr   )r   Ú	classnamer   r.   s       r   Ú__repr__zTimeout.__repr__8  sv   € Ý˜‘J”JÔ'ˆ	ØŒ<ð 	Ø ˆGˆGàˆGØŒ>Ð!ØˆIˆIà'¨$¬.Ñ8ˆIøØ.7¨i¨i½½RÀ¹X¼X¹¼¸¸ÈÌÈÐV_ÐV_ÐahÐahÐahÐiÐir   c                 ó¬   — | j         €dS | j         dk    rdnd}| j        €| j         ›d|›�S | j        du r| j         ›d|›d�S | j         ›d|›d| j        ›�S )	z‘
        >>> raise Timeout #doctest: +IGNORE_EXCEPTION_DETAIL
        Traceback (most recent call last):
            ...
        Timeout
        NrO   é   Úsz secondFz	 (silent)z: )r   r.   )r   Úsuffixs     r   Ú__str__zTimeout.__str__D  s~   € ð Œ<ÐØ�2à”| qÒ(Ð(��¨cˆàŒ>Ð!Ø$(¤L L L°&°&Ð9Ð9ØŒ>˜UÐ"Ð"Ø-1¬\¨\¨\¸6¸6¸6ÐBÐBØ$(¤L L L°&°&°&¸$¼.¸.ÐIÐIr   c                 ó<   — | j         s|                      ¦   «          | S )z^
        Start and return the timer. If the timer is already started, just return it.
        )r   r   r   s    r   r!   zTimeout.__enter__V  s    € ð Œ|ð 	Ø�JŠJ‰LŒLˆLØˆr   c                 óP   — |                       ¦   «          || u r| j        du rdS dS dS )z©
        Stop the timer.

        .. versionchanged:: 1.3a1
           The underlying native timer is also stopped. This object cannot be
           used again.
        FTN)r0   r.   )r   ÚtypÚvalueÚtbs       r   r&   zTimeout.__exit__^  s8   € ð 	�
Š
‰ŒˆØ�Dˆ=ˆ=˜Tœ^¨uÐ4Ð4Ø�4ð ˆ=Ð4Ð4r   )NNTr2   F)NNTF)NT)r'   r(   r)   Ú__doc__r7   r   r>   ÚclassmethodrG   ÚstaticmethodrJ   r+   r   r/   r0   rV   r[   r!   r&   r   r   r   r
   r
   H   s#  € € € € € ðzð zð~ IKØ ðZð Zð Zð Zð(Qð Qð Qð( ð  ð  ð ðð ð ñ „[ðð( ðJð Jð Jñ „\ðJð( ð7ð 7ñ „Xð7ð
ð 
ð 
ð ð  ð  ð
jð 
jð 
jðJð Jð Jð$ð ð ð
ð 
ð 
ð 
ð 
r   c                 óT  — |                      dt          ¦  «        }t                               | d¬¦  «        }	 	  ||i |¤Ž|                     ¦   «          S # t          $ r.}||u r$|t          ur|cY d}~|                     ¦   «          S ‚ d}~ww xY w# |                     ¦   «          w xY w)aZ  Wrap a call to *function* with a timeout; if the called
    function fails to return before the timeout, cancel it and return a
    flag value, provided by *timeout_value* keyword argument.

    If timeout expires but *timeout_value* is not provided, raise :class:`Timeout`.

    Keyword argument *timeout_value* is not passed to *function*.
    Útimeout_valueTrI   N)Úpopr   r
   rG   r/   )r   Úfunctionr   Úkwdsre   rF   rC   s          r   r   r   k  sÙ   € ð —H’H˜_­eÑ4Ô4€MÝ×Ò °4ÐÑ8Ô8€Gðð	Ø�8˜TÐ* TÐ*Ð*ð 	�ŠÑÔÐÐøõ ð 	ð 	ð 	Ø�Wˆ}ˆ} µeÐ!;Ð!;Ø$Ð$Ð$Ð$Ð$Ð$ð 	�ŠÑÔÐÐð øøøøð	øøøøð
 	�ŠÑÔÐÐøøøs5   ºA Á
BÁ B	Á.BÁ/B ÂB	Â	BÂB ÂB'N)ra   Ú
__future__r   r   r   Úgevent._compatr   Úgevent._utilr   Úgreenletr   Úgevent._hub_localr	   r9   Ú__all__Úobjectr   r6   r
   r   r   r   r   ú<module>rp      s  ððð ð AÐ @Ð @Ð @Ð @Ð @Ð @Ð @Ð @Ð @à 'Ð 'Ð 'Ð 'Ð 'Ð 'Ø Ð Ð Ð Ð Ð à Ð Ð Ð Ð Ð Ø 7Ð 7Ð 7Ð 7Ð 7Ð 7ð Øð€ð%ð %ð %ð %ð %�ñ %ô %ð %ðN ˆZ‰\Œ\€
ð`ð `ð `ð `ð `ˆmñ `ô `ð `ðF	ð ð ð ð r   