
[XMc           @   s	  d  Z  d d l Z d d l Z d d l Z d d l Z d d l m Z m Z d d l m	 Z	 d d l
 m Z m Z d d l
 m Z m Z m Z m Z d d l
 m Z m Z d d l
 m Z d d	 l m Z d d
 l m Z m Z d d l m Z m Z m Z d d l m Z d d l m Z m  Z  d d l! m" Z" m# Z# y d d l m$ Z$ e% Z& Wn e' k
 rke( Z& n Xy d d l m) Z) e% Z* Wn e' k
 re( Z* n Xe( Z+ e d k rd d l m, Z, m- Z- m. Z. e% Z+ n  e  j/   ry d d l0 Z0 e% Z+ Wn e' k
 re1 Z0 n Xn  d e j2 e j3 f d     YZ4 d e5 e j2 e j3 f d     YZ6 d e6 f d     YZ7 e d k re7 Z8 n e4 Z8 d e6 f d     YZ9 d e# e" f d     YZ: e& re e: e  n  e* re e: e e  n  e+ re e: e  n  d g Z; d S(   s   
Posix reactor base class
iN(   t
   implementst   classImplements(   t   set(   t   IReactorUNIXt   IReactorUNIXDatagram(   t   IReactorTCPt   IReactorUDPt   IReactorSSLt   _IReactorArbitrary(   t   IReactorProcesst   IReactorMulticast(   t   IHalfCloseableDescriptor(   t   error(   t   tcpt   udp(   t   logt   failuret   util(   t   styles(   t   platformTypet   platform(   t   ReactorBaset   _SignalReactorMixin(   t   ssl(   t   unixt   posix(   t   fdesct   processt   _signalst   _SocketWakerc           B   s8   e  Z d  Z d Z d   Z d   Z d   Z d   Z RS(   s   
    The I{self-pipe trick<http://cr.yp.to/docs/selfpipe.html>}, implemented
    using a pair of sockets rather than pipes (due to the lack of support in
    select() on Windows for pipes), used to wake up the main loop from
    another thread.
    i    c         C   s   | |  _  t j t j t j  } t j t j t j  } | j t j t j d  | j d  | j d  | j	 | j
    | j   \ } } | j d  | j d  | |  _ | |  _ |  j j |  _ d S(   s   Initialize.
        i   s	   127.0.0.1i    N(   s	   127.0.0.1i    (   t   reactort   sockett   AF_INETt   SOCK_STREAMt
   setsockoptt   IPPROTO_TCPt   TCP_NODELAYt   bindt   listent   connectt   getsocknamet   acceptt   setblockingt   rt   wt   fileno(   t   selfR   t   servert   clientt   readert
   clientaddr(    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt   __init__@   s    			c         C   sR   y t  j |  j j d  Wn1 t j k
 rM \ } } | t j k rN   qN n Xd S(   s&   Send a byte to my connection.
        t   xN(   R   t   untilConcludesR,   t   sendR   R   t   errnot   WSAEWOULDBLOCK(   R.   t   errt   msg(    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt   wakeUpR   s
    c         C   s/   y |  j  j d  Wn t j k
 r* n Xd S(   s+   Read some data from my connection.
        i    N(   R+   t   recvR   R   (   R.   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt   doRead[   s    c         C   s   |  j  j   |  j j   d  S(   N(   R+   t   closeR,   (   R.   t   reason(    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt   connectionLostc   s    (   t   __name__t
   __module__t   __doc__t   disconnectedR3   R;   R=   R@   (    (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyR   7   s   				t   _FDWakerc           B   s;   e  Z d  Z d Z d Z d Z d   Z d   Z d   Z	 RS(   s  
    The I{self-pipe trick<http://cr.yp.to/docs/selfpipe.html>}, used to wake
    up the main loop from another thread or a signal handler.

    L{_FDWaker} is a base class for waker implementations based on
    writing to a pipe being monitored by the reactor.

    @ivar o: The file descriptor for the end of the pipe which can be
        written to to wake up a reactor monitoring this waker.

    @ivar i: The file descriptor which should be monitored in order to
        be awoken by this waker.
    i    c            sw   |   _  t j   \   _   _ t j   j  t j   j  t j   j  t j   j    f d     _ d S(   s   Initialize.
        c              s     j  S(   N(   t   i(    (   R.   (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt   <lambda>   s    N(	   R   t   ost   pipeRF   t   oR   t   setNonBlockingt   _setCloseOnExecR-   (   R.   R   (    (   R.   s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyR3   |   s    	c         C   s   t  j |  j   d    d S(   sA   
        Read some bytes from the pipe and discard them.
        c         S   s   d  S(   N(   t   None(   t   data(    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyRG      s    N(   R   t
   readFromFDR-   (   R.   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyR=      s    c         C   se   t  |  d  s d Sx? |  j |  j f D]+ } y t j |  Wq& t k
 rP q& Xq& W|  ` |  ` d S(   s$   Close both ends of my pipe.
        RJ   N(   t   hasattrRF   RJ   RH   R>   t   IOError(   R.   R?   t   fd(    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyR@      s    N(
   RA   RB   RC   RD   RM   RF   RJ   R3   R=   R@   (    (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyRE   i   s   		t
   _UnixWakerc           B   s   e  Z d  Z d   Z RS(   s   
    This class provides a simple interface to wake up the event loop.

    This is used by threads or signals to wake up the event loop.
    c         C   sa   |  j  d k	 r] y t j t j |  j  d  Wq] t k
 rY } | j t j k rZ   qZ q] Xn  d S(   s2   Write one byte to the pipe, and flush it.
        R4   N(	   RJ   RM   R   R5   RH   t   writet   OSErrorR7   t   EAGAIN(   R.   t   e(    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyR;      s    (   RA   RB   RC   R;   (    (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyRS      s   t   _SIGCHLDWakerc           B   s2   e  Z d  Z d   Z d   Z d   Z d   Z RS(   s}   
    L{_SIGCHLDWaker} can wake up a reactor whenever C{SIGCHLD} is
    received.

    @see: L{twisted.internet._signals}
    c         C   s   t  j |  |  d  S(   N(   RE   R3   (   R.   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyR3      s    c         C   s   t  j |  j  d S(   sJ   
        Install the handler necessary to make this waker active.
        N(   R   t   installHandlerRJ   (   R.   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt   install   s    c         C   s   t  j d  d S(   sC   
        Remove the handler which makes this waker active.
        iN(   R   RY   (   R.   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt	   uninstall   s    c         C   s   t  j |   t j   d S(   s  
        Having woken up the reactor in response to receipt of
        C{SIGCHLD}, reap the process which exited.

        This is called whenever the reactor notices the waker pipe is
        writeable, which happens soon after any call to the C{wakeUp}
        method.
        N(   RE   R=   R   t   reapAllProcesses(   R.   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyR=      s    	(   RA   RB   RC   R3   RZ   R[   R=   (    (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyRX      s
   			t   PosixReactorBasec           B   sX  e  Z d  Z e e e e e  i e j	 e
 j    e
 j 6e j	 e
 j    e
 j 6d  Z d   Z d Z d   Z d   Z d i  d d d d d d  Z d d d	  Z d d e d
  Z d d d  Z d d d d  Z d d d  Z d d d d  Z d d d  Z d d d  Z d d d  Z d d d  Z d   Z d   Z  d   Z! RS(   s   
    A basis for reactors that use file descriptors.

    @ivar _childWaker: C{None} or a reference to the L{_SIGCHLDWaker}
        which is used to properly notice child process termination.
    c         C   s   |  j  |  | j | j  } | ry | r\ | j t j k r\ t j |  r\ | j |  q |  j |  | j	 |  n# |  j |  | j	 t
 j |   d S(   s   
        Utility function for disconnecting a selectable.

        Supports half-close notification, isRead should be boolean indicating
        whether error resulted from doRead().
        N(   t   removeReadert   gett	   __class__R   t   ConnectionDoneR   t
   providedByt   readConnectionLostt   removeWriterR@   R   t   Failure(   R.   t
   selectablet   whyt   isReadt   faildictt   f(    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt   _disconnectSelectable   s    
c         C   sB   |  j  s> t |   |  _  |  j j |  j   |  j |  j   n  d S(   s   
        Install a `waker' to allow threads and signals to wake up the IO thread.

        We use the self-pipe trick (http://cr.yp.to/docs/selfpipe.html) to wake
        the reactor. On Windows we use a pair of sockets.
        N(   t   wakert   _Wakert   _internalReaderst   addt	   addReader(   R.   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt   installWaker  s    	c         C   su   t  j |   t d k rq |  j sW t |   |  _ |  j j |  j  |  j |  j  n  |  j j   t	 j
   n  d S(   s   
        Extend the basic signal handling logic to also support
        handling SIGCHLD to know when to try to reap child processes.
        R   N(   R   t   _handleSignalsR   t   _childWakerRX   Rn   Ro   Rp   RZ   R   R\   (   R.   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyRr     s    	c         C   s   |  j  r |  j  j   n  d S(   s  
        If a child waker was created and installed, uninstall it now.

        Since this disables reactor functionality and is only called
        when the reactor is stopping, it doesn't provide any directly
        useful functionality, but the cleanup of reactor-related
        process-global state that it does helps in unit tests
        involving multiple reactors and is generally just a nice
        thing.
        N(   Rs   R[   (   R.   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt   _uninstallHandler#  s    	i    c
      
   C   s7  |  j  | |  \ } } t d k r | rj |	 d  k	 rE t d   n  t j |  | | | | | | | | 	 St j |  | | | | | | | |	 	 Sn t d k r*| d  k	 s | d  k	 r t d   n  | r t d   n  |	 r t d   n  t rd d l m }
 |
 |  | | | | |  St	 d	  n	 t	 d
  d  S(   NR   s1   Using childFDs is not supported with usePTY=True.t   win32s8   The uid and gid parameters are not supported on Windows.s1   The usePTY parameter is not supported on Windows.s1   Customizing childFDs is not supported on Windows.i(   t   Processs:   spawnProcess not available since pywin32 is not installed.s0   spawnProcess only available on Windows or POSIX.(
   t   _checkProcessArgsR   RM   t
   ValueErrorR   t
   PTYProcessRv   t   win32processt   twisted.internet._dumbwin32proct   NotImplementedError(   R.   t   processProtocolt
   executablet   argst   envt   patht   uidt   gidt   usePTYt   childFDsRv   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt   spawnProcess6  s*    t    i    c         C   s)   t  j | | | | |   } | j   | S(   s   Connects a given L{DatagramProtocol} to the given numeric UDP port.

        @returns: object conforming to L{IListeningPort}.
        (   R   t   Portt   startListening(   R.   t   portt   protocolt	   interfacet   maxPacketSizet   p(    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt	   listenUDPU  s    
c         C   s,   t  j | | | | |  |  } | j   | S(   s   Connects a given DatagramProtocol to the given numeric UDP port.

        EXPERIMENTAL.

        @returns: object conforming to IListeningPort.
        (   R   t   MulticastPortR   (   R.   R   R   R   R   t   listenMultipleR   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt   listenMulticast`  s    
i   c         C   s8   t  s t d  t j | | | |  |  } | j   | S(   sC   @see: twisted.internet.interfaces.IReactorUNIX.connectUNIX
        s   UNIX support is not present(   t   unixEnabledt   AssertionErrorR   t	   ConnectorR'   (   R.   t   addresst   factoryt   timeoutt   checkPIDt   c(    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt   connectUNIXn  s    
i2   i  c         C   s;   t  s t d  t j | | | | |  |  } | j   | S(   sK   
        @see: twisted.internet.interfaces.IReactorUNIX.listenUNIX
        s   UNIX support is not present(   R   R   R   R   R   (   R.   R   R   t   backlogt   modet   wantPIDR   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt
   listenUNIXv  s    
c         C   s8   t  s t d  t j | | | | |   } | j   | S(   s   
        Connects a given L{DatagramProtocol} to the given path.

        EXPERIMENTAL.

        @returns: object conforming to L{IListeningPort}.
        s   UNIX support is not present(   R   R   R   t   DatagramPortR   (   R.   R   R   R   R   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt   listenUNIXDatagram  s    	
c         C   s;   t  s t d  t j | | | | | |   } | j   | S(   sd   
        Connects a L{ConnectedDatagramProtocol} instance to a path.

        EXPERIMENTAL.
        s   UNIX support is not present(   R   R   R   t   ConnectedDatagramPortR   (   R.   R   R   R   R   t   bindAddressR   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt   connectUNIXDatagram  s    
c         C   s)   t  j | | | | |   } | j   | S(   s@   @see: twisted.internet.interfaces.IReactorTCP.listenTCP
        (   R   R   R   (   R.   R   R   R   R   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt	   listenTCP  s    
c         C   s,   t  j | | | | | |   } | j   | S(   sA   @see: twisted.internet.interfaces.IReactorTCP.connectTCP
        (   R   R   R'   (   R.   t   hostR   R   R   R   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt
   connectTCP  s    
c         C   s>   t  s t d  t j | | | | | | |   } | j   | S(   sA   @see: twisted.internet.interfaces.IReactorSSL.connectSSL
        s   SSL support is not present(   t
   sslEnabledR   R   R   R'   (   R.   R   R   R   t   contextFactoryR   R   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt
   connectSSL  s    !
c         C   s;   t  s t d  t j | | | | | |   } | j   | S(   s@   @see: twisted.internet.interfaces.IReactorSSL.listenSSL
        s   SSL support is not present(   R   R   R   R   R   (   R.   R   R   R   R   R   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt	   listenSSL  s    
c         O   s@   t  j d d t d d |  | d <| | |   } | j   | S(   Ns@   listenWith is deprecated since Twisted 10.1.  See IReactorFDSet.t   categoryt
   stackleveli   R   (   t   warningst   warnt   DeprecationWarningR   (   R.   t   portTypeR   t   kwR   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt
   listenWith  s    

c         O   s@   t  j d d t d d |  | d <| | |   } | j   | S(   NsA   connectWith is deprecated since Twisted 10.1.  See IReactorFDSet.R   R   i   R   (   R   R   R   R'   (   R.   t   connectorTypeR   R   R   (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt   connectWith  s    

c         C   si   t  |  |  j } x | D] } |  j |  q Wt  |  } x | D] } |  j |  qD Wt | | B S(   sg  
        Remove all readers and writers, and list of removed L{IReadDescriptor}s
        and L{IWriteDescriptor}s.

        Meant for calling from subclasses, to implement removeAll, like::

          def removeAll(self):
              return self._removeAll(self._reads, self._writes)

        where C{self._reads} and C{self._writes} are iterables.
        (   R   Rn   R^   Rd   t   list(   R.   t   readerst   writerst   removedReadersR1   t   removedWriterst   writer(    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt
   _removeAll  s    N(    ("   RA   RB   RC   R    R   R   R   R
   R   Re   R   Ra   t   ConnectionLostRk   Rq   RM   Rs   Rr   Rt   R   R   t   FalseR   R   R   R   R   R   R   R   R   R   R   R   (    (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyR]      s6   "				
		(<   RC   R   R   R7   RH   t   zope.interfaceR    R   t   twisted.python.compatR   t   twisted.internet.interfacesR   R   R   R   R   R   R	   R
   R   t   twisted.internetR   R   R   t   twisted.pythonR   R   R   t   twisted.persistedR   t   twisted.python.runtimeR   R   t   twisted.internet.baseR   R   R   t   TrueR   t   ImportErrorR   R   R   t   processEnabledR   R   R   t	   isWindowsRz   RM   t   Loggert	   EphemeralR   t   objectRE   RS   Rm   RX   R]   t   __all__(    (    (    s>   /usr/lib/python2.7/dist-packages/twisted/internet/posixbase.pyt   <module>   sh   "



	
2"4	' 