ó
ÐH/\c           @@ sy  d  Z  d d l m Z m Z d d l m Z d d l Z d d l 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 g Z d e f d „  ƒ  YZ e ƒ  Z Z d e f d „  ƒ  YZ d d „ Z e j d d k r,e e f Z e f Z n' d d l  Z  e  j f Z e e  j! f Z d Z" d Z# d e f d „  ƒ  YZ$ d S(   s0   
Basic loop implementation for ffi-based cores.
i    (   t   absolute_importt   print_function(   t   dequeN(   t   _dbg(   t   GEVENT_DEBUG_LEVEL(   t   TRACE(   t   callback(   t   PYPY(   t   getswitchintervalt   AbstractLoopt   assign_standard_callbackst   _EVENTSTypec           B@ s   e  Z d  „  Z RS(   c         C@ s   d S(   Ns   gevent.core.EVENTS(    (   t   self(    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyt   __repr__   s    (   t   __name__t
   __module__R   (    (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyR      s   t   AbstractCallbacksc           B@ st   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z e sN d „  Z	 n	 d „  Z	 d „  Z
 d	 „  Z d
 „  Z RS(   c         C@ s1   | |  _  g  |  _ t t k  r- | j |  _ n  d  S(   N(   t   ffit	   callbacksR   R   t   from_handle(   R   R   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyt   __init__F   s    		c         C@ s   |  j  j | ƒ } | S(   N(   R   R   (   R   t   handlet   x(    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyR   L   s    c         C@ sG  d } y… | s t d ƒ d S|  j | ƒ } | j } | j } | d k rS t } n  | r} | d t k r} | f | d } n  | j | Œ  Wn} t d | t j	 ƒ  ƒ y | Wn  t
 k
 rÑ |  j | ƒ } n Xt j	 ƒ  | _ | j d k	 r| j j j | ƒ n  d SX| j d k	 r?| | j j k r?| j | k r?d Sd Sd S(   s¬  
        Returns an integer having one of three values:

        - -1
          An exception occurred during the callback and you must call
          :func:`_python_handle_error` to deal with it. The Python watcher
          object will have the exception tuple saved in ``_exc_info``.
        - 1
          Everything went according to plan. You should check to see if the libev
          watcher is still active, and call :func:`python_stop` if it is not. This will
          clean up the memory. Finding the watcher still active at the event loop level,
          but not having stopped itself at the gevent level is a buggy scenario and
          shouldn't happen.
        - 2
          Everything went according to plan, but the watcher has already
          been stopped. Its memory may no longer be valid.

        This function should never return 0, as that's the default value that
        Python exceptions will produce.
        s   python_callback got null handlei   i    s+   Got exception servicing watcher with handleiÿÿÿÿi   N(   t   NoneR   R   t   _watchert   argst   _NOARGSt   GEVENT_CORE_EVENTSR   t   syst   exc_infot   UnboundLocalErrort	   _exc_infot   loopt   _keepalivesett   add(   R   R   t   reventst   orig_ffi_watchert   the_watcherR   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyt   python_callbackP   s8    
			c         C@ s‰   t  d | ƒ | r d  Sn  z5 |  j | ƒ } | j } | ` | j j t | Œ Wd  y | j ƒ  Wn  | j j | t j ƒ  Œ n Xd  SX(   Ns   Handling error for handle(	   R   R   R   R    t   handle_errorR   t   stopR   R   (   R   R   t   _reventst   watcherR   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyt   python_handle_error    s    	c         C@ s“   d  } | d  k	 r" | j j d n d  } | r@ |  j | ƒ } n  | d  k	 ri | j j d  | | | ƒ d St d d t j ƒt	 j
 | | | ƒ d  S(   NR   i   s0   WARNING: gevent: Unhandled error with no watchert   file(   R   t   tb_framet   f_localsR   R    R'   t   printR   t   stderrt	   tracebackt   print_exception(   R   t   tt   vt   tbR*   R   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyt   unhandled_onerrorÀ   s    "	
c         C@ s:   | s t  d d t j ƒd  S|  j | ƒ } | j ƒ  d  S(   Ns„   WARNING: gevent: Unable to dereference handle; not stopping watcher. Native resources may leak. This is most likely a bug in gevent.R,   (   R/   R   R0   R   R(   (   R   R   R*   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyt   python_stopÛ   s    
c         C@ s   d  S(   N(    (   R   t   watcher_ptr(    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyt   python_check_callbackê   s    c         C@ s%   y t  ƒ  ‚ Wn t  k
 r  n Xd  S(   N(   t   MemoryError(   R   R8   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyR9   û   s    c         C@ s:   |  j  | ƒ } | d  k r, t d | ƒ d  S| j ƒ  d  S(   NsD   WARNING: gevent: running prepare callbacks from a destroyed handle: (   t   _find_loop_from_c_watcherR   R/   t   _run_callbacks(   R   R8   R    (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyt   python_prepare_callback  s    c         C@ sj   | d  k	 r | j j d n d  } | r: |  j | ƒ } n  | d  k	 r` | j d  | | | ƒ d  S| ‚ d  S(   NR8   (   R   R-   R.   R;   R'   (   R   R3   R4   R5   R8   R    (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyt   check_callback_onerror  s    "c         C@ s   t  ƒ  ‚ d  S(   N(   t   NotImplementedError(   R   R8   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyR;     s    (   R   R   R   R   R&   R+   R6   R7   R   R9   R=   R>   R;   (    (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyR   C   s   			P	 						c   
      C@ sØ   | |  ƒ } t  g  | D]! \ } } t | | ƒ | f ^ q ƒ } x‘ | j d  f | j d  f | j d  f | j | j f | j | j f f | D]C \ } } | p¥ | j	 } |  j
 d | ƒ | ƒ }	 | j j |	 ƒ q W| S(   Nt   onerror(   t   tuplet   getattrR&   R   R+   R7   R9   R>   R=   R6   t
   def_externR   t   append(
   R   t   libt   callbacks_classt   extrasR   t   namet   errort   funct
   error_funcR   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyR
     s    4	i   i2   c           B@ sÜ  e  Z d; Z d; Z d; Z d; Z d; Z e Z	 d; Z
 d; d; d  „ Z d; d; d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z d „  Z d „  Z d „  Z  e e d „ Z! d „  Z" d „  Z# d „  Z$ d; d „ Z% d „  Z& d „  Z' d „  Z( d „  Z) d „  Z* e d „  ƒ Z+ e d  „  ƒ Z, e d! „  ƒ Z- e d" „  ƒ Z. e d# „  ƒ Z/ e d$ „  ƒ Z0 e1 d; d% „ Z2 d& e1 d; d' „ Z3 e1 d; d( „ Z4 e1 d; d) „ Z5 e1 d; d* „ Z6 e1 d; d+ „ Z7 e1 d; d, „ Z8 e1 d; d- „ Z9 e9 e: ƒ  d. <e; j< d/ k r€d0 e1 d1 „ Z= d2 „  Z> n  d& e1 d; d3 „ Z? d; d4 „ Z@ d5 „  ZA d6 „  ZB d7 „  ZC d8 „  ZD d9 „  ZE e d: „  ƒ ZF RS(<   c         C@ sn   | |  _  | |  _ d  |  _ |  j  j |  ƒ |  _ | |  _ t |  _ t	 ƒ  |  _
 t ƒ  |  _ |  j | | ƒ d  S(   N(   t   _ffit   _libR   t   _ptrt
   new_handlet   _handle_to_selft	   _watcherst   Falset   _in_callbackR   t
   _callbackst   setR!   t   _init_loop_and_aux_watchers(   R   R   RE   t   watcherst   flagst   default(    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyR   W  s    					c         C@ s¬   |  j  | | ƒ |  _ |  j j |  j ƒ |  _ |  j |  j _ |  j ƒ  |  j j |  j	 ƒ |  _
 |  j |  j
 _ |  j ƒ  |  j j |  j ƒ |  _ |  j |  j _ |  j ƒ  d  S(   N(   t
   _init_loopRN   RL   t   newt   _CHECK_POINTERt   _checkRP   t   datat   _init_and_start_checkt   _PREPARE_POINTERt   _preparet   _init_and_start_preparet   _TIMER_POINTERt   _timer0t   _init_callback_timer(   R   RX   RY   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyRV   d  s    

	c         C@ s   t  ƒ  ‚ d S(   ss   
        Called by __init__ to create or find the loop. The return value
        is assigned to self._ptr.
        N(   R?   (   R   RX   RY   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyRZ   ˆ  s    c         C@ s   t  ƒ  ‚ d  S(   N(   R?   (   R   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyR_     s    c         C@ s   t  ƒ  ‚ d  S(   N(   R?   (   R   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyRb   ’  s    c         C@ s   t  ƒ  ‚ d  S(   N(   R?   (   R   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyRe   •  s    c         C@ s   t  ƒ  ‚ d  S(   N(   R?   (   R   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyt   _stop_callback_timer˜  s    c         C@ s   t  ƒ  ‚ d  S(   N(   R?   (   R   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyt   _start_callback_timer›  s    c         C@ s   |  j  d  | | | ƒ d  S(   N(   R'   R   (   R   R3   R4   R5   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyt   _check_callback_handle_errorž  s    c         C@ sž  t  |  _ z„t } |  j ƒ  } | t ƒ  } |  j ƒ  x%|  j r\|  j j ƒ  } | d 8} |  j ƒ  | j	 } d  | _	 | j } | d  k s8 | d  k r q8 n  zk y | | Œ  WnV y |  j | t j ƒ  Œ Wqy! t d d t j ƒt j ƒ  WqqXqXn XWd  d  | _ X| d k r8 |  j r8 t } |  j ƒ  |  j ƒ  | k rYd } PqYq8 q8 W| d k rv|  j ƒ  n  |  j rŒ|  j ƒ  n  Wd  t |  _ Xd  S(   Ni   s&   Exception while handling another errorR,   i    (   t   Truet#   starting_timer_may_update_loop_timet   CALLBACK_CHECK_COUNTt   nowR   Rf   RT   t   popleftt   unrefR   R   R   R'   R   R   R/   R0   R1   t	   print_exct
   update_nowRg   RR   (   R   t   countRl   t
   expirationt   cbR   R   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyR<   ¡  sN    	


			 

	c         C@ s   t  ƒ  ‚ d  S(   N(   R?   (   R   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyt   _stop_aux_watchersñ  s    c         C@ sd   |  j  r` z4 |  j |  j  ƒ s" t S|  j ƒ  |  j |  j  ƒ Wd  d  |  _  |  ` |  ` |  ` Xt	 Sd  S(   N(
   RN   t   _can_destroy_loopRR   Rt   t   _destroy_loopR   RP   RT   R!   Ri   (   R   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyt   destroyô  s    	
	c         C@ s   t  ƒ  ‚ d  S(   N(   R?   (   R   t   ptr(    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyRu     s    c         C@ s   t  ƒ  ‚ d  S(   N(   R?   (   R   Rx   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyRv   	  s    c         C@ s   |  j  S(   N(   RN   (   R   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyRx     s    c         C@ s
   |  j  j S(   N(   RQ   R*   (   R   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyt   WatcherType  s    c         C@ s   d S(   Ni   (    (   R   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyt   MAXPRI  s    c         C@ s   d S(   Ni   (    (   R   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyt   MINPRI  s    c         C@ so   y t  j | ƒ } Wn t j ƒ  n Xy d | | f } Wn t j ƒ  n X|  j d  t t | ƒ d  ƒ d  S(   Ns   %s: %s(   t   ost   strerrorR1   Ro   R'   R   t   SystemError(   R   t   messaget   errno(    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyt   _handle_syserr  s    c         C@ s]   d  } |  j } | d  k	 rC t | d | ƒ } | | | | | ƒ n |  j | | | | ƒ d  S(   NR'   (   R   t   error_handlerRB   t   _default_handle_error(   R   t   contextt   typet   valueR5   R'   R‚   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyR'   '  s    	c         C@ s   t  j | | | ƒ d  S(   N(   R1   R2   (   R   R„   R…   R†   R5   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyRƒ   1  s    c         C@ s   t  ƒ  ‚ d  S(   N(   R?   (   R   t   nowaitt   once(    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyt   run8  s    c         C@ s   t  ƒ  ‚ d  S(   N(   R?   (   R   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyt   reinit;  s    c         C@ s   t  ƒ  ‚ d  S(   N(   R?   (   R   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyt   ref>  s    c         C@ s   t  ƒ  ‚ d  S(   N(   R?   (   R   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyRn   B  s    c         C@ s   t  ƒ  ‚ d  S(   N(   R?   (   R   t   how(    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyt   break_E  s    c         C@ s   d  S(   N(    (   R   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyt   verifyH  s    c         C@ s   t  ƒ  ‚ d  S(   N(   R?   (   R   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyRl   K  s    c         C@ s   t  ƒ  ‚ d  S(   N(   R?   (   R   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyRp   N  s    c         C@ s0   d d  l  } | j d t d d ƒ|  j ƒ  d  S(   Ni    s(   'update' is deprecated; use 'update_now't
   stackleveli   (   t   warningst   warnt   DeprecationWarningRp   (   R   R   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyt   updateQ  s
    	c         C@ s#   d |  j  j t |  ƒ |  j ƒ  f S(   Ns   <%s at 0x%x %s>(   t	   __class__R   t   idt   _format(   R   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyR   X  s    c         C@ s   |  j  r |  j St S(   N(   RN   t   _defaultRR   (   R   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyRY   [  s    c         C@ s   d S(   Niÿÿÿÿ(    (   R   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyt	   iteration_  s    c         C@ s   d S(   Niÿÿÿÿ(    (   R   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyt   depthc  s    c         C@ s   d S(   Ni    (    (   R   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyt   backend_intg  s    c         C@ s   d S(   NRY   (    (   R   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyt   backendk  s    c         C@ s   d S(   Ni    (    (   R   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyt
   pendingcnto  s    c         C@ s   |  j  j |  | | | | ƒ S(   N(   RQ   t   io(   R   t   fdt   eventsR‹   t   priority(    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyR   s  s    g        c         C@ s   |  j  j |  | | | | ƒ S(   N(   RQ   t   timer(   R   t   aftert   repeatR‹   R    (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyR¡   v  s    c         C@ s   |  j  j |  | | | ƒ S(   N(   RQ   t   signal(   R   t   signumR‹   R    (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyR¤   y  s    c         C@ s   |  j  j |  | | ƒ S(   N(   RQ   t   idle(   R   R‹   R    (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyR¦   |  s    c         C@ s   |  j  j |  | | ƒ S(   N(   RQ   t   prepare(   R   R‹   R    (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyR§     s    c         C@ s   |  j  j |  | | ƒ S(   N(   RQ   t   check(   R   R‹   R    (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyR¨   ‚  s    c         C@ s   |  j  j |  | | ƒ S(   N(   RQ   t   fork(   R   R‹   R    (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyR©   …  s    c         C@ s   |  j  j |  | | ƒ S(   N(   RQ   t   async_(   R   R‹   R    (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyRª   ˆ  s    t   asynct   win32i    c         C@ s   |  j  j |  | | | ƒ S(   N(   RQ   t   child(   R   t   pidt   traceR‹   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyR­     s    c         C@ s   d  S(   N(    (   R   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyt   install_sigchld“  s    c         C@ s   |  j  j |  | | | | ƒ S(   N(   RQ   t   stat(   R   t   patht   intervalR‹   R    (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyR±   –  s    c         C@ s   t  |  | ƒ S(   N(   R   (   R   R    (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyR   ™  s    c         C@ s   t  ƒ  ‚ d  S(   N(   R?   (   R   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyt   _setup_for_run_callbackœ  s    c         G@ s-   t  | | ƒ } |  j j | ƒ |  j ƒ  | S(   N(   R   RT   RD   R´   (   R   RJ   R   Rs   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyt   run_callbackŸ  s    
c         C@ sQ   |  j  s d S|  j } |  j r, | d 7} n  | d |  j 7} | |  j ƒ  7} | S(   Nt	   destroyeds    defaults    pending=%s(   RN   R›   RY   Rœ   t   _format_details(   R   t   msg(    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyR–   ©  s    			c         C@ sƒ   d } |  j  ƒ  } y |  j } Wn t k
 r8 d  } n X| d  k	 r\ | d t | ƒ 7} n  | d  k	 r | d t | ƒ 7} n  | S(   Nt    s    ref=s    fileno=(   t   filenot	   activecntt   AttributeErrorR   t   repr(   R   R¸   Rº   R»   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyR·   ³  s    
c         C@ s   d  S(   N(   R   (   R   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyRº   Â  s    c         C@ s   |  j  s t d ƒ ‚ n  d S(   Ns   operation on destroyed loopi    (   RN   t
   ValueError(   R   (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyR»   Å  s    	N(G   R   R   R   R‚   R\   Rc   t   _TIMER_CALLBACK_SIGR`   RR   Rj   R—   R   RV   RZ   R_   Rb   Re   Rf   Rg   Rh   R<   Rt   Rw   Ru   Rv   t   propertyRx   Ry   Rz   R{   R   R'   Rƒ   R‰   RŠ   R‹   Rn   R   RŽ   Rl   Rp   R“   R   RY   R˜   R™   Rš   R›   Rœ   Ri   R   R¡   R¤   R¦   R§   R¨   R©   Rª   t   localsR   t   platformR­   R°   R±   R   R´   Rµ   R–   R·   Rº   R»   (    (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyR	   E  s€   $								P						
											
	
		(    (    (%   t   __doc__t
   __future__R    R   t   collectionsR   R   R|   R1   t   gevent._ffiR   R   R   t   gevent._ffi.callbackR   t   gevent._compatR   t   geventR   t   __all__t   objectR   t   EVENTSR   R   R
   t   version_infot   bytest   strt
   basestringt   intt   integer_typest   __builtin__t   longR   Rk   R	   (    (    (    s/   lib/python2.7/site-packages/gevent/_ffi/loop.pyt   <module>   s4   	%Ù