3 \l@sdZddlZddlZddlZddlZddlZddlZddlmZddlm Z ddlm Z ddlm Z ddlm Z dd lm Z dd lmZdd lmZdd lmZdd lmZddddgZdZdZdZdZdZdZGddde jZGddde jZGdddeZGdddeZGdd d e Z!Gd!d"d"e j"Z#Gd#dde j$Z%Gd$ddZ&Gd%d&d&e j'Z(e#Z)Gd'd(d(ej*Z+e+Z,dS))z.Selector and proactor event loops for Windows.N)events)base_subprocess)futures)proactor_events)selector_events)tasks) windows_utils) _overlapped) coroutine)loggerSelectorEventLoopProactorEventLoop IocpProactorDefaultEventLoopPolicyliigMbP?g?cs^eZdZdZddfdd ZfddZdd Zfd d Zfd d ZfddZ Z S)_OverlappedFuturezSubclass of Future which represents an overlapped operation. Cancelling it will immediately cancel the overlapped operation. N)loopcs&tj|d|jr|jd=||_dS)N)rr)super__init___source_traceback_ov)selfovr) __class__&/usr/lib64/python3.6/windows_events.pyr-sz_OverlappedFuture.__init__cs@tj}|jdk r<|jjr dnd}|jdd||jjf|S)NpendingZ completedrzoverlapped=<%s, %#x>)r _repr_inforrinsertaddress)rinfostate)rrrr3s   z_OverlappedFuture._repr_infocCsr|jdkrdSy|jjWnJtk rf}z.d||d}|jrJ|j|d<|jj|WYdd}~XnXd|_dS)Nz&Cancelling an overlapped future failed)message exceptionfuturesource_traceback)rcancelOSErrorr_loopcall_exception_handler)rexccontextrrr_cancel_overlapped:s  z$_OverlappedFuture._cancel_overlappedcs|jtjS)N)r-rr')r)rrrr'Jsz_OverlappedFuture.cancelcstj||jdS)N)r set_exceptionr-)rr$)rrrr.Ns z_OverlappedFuture.set_exceptioncstj|d|_dS)N)r set_resultr)rresult)rrrr/Rs z_OverlappedFuture.set_result) __name__ __module__ __qualname____doc__rrr-r'r.r/ __classcell__rr)rrr's   rcsneZdZdZddfdd ZddZfdd Zd d Zd d ZfddZ fddZ fddZ Z S)_BaseWaitHandleFuturez2Subclass of Future which represents a wait handle.N)rcs8tj|d|jr|jd=||_||_||_d|_dS)N)rrTr)rrrr_handle _wait_handle _registered)rrhandle wait_handler)rrrrZsz_BaseWaitHandleFuture.__init__cCstj|jdtjkS)Nr)_winapiZWaitForSingleObjectr7Z WAIT_OBJECT_0)rrrr_pollhs z_BaseWaitHandleFuture._pollcs\tj}|jd|j|jdk r>|jr0dnd}|j||jdk rX|jd|j|S)Nz handle=%#xZsignaledZwaitingzwait_handle=%#x)rrappendr7r=r8)rr!r")rrrrms    z _BaseWaitHandleFuture._repr_infocCs d|_dS)N)r)rfutrrr_unregister_wait_cbwsz)_BaseWaitHandleFuture._unregister_wait_cbcCs|js dSd|_|j}d|_ytj|WnZtk r}z>|jtjkrtd||d}|jrd|j|d<|jj |dSWYdd}~XnX|j ddS)NFz$Failed to unregister the wait handle)r#r$r%r&) r9r8r ZUnregisterWaitr(winerrorERROR_IO_PENDINGrr)r*r@)rr;r+r,rrr_unregister_wait|s"   z&_BaseWaitHandleFuture._unregister_waitcs|jtjS)N)rCrr')r)rrrr'sz_BaseWaitHandleFuture.cancelcs|jtj|dS)N)rCrr.)rr$)rrrr.sz#_BaseWaitHandleFuture.set_exceptioncs|jtj|dS)N)rCrr/)rr0)rrrr/sz _BaseWaitHandleFuture.set_result) r1r2r3r4rr=rr@rCr'r.r/r5rr)rrr6Ws   r6csFeZdZdZddfdd ZddZfdd Zfd d ZZS) _WaitCancelFuturezoSubclass of Future which represents a wait for the cancellation of a _WaitHandleFuture using an event. N)rcstj||||dd|_dS)N)r)rr_done_callback)rreventr;r)rrrrsz_WaitCancelFuture.__init__cCs tddS)Nz'_WaitCancelFuture must not be cancelled) RuntimeError)rrrrr'sz_WaitCancelFuture.cancelcs$tj||jdk r |j|dS)N)rr/rE)rr0)rrrr/s  z_WaitCancelFuture.set_resultcs$tj||jdk r |j|dS)N)rr.rE)rr$)rrrr.s  z_WaitCancelFuture.set_exception) r1r2r3r4rr'r/r.r5rr)rrrDs  rDcs6eZdZddfdd ZfddZddZZS) _WaitHandleFutureN)rcs<tj||||d||_d|_tjdddd|_d|_dS)N)rTF)rr _proactorZ_unregister_proactorr Z CreateEvent_event _event_fut)rrr:r;proactorr)rrrrs z_WaitHandleFuture.__init__csF|jdk r"tj|jd|_d|_|jj|jd|_tj|dS)N) rJr< CloseHandlerKrI _unregisterrrr@)rr?)rrrr@s   z%_WaitHandleFuture._unregister_wait_cbcCs|js dSd|_|j}d|_ytj||jWnZtk r}z>|jtjkrxd||d}|jrh|j|d<|j j |dSWYdd}~XnX|j j |j|j |_dS)NFz$Failed to unregister the wait handle)r#r$r%r&)r9r8r ZUnregisterWaitExrJr(rArBrr)r*rI _wait_cancelr@rK)rr;r+r,rrrrCs$    z"_WaitHandleFuture._unregister_wait)r1r2r3rr@rCr5rr)rrrHs rHc@s<eZdZdZddZddZddZdd Zd d ZeZ d S) PipeServerzXClass representing a pipe server. This is much like a bound, listening socket. cCs,||_tj|_d|_d|_|jd|_dS)NT)_addressweakrefWeakSet_free_instances_pipe_accept_pipe_future_server_pipe_handle)rr rrrrs  zPipeServer.__init__cCs|j|jd}|_|S)NF)rUrW)rtmprrr_get_unconnected_pipesz PipeServer._get_unconnected_pipec Csr|jr dStjtjB}|r&|tjO}tj|j|tjtjBtj Btj t j t j tj tj}t j|}|jj||S)N)closedr<ZPIPE_ACCESS_DUPLEXZFILE_FLAG_OVERLAPPEDZFILE_FLAG_FIRST_PIPE_INSTANCEZCreateNamedPiperQZPIPE_TYPE_MESSAGEZPIPE_READMODE_MESSAGEZ PIPE_WAITZPIPE_UNLIMITED_INSTANCESr ZBUFSIZEZNMPWAIT_WAIT_FOREVERNULL PipeHandlerTadd)rfirstflagshpiperrrrWs      zPipeServer._server_pipe_handlecCs |jdkS)N)rQ)rrrrrZszPipeServer.closedcCsV|jdk r|jjd|_|jdk rRx|jD] }|jq,Wd|_d|_|jjdS)N)rVr'rQrTcloserUclear)rrarrrrbs     zPipeServer.closeN) r1r2r3r4rrYrWrZrb__del__rrrrrPs  rPc@seZdZdZddZdS)_WindowsSelectorEventLoopz'Windows version of selector event loop.cCstjS)N)r socketpair)rrrr _socketpair+sz%_WindowsSelectorEventLoop._socketpairN)r1r2r3r4rgrrrrre(srecsPeZdZdZd fdd ZddZeddZed d Zedd d Z Z S)rz2Windows version of proactor event loop using IOCP.Ncs|dkrt}tj|dS)N)rrr)rrL)rrrr2szProactorEventLoop.__init__cCstjS)N)r rf)rrrrrg7szProactorEventLoop._socketpairccs8|jj|}|EdH}|}|j||d|id}||fS)Naddr)extra)rI connect_pipe_make_duplex_pipe_transport)rprotocol_factoryr fraprotocoltransrrrcreate_pipe_connection:s    z(ProactorEventLoop.create_pipe_connectioncs.tdfdd jgS)Ncsd}yj|rL|j}jj|jr2|jdS}j||didj}|dkr`dSjj|}Wnt k r}zH|r|j d krj d||d|jnj rt jd|ddWYdd}~Xn2tjk r|r|jYnX|_|jdS) Nrh)rirzPipe accept failed)r#r$razAccept pipe failed on pipe %rT)exc_infor)r0rTdiscardrZrbrkrYrI accept_piper(filenor*Z_debugr ZwarningrCancelledErrorrVadd_done_callback)rmrarnr+)r loop_accept_piperlrserverrrrwGs<   z>ProactorEventLoop.start_serving_pipe..loop_accept_pipe)N)rPZ call_soon)rrlr r)r rwrlrrxrstart_serving_pipeCs( z$ProactorEventLoop.start_serving_pipec ks|j} t||||||||f| |d| } y| EdHWn&tk r`} z | } WYdd} ~ XnXd} | dk r| j| jEdH| | S)N)waiterri) create_future_WindowsSubprocessTransport ExceptionrbZ_wait)rrnargsshellstdinstdoutstderrbufsizerikwargsrzZtranspr+errrrr_make_subprocess_transportrs  z,ProactorEventLoop._make_subprocess_transport)N)N) r1r2r3r4rrgr rpryrr5rr)rrr/s /c@seZdZdZd1ddZddZddZd2d d Zd d Zd3ddZ d4ddZ ddZ ddZ ddZ eddZd5ddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd6d)d*Zd+d,Zd-d.Zd/d0Zd S)7rz#Proactor implementation using IOCP.cCsDd|_g|_tjtjtd||_i|_tj |_ g|_ tj |_ dS)Nr) r)_resultsr CreateIoCompletionPortINVALID_HANDLE_VALUEr[_iocp_cacherRrSr9 _unregistered_stopped_serving)rZ concurrencyrrrrs zIocpProactor.__init__cCsd|jjt|jt|jfS)Nz<%s overlapped#=%s result#=%s>)rr1lenrr)rrrr__repr__szIocpProactor.__repr__cCs ||_dS)N)r))rrrrrset_loopszIocpProactor.set_loopNcCs |js|j||j}g|_|S)N)rr=)rtimeoutrXrrrselects  zIocpProactor.selectcCs|jj}|j||S)N)r)r{r/)rvaluer?rrr_results  zIocpProactor._resultrc Csz|j|tjt}y4t|tjr6|j|j||n|j|j|Wnt k rb|j dSXdd}|j |||S)NcSsJy|jStk rD}z |jtjkr2t|jnWYdd}~XnXdS)N) getresultr(rAr ERROR_NETNAME_DELETEDConnectionResetErrorr~)rokeyrr+rrr finish_recvs   z&IocpProactor.recv..finish_recv) _register_with_iocpr Overlappedr[ isinstancesocketZWSARecvrtZReadFileBrokenPipeErrorr _register)rconnnbytesr_rrrrrrecvs     zIocpProactor.recvcCsZ|j|tjt}t|tjr4|j|j||n|j|j|dd}|j |||S)NcSsJy|jStk rD}z |jtjkr2t|jnWYdd}~XnXdS)N)rr(rAr rrr~)rorrr+rrr finish_sends   z&IocpProactor.send..finish_send) rr rr[rrZWSASendrtZ WriteFiler)rrbufr_rrrrrsends    zIocpProactor.sendcsz|j|jjtjt}|jjjfdd}tdd}|j ||}||}t j ||j d|S)NcsD|jtjdj}jtjtj|j j j fS)Nz@P) rstructpackrt setsockoptr SOL_SOCKETr ZSO_UPDATE_ACCEPT_CONTEXT settimeoutZ gettimeoutZ getpeername)rorrr)rlistenerrr finish_accepts  z*IocpProactor.accept..finish_acceptc ss4y|EdHWn tjk r.|jYnXdS)N)rrurb)r%rrrr accept_coros z(IocpProactor.accept..accept_coro)r) r_get_accept_socketfamilyr rr[ZAcceptExrtr rrZ ensure_futurer))rrrrrr%coror)rrraccepts     zIocpProactor.acceptcs|jytjjjWnBtk rb}z&|jtjkr@j ddkrRWYdd}~XnXtj t }|j j|fdd}|j ||S)Nrrcs|jjtjtjdS)Nr)rrrrr ZSO_UPDATE_CONNECT_CONTEXT)rorr)rrrfinish_connects z,IocpProactor.connect..finish_connect)rr Z BindLocalrtrr(rAerrnoZ WSAEINVALZ getsocknamerr[Z ConnectExr)rrr errr)rrconnects    zIocpProactor.connectcsJ|jtjt}|jj}|r0|jSfdd}|j||S)Ncs |jS)N)r)rorr)rarrfinish_accept_pipesz4IocpProactor.accept_pipe..finish_accept_pipe)rr rr[ZConnectNamedPipertrr)rrarZ connectedrr)rarrs s    zIocpProactor.accept_pipeccszt}xjytj|}PWn0tk rF}z|jtjkr6WYdd}~XnXt|dt}tj ||j dEdHqWt j |S)N)r) CONNECT_PIPE_INIT_DELAYr Z ConnectPiper(rAZERROR_PIPE_BUSYminCONNECT_PIPE_MAX_DELAYrZsleepr)r r\)rr Zdelayr:r+rrrrjs  zIocpProactor.connect_pipecCs|j||dS)zWait for a handle. Return a Future object. The result of the future is True if the wait completed, or False if the wait did not complete (on timeout). F)_wait_for_handle)rr:rrrrwait_for_handle/szIocpProactor.wait_for_handlecCs|j|dd}||_|S)NT)rrE)rrFZ done_callbackr?rrrrO7szIocpProactor._wait_cancelcs|dkrtj}ntj|d}tjt}tj||j|j |}|rTt ||||j dnt |||||j dj rvj d=fdd}|d|f|j|j <S)Ng@@)rrcsjS)N)r=)rorr)rmrrfinish_wait_for_handleRsz=IocpProactor._wait_for_handle..finish_wait_for_handlerr)r<INFINITEmathceilr rr[ZRegisterWaitWithQueuerr rDr)rHrr)rr:rZ _is_cancelmsrr;rr)rmrr>s    zIocpProactor._wait_for_handlecCs0||jkr,|jj|tj|j|jdddS)Nr)r9r]r rrtr)robjrrrr^s  z IocpProactor._register_with_iocpcCst||jd}|jr|jd=|jsjy|dd|}Wn,tk r^}z|j|WYdd}~Xn X|j|||||f|j|j<|S)N)rrr) rr)rrr(r.r/rr )rrrcallbackrmrrrrrrhs zIocpProactor._registercCs|jj|dS)a Unregister an overlapped object. Call this method when its future has been cancelled. The event can already be signalled (pending in the proactor event queue). It is also safe if the event is never signalled (because it was cancelled). N)rr>)rrrrrrNszIocpProactor._unregistercCstj|}|jd|S)Nr)rr)rrsrrrrs  zIocpProactor._get_accept_socketcCs|dkrt}n0|dkr tdntj|d}|tkr>tdxtj|j|}|dkrZPd}|\}}}}y|jj|\}} } } WnVt k r|j j r|j j dd||||fd|dtj fkrtj|wBYnX| |jkr|jqB|jsBy| ||| } Wn:tk r@} z|j| |jj|WYdd} ~ XqBX|j| |jj|qBWx |jD]} |jj| jdqdW|jjdS)Nrznegative timeoutg@@ztimeout too bigz8GetQueuedCompletionStatus() returned an unexpected eventz)err=%s transferred=%s key=%#x address=%#x)r#status)r ValueErrorrrr ZGetQueuedCompletionStatusrrpopKeyErrorr)Z get_debugr*rr<rMrr'doner(r.rr>r/rr rc)rrrrrZ transferredrr rmrrrrrrrrr=sJ         zIocpProactor._pollcCs|jj|dS)N)rr])rrrrr _stop_servingszIocpProactor._stop_servingcCsxt|jjD]\}\}}}}|jr*qt|tr6qy |jWqtk r}z8|jdk rd||d}|j rz|j |d<|jj |WYdd}~XqXqWx|jr|j dst j dqWg|_|jdk rtj|jd|_dS)NzCancelling a future failed)r#r$r%r&rz"taking long time to close proactor)listritemsZ cancelledrrDr'r(r)rr*r=r debugrrr<rM)rr r?rrrr+r,rrrrbs,     "   zIocpProactor.closecCs |jdS)N)rb)rrrrrdszIocpProactor.__del__)r)N)r)r)N)N)r1r2r3r4rrrrrrrrrrsr rjrrOrrrrNrr=rrbrdrrrrrs.          7 c@seZdZddZdS)r|c  sPtj|f|||||d|_fdd}jjjtjj} | j|dS)N)rrrrrcsjj}j|dS)N)_procZpollZ_process_exited)rm returncode)rrrrs z4_WindowsSubprocessTransport._start..callback) r Popenrr)rIrintr7rv) rr~rrrrrrrrmr)rr_starts   z"_WindowsSubprocessTransport._startN)r1r2r3rrrrrr|sr|c@seZdZeZdS)_WindowsDefaultEventLoopPolicyN)r1r2r3r Z _loop_factoryrrrrrsr)-r4r<rrrrrRrrrrrrr r Z coroutinesr logr __all__r[rZERROR_CONNECTION_REFUSEDZERROR_CONNECTION_ABORTEDrrZFuturerr6rDrHobjectrPZBaseSelectorEventLoopreZBaseProactorEventLooprrZBaseSubprocessTransportr|r ZBaseDefaultEventLoopPolicyrrrrrrsL          0J4;]k