ó
8Þ	Pc           @   sy  d  d d g Z  d d l Z d d l Z d d l Z d d l 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 m Z m Z m Z d d l m Z m Z d Z d	 Z e j ƒ  Z d
 Z d
 g Z e e d ƒ r	d Z e d g 7Z n  e j d k r.d Z e d g 7Z n  e d „ Z d „  Z d „  Z d „  Z d e  f d „  ƒ  YZ! d d d „ Z# e j d k r˜e$ d „ Z% n d d l m& Z& e$ d „ Z% d e  f d „  ƒ  YZ' d „  Z( e j d k rd e  f d „  ƒ  YZ) d „  Z* n  d Z+ d Z, d Z- d  Z. d! „  Z/ d" „  Z0 d# e  f d$ „  ƒ  YZ1 d% „  Z2 d& „  Z3 d' e! f d( „  ƒ  YZ4 d) „  Z5 d S(*   t   Clientt   Listenert   PipeiÿÿÿÿN(   t   current_processt   AuthenticationError(   t   get_temp_dirt   Finalizet	   sub_debugt   debug(   t	   duplicatet   closei    g      4@t   AF_INETt   AF_UNIXt   win32t   AF_PIPEc         C   s   t  j  ƒ  |  S(   N(   t   time(   t   timeout(    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyt   _init_timeoutI   s    c         C   s   t  j  ƒ  |  k S(   N(   R   (   t   t(    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyt   _check_timeoutL   s    c         C   sw   |  d k r d S|  d k r5 t  j d d d t ƒ  ƒ S|  d k rg t  j d d	 t j ƒ  t j ƒ  f ƒ St d
 ƒ ‚ d S(   s?   
    Return an arbitrary free address for the given family
    R   t	   localhosti    R   t   prefixs	   listener-t   dirR   s   \\.\pipe\pyc-%d-%d-s   unrecognized familyN(   R   i    (   t   tempfilet   mktempR   t   ost   getpidt   _mmap_countert   nextt
   ValueError(   t   family(    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyt   arbitrary_addressS   s    c         C   se   t  |  ƒ t k r d St  |  ƒ t k r; |  j d ƒ r; d St  |  ƒ t k rQ d St d |  ƒ ‚ d S(   s]   
    Return the types of the address

    This can be 'AF_INET', 'AF_UNIX', or 'AF_PIPE'
    R   s   \\R   R   s   address type of %r unrecognizedN(   t   typet   tuplet   strt
   startswithR   (   t   address(    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyt   address_typeb   s    !c           B   sS   e  Z d  Z d d d d d „ Z d „  Z d „  Z e d „  ƒ Z e d „  ƒ Z	 RS(   s•   
    Returns a listener object.

    This is a wrapper for a bound socket which is 'listening' for
    connections, or for a Windows named pipe.
    i   c         C   s›   | p | r t  | ƒ p t } | p- t | ƒ } | d k rQ t | | ƒ |  _ n t | | | ƒ |  _ | d  k	 rŽ t | t ƒ rŽ t	 d ‚ n  | |  _
 d  S(   NR   s   authkey should be a byte string(   R%   t   default_familyR   t   PipeListenert	   _listenert   SocketListenert   Nonet
   isinstancet   bytest	   TypeErrort   _authkey(   t   selfR$   R   t   backlogt   authkey(    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyt   __init__|   s    c         C   s?   |  j  j ƒ  } |  j r; t | |  j ƒ t | |  j ƒ n  | S(   sz   
        Accept a connection on the bound socket or named pipe of `self`.

        Returns a `Connection` object.
        (   R(   t   acceptR.   t   deliver_challenget   answer_challenge(   R/   t   c(    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyR3   ‹   s
    	c         C   s   |  j  j ƒ  S(   sA   
        Close the bound socket or named pipe of `self`.
        (   R(   R
   (   R/   (    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyR
   —   s    c         C   s
   |  j  j S(   N(   R(   t   _address(   R/   (    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyt   <lambda>   s    c         C   s
   |  j  j S(   N(   R(   t   _last_accepted(   R/   (    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyR8   ž   s    N(
   t   __name__t
   __module__t   __doc__R*   R2   R3   R
   t   propertyR$   t   last_accepted(    (    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyR   u   s   		c         C   sŽ   | p t  |  ƒ } | d k r- t |  ƒ } n t |  ƒ } | d k	 ra t | t ƒ ra t d ‚ n  | d k	 rŠ t | | ƒ t | | ƒ n  | S(   s=   
    Returns a connection to the address of a `Listener`
    R   s   authkey should be a byte stringN(	   R%   t
   PipeClientt   SocketClientR*   R+   R,   R-   R5   R4   (   R$   R   R1   R6   (    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyR    ¡   s    c         C   s±   |  rk t  j ƒ  \ } } t j t j | j ƒ  ƒ ƒ } t j t j | j ƒ  ƒ ƒ } | j ƒ  | j ƒ  n< t j ƒ  \ } } t j | d t	 ƒ} t j | d t	 ƒ} | | f S(   sL   
        Returns pair of connection objects at either end of a pipe
        t   writablet   readable(
   t   sockett
   socketpairt   _multiprocessingt
   ConnectionR   t   dupt   filenoR
   t   pipet   False(   t   duplext   s1t   s2t   c1t   c2t   fd1t   fd2(    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyR   ·   s    
(   R   c      	   C   sU  t  d ƒ } |  r; t j } t j t j B} t t } } n t j } t j } d t } } t j | | t j t j	 Bt j
 Bd | | t j t j ƒ } t j | | d t j t j d t j ƒ } t j | t j	 d d ƒ y t j | t j ƒ Wn/ t k
 r } | j d t j k r!‚  q!n Xt j | d |  ƒ}	 t j | d |  ƒ}
 |	 |
 f S(   sL   
        Returns pair of connection objects at either end of a pipe
        R   i    i   RA   RB   N(   R   R   t   PIPE_ACCESS_DUPLEXt   GENERIC_READt   GENERIC_WRITEt   BUFSIZEt   PIPE_ACCESS_INBOUNDt   CreateNamedPipet   PIPE_TYPE_MESSAGEt   PIPE_READMODE_MESSAGEt	   PIPE_WAITt   NMPWAIT_WAIT_FOREVERt   NULLt
   CreateFilet   OPEN_EXISTINGt   SetNamedPipeHandleStateR*   t   ConnectNamedPipet   WindowsErrort   argst   ERROR_PIPE_CONNECTEDRE   t   PipeConnection(   RK   R$   t   openmodet   accesst   obsizet   ibsizet   h1t   h2t   eRN   RO   (    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyR   Ì   s2    			$
R)   c           B   s,   e  Z d  Z d d „ Z d „  Z d „  Z RS(   sO   
    Representation of a socket which is bound to an address and listening
    i   c         C   s»   t  j  t t  | ƒ ƒ |  _ |  j j t  j t  j d ƒ |  j j | ƒ |  j j | ƒ |  j j ƒ  |  _	 | |  _
 d  |  _ | d k r® t |  t j d | f d d ƒ|  _ n	 d  |  _ d  S(   Ni   R   Rb   t   exitpriorityi    (   RC   t   getattrt   _sockett
   setsockoptt
   SOL_SOCKETt   SO_REUSEADDRt   bindt   listent   getsocknameR7   t   _familyR*   R9   R   R   t   unlinkt   _unlink(   R/   R$   R   R0   (    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyR2   ú   s    		$c         C   sG   |  j  j ƒ  \ } |  _ t | j ƒ  ƒ } t j | ƒ } | j ƒ  | S(   N(   Rn   R3   R9   R	   RH   RE   RF   R
   (   R/   t   st   fdt   conn(    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyR3   
  s
    
c         C   s-   |  j  j ƒ  |  j d  k	 r) |  j ƒ  n  d  S(   N(   Rn   R
   Rw   R*   (   R/   (    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyR
     s    (   R:   R;   R<   R2   R3   R
   (    (    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyR)   ö   s   	c         C   sÒ   t  |  ƒ } t j t t | ƒ ƒ } t ƒ  } xs y | j |  ƒ WnX t j k
 r› } | j d t j k sx t	 | ƒ r‹ t
 d |  ƒ ‚  n  t j d ƒ q0 XPq0 ‚  t | j ƒ  ƒ } t j | ƒ } | j ƒ  | S(   sO   
    Return a connection object connected to the socket given by `address`
    i    s   failed to connect to address %sg{®Gáz„?(   R%   RC   Rm   R   t   connectt   errorRb   t   errnot   ECONNREFUSEDR   R   R   t   sleepR	   RH   RE   RF   R
   (   R$   R   Rx   R   Rk   Ry   Rz   (    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyR@     s"    	"
R'   c           B   s2   e  Z d  Z d d „ Z d „  Z e d „  ƒ Z RS(   s0   
        Representation of a named pipe
        c      	   C   s    | |  _  t j | t j t j t j Bt j Bt j t t t j	 t j
 ƒ } | g |  _ d  |  _ t d |  j  ƒ t |  t j d |  j |  j  f d d ƒ|  _ d  S(   Ns    listener created with address=%rRb   Rl   i    (   R7   R   RW   RR   RX   RY   RZ   t   PIPE_UNLIMITED_INSTANCESRU   R[   R\   t   _handle_queueR*   R9   R   R   R'   t   _finalize_pipe_listenerR
   (   R/   R$   R0   t   handle(    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyR2   ;  s    			c      	   C   s¼   t  j |  j t  j t  j t  j Bt  j Bt  j t t t  j	 t  j
 ƒ } |  j j | ƒ |  j j d ƒ } y t  j | t  j
 ƒ Wn/ t k
 r® } | j d t  j k r¯ ‚  q¯ n Xt j | ƒ S(   Ni    (   R   RW   R7   RR   RX   RY   RZ   R€   RU   R[   R\   R   t   appendt   popR`   Ra   Rb   Rc   RE   Rd   (   R/   t	   newhandleRƒ   Rk   (    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyR3   N  s    
c         C   s,   t  d | ƒ x |  D] } t | ƒ q Wd  S(   Ns    closing listener with address=%r(   R   R
   (   t   queueR$   Rƒ   (    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyR‚   _  s    N(   R:   R;   R<   R*   R2   R3   t   staticmethodR‚   (    (    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyR'   7  s   	c         C   sÈ   t  ƒ  } x– yH t j |  d ƒ t j |  t j t j Bd t j t j d t j ƒ } WnD t k
 rš } | j	 d t j
 t j f k s‘ t | ƒ rœ ‚  qœ q XPq ‚  t j | t j d d ƒ t j | ƒ S(   sU   
        Return a connection object connected to the pipe given by `address`
        iè  i    N(   R   R   t   WaitNamedPipeR]   RS   RT   R\   R^   Ra   Rb   t   ERROR_SEM_TIMEOUTt   ERROR_PIPE_BUSYR   R_   RY   R*   RE   Rd   (   R$   R   t   hRk   (    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyR?   e  s     	"
i   s   #CHALLENGE#s	   #WELCOME#s	   #FAILURE#c         C   s¡   d d  l  } t | t ƒ s! t ‚ t j t ƒ } |  j t | ƒ | j	 | | ƒ j
 ƒ  } |  j d ƒ } | | k r„ |  j t ƒ n |  j t ƒ t d ƒ ‚ d  S(   Niÿÿÿÿi   s   digest received was wrong(   t   hmacR+   R,   t   AssertionErrorR   t   urandomt   MESSAGE_LENGTHt
   send_bytest	   CHALLENGEt   newt   digestt
   recv_bytest   WELCOMEt   FAILURER   (   t
   connectionR1   R   t   messageR”   t   response(    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyR4   ‰  s    c         C   s¶   d d  l  } t | t ƒ s! t ‚ |  j d ƒ } | t t ƒ  t k sS t d | ‚ | t t ƒ } | j | | ƒ j ƒ  } |  j	 | ƒ |  j d ƒ } | t
 k r² t d ƒ ‚ n  d  S(   Niÿÿÿÿi   s   message = %rs   digest sent was rejected(   R   R+   R,   RŽ   R•   t   lenR’   R“   R”   R‘   R–   R   (   R˜   R1   R   R™   R”   Rš   (    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyR5   –  s    #t   ConnectionWrapperc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   sO   | |  _  | |  _ | |  _ x- d D]% } t | | ƒ } t |  | | ƒ q" Wd  S(   NRH   R
   t   pollR•   R‘   (   s   filenos   closeR   s
   recv_bytess
   send_bytes(   t   _connt   _dumpst   _loadsRm   t   setattr(   R/   Rz   t   dumpst   loadst   attrt   obj(    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyR2   §  s    			c         C   s#   |  j  | ƒ } |  j j | ƒ d  S(   N(   RŸ   Rž   R‘   (   R/   R¥   Rx   (    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyt   send®  s    c         C   s   |  j  j ƒ  } |  j | ƒ S(   N(   Rž   R•   R    (   R/   Rx   (    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyt   recv±  s    (   R:   R;   R2   R¦   R§   (    (    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyRœ   ¦  s   		c         C   s%   t  j |  f d  d  d  d ƒ j d ƒ S(   Ni   t   utf8(   t	   xmlrpclibR¢   R*   t   encode(   R¥   (    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyt
   _xml_dumpsµ  s    c         C   s%   t  j |  j d ƒ ƒ \ \ } } | S(   NR¨   (   R©   R£   t   decode(   Rx   R¥   t   method(    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyt
   _xml_loads¸  s    !t   XmlListenerc           B   s   e  Z d  „  Z RS(   c         C   s+   d d  l  a  t j |  ƒ } t | t t ƒ S(   Niÿÿÿÿ(   R©   R   R3   Rœ   R«   R®   (   R/   R¥   (    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyR3   ½  s    (   R:   R;   R3   (    (    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyR¯   ¼  s   c          O   s%   d d  l  a  t t |  | Ž  t t ƒ S(   Niÿÿÿÿ(   R©   Rœ   R    R«   R®   (   Rb   t   kwds(    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyt	   XmlClientÃ  s    (6   t   __all__R   t   sysRC   R}   R   R   t	   itertoolsRE   t   multiprocessingR   R   t   multiprocessing.utilR   R   R   R   t   multiprocessing.forkingR	   R
   RU   t   CONNECTION_TIMEOUTt   countR   R&   t   familiest   hasattrt   platformR   R   R   R%   t   objectR   R*   R    t   TrueR   R   R)   R@   R'   R?   R   R’   R–   R—   R4   R5   Rœ   R«   R®   R¯   R±   (    (    (    s0   /usr/lib/python2.7/multiprocessing/connection.pyt   <module>#   s`   "				,*!	.				