o
    e6                     @   s  d Z ddlmZ ddlmZ ddlmZ ddl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dlmZ ddlmZ edje d< dd e d< eje d< eje d< e
je d< g dZG dd deZG dd deZd0ddZd1ddZea dd Z!d d! Z"d2d"d#Z#dedfd$d%Z$defd&d'Z%deefd(d)Z&deefd*d+Z'd,d- Z(e(  dd.lm)Z) e)e* d/ dS )3z{
A collection of primitives used by the hub, and suitable for
compilation with Cython because of their frequency of use.


    )absolute_import)division)print_functionN)InvalidSwitchError)ConcurrentObjectUseError)_greenlet_primitives)_waiter)_NONE)get_hub_noargs)TimeoutZgreenlet
getcurrentc                   C   s   d S N r   r   r   FD:\Projects\ConvertPro\env\Lib\site-packages\gevent/_hub_primitives.py<lambda>   s    r   greenlet_initWaiterMultipleWaiterSwitchOutGreenletWithLoop)WaitOperationsGreenletiwait_on_objectswait_on_objects	wait_read
wait_writewait_readwritec                   @   s6   e Zd Zdd Zdd Zdd Zddd	Zd
d ZdS )r   c                 C   sX   t | }||j| z| }||ur tdt ||| |f W |  dS |  w )z
        Wait until the *watcher* (which must not be started) is ready.

        The current greenlet will be unscheduled during this time.
        zCInvalid switch into %s: got %r (expected %r; waiting on %r with %r)N)r   startswitchgetr   r   stop)selfwatcherwaiterresultr   r   r   wait.   s    zWaitOperationsGreenlet.waitc                 G   s`   g }|D ]}|d u rq|j d u r|  q|| q|r*| j| j|||| d S ||  d S r   )callbackcloseappendlooprun_callback_cancel_waits_then)r   watchersexc_kindthen	then_argsdeferredr    r   r   r   cancel_waits_close_and_thenE   s   

z2WaitOperationsGreenlet.cancel_waits_close_and_thenc                 C   s$   |D ]	}|  ||d q||  d S )NT)_cancel_wait)r   r*   r+   r,   r-   r    r   r   r   r)   S   s   z)WaitOperationsGreenlet._cancel_waits_thenFc                 C   sB   |du rdS |j dur| j| j||| dS |r|  dS dS )a  
        Cancel an in-progress call to :meth:`wait` by throwing the given *error*
        in the waiting greenlet.

        .. versionchanged:: 1.3a1
           Added the *close_watcher* parameter. If true, the watcher
           will be closed after the exception is thrown. The watcher should then
           be discarded. Closing the watcher is important to release native resources.
        .. versionchanged:: 1.3a2
           Allow the *watcher* to be ``None``. No action is taken in that case.

        N)r$   r'   r(   r0   r%   )r   r    errorclose_watcherr   r   r   cancel_waitX   s   
z"WaitOperationsGreenlet.cancel_waitc                 C   sF   |j }|j}|r|  |rt|dd }|d ur!|| d S d S d S )N__self__)activer$   r%   getattrthrow)r   r    r1   r2   r5   cbZgletr   r   r   r0   q   s   z#WaitOperationsGreenlet._cancel_waitN)F)__name__
__module____qualname__r#   r/   r)   r3   r0   r   r   r   r   r   ,   s    
r   c                   @   sH   e Zd Zdd Zdd Zdd Zdd ZeZd	d
 Zdd Z	dd Z
dS )_WaitIteratorc                 C   s\   || _ t|| _| jj| _|| _|| _d | _d| _|d u r$t	|| _d S t
|t	|| _d S )NF)_hubr   r   r   _switch_timeout_objects_timer_begunlenmin_count)r   objectshubtimeoutcountr   r   r   __init__   s   

*z_WaitIterator.__init__c                 C   s`   | j rd S d| _ | jD ]}|| j q| jd ur.| jjj| jdd| _| j	| j|  d S d S )NT)priority)
rB   r@   Zrawlinkr>   r?   r=   r'   timerrA   r   )r   objr   r   r   _begin   s   

z_WaitIterator._beginc                 C      | S r   r   r   r   r   r   __iter__      z_WaitIterator.__iter__c                 C   sp   |    | jdkr|   t |  jd8  _z| j }| j  || u r-|   t |W S    |    )Nr      )rO   rE   _cleanupStopIterationr   r   clear)r   itemr   r   r   __next__   s   


z_WaitIterator.__next__c                 C   sj   | j d ur| j   d | _ | j}d| _|D ]}t|dd }|d ur2z|| j W q   t  Y qqd S )Nr   unlink)rA   r%   r@   r6   r>   	traceback	print_exc)r   ZobjsZaobjrZ   r   r   r   rU      s   

z_WaitIterator._cleanupc                 C   rP   r   r   rQ   r   r   r   	__enter__   rS   z_WaitIterator.__enter__c                 C   s   |    d S r   )rU   )r   typvaluetbr   r   r   __exit__   s   z_WaitIterator.__exit__N)r9   r:   r;   rJ   rO   rR   rY   nextrU   r]   ra   r   r   r   r   r<      s    r<   c                 C   s*   t  }| du r|j|dgS t| |||S )a  
    Iteratively yield *objects* as they are ready, until all (or *count*) are ready
    or *timeout* expired.

    If you will only be consuming a portion of the *objects*, you should
    do so inside a ``with`` block on this object to avoid leaking resources::

        with gevent.iwait((a, b, c)) as it:
            for i in it:
                if i is a:
                    break

    :param objects: A sequence (supporting :func:`len`) containing objects
        implementing the wait protocol (rawlink() and unlink()).
    :keyword int count: If not `None`, then a number specifying the maximum number
        of objects to wait for. If ``None`` (the default), all objects
        are waited for.
    :keyword float timeout: If given, specifies a maximum number of seconds
        to wait. If the timeout expires before the desired waited-for objects
        are available, then this method returns immediately.

    .. seealso:: :func:`wait`

    .. versionchanged:: 1.1a1
       Add the *count* parameter.
    .. versionchanged:: 1.1a2
       No longer raise :exc:`LoopExit` if our caller switches greenlets
       in between items yielded by this function.
    .. versionchanged:: 1.4
       Add support to use the returned object as a context manager.
    NrH   )get_hubjoinr<   rF   rH   rI   rG   r   r   r   r      s   !r   c                 C   s*   | du rt  }|j|dS tt| ||S )af  
    Wait for *objects* to become ready or for event loop to finish.

    If *objects* is provided, it must be a list containing objects
    implementing the wait protocol (rawlink() and unlink() methods):

    - :class:`gevent.Greenlet` instance
    - :class:`gevent.event.Event` instance
    - :class:`gevent.lock.Semaphore` instance
    - :class:`gevent.subprocess.Popen` instance

    If *objects* is ``None`` (the default), ``wait()`` blocks until
    the current event loop has nothing to do (or until *timeout* passes):

    - all greenlets have finished
    - all servers were stopped
    - all event loop watchers were stopped.

    If *count* is ``None`` (the default), wait for all *objects*
    to become ready.

    If *count* is a number, wait for (up to) *count* objects to become
    ready. (For example, if count is ``1`` then the function exits
    when any object in the list is ready).

    If *timeout* is provided, it specifies the maximum number of
    seconds ``wait()`` will block.

    Returns the list of ready objects, in the order in which they were
    ready.

    .. seealso:: :func:`iwait`
    Nrc   )rd   re   listr   rf   r   r   r   r      s   "r   c                 C   s   | a d S r   )_timeout_error)er   r   r   set_default_timeout_error#  s   rj   c                 C   s   | j d urtd| j f |d u rt }|d u r||  d S t||tus*|d u r,|ntd}| ||  W d    d S 1 sDw   Y  d S )Nz3This socket is already used by another greenlet: %rz	timed out)r$   r   rd   r#   r   Z_start_new_or_dummyr	   rh   r    rH   timeout_excrG   r   r   r   _primitive_wait'  s$   

"rm   c                 C   s:   | d u s|d u rt dt|| j|d ur|nt| j d S )Nz6The socket has already been closed by another greenlet)r   rm   rH   r	   rG   )socketr    rl   r   r   r   wait_on_socket=  s   ro   c                 C   s   t | ||| dS )a  
    wait(watcher, timeout=None, [timeout_exc=None]) -> None

    Block the current greenlet until *watcher* is ready.

    If *timeout* is non-negative, then *timeout_exc* is raised after
    *timeout* second has passed.

    If :func:`cancel_wait` is called on *watcher* by another greenlet,
    raise an exception in this blocking greenlet
    (``socket.error(EBADF, 'File descriptor was closed in another
    greenlet')`` by default).

    :param watcher: An event loop watcher, most commonly an IO watcher obtained from
        :meth:`gevent.core.loop.io`
    :keyword timeout_exc: The exception to raise if the timeout expires.
        By default, a :class:`socket.timeout` exception is raised.
        If you pass a value for this keyword, it is interpreted as for
        :class:`gevent.timeout.Timeout`.

    :raises ~gevent.hub.ConcurrentObjectUseError: If the *watcher* is
        already started.
    N)rm   rk   r   r   r   wait_on_watcherF  s   rp   c                 C   s8   t  }|j| d}zt||||W |  S |  w )a  
    wait_read(fileno, timeout=None, [timeout_exc=None]) -> None

    Block the current greenlet until *fileno* is ready to read.

    For the meaning of the other parameters and possible exceptions,
    see :func:`wait`.

    .. seealso:: :func:`cancel_wait`
    rT   rd   r'   iorp   r%   )filenorH   rl   rG   rr   r   r   r   r   a  s
   r   c                 C   8   t  }|j| d}zt||||W |  S |  w )a  
    wait_write(fileno, timeout=None, [timeout_exc=None]) -> None

    Block the current greenlet until *fileno* is ready to write.

    For the meaning of the other parameters and possible exceptions,
    see :func:`wait`.

    .. deprecated:: 1.1
       The keyword argument *event* is ignored. Applications should not pass this parameter.
       In the future, doing so will become an error.

    .. seealso:: :func:`cancel_wait`
       rq   rs   rH   rl   eventrG   rr   r   r   r   r   t  s
   r   c                 C   rt   )a  
    wait_readwrite(fileno, timeout=None, [timeout_exc=None]) -> None

    Block the current greenlet until *fileno* is ready to read or
    write.

    For the meaning of the other parameters and possible exceptions,
    see :func:`wait`.

    .. deprecated:: 1.1
       The keyword argument *event* is ignored. Applications should not pass this parameter.
       In the future, doing so will become an error.

    .. seealso:: :func:`cancel_wait`
       rq   rv   r   r   r   r     s
   r   c                   C   s
   t   d S r   )r   r   r   r   r   _init  s   
ry   )import_c_accelzgevent.__hub_primitives)NN)NNNr   )+__doc__
__future__r   r   r   r[   Zgevent.exceptionsr   r   Zgeventr   r   Zgevent._utilr	   Zgevent._hub_localr
   rd   Zgevent.timeoutr   
__import__r   localsr   r   r   __all__r   objectr<   r   r   	Exceptionrh   rj   rm   ro   rp   r   r   r   ry   rz   globalsr   r   r   r   <module>   sD   	W
P
''
	