o
    tPf6                     @   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)Timeoutgreenlet
getcurrentc                   C   s   d S N r   r   r   W/var/www/html/humari/django-venv/lib/python3.10/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)   r1   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!   r2   r3   r6   cbgletr   r   r   r1   q   s   z#WaitOperationsGreenlet._cancel_waitN)F)__name__
__module____qualname__r$   r0   r*   r4   r1   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)
rD   rB   rawlinkr@   rA   r?   r(   timerrC   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      )rR   rG   _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)rC   r&   rB   r7   r@   	traceback	print_exc)r    objsaobjr]   r   r   r   rX      s   

z_WaitIterator._cleanupc                 C   rS   r   r   rT   r   r   r   	__enter__   rV   z_WaitIterator.__enter__c                 C   s   |    d S r   )rX   )r    typvaluetbr   r   r   __exit__   s   z_WaitIterator.__exit__N)r;   r<   r=   rL   rR   rU   r\   nextrX   rb   rf   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rJ   )get_hubjoinr>   rH   rJ   rK   rI   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`
    Nrh   )ri   rj   listr   rk   r   r   r   r      s   "r   c                 C   s   | a d S r   )_timeout_error)er   r   r   set_default_timeout_error#  s   ro   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   ri   r$   r   _start_new_or_dummyr	   rm   r!   rJ   timeout_excrI   r   r   r   _primitive_wait'  s$   

"rs   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   rs   rJ   r	   rI   )socketr!   rr   r   r   r   wait_on_socket=  s   ru   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)rs   rq   r   r   r   wait_on_watcherF  s   rv   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`
    rW   ri   r(   iorv   r&   )filenorJ   rr   rI   rx   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`
       rw   ry   rJ   rr   eventrI   rx   r   r   r   r   t  s
   r   c                 C   rz   )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`
       rw   r|   r   r   r   r     s
   r   c                   C   s
   t   d S r   )r   r   r   r   r   _init  s   
r   )import_c_accelzgevent.__hub_primitives)NN)NNNr   )+__doc__
__future__r   r   r   r^   gevent.exceptionsr   r   geventr   r   gevent._utilr	   gevent._hub_localr
   ri   gevent.timeoutr   
__import__r   localsr   r   r   __all__r   objectr>   r   r   	Exceptionrm   ro   rs   ru   rv   r   r   r   r   r   globalsr   r   r   r   <module>   sD   	W
P
''
	