[iw @sXddddgZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl m Z ddl m Z dd l m Z mZdd l mZy2ddlZdd lmZmZmZmZWn-ek r%ejd krndZYnXd ZdZejZdZdgZeedrrdZedg7Znejd krdZedg7ZneddZddZddZ ddZ!ddZ"GdddZ#erGddde#Z$nGd d!d!e#Z%Gd"dde&Z'ddd#dZ(ejd krgd$d%dZ)nd$d&dZ)Gd'd(d(e&Z*d)d*Z+ejd krGd+d,d,e&Z,d-d.Z-nd/Z.d0Z/d1Z0d2Z1d3d4Z2d5d6Z3Gd7d8d8e&Z4d9d:Z5d;d<Z6Gd=d>d>e'Z7d?d@Z8ejd krdAdBZ9ej:ej;hZ<ddCdZ=n?ddl>Z>ee>dDre>j?Z@n e>jAZ@ddEdZ=ejd kr,dFdGZBdHdIZCe jDe%eBdJdKZEdLdMZFe jDe$eEn(dNdGZBdOdIZCe jDe%eBdS)PClientListenerPipewaitN) reduction)util)AuthenticationErrorBufferTooShort)ForkingPickler) WAIT_OBJECT_0WAIT_ABANDONED_0 WAIT_TIMEOUTINFINITEwin32i g4@AF_INETAF_UNIXAF_PIPEcCstj|S)N)time)timeoutr0/usr/lib/python3.4/multiprocessing/connection.py _init_timeout;srcCstj|kS)N)r)trrr_check_timeout>srcCs|dkrd S|dkr8tjdddtjS|dkrptjdd tjttfdd Std d S)z? Return an arbitrary free address for the given family r localhostrrprefixz listener-dirrz\\.\pipe\pyc-%d-%d-zunrecognized familyN)rr) tempfileZmktemprZ get_temp_dirosgetpidnext _mmap_counter ValueError)familyrrrarbitrary_addressEs     r&cCsrtjdkr.|dkr.td|ntjdkrn|dkrntt|sntd|qnndS)zD Checks if the family is valid for the current environment. rrzFamily %s is not recognized.rN)sysplatformr$hasattrsocket)r%rrr_validate_familySs r+cCset|tkrdSt|tkr;|jdr;dSt|tkrQdStd|dS)z] Return the types of the address This can be 'AF_INET', 'AF_UNIX', or 'AF_PIPE' rz\\rrzaddress type of %r unrecognizedN)typetuplestr startswithr$)addressrrr address_type_s!r1c@s!eZdZdZddddZddZddZd d Zd d Zd dZ e ddZ e ddZ e ddZ ddZddZddddZddZdddZdd d!Zd"d#Zd$d%d&Zd'd(Zd)d*ZdS)+_ConnectionBaseNTcCsc|j}|dkr'tdn| rD| rDtdn||_||_||_dS)Nrzinvalid handlez6at least one of `readable` and `writable` must be True) __index__r$_handle _readable _writable)selfhandlereadablewritablerrr__init__us     z_ConnectionBase.__init__cCs |jdk r|jndS)N)r4_close)r7rrr__del__sz_ConnectionBase.__del__cCs"|jdkrtdndS)Nzhandle is closed)r4OSError)r7rrr _check_closedsz_ConnectionBase._check_closedcCs|jstdndS)Nzconnection is write-only)r5r>)r7rrr_check_readables z_ConnectionBase._check_readablecCs|jstdndS)Nzconnection is read-only)r6r>)r7rrr_check_writables z_ConnectionBase._check_writablecCs/|jrd|_n |jtddS)NFzbad message length)r6r5closer>)r7rrr_bad_message_lengths   z#_ConnectionBase._bad_message_lengthcCs |jdkS)z True if the connection is closedN)r4)r7rrrclosedsz_ConnectionBase.closedcCs|jS)z"True if the connection is readable)r5)r7rrrr9sz_ConnectionBase.readablecCs|jS)z"True if the connection is writable)r6)r7rrrr:sz_ConnectionBase.writablecCs|j|jS)z+File descriptor or handle of the connection)r?r4)r7rrrfilenos z_ConnectionBase.filenoc Cs1|jdk r-z|jWdd|_XndS)zClose the connectionN)r4r<)r7rrrrBsz_ConnectionBase.closercCs|j|jt|}|jdkrDtt|}nt|}|dkrktdn||krtdn|dkr||}n:|dkrtdn|||krtdn|j||||dS)z,Send the bytes data from a bytes-like objectrrzoffset is negativezbuffer length < offsetNzsize is negativezbuffer length < offset + size)r?rA memoryviewitemsizebyteslenr$ _send_bytes)r7bufoffsetsizemnrrr send_bytess"         z_ConnectionBase.send_bytescCs.|j|j|jtj|dS)zSend a (picklable) objectN)r?rArJr dumps)r7objrrrsends  z_ConnectionBase.sendcCsm|j|j|dk r;|dkr;tdn|j|}|dkrc|jn|jS)z7 Receive bytes data as a bytes object. Nrznegative maxlength)r?r@r$ _recv_bytesrCgetvalue)r7Z maxlengthrKrrr recv_bytess    z_ConnectionBase.recv_bytesc Cs|j|jt|}|j}|t|}|dkrWtdn||krrtdn|j}|j}|||krt|j n|j d|j |||||||SWdQXdS)zq Receive bytes data into a writeable bytes-like object. Return the number of bytes read. rznegative offsetzoffset too largeN) r?r@rFrGrIr$rTtellr rUseekreadinto)r7rKrLrNrGZbytesizeresultrMrrrrecv_bytes_intos"        z_ConnectionBase.recv_bytes_intocCs3|j|j|j}tj|jS)zReceive a (picklable) object)r?r@rTr loads getbuffer)r7rKrrrrecvs   z_ConnectionBase.recvgcCs!|j|j|j|S)z/Whether there is any input available to be read)r?r@_poll)r7rrrrpolls  z_ConnectionBase.pollcCs|S)Nr)r7rrr __enter__sz_ConnectionBase.__enter__cCs|jdS)N)rB)r7exc_type exc_valueexc_tbrrr__exit__sz_ConnectionBase.__exit__)__name__ __module__ __qualname__r4r;r=r?r@rArCpropertyrDr9r:rErBrPrSrVr[r^r`rarerrrrr2rs(            r2c@saeZdZdZdZejddZddZddd Z d d Z d d Z dS)PipeConnectionz Connection class based on a Windows named pipe. Overlapped I/O is used, so the handles must have been created with FILE_FLAG_OVERLAPPED. FcCs||jdS)N)r4)r7Z _CloseHandlerrrr<szPipeConnection._closecCstj|j|dd\}}z_yC|tjkrftj|jgdt}|tksftnWn|j YnXWd|j d\}}X|dkst|t |kstdS)N overlappedTFr) _winapiZ WriteFiler4ERROR_IO_PENDINGWaitForMultipleObjectseventrr AssertionErrorcancelGetOverlappedResultrI)r7rKoverrwaitresZnwrittenrrrrJs!  zPipeConnection._send_bytesNc Cs~|jrd|_tjS|dkr.dn t|d}ytj|j|dd\}}z_yC|tjkrtj|j gdt }|t kst nWn|j YnXWd|jd\}}|dkrtj}|j|j|S|tjkr&|j||SXWnCtk rm}z#|jtjkrXtnWYdd}~XnXtddS)NFrkTrz.shouldn't get here; expected KeyboardInterrupt)_got_empty_messageioBytesIOminrlReadFiler4rmrnrorr rprqrrwriter]ZERROR_MORE_DATA_get_more_datar>winerrorERROR_BROKEN_PIPEEOFError RuntimeError) r7maxsizeZbsizersrtruZnreadferrrrT&s6   !     zPipeConnection._recv_bytescCs?|js%tj|jddkr)dStt|g|S)NrT)rwrl PeekNamedPiper4boolr)r7rrrrr_Fs zPipeConnection._pollcCs|j}tj}|j|tj|jd}|dksMt|dk r|t|||kr||j ntj |j|dd\}}|j d\}}|dkst||kst|j|j|S)NrrrkT) r]rxryr|rlrr4rprIrCr{rr)r7rsrrKrleftrtZrbytesrrrr}Ls   " !zPipeConnection._get_more_data) rfrgrh__doc__rwrl CloseHandler<rJrTr_r}rrrrrj s   rjc@seZdZdZer?ejddZejZ ej Z n$e j ddZe jZ e jZ e ddZe ddZd d Zd d d ZddZd S) Connectionzo Connection class based on an arbitrary file descriptor (Unix only), or a socket handle (Windows). cCs||jdS)N)r4)r7r<rrrr<cszConnection._closecCs||jdS)N)r4)r7r<rrrr<hsc Csot|}x\y||j|}Wntk r<wYnX||8}|dkrWPn||d}qWdS)Nr)rIr4InterruptedError)r7rKr| remainingrOrrr_sendms    zConnection._sendc Cstj}|j}|}x|dkry|||}Wntk rTwYnXt|}|dkr||krtqtdn|j|||8}qW|S)Nrzgot end of file during message)rxryr4rrIrr>r|)r7rMreadrKr8rchunkrOrrr_recvys         zConnection._recvcCst|}tjd|}|dkr9||g}n%|dkrU||g}n |g}x|D]}|j|qeWdS)Nz!ii@r)rIstructZpackr)r7rKrOheaderZchunksrrrrrJs     zConnection._send_bytesNcCsS|jd}tjd|j\}|dk rF||krFdS|j|S)Nz!i)rrZunpackrU)r7rrKrMrrrrTs zConnection._recv_bytescCst|g|}t|S)N)rr)r7rrrrrr_szConnection._poll)rfrgrhrrl_multiprocessingZ closesocketr<rSZ_writer^Z_readr rBr|rrrrJrTr_rrrrr\s       rc@seZdZdZddddddZddZdd Zed d Zed d Z d dZ ddZ dS)rz Returns a listener object. This is a wrapper for a bound socket which is 'listening' for connections, or for a Windows named pipe. NrcCs|p|rt|pt}|p-t|}t||dkr[t|||_nt||||_|dk rt|t rt dn||_ dS)Nrzauthkey should be a byte string) r1default_familyr&r+ PipeListener _listenerSocketListener isinstancerH TypeError_authkey)r7r0r%backlogauthkeyrrrr;s  zListener.__init__cCs]|jdkrtdn|jj}|jrYt||jt||jn|S)zz Accept a connection on the bound socket or named pipe of `self`. Returns a `Connection` object. Nzlistener is closed)rr>acceptrdeliver_challengeanswer_challenge)r7crrrrs zListener.acceptcCs,|jdk r(|jjd|_ndS)zA Close the bound socket or named pipe of `self`. N)rrB)r7rrrrBs zListener.closecCs |jjS)N)r_address)r7rrrszListener.cCs |jjS)N)r_last_accepted)r7rrrrscCs|S)Nr)r7rrrraszListener.__enter__cCs|jdS)N)rB)r7rbrcrdrrrreszListener.__exit__) rfrgrhrr;rrBrir0Z last_acceptedrarerrrrrs    cCs|pt|}t||dkr7t|}n t|}|dk rnt|t rntdn|dk rt||t||n|S)z= Returns a connection to the address of a `Listener` rNzauthkey should be a byte string) r1r+ PipeClient SocketClientrrHrrr)r0r%rrrrrrs     TcCs|rYtj\}}|jd|jdt|j}t|j}n6tj\}}t|dd}t|dd}||fS)zL Returns pair of connection objects at either end of a pipe Tr:Fr9)r*Z socketpair setblockingrdetachr pipe)duplexs1s2c1c2Zfd1Zfd2rrrrs  c CsStd}|r;tj}tjtjB}tt}}ntj}tj}dt}}tj||tjBtj Btj tj Btj Bd||tj tj}tj||dtjtjtjtj}tj|tj ddtj|dd}|jd\} } | dks%tt|d|} t|d|} | | fS) zL Returns pair of connection objects at either end of a pipe rrrNrkTr:r9)r&rlPIPE_ACCESS_DUPLEX GENERIC_READ GENERIC_WRITEBUFSIZEZPIPE_ACCESS_INBOUNDCreateNamedPipeFILE_FLAG_OVERLAPPEDFILE_FLAG_FIRST_PIPE_INSTANCEPIPE_TYPE_MESSAGEPIPE_READMODE_MESSAGE PIPE_WAITNMPWAIT_WAIT_FOREVERNULL CreateFile OPEN_EXISTINGSetNamedPipeHandleStateConnectNamedPiperrrprj) rr0ZopenmodeaccessZobsizeZibsizeZh1Zh2rk_rtrrrrrrs4        c@s=eZdZdZdddZddZddZd S) rzO Representation of a socket which is bound to an address and listening rcCs tjtt||_yttjdkrL|jjtjtjdn|jjd|jj ||jj ||jj |_ Wn"t k r|jjYnX||_d|_|dkrtj|tjd|fdd|_n d|_dS)NposixrTrargs exitpriorityr)r*getattr_socketr nameZ setsockoptZ SOL_SOCKETZ SO_REUSEADDRrZbindZlistenZ getsocknamerr>rBZ_familyrrFinalizeunlink_unlink)r7r0r%rrrrr;@s$     $zSocketListener.__init__c CsVx6y|jj\}|_Wntk r3YqXPqW|jdt|jS)NT)rrrrrrr)r7srrrrXs  zSocketListener.acceptcCs-|jj|jdk r)|jndS)N)rrBr)r7rrrrBcs zSocketListener.closeN)rfrgrhrr;rrBrrrrr<s  rc Cs[t|}tjtt|1}|jd|j|t|jSWdQXdS)zO Return a connection object connected to the socket given by `address` TN)r1r*rrZconnectrr)r0r%rrrrris    rc@sReZdZdZdddZdddZdd Zed d ZdS) rz0 Representation of a named pipe NcCsq||_|jddg|_d|_tjd|jtj|tjd|j|jfdd|_ dS)NfirstTz listener created with address=%rrrr) r _new_handle _handle_queuerr sub_debugrr_finalize_pipe_listenerrB)r7r0rrrrr;}s   zPipeListener.__init__Fc CsetjtjB}|r&|tjO}ntj|j|tjtjBtjBtj t t tj tj S)N) rlrrrrrrrrZPIPE_UNLIMITED_INSTANCESrrr)r7rflagsrrrrs   zPipeListener._new_handlecCs|jj|j|jjd}ytj|dd}Wn=tk r}z|jtjkrnnWYdd}~XntXzHytj |j gdt }Wn"|j tj |YnXWd|jd\}}|dkstXt|S)NrrkTF)rappendrpoprlrr>r~Z ERROR_NO_DATArnrorrqrrrrprj)r7r8rsrresrrtrrrrs"   zPipeListener.acceptcCs2tjd|x|D]}tj|qWdS)Nz closing listener with address=%r)rrrlr)Zqueuer0r8rrrrs z$PipeListener._finalize_pipe_listener) rfrgrhrr;rr staticmethodrrrrrrys   rcCst}xyKtj|dtj|tjtjBdtjtjtjtj}WnRt k r}z2|j tj tj fkst |rnWYdd}~Xq XPq Wtj|tjddt|S)zU Return a connection object connected to the pipe given by `address` irN)rrlZ WaitNamedPiperrrrrrr>r~ZERROR_SEM_TIMEOUTZERROR_PIPE_BUSYrrrrj)r0rhrrrrrs"  rs #CHALLENGE#s #WELCOME#s #FAILURE#cCsddl}t|ts!ttjt}|jt||j ||dj }|j d}||kr|jt n|jt tddS)Nrmd5zdigest received was wrong)hmacrrHrpr urandomMESSAGE_LENGTHrP CHALLENGEnewdigestrVWELCOMEFAILUREr ) connectionrrmessagerresponserrrrs   rcCsddl}t|ts!t|jd}|dtttks\td||ttd}|j||dj}|j ||jd}|t krt dndS)Nrrz message = %rrzdigest sent was rejected) rrrHrprVrIrrrrPrr )rrrrrrrrrrs ,  rc@s4eZdZddZddZddZdS)ConnectionWrappercCsO||_||_||_x-dD]%}t||}t|||q"WdS)NrErBr`rVrP)zfilenozclosezpollz recv_bytesz send_bytes)_conn_dumps_loadsrsetattr)r7connrQr\attrrRrrrr;s     zConnectionWrapper.__init__cCs#|j|}|jj|dS)N)rrrP)r7rRrrrrrSszConnectionWrapper.sendcCs|jj}|j|S)N)rrVr)r7rrrrr^szConnectionWrapper.recvN)rfrgrhr;rSr^rrrrrs   rcCs%tj|fddddjdS)Nrzutf-8) xmlrpclibrQencode)rRrrr _xml_dumpssrcCs%tj|jd\\}}|S)Nzutf-8)rr\decode)rrRmethodrrr _xml_loadss!rc@seZdZddZdS) XmlListenercCs.ddljatj|}t|ttS)Nr) xmlrpc.clientclientrrrrrr)r7rRrrrr szXmlListener.acceptN)rfrgrhrrrrrr s rcOs(ddljatt||ttS)Nr)rrrrrrr)rkwdsrrr XmlClientsrcCst|}g}x|rtj|d|}|tkr@Pnrt|koatt|knrs|t8}n?t|kott|knr|t8}n td|j||||dd}d}qW|S)NFzShould not get hererr) listrlrnrr rIr rr)ZhandlesrLreadyrrrr_exhaustive_waits   & &   rc s|dkrt}n)|dkr*d}nt|dd}t|}ig}tt}zx|D]}yt|d}Wn"tk r||jsszwait..cs"g|]}|kr|qSrr)ro) ready_objectsrr ts zwait..)rintrsetrAttributeErrorr3rlr{r>r~ _ready_errorsrmrroaddrkeysrqrrZERROR_OPERATION_ABORTEDr)rwupdate) object_listrZov_listZ ready_handlesrrErsrtrrr)rrrr1sZ         "              PollSelectorc Cst}x!|D]}|j|tjqW|dk rOtj|}nxY|j|}|rxdd|DS|dk rR|tj}|dkr|SqRqRWWdQXdS)z Wait till an object in object_list is ready/readable. Returns list of those objects in object_list which are ready/readable. NcSsg|]\}}|jqSr)Zfileobj)rkeyZeventsrrrrs zwait..r) _WaitSelectorregister selectorsZ EVENT_READrZselect)rrZselectorrRZdeadlinerrrrrs     c Csl|j}tj|tjtj?}ddlm}|j|}t||j |j ffSWdQXdS)Nr)resource_sharer) rEr*ZfromfdrZ SOCK_STREAMrrZ DupSocketrebuild_connectionr9r:)rr8rrdsrrrreduce_connections  rcCs"|j}t|j||S)N)rr)rr9r:Zsockrrrrs rcCs_|jrtjnd|jr'tjndB}tj|j|}t||j|jffS)Nr) r9rlZFILE_GENERIC_READr:ZFILE_GENERIC_WRITErZ DupHandlerErebuild_pipe_connection)rrdhrrrreduce_pipe_connectionsrcCs|j}t|||S)N)rrj)rr9r:r8rrrrs rcCs.tj|j}t||j|jffS)N)rZDupFdrErr9r:)rdfrrrrscCs|j}t|||S)N)rr)rr9r:fdrrrrs )G__all__rxr r'r*rrr itertoolsrrrrr r r rlr r rr ImportErrorr(rZCONNECTION_TIMEOUTcountr#rZfamiliesr)rrr&r+r1r2rjrobjectrrrrrrrrrrrrrrrrrrrrZERROR_NETNAME_DELETEDrrr r r ZSelectSelectorrrr rrrrrr s          &       PS7,- 8     G