
    i6                     4   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  e            d<   ej         e            d<   ej         e            d<   e
j         e            d<   g dZ G d de          Z G d de          Zd%dZd&dZea d Z!d Z"d'dZ#dedfdZ$defdZ%deefd Z&deefd!Z'd" Z( e(             dd#lm)Z)  e) e*            d$           dS )(z{
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)Timeoutgreenlet
getcurrentc                      d S N r       A/usr/local/lib/python3.11/dist-packages/gevent/_hub_primitives.py<lambda>r      s    D r   greenlet_initWaiterMultipleWaiterSwitchOutGreenletWithLoop)WaitOperationsGreenletiwait_on_objectswait_on_objects	wait_read
wait_writewait_readwritec                   .    e Zd Zd Zd Zd ZddZd ZdS )	r   c                 >   t          |           }|                    |j        |           	 |                                }||ur+t	          dt                      d|d|d| d|d          	 |                                 dS # |                                 w xY w)z
        Wait until the *watcher* (which must not be started) is ready.

        The current greenlet will be unscheduled during this time.
        zInvalid switch into z: got z (expected z; waiting on z with )N)r   startswitchgetr   r   stop)selfwatcherwaiterresults       r   waitzWaitOperationsGreenlet.wait.   s     fmV,,,	ZZ\\FV##(("   $ LLNNNNNGLLNNNNs   AB Bc                     g }|D ]6}||j         |                                 !|                    |           7|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'   s          r   cancel_waits_close_and_thenz2WaitOperationsGreenlet.cancel_waits_close_and_thenE   s     	) 	)G'(((( 	I""4#:HhPTV_`````D)r   c                 H    |D ]}|                      ||d            ||  d S )NT)_cancel_wait)r&   r2   r3   r4   r5   r'   s         r   r1   z)WaitOperationsGreenlet._cancel_waits_thenS   s?     	7 	7Ggx6666ir   Fc                     |dS |j         $| 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/   r0   r9   r-   )r&   r'   errorclose_watchers       r   cancel_waitz"WaitOperationsGreenlet.cancel_waitX   s`     ? F'I""4#4gumTTTF 	MMOOOOO	 	r   c                     |j         }|j        }|r|                                 |r*t          |dd           }||                    |           d S d S d S )N__self__)activer,   r-   getattrthrow)r&   r'   r;   r<   r@   cbglets          r   r9   z#WaitOperationsGreenlet._cancel_waitq   sq      	MMOOO 	"2z400D

5!!!!!		" 	"  r   N)F)__name__
__module____qualname__r*   r7   r1   r=   r9   r   r   r   r   r   ,   sd          .    
   2" " " " "r   r   c                   <    e Zd Zd Zd Zd Zd ZeZd Zd Z	d Z
dS )	_WaitIteratorc                     || _         t          |          | _        | j        j        | _        || _        || _        d | _        d| _        |t          |          nt          |t          |                    | _        d S )NF)_hubr   r	   r#   _switch_timeout_objects_timer_begunlenmin_count)r&   objectshubtimeoutcounts        r   __init__z_WaitIterator.__init__   sk    	%c**|*
 ',mc'lllUCLL9Q9Qr   c                    | j         rd S d| _         | j        D ]}|                    | j                   | j        M| j        j                            | j        d          | _        | j        	                    | j        |            d S d S )NT)priority)
rP   rN   rawlinkrL   rM   rK   r/   timerrO   r"   )r&   objs     r   _beginz_WaitIterator._begin   s    ; 	F = 	& 	&CKK%%%%=$)...t}r.JJDKKdlD11111 %$r   c                     | S r   r   r&   s    r   __iter__z_WaitIterator.__iter__       r   c                    |                                   | j        dk    r"|                                  t                      | xj        dz  c_        	 | j                                        }| j                                         || u r"|                                  t                      |S #  |                                   xY w)Nr      )r_   rS   _cleanupStopIterationr	   r$   clear)r&   items     r   __next__z_WaitIterator.__next__   s    ;!MMOOO//!q
	<##%%DL   t||#oo%K	MMOOOs   AB- -Cc                     | j          | j                                          d | _         | j        }d| _        |D ]A}t          |dd           }|,	  || j                   '#  t          j                     Y >xY wBd S )Nr   unlink)rO   r-   rN   rA   rL   	traceback	print_exc)r&   objsaobjrl   s       r   rf   z_WaitIterator._cleanup   s    ;"KDK} 	* 	*DT8T22F!*F4<((((*')))))	 "	* 	*s   AA6c                     | S r   r   ra   s    r   	__enter__z_WaitIterator.__enter__   rc   r   c                 .    |                                   d S r   )rf   )r&   typvaluetbs       r   __exit__z_WaitIterator.__exit__   s    r   N)rE   rF   rG   rX   r_   rb   rj   nextrf   rr   rw   r   r   r   rI   rI      s        R R R2 2 2    * D* * *      r   rI   c                 t    t                      }| |                    |          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rV   )get_hubjoinrI   rT   rV   rW   rU   s       r   r   r      s>    B ))C))**#w666r   c                     | $t                      }|                    |          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`
    Nrz   )r{   r|   listr   r}   s       r   r   r      sB    D iixxx((( '599:::r   c                 
    | a d S r   )_timeout_error)es    r   set_default_timeout_errorr   #  s    NNNr   c                 R   | j         t          d| j                   |t                      }||                    |            d S t	          j        ||t          us||nt          d                    }|5  |                    |            d d d            d S # 1 swxY w Y   d S )Nz1This socket is already used by another greenlet: z	timed out)r,   r   r{   r*   r   _start_new_or_dummyr
   r   r'   rV   timeout_excrU   s       r   _primitive_waitr   '  s   #&&*1*:*:(> ? ? 	? {ii)u$$ 
[))	, ,G 
                   s   9BB #B c                 v    | |t          d          t          || j        ||nt          | j                   d S )Nz6The socket has already been closed by another greenlet)r   r   rV   r
   rU   )socketr'   r   s      r   wait_on_socketr   =  sO    ~ ''_```GV^#.#:KKJ         r   c                 *    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)r   r   s       r   wait_on_watcherr   F  s    0 GWk377777r   c                     t                      }|j                            | d          }	 t          ||||          |                                 S # |                                 w xY 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`
    re   r{   r/   ior   r-   )filenorV   r   rU   r   s        r   r   r   a  sY     ))C	VQ		Br7K==








   A A'c                     t                      }|j                            | d          }	 t          ||||          |                                 S # |                                 w xY 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`
       r   r   rV   r   eventrU   r   s         r   r   r   t  sY      ))C	VQ		Br7K==








r   c                     t                      }|j                            | d          }	 t          ||||          |                                 S # |                                 w xY w)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`
       r   r   s         r   r   r     sY    " ))C	VQ		Br7K==








r   c                  "    t                       d S r   )r   r   r   r   _initr     s    OOOOOr   )import_c_accelzgevent.__hub_primitives)NN)NNNr   )+__doc__
__future__r   r   r   rm   gevent.exceptionsr   r   geventr   r	   gevent._utilr
   gevent._hub_localr   r{   gevent.timeoutr   
__import__r   localsr   r   r   __all__r   objectrI   r   r   	Exceptionr   r   r   r   r   r   r   r   r   r   globalsr   r   r   <module>r      s    ' & & & & &       % % % % % %     0 0 0 0 0 0 6 6 6 6 6 6 ' ' ' ' ' '             7 7 7 7 7 7 " " " " " " $J//: (L ^ $3	 (<(V	$ %  T" T" T" T" T"6 T" T" T"nM M M M MF M M M`$7 $7 $7 $7N%; %; %; %;N     ,        &*u$ 8 8 8 86 #    &  $e    0 $(U%    2    ' ' ' ' ' ' wwyy3 4 4 4 4 4r   