B €/g^vã@sdZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl m Z ddl m Z ddl m Z ddl mZddl mZdd l mZdd l mZdd lmZd ZdZd ZdZdZdZdZGdd„de jƒZGdd„de jƒZGdd„deƒZGdd„deƒZGdd„de ƒZ!Gdd„dej"ƒZ#Gdd„dej$ƒZ%Gd d!„d!ƒZ&Gd"d#„d#e j'ƒZ(e#Z)Gd$d%„d%e j*ƒZ+Gd&d'„d'e j*ƒZ,e+Z-dS)(z.Selector and proactor event loops for Windows.éNé)Úevents)Úbase_subprocess)Úfutures)Úproactor_events)Úselector_events)Útasks)Ú windows_utils)Úlogger)ÚSelectorEventLoopÚProactorEventLoopÚ IocpProactorÚDefaultEventLoopPolicyÚWindowsSelectorEventLoopPolicyÚWindowsProactorEventLoopPolicylÿÿiÉiÔgü©ñÒMbP?gš™™™™™¹?cs^eZdZdZddœ‡fdd„ Z‡fdd„Zdd „Z‡fd d „Z‡fd d „Z‡fdd„Z ‡Z S)Ú_OverlappedFuturez†Subclass of Future which represents an overlapped operation. Cancelling it will immediately cancel the overlapped operation. N)Úloopcs&tƒj|d|jr|jd=||_dS)N)réÿÿÿÿ)ÚsuperÚ__init__Ú_source_tracebackÚ_ov)ÚselfÚovr)Ú __class__©ú./usr/lib64/python3.7/asyncio/windows_events.pyr0sz_OverlappedFuture.__init__csHtƒ ¡}|jdk rD|jjr dnd}| dd|›d|jjd›d¡|S)NÚpendingZ completedrz overlapped=)rÚ _repr_inforrÚinsertÚaddress)rÚinfoÚstate)rrrr6s    z_OverlappedFuture._repr_infoc Csr|jdkrdSy|j ¡WnJtk rf}z,d||dœ}|jrJ|j|d<|j |¡Wdd}~XYnXd|_dS)Nz&Cancelling an overlapped future failed)ÚmessageÚ exceptionÚfutureÚsource_traceback)rÚcancelÚOSErrorrÚ_loopÚcall_exception_handler)rÚexcÚcontextrrrÚ_cancel_overlapped=s  z$_OverlappedFuture._cancel_overlappedcs| ¡tƒ ¡S)N)r.rr()r)rrrr(Msz_OverlappedFuture.cancelcstƒ |¡| ¡dS)N)rÚ set_exceptionr.)rr%)rrrr/Qs z_OverlappedFuture.set_exceptioncstƒ |¡d|_dS)N)rÚ set_resultr)rÚresult)rrrr0Us z_OverlappedFuture.set_result) Ú__name__Ú __module__Ú __qualname__Ú__doc__rrr.r(r/r0Ú __classcell__rr)rrr*s   rcsneZdZdZddœ‡fdd„ Zdd„Z‡fdd „Zd d „Zd d „Z‡fdd„Z ‡fdd„Z ‡fdd„Z ‡Z S)Ú_BaseWaitHandleFuturez2Subclass of Future which represents a wait handle.N)rcs8tƒj|d|jr|jd=||_||_||_d|_dS)N)rrT)rrrrÚ_handleÚ _wait_handleÚ _registered)rrÚhandleÚ wait_handler)rrrr]sz_BaseWaitHandleFuture.__init__cCst |jd¡tjkS)Nr)Ú_winapiZWaitForSingleObjectr8Z WAIT_OBJECT_0)rrrrÚ_pollks z_BaseWaitHandleFuture._pollcsdtƒ ¡}| d|jd›¡|jdk rB| ¡r4dnd}| |¡|jdk r`| d|jd›¡|S)Nzhandle=z#xZsignaledZwaitingz wait_handle=)rrÚappendr8r>r9)rr"r#)rrrrps    z _BaseWaitHandleFuture._repr_infocCs d|_dS)N)r)rÚfutrrrÚ_unregister_wait_cbzsz)_BaseWaitHandleFuture._unregister_wait_cbc Cs”|js dSd|_|j}d|_yt |¡WnZtk r„}z<|jtjkrtd||dœ}|jrd|j|d<|j  |¡dSWdd}~XYnX|  d¡dS)NFz$Failed to unregister the wait handle)r$r%r&r') r:r9Ú _overlappedZUnregisterWaitr)ÚwinerrorÚERROR_IO_PENDINGrr*r+rA)rr<r,r-rrrÚ_unregister_waits"   z&_BaseWaitHandleFuture._unregister_waitcs| ¡tƒ ¡S)N)rErr()r)rrrr(—sz_BaseWaitHandleFuture.cancelcs| ¡tƒ |¡dS)N)rErr/)rr%)rrrr/›sz#_BaseWaitHandleFuture.set_exceptioncs| ¡tƒ |¡dS)N)rErr0)rr1)rrrr0Ÿsz _BaseWaitHandleFuture.set_result) r2r3r4r5rr>rrArEr(r/r0r6rr)rrr7Zs   r7csFeZdZdZddœ‡fdd„ Zdd„Z‡fdd „Z‡fd d „Z‡ZS) Ú_WaitCancelFuturezoSubclass of Future which represents a wait for the cancellation of a _WaitHandleFuture using an event. N)rcstƒj||||dd|_dS)N)r)rrÚ_done_callback)rrÚeventr<r)rrrr©sz_WaitCancelFuture.__init__cCs tdƒ‚dS)Nz'_WaitCancelFuture must not be cancelled)Ú RuntimeError)rrrrr(®sz_WaitCancelFuture.cancelcs$tƒ |¡|jdk r | |¡dS)N)rr0rG)rr1)rrrr0±s  z_WaitCancelFuture.set_resultcs$tƒ |¡|jdk r | |¡dS)N)rr/rG)rr%)rrrr/¶s  z_WaitCancelFuture.set_exception) r2r3r4r5rr(r0r/r6rr)rrrF¤s  rFcs6eZdZddœ‡fdd„ Z‡fdd„Zdd„Z‡ZS) Ú_WaitHandleFutureN)rcs<tƒj||||d||_d|_t dddd¡|_d|_dS)N)rTF)rrÚ _proactorZ_unregister_proactorrBZ CreateEventÚ_eventÚ _event_fut)rrr;r<Úproactorr)rrrr½s z_WaitHandleFuture.__init__csF|jdk r"t |j¡d|_d|_|j |j¡d|_tƒ |¡dS)N) rLr=Ú CloseHandlerMrKÚ _unregisterrrrA)rr@)rrrrAÄs   z%_WaitHandleFuture._unregister_wait_cbc Cs¢|js dSd|_|j}d|_yt ||j¡WnZtk rˆ}z<|jtjkrxd||dœ}|jrh|j|d<|j   |¡dSWdd}~XYnX|j   |j|j ¡|_dS)NFz$Failed to unregister the wait handle)r$r%r&r')r:r9rBZUnregisterWaitExrLr)rCrDrr*r+rKÚ _wait_cancelrArM)rr<r,r-rrrrEÖs$    z"_WaitHandleFuture._unregister_wait)r2r3r4rrArEr6rr)rrrJ¼s rJc@s<eZdZdZdd„Zdd„Zdd„Zdd „Zd d „ZeZ d S) Ú PipeServerzXClass representing a pipe server. This is much like a bound, listening socket. cCs,||_t ¡|_d|_d|_| d¡|_dS)NT)Ú_addressÚweakrefÚWeakSetÚ_free_instancesÚ_pipeÚ_accept_pipe_futureÚ_server_pipe_handle)rr!rrrrõs  zPipeServer.__init__cCs|j| d¡}|_|S)NF)rWrY)rÚtmprrrÚ_get_unconnected_pipeÿsz PipeServer._get_unconnected_pipec Csr| ¡r dStjtjB}|r&|tjO}t |j|tjtjBtj Btj t j t j tj tj¡}t  |¡}|j |¡|S)N)Úclosedr=ZPIPE_ACCESS_DUPLEXZFILE_FLAG_OVERLAPPEDZFILE_FLAG_FIRST_PIPE_INSTANCEZCreateNamedPiperSZPIPE_TYPE_MESSAGEZPIPE_READMODE_MESSAGEZ PIPE_WAITZPIPE_UNLIMITED_INSTANCESr ZBUFSIZEZNMPWAIT_WAIT_FOREVERÚNULLÚ PipeHandlerVÚadd)rÚfirstÚflagsÚhÚpiperrrrYs      zPipeServer._server_pipe_handlecCs |jdkS)N)rS)rrrrr\szPipeServer.closedcCsV|jdk r|j ¡d|_|jdk rRx|jD] }| ¡q,Wd|_d|_|j ¡dS)N)rXr(rSrVÚcloserWÚclear)rrcrrrrds     zPipeServer.closeN) r2r3r4r5rr[rYr\rdÚ__del__rrrrrRðs  rRc@seZdZdZdS)Ú_WindowsSelectorEventLoopz'Windows version of selector event loop.N)r2r3r4r5rrrrrg+srgcs<eZdZdZd ‡fdd„ Zdd„Zdd„Zd d d „Z‡ZS) r z2Windows version of proactor event loop using IOCP.Ncs|dkrtƒ}tƒ |¡dS)N)r rr)rrN)rrrr2szProactorEventLoop.__init__cÃs8|j |¡}|IdH}|ƒ}|j||d|id}||fS)NÚaddr)Úextra)rKÚ connect_pipeÚ_make_duplex_pipe_transport)rÚprotocol_factoryr!ÚfrcÚprotocolÚtransrrrÚcreate_pipe_connection7s    z(ProactorEventLoop.create_pipe_connectioncƒs.tˆƒ‰d‡‡‡‡‡fdd„ ‰ˆ ˆ¡ˆgS)Nc sd}yj|rL| ¡}ˆj |¡ˆ ¡r2| ¡dSˆƒ}ˆj||dˆidˆ ¡}|dkr`dSˆj |¡}Wn„t k rÒ}zF|r¬|  ¡dkr¬ˆ  d||dœ¡| ¡nˆj rÂt jd|ddWdd}~XYn2tjk rò|rî| ¡YnX|ˆ_| ˆ¡dS) Nrh)rirzPipe accept failed)r$r%rczAccept pipe failed on pipe %rT)Úexc_info)r1rVÚdiscardr\rdrkr[rKÚ accept_piper)Úfilenor+Z_debugr ZwarningrÚCancelledErrorrXÚadd_done_callback)rmrcrnr,)r!Úloop_accept_piperlrÚserverrrrwBs<   z>ProactorEventLoop.start_serving_pipe..loop_accept_pipe)N)rRZ call_soon)rrlr!r)r!rwrlrrxrÚstart_serving_pipe?s( z$ProactorEventLoop.start_serving_pipec Ësl| ¡} t||||||||f| |dœ| —Ž} y| IdHWn,tk rf|  ¡|  ¡IdH‚YnX| S)N)Úwaiterri)Ú create_futureÚ_WindowsSubprocessTransportÚ ExceptionrdZ_wait) rrnÚargsÚshellÚstdinÚstdoutÚstderrÚbufsizeriÚkwargsrzZtransprrrÚ_make_subprocess_transportms  z,ProactorEventLoop._make_subprocess_transport)N)N) r2r3r4r5rrpryr…r6rr)rrr /s 0r c@sæeZdZdZd7dd„Zdd„Zdd„Zd d „Zd8d d „Zdd„Z d9dd„Z d:dd„Z d;dd„Z dd„Z dd„Zdd„Zdd„Zdd „Zdr z#Proactor implementation using IOCP.ìÿÿcCsDd|_g|_t tjtd|¡|_i|_t  ¡|_ g|_ t  ¡|_ dS)Nr) r*Ú_resultsrBÚCreateIoCompletionPortÚINVALID_HANDLE_VALUEr]Ú_iocpÚ_cacherTrUr:Ú _unregisteredÚ_stopped_serving)rZ concurrencyrrrr‚s zIocpProactor.__init__cCs|jdkrtdƒ‚dS)NzIocpProactor is closed)rŠrI)rrrrÚ _check_closedŒs zIocpProactor._check_closedcCsFdt|jƒdt|jƒg}|jdkr0| d¡d|jjd |¡fS)Nzoverlapped#=%sz result#=%sr\z<%s %s>ú )Úlenr‹r‡rŠr?rr2Újoin)rr"rrrÚ__repr__s    zIocpProactor.__repr__cCs ||_dS)N)r*)rrrrrÚset_loop—szIocpProactor.set_loopNcCs |js| |¡|j}g|_|S)N)r‡r>)rÚtimeoutrZrrrÚselectšs  zIocpProactor.selectcCs|j ¡}| |¡|S)N)r*r{r0)rÚvaluer@rrrÚ_result¡s  zIocpProactor._resultrcCsz| |¡t t¡}y4t|tjƒr6| | ¡||¡n| | ¡|¡Wnt k rb|  d¡SXdd„}|  |||¡S)Nóc SsPy| ¡Stk rJ}z$|jtjtjfkr8t|jŽ‚n‚Wdd}~XYnXdS)N)Ú getresultr)rCrBÚERROR_NETNAME_DELETEDÚERROR_OPERATION_ABORTEDÚConnectionResetErrorr~)roÚkeyrr,rrrÚ finish_recv±s  z&IocpProactor.recv..finish_recv) Ú_register_with_iocprBÚ Overlappedr]Ú isinstanceÚsocketZWSARecvrtZReadFileÚBrokenPipeErrorr—Ú _register)rÚconnÚnbytesrarržrrrÚrecv¦s     zIocpProactor.recvcCsz| |¡t t¡}y4t|tjƒr6| | ¡||¡n| | ¡|¡Wnt k rb|  d¡SXdd„}|  |||¡S)Nr˜c SsPy| ¡Stk rJ}z$|jtjtjfkr8t|jŽ‚n‚Wdd}~XYnXdS)N)r™r)rCrBršr›rœr~)rorrr,rrrržÈs  z+IocpProactor.recv_into..finish_recv) rŸrBr r]r¡r¢Z WSARecvIntortZ ReadFileIntor£r—r¤)rr¥ÚbufrarržrrrÚ recv_into½s     zIocpProactor.recv_intocCsZ| |¡t t¡}t|tjƒr4| | ¡||¡n| | ¡|¡dd„}|  |||¡S)Nc SsPy| ¡Stk rJ}z$|jtjtjfkr8t|jŽ‚n‚Wdd}~XYnXdS)N)r™r)rCrBršr›rœr~)rorrr,rrrÚ finish_sendÜs  z&IocpProactor.send..finish_send) rŸrBr r]r¡r¢ZWSASendrtZ WriteFiler¤)rr¥r¨rarrªrrrÚsendÔs    zIocpProactor.sendcsv| ˆ¡| ˆj¡‰t t¡}| ˆ ¡ˆ ¡¡‡‡fdd„}dd„}| |ˆ|¡}||ˆƒ}t j ||j d|S)NcsD| ¡t dˆ ¡¡}ˆ tjtj|¡ˆ  ˆ  ¡¡ˆˆ  ¡fS)Nz@P) r™ÚstructZpackrtÚ setsockoptr¢Ú SOL_SOCKETrBZSO_UPDATE_ACCEPT_CONTEXTÚ settimeoutZ gettimeoutZ getpeername)rorrr¨)r¥ÚlistenerrrÚ finish_acceptîs  z*IocpProactor.accept..finish_acceptcÓs4y|IdHWn tjk r.| ¡‚YnXdS)N)rrurd)r&r¥rrrÚ accept_coro÷s z(IocpProactor.accept..accept_coro)r) rŸÚ_get_accept_socketÚfamilyrBr r]ZAcceptExrtr¤rZ ensure_futurer*)rr°rr±r²r&Úcoror)r¥r°rÚacceptès     zIocpProactor.acceptc s˜| ˆ¡yt ˆ ¡ˆj¡WnBtk rb}z$|jtjkr@‚ˆ  ¡ddkrR‚Wdd}~XYnXt  t ¡}|  ˆ ¡|¡‡fdd„}|  |ˆ|¡S)Nrrcs| ¡ˆ tjtjd¡ˆS)Nr)r™r­r¢r®rBZSO_UPDATE_CONNECT_CONTEXT)rorr)r¥rrÚfinish_connects z,IocpProactor.connect..finish_connect)rŸrBZ BindLocalrtr´r)rCÚerrnoZ WSAEINVALZ getsocknamer r]Z ConnectExr¤)rr¥r!Úerr·r)r¥rÚconnects    zIocpProactor.connectc Csb| |¡t t¡}|d@}|d?d@}| | ¡t | ¡¡|||dd¡dd„}| |||¡S)Nlÿÿé rc SsPy| ¡Stk rJ}z$|jtjtjfkr8t|jŽ‚n‚Wdd}~XYnXdS)N)r™r)rCrBršr›rœr~)rorrr,rrrÚfinish_sendfile%s  z.IocpProactor.sendfile..finish_sendfile) rŸrBr r]Z TransmitFilertÚmsvcrtZ get_osfhandler¤) rZsockÚfileÚoffsetÚcountrZ offset_lowZ offset_highr¼rrrÚsendfiles       zIocpProactor.sendfilecsJ| ˆ¡t t¡}| ˆ ¡¡}|r0| ˆ¡S‡fdd„}| |ˆ|¡S)Ncs | ¡ˆS)N)r™)rorr)rcrrÚfinish_accept_pipe;sz4IocpProactor.accept_pipe..finish_accept_pipe)rŸrBr r]ZConnectNamedPipertr—r¤)rrcrZ connectedrÂr)rcrrs0s    zIocpProactor.accept_pipec Ãszt}xjyt |¡}PWn0tk rF}z|jtjkr6‚Wdd}~XYnXt|dtƒ}tj ||j dIdHqWt   |¡S)Né)r) ÚCONNECT_PIPE_INIT_DELAYrBZ ConnectPiper)rCZERROR_PIPE_BUSYÚminÚCONNECT_PIPE_MAX_DELAYrZsleepr*r r^)rr!Zdelayr;r,rrrrjAs  zIocpProactor.connect_pipecCs| ||d¡S)z°Wait 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;r”rrrÚwait_for_handleTszIocpProactor.wait_for_handlecCs| |dd¡}||_|S)NT)rÇrG)rrHZ done_callbackr@rrrrQ\szIocpProactor._wait_cancelcs¢| ¡|dkrtj}nt |d¡}t t¡}t ||j |j |¡}|r\t ||||j d‰nt |||||j d‰ˆjr~ˆjd=‡fdd„}ˆ|d|f|j|j <ˆS)Ng@@)rrcsˆ ¡S)N)r>)rorr)rmrrÚfinish_wait_for_handleysz=IocpProactor._wait_for_handle..finish_wait_for_handler)rŽr=ÚINFINITEÚmathÚceilrBr r]ZRegisterWaitWithQueuerŠr!rFr*rJrr‹)rr;r”Z _is_cancelÚmsrr<rÉr)rmrrÇcs     zIocpProactor._wait_for_handlecCs0||jkr,|j |¡t | ¡|jdd¡dS)Nr)r:r_rBrˆrtrŠ)rÚobjrrrrŸ…s  z IocpProactor._register_with_iocpc CsŠ| ¡t||jd}|jr$|jd=|jsry|dd|ƒ}Wn,tk rf}z| |¡Wdd}~XYn X| |¡||||f|j|j <|S)N)rr) rŽrr*rrr)r/r0r‹r!)rrrÎÚcallbackrmr–r¹rrrr¤s zIocpProactor._registercCs| ¡|j |¡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)rŽrŒr?)rrrrrrP¯szIocpProactor._unregistercCst |¡}| d¡|S)Nr)r¢r¯)rr´Úsrrrr³¹s  zIocpProactor._get_accept_socketc CsŒ|dkrt}n0|dkr tdƒ‚nt |d¡}|tkr>tdƒ‚xt |j|¡}|dkrZPd}|\}}}}y|j |¡\}} } } WnVt k rØ|j   ¡rº|j   dd||||fdœ¡|dtj fkrÒt |¡wBYnX| |jkrî| ¡qB| ¡sBy| ||| ƒ} Wn:tk r@} z| | ¡|j |¡Wdd} ~ XYqBX| | ¡|j |¡qBWx |jD]} |j | jd¡qdW|j ¡dS)Nrznegative timeoutg@@ztimeout too bigz8GetQueuedCompletionStatus() returned an unexpected eventz)err=%s transferred=%s key=%#x address=%#x)r$Ústatus)rÊÚ ValueErrorrËrÌrBZGetQueuedCompletionStatusrŠr‹ÚpopÚKeyErrorr*Z get_debugr+r‰r=rOrr(Zdoner)r/r‡r?r0rŒr!re)rr”rÍrÑÚerrZ transferredrr!rmrrÎrÏr–r¹rrrr>¾sJ         zIocpProactor._pollcCs|j |¡dS)N)rr_)rrÎrrrÚ _stop_servingõszIocpProactor._stop_servingc Cs |jdkrdSxšt|j ¡ƒD]ˆ\}\}}}}| ¡r8qt|tƒrDqy | ¡Wqtk r¤}z6|j dk r”d||dœ}|j rˆ|j |d<|j   |¡Wdd}~XYqXqWd}t   ¡} | |} xD|jr| t   ¡kröt d|t   ¡| ¡t   ¡|} | |¡qÀWg|_t |j¡d|_dS)NzCancelling a future failed)r$r%r&r'gð?z,%r is running after closing for %.1f seconds)rŠÚlistr‹ÚitemsZ cancelledr¡rFr(r)r*rr+ÚtimeZ monotonicr Údebugr>r‡r=rO) rr!r@rrÎrÏr,r-Z msg_updateZ start_timeZnext_msgrrrrdûs:      "    zIocpProactor.closecCs | ¡dS)N)rd)rrrrrf*szIocpProactor.__del__)r†)N)r)r)r)N)N)r2r3r4r5rrŽr’r“r•r—r§r©r«r¶rºrÁrsrjrÈrQrÇrŸr¤rPr³r>rÖrdrfrrrrr s4      "    7/r c@seZdZdd„ZdS)r|c  sPtj|f|||||dœ|—Žˆ_‡fdd„}ˆjj tˆjjƒ¡} |  |¡dS)N)rr€rr‚rƒcsˆj ¡}ˆ |¡dS)N)Ú_procZpollZ_process_exited)rmÚ returncode)rrrrÏ5s z4_WindowsSubprocessTransport._start..callback) r ÚPopenrÛr*rKrÈÚintr8rv) rr~rr€rr‚rƒr„rÏrmr)rrÚ_start0s   z"_WindowsSubprocessTransport._startN)r2r3r4rßrrrrr|.sr|c@seZdZeZdS)rN)r2r3r4r Ú _loop_factoryrrrrr@src@seZdZeZdS)rN)r2r3r4r ràrrrrrDsr).r5rBr=r¸rËr½r¢r¬rÙrTÚrrrrrrr Úlogr Ú__all__r]rÊZERROR_CONNECTION_REFUSEDZERROR_CONNECTION_ABORTEDrÄrÆZFuturerr7rFrJÚobjectrRZBaseSelectorEventLooprgZBaseProactorEventLoopr r ZBaseSubprocessTransportr|r ZBaseDefaultEventLoopPolicyrrrrrrrÚsP        0J4;P2