ó
ÐH/\c           @` 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 „  e ƒ  d <e j e ƒ  d <e j e ƒ  d <e
 j e ƒ  d <d d d d d d g Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d d d „ Z d d d d „ Z e  a! d „  Z" d  „  Z# d d! „ Z$ d e d d" „ Z% d e d# „ Z& d e e d$ „ Z' d e e d% „ Z( d& „  Z) e) ƒ  d d' l m* Z* e* e+ ƒ  d( ƒ d S()   s{   
A collection of primitives used by the hub, and suitable for
compilation with Cython because of their frequency of use.


i    (   t   absolute_import(   t   division(   t   print_functionN(   t   InvalidSwitchError(   t   ConcurrentObjectUseError(   t   _greenlet_primitives(   t   _waiter(   t   _NONE(   t   get_hub_noargs(   t   Timeoutt   greenlett
   getcurrentc           C` s   d  S(   N(   t   None(    (    (    s5   lib/python2.7/site-packages/gevent/_hub_primitives.pyt   <lambda>   s    t   greenlet_initt   Waitert   MultipleWaitert   SwitchOutGreenletWithLoopt   WaitOperationsGreenlett   iwait_on_objectst   wait_on_objectst	   wait_readt
   wait_writet   wait_readwritec           B` s&   e  Z d  „  Z e d „ Z d „  Z RS(   c         C` sl   t  |  ƒ } | j | j | ƒ z; | j ƒ  } | | k	 rY t d t ƒ  | | f ƒ ‚ n  Wd | j ƒ  Xd S(   s”   
        Wait until the *watcher* (which must not be started) is ready.

        The current greenlet will be unscheduled during this time.
        s(   Invalid switch into %s: %r (expected %r)N(   R   t   startt   switcht   getR   R   t   stop(   t   selft   watchert   waitert   result(    (    s5   lib/python2.7/site-packages/gevent/_hub_primitives.pyt   wait.   s    c         C` sU   | d k r d S| j d k	 r> |  j j |  j | | | ƒ n | rQ | j ƒ  n  d S(   sý  
        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   t   callbackt   loopt   run_callbackt   _cancel_waitt   close(   R   R   t   errort   close_watcher(    (    s5   lib/python2.7/site-packages/gevent/_hub_primitives.pyt   cancel_wait?   s    c         C` s`   | j  } | j } | r% | j ƒ  n  | r\ t | d d  ƒ } | d  k	 r\ | j | ƒ q\ n  d  S(   Nt   __self__(   t   activeR!   R%   t   getattrR   t   throw(   R   R   R&   R'   R*   t   cbt   glet(    (    s5   lib/python2.7/site-packages/gevent/_hub_primitives.pyR$   T   s    		(   t   __name__t
   __module__R    t   FalseR(   R$   (    (    (    s5   lib/python2.7/site-packages/gevent/_hub_primitives.pyR   ,   s   	t   _WaitIteratorc           B` sM   e  Z d  „  Z d „  Z d „  Z d „  Z e Z d „  Z d „  Z d „  Z	 RS(   c         C` s   | |  _  t | ƒ |  _ |  j j |  _ | |  _ | |  _ d  |  _ t	 |  _
 | d  k rc t | ƒ n t | t | ƒ ƒ |  _ d  S(   N(   t   _hubR   R   R   t   _switcht   _timeoutt   _objectsR   t   _timerR1   t   _begunt   lent   mint   _count(   R   t   objectst   hubt   timeoutt   count(    (    s5   lib/python2.7/site-packages/gevent/_hub_primitives.pyt   __init__d   s    					c         C` s‡   |  j  r d  St |  _  x! |  j D] } | j |  j ƒ q  W|  j d  k	 rƒ |  j j j	 |  j d d ƒ|  _
 |  j
 j |  j |  ƒ n  d  S(   Nt   priorityiÿÿÿÿ(   R8   t   TrueR6   t   rawlinkR4   R5   R   R3   R"   t   timerR7   R   (   R   t   obj(    (    s5   lib/python2.7/site-packages/gevent/_hub_primitives.pyt   _begins   s    		!c         C` s   |  S(   N(    (   R   (    (    s5   lib/python2.7/site-packages/gevent/_hub_primitives.pyt   __iter__‚   s    c         C` sŸ   |  j  ƒ  |  j d k r/ |  j ƒ  t ƒ  ‚ n  |  j d 8_ yF |  j j ƒ  } |  j j ƒ  | |  k r |  j ƒ  t ƒ  ‚ n  | SWn |  j ƒ  ‚  n Xd  S(   Ni    i   (   RF   R;   t   _cleanupt   StopIterationR   R   t   clear(   R   t   item(    (    s5   lib/python2.7/site-packages/gevent/_hub_primitives.pyt   __next__…   s    



c         C` s•   |  j  d  k	 r( |  j  j ƒ  d  |  _  n  |  j } d |  _ xT | D]L } t | d d  ƒ } | d  k	 rA y | |  j ƒ Wq t j ƒ  q XqA qA Wd  S(   Nt   unlink(    (   R7   R   R%   R6   R+   R4   t	   tracebackt	   print_exc(   R   t   objst   aobjRM   (    (    s5   lib/python2.7/site-packages/gevent/_hub_primitives.pyRH   œ   s    		c         C` s   |  S(   N(    (   R   (    (    s5   lib/python2.7/site-packages/gevent/_hub_primitives.pyt	   __enter__«   s    c         C` s   |  j  ƒ  d  S(   N(   RH   (   R   t   typt   valuet   tb(    (    s5   lib/python2.7/site-packages/gevent/_hub_primitives.pyt   __exit__®   s    (
   R/   R0   R@   RF   RG   RL   t   nextRH   RR   RV   (    (    (    s5   lib/python2.7/site-packages/gevent/_hub_primitives.pyR2   b   s   						c         C` s;   t  ƒ  } |  d k r( | j d | ƒ g St |  | | | ƒ S(   sô  
    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.
    R>   N(   t   get_hubR   t   joinR2   (   R<   R>   R?   R=   (    (    s5   lib/python2.7/site-packages/gevent/_hub_primitives.pyR   ²   s    !	c         C` s;   |  d k r% t ƒ  } | j d | ƒ St t |  | | ƒ ƒ S(   sx  
    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`
    R>   N(   R   RX   RY   t   listR   (   R<   R>   R?   R=   (    (    s5   lib/python2.7/site-packages/gevent/_hub_primitives.pyR   Ù   s    "	c         C` s
   |  a  d  S(   N(   t   _timeout_error(   t   e(    (    s5   lib/python2.7/site-packages/gevent/_hub_primitives.pyt   set_default_timeout_error  s    c         C` s±   |  j  d  k	 r( t d |  j  f ƒ ‚ n  | d  k r@ t ƒ  } n  | d  k r] | j |  ƒ d  St j | | t k	 s~ | d  k r„ | n	 t d ƒ ƒ } |  | j |  ƒ Wd  QXd  S(   Ns3   This socket is already used by another greenlet: %rs	   timed out(	   R!   R   R   RX   R    R	   t   _start_new_or_dummyR   R[   (   R   R>   t   timeout_excR=   (    (    s5   lib/python2.7/site-packages/gevent/_hub_primitives.pyt   _primitive_wait  s    c         C` sV   |  d  k s | d  k r' t d ƒ ‚ n  t | |  j | d  k	 rE | n t |  j ƒ d  S(   Ns6   The socket has already been closed by another greenlet(   R   R   R`   R>   R   R=   (   t   socketR   R_   (    (    s5   lib/python2.7/site-packages/gevent/_hub_primitives.pyt   wait_on_socket  s
    c         C` s   t  |  | | | ƒ d S(   s‡  
    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 *io* by another greenlet,
    raise an exception in this blocking greenlet
    (``socket.error(EBADF, 'File descriptor was closed in another
    greenlet')`` by default).

    :param io: 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   R>   R_   R=   (    (    s5   lib/python2.7/site-packages/gevent/_hub_primitives.pyt   wait_on_watcher%  s    c         C` sG   t  ƒ  } | j j |  d ƒ } z t | | | | ƒ SWd | j ƒ  Xd S(   s  
    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`
    i   N(   RX   R"   t   ioRc   R%   (   t   filenoR>   R_   R=   Rd   (    (    s5   lib/python2.7/site-packages/gevent/_hub_primitives.pyR   @  s
    	c         C` sG   t  ƒ  } | j j |  d ƒ } z t | | | | ƒ SWd | j ƒ  Xd S(   sµ  
    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`
    i   N(   RX   R"   Rd   Rc   R%   (   Re   R>   R_   t   eventR=   Rd   (    (    s5   lib/python2.7/site-packages/gevent/_hub_primitives.pyR   S  s
    	c         C` sG   t  ƒ  } | j j |  d ƒ } z t | | | | ƒ SWd | j ƒ  Xd S(   sÅ  
    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`
    i   N(   RX   R"   Rd   Rc   R%   (   Re   R>   R_   Rf   R=   Rd   (    (    s5   lib/python2.7/site-packages/gevent/_hub_primitives.pyR   k  s
    	c           C` s   t  ƒ  d  S(   N(   R   (    (    (    s5   lib/python2.7/site-packages/gevent/_hub_primitives.pyt   _init„  s    (   t   import_c_accels   gevent.__hub_primitives(,   t   __doc__t
   __future__R    R   R   RN   t   gevent.exceptionsR   R   t   geventR   R   t   gevent._utilR   t   gevent._hub_localR   RX   t   gevent.timeoutR	   t
   __import__R   t   localsR   R   R   t   __all__R   t   objectR2   R   R   R   t	   ExceptionR[   R]   R`   Rb   Rc   R   R   R   Rg   Rh   t   globals(    (    (    s5   lib/python2.7/site-packages/gevent/_hub_primitives.pyt   <module>	   sL   	6P''				