B €/g^vc@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ÚWindowsProactorEventLoopPolicyìÿÿ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©Nréÿÿÿÿ)Ú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 ZinsertÚaddress©rÚinfoZstaterrrr"6s    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_overlappedcó| ¡tƒ ¡S©N)r1rr+©rrrrr+Móz_OverlappedFuture.cancelcstƒ |¡| ¡dSr3)rÚ set_exceptionr1©rr(rrrr6Qó z_OverlappedFuture.set_exceptioncstƒ |¡d|_dSr3)rÚ set_resultr©rÚresultrrrr9Ur8z_OverlappedFuture.set_result) Ú__name__Ú __module__Ú __qualname__Ú__doc__rr"r1r+r6r9Ú __classcell__rrrrr*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.Nrcs8tƒj|d|jr|jd=||_||_||_d|_dS)NrrT)rrrrÚ_handleÚ _wait_handleÚ _registered)rrÚhandleÚ wait_handlerrrrr]sz_BaseWaitHandleFuture.__init__cCst |jd¡tjkS©Nr)Ú_winapiZWaitForSingleObjectrBZ WAIT_OBJECT_0r4rrrÚ_pollks z_BaseWaitHandleFuture._pollcsdtƒ ¡}| d|jd›¡|jdk rB| ¡r4dnd}| |¡|jdk r`| d|jd›¡|S)Nzhandle=r!ZsignaledZwaitingz wait_handle=)rr"ÚappendrBrIrCr$rrrr"ps    z _BaseWaitHandleFuture._repr_infocCs d|_dSr3)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 handler&r*) rDrCÚ _overlappedZUnregisterWaitr,ÚwinerrorÚERROR_IO_PENDINGrr-r.rM©rrFr/r0rrrÚ_unregister_waits"   z&_BaseWaitHandleFuture._unregister_waitcr2r3)rSrr+r4rrrr+—r5z_BaseWaitHandleFuture.cancelcó| ¡tƒ |¡dSr3)rSrr6r7rrrr6›r5z#_BaseWaitHandleFuture.set_exceptioncrTr3)rSrr9r:rrrr9Ÿr5z _BaseWaitHandleFuture.set_result) r<r=r>r?rrIr"rMrSr+r6r9r@rrrrrAZs   rAcsFeZdZdZddœ‡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. Nrcstƒj||||dd|_dS)Nr)rrÚ_done_callback)rrÚeventrFrrrrr©sz_WaitCancelFuture.__init__cCs tdƒ‚dS)Nz'_WaitCancelFuture must not be cancelled)Ú RuntimeErrorr4rrrr+®óz_WaitCancelFuture.cancelcó$tƒ |¡|jdk r | |¡dSr3)rr9rVr:rrrr9±ó  z_WaitCancelFuture.set_resultcrZr3)rr6rVr7rrrr6¶r[z_WaitCancelFuture.set_exception) r<r=r>r?rr+r9r6r@rrrrrU¤s  rUcs6eZdZddœ‡fdd„ Z‡fdd„Zdd„Z‡ZS) Ú_WaitHandleFutureNrcs<tƒj||||d||_d|_t dddd¡|_d|_dS)NrTF)rrÚ _proactorZ_unregister_proactorrOZ CreateEventÚ_eventÚ _event_fut)rrrErFÚproactorrrrrr½s z_WaitHandleFuture.__init__csF|jdk r"t |j¡d|_d|_|j |j¡d|_tƒ |¡dSr3) r^rHÚ CloseHandler_r]Ú _unregisterrrrMrKrrrrMÄ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 ¡|_dSrN)rDrCrOZUnregisterWaitExr^r,rPrQrr-r.r]Ú _wait_cancelrMr_rRrrrrSÖs$    z"_WaitHandleFuture._unregister_wait)r<r=r>rrMrSr@rrrrr\¼s r\c@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)rjrl)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 |¡|Sr3)ÚclosedrHZPIPE_ACCESS_DUPLEXZFILE_FLAG_OVERLAPPEDZFILE_FLAG_FIRST_PIPE_INSTANCEZCreateNamedPiperfZPIPE_TYPE_MESSAGEZPIPE_READMODE_MESSAGEZ PIPE_WAITZPIPE_UNLIMITED_INSTANCESrZBUFSIZEZNMPWAIT_WAIT_FOREVERÚNULLÚ PipeHandleriÚadd)rZfirstÚflagsZhÚpiperrrrls      zPipeServer._server_pipe_handlecCs |jdkSr3)rfr4rrrrorYzPipeServer.closedcCsV|jdk r|j ¡d|_|jdk rRx|jD] }| ¡q,Wd|_d|_|j ¡dSr3)rkr+rfriÚcloserjÚclear)rrtrrrrus     zPipeServer.closeN) r<r=r>r?rrnrlroruÚ__del__rrrrrdðs  rdc@seZdZdZdS)Ú_WindowsSelectorEventLoopz'Windows version of selector event loop.N)r<r=r>r?rrrrrx+órxcs<eZdZdZd ‡fdd„ Zdd„Zdd„Zd d d „Z‡ZS) r z2Windows version of proactor event loop using IOCP.Ncs|dkrtƒ}tƒ |¡dSr3)r rr)rr`rrrr2szProactorEventLoop.__init__cÃs8|j |¡}|IdH}|ƒ}|j||d|id}||fS)NÚaddr©Úextra)r]Ú connect_pipeÚ_make_duplex_pipe_transport)rÚprotocol_factoryr#ÚfrtÚ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) Nrzr{rzPipe accept failed)r'r(rtzAccept pipe failed on pipe %rT)Zexc_info)r;riZdiscardrorur~rnr]Ú accept_piper,Úfilenor.Z_debugr ZwarningrÚCancelledErrorrkÚadd_done_callback)r€rtrr/©r#Úloop_accept_piperrZserverrrr‰Bs<   z>ProactorEventLoop.start_serving_pipe..loop_accept_piper3)rdZ call_soon)rrr#rrˆrÚstart_serving_pipe?s( z$ProactorEventLoop.start_serving_pipec Ësl| ¡} t||||||||f| |dœ| —Ž} y| IdHWn,tk rf|  ¡|  ¡IdH‚YnX| S)N)Úwaiterr|)Ú create_futureÚ_WindowsSubprocessTransportZ ExceptionruZ_wait) rrÚargsÚshellÚstdinÚstdoutÚstderrÚbufsizer|Úkwargsr‹ZtransprrrÚ_make_subprocess_transportms  z,ProactorEventLoop._make_subprocess_transportr3r3) r<r=r>r?rrƒrŠr•r@rrrrr /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.rcCsDd|_g|_t tjtd|¡|_i|_t  ¡|_ g|_ t  ¡|_ dSrG) r-Ú_resultsrOÚCreateIoCompletionPortÚINVALID_HANDLE_VALUErpÚ_iocpÚ_cachergrhrDÚ _unregisteredÚ_stopped_serving)rZ concurrencyrrrr‚s zIocpProactor.__init__cCs|jdkrtdƒ‚dS)NzIocpProactor is closed)r™rXr4rrrÚ _check_closedŒó zIocpProactor._check_closedcCsFdt|jƒdt|jƒg}|jdkr0| d¡d|jjd |¡fS)Nzoverlapped#=%sz result#=%sroz<%s %s>z )Zlenršr–r™rJrr<Zjoin)rr%rrrÚ__repr__s    zIocpProactor.__repr__cCs ||_dSr3)r-)rrrrrÚset_loop—rYzIocpProactor.set_loopNcCs |js| |¡|j}g|_|Sr3)r–rI)rÚtimeoutrmrrrÚselectšs  zIocpProactor.selectcCs|j ¡}| |¡|Sr3)r-rŒr9)rÚvaluerLrrrÚ_result¡ó  zIocpProactor._resultrcCóz| |¡t t¡}y4t|tjƒr6| | ¡||¡n| | ¡|¡Wnt k rb|  d¡SXdd„}|  |||¡S)Nóc SóPy| ¡Stk rJ}z$|jtjtjfkr8t|jŽ‚n‚Wdd}~XYnXdSr3©Ú getresultr,rPrOZERROR_NETNAME_DELETEDZERROR_OPERATION_ABORTEDZConnectionResetErrorrŽ©r‚Úkeyrr/rrrÚ finish_recv±ó  z&IocpProactor.recv..finish_recv) Ú_register_with_iocprOÚ OverlappedrpÚ isinstanceÚsocketZWSARecvr…ZReadFileÚBrokenPipeErrorr¤Ú _register)rÚconnZnbytesrsrr­rrrÚrecv¦ó     zIocpProactor.recvcCr¦)Nr§c Sr¨r3r©r«rrrr­Èr®z+IocpProactor.recv_into..finish_recv) r¯rOr°rpr±r²Z WSARecvIntor…Z ReadFileIntor³r¤r´)rrµÚbufrsrr­rrrÚ recv_into½r·zIocpProactor.recv_intocCsZ| |¡t t¡}t|tjƒr4| | ¡||¡n| | ¡|¡dd„}|  |||¡S)Nc Sr¨r3r©r«rrrÚ finish_sendÜr®z&IocpProactor.send..finish_send) r¯rOr°rpr±r²ZWSASendr…Z WriteFiler´)rrµr¸rsrrºrrrÚsendÔs    zIocpProactor.sendcsv| ˆ¡| ˆj¡‰t t¡}| ˆ ¡ˆ ¡¡‡‡fdd„}dd„}| |ˆ|¡}||ˆƒ}t j ||j d|S)NcsD| ¡t dˆ ¡¡}ˆ tjtj|¡ˆ  ˆ  ¡¡ˆˆ  ¡fS)Nz@P) rªÚstructZpackr…Ú setsockoptr²Ú SOL_SOCKETrOZSO_UPDATE_ACCEPT_CONTEXTÚ settimeoutZ gettimeoutZ getpeername)r‚r¬rr¸©rµÚlistenerrrÚ finish_acceptîs  z*IocpProactor.accept..finish_acceptcÓs4y|IdHWn tjk r.| ¡‚YnXdSr3)rr†ru)r)rµrrrÚ accept_coro÷s z(IocpProactor.accept..accept_coror) r¯Ú_get_accept_socketÚfamilyrOr°rpZAcceptExr…r´rZ ensure_futurer-)rrÁrrÂrÃr)ZcororrÀrÚacceptès     zIocpProactor.acceptc s˜| ˆ¡yt ˆ ¡ˆj¡WnBtk rb}z$|jtjkr@‚ˆ  ¡ddkrR‚Wdd}~XYnXt  t ¡}|  ˆ ¡|¡‡fdd„}|  |ˆ|¡S)Nrrcs| ¡ˆ tjtjd¡ˆSrG)rªr½r²r¾rOZSO_UPDATE_CONNECT_CONTEXT©r‚r¬r©rµrrÚfinish_connects z,IocpProactor.connect..finish_connect)r¯rOZ BindLocalr…rÅr,rPÚerrnoZ WSAEINVALZ getsocknamer°rpZ ConnectExr´)rrµr#ÚerrÉrrÈrÚconnects    zIocpProactor.connectc Csb| |¡t t¡}|d@}|d?d@}| | ¡t | ¡¡|||dd¡dd„}| |||¡S)Nri rc Sr¨r3r©r«rrrÚfinish_sendfile%r®z.IocpProactor.sendfile..finish_sendfile) r¯rOr°rpZ TransmitFiler…ÚmsvcrtZ get_osfhandler´) rZsockZfileZoffsetZcountrZ offset_lowZ offset_highrÍrrrÚsendfiles       zIocpProactor.sendfilecsJ| ˆ¡t t¡}| ˆ ¡¡}|r0| ˆ¡S‡fdd„}| |ˆ|¡S)Ncs | ¡ˆSr3)rªrÇ©rtrrÚfinish_accept_pipe;r5z4IocpProactor.accept_pipe..finish_accept_pipe)r¯rOr°rpZConnectNamedPiper…r¤r´)rrtrZ connectedrÑrrÐrr„0s    zIocpProactor.accept_pipec Ãszt}xjyt |¡}PWn0tk rF}z|jtjkr6‚Wdd}~XYnXt|dtƒ}tj ||j dIdHqWt   |¡S)Nir) ÚCONNECT_PIPE_INIT_DELAYrOZ ConnectPiper,rPZERROR_PIPE_BUSYZminÚCONNECT_PIPE_MAX_DELAYrZsleepr-rrq)rr#ZdelayrEr/rrrr}As  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)rrEr¡rrrÚwait_for_handleTszIocpProactor.wait_for_handlecCs| |dd¡}||_|Sre)rÔrV)rrWZ done_callbackrLrrrrc\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)Nç@@rrcsˆ ¡Sr3)rIrÇ©r€rrÚfinish_wait_for_handleysz=IocpProactor._wait_for_handle..finish_wait_for_handler)rrHÚINFINITEÚmathÚceilrOr°rpZRegisterWaitWithQueuer™r#rUr-r\rrš)rrEr¡Z _is_cancelÚmsrrFrØrr×rrÔcs     zIocpProactor._wait_for_handlecCs0||jkr,|j |¡t | ¡|jdd¡dSrG)rDrrrOr—r…r™©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 <|Sr) rrr-rr r,r6r9ršr#)rrrÞÚcallbackr€r£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)rr›rJ)rrrrrrb¯szIocpProactor._unregistercCst |¡}| d¡|SrG)r²r¿)rrÅZsrrrrĹr¥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 timeoutrÖztimeout too bigz8GetQueuedCompletionStatus() returned an unexpected eventz)err=%s transferred=%s key=%#x address=%#x)r'Ústatus)rÙZ ValueErrorrÚrÛrOZGetQueuedCompletionStatusr™ršZpopZKeyErrorr-Z get_debugr.r˜rHrarœr+Zdoner,r6r–rJr9r›r#rv)rr¡rÜràZerrZ transferredr¬r#r€rrÞrßr£rËrrrrI¾sJ         zIocpProactor._pollcCs|j |¡dSr3)rœrrrÝ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 failedr&r*gð?z,%r is running after closing for %.1f seconds)r™ZlistršZitemsZ cancelledr±rUr+r,r-rr.ÚtimeZ monotonicr ZdebugrIr–rHra) rr#rLrrÞrßr/r0Z msg_updateZ start_timeZnext_msgrrrruûs:      "    zIocpProactor.closecCs | ¡dSr3)rur4rrrrw*rYzIocpProactor.__del__)rr3©rrãrãr3r3)r<r=r>r?rrrŸr r¢r¤r¶r¹r»rÆrÌrÏr„r}rÕrcrÔr¯r´rbrÄrIrárurwrrrrr s4      "    7/r c@seZdZdd„ZdS)rc  sPtj|f|||||dœ|—Žˆ_‡fdd„}ˆjj tˆjjƒ¡} |  |¡dS)N)rrr‘r’r“csˆj ¡}ˆ |¡dSr3)Ú_procZpollZ_process_exited)r€Z returncoder4rrrß5ržz4_WindowsSubprocessTransport._start..callback) rZPopenrär-r]rÕZintrBr‡) rrŽrrr‘r’r“r”rßr€rr4rÚ_start0s   z"_WindowsSubprocessTransport._startN)r<r=r>rårrrrr.src@óeZdZeZdS)rN)r<r=r>r Ú _loop_factoryrrrrr@ryrc@ræ)rN)r<r=r>r rçrrrrrDryr).r?rOrHrÊrÚrÎr²r¼rârgZrrrrrrrZlogr Z__all__rprÙZERROR_CONNECTION_REFUSEDZERROR_CONNECTION_ABORTEDrÒrÓZFuturerrArUr\ZobjectrdZBaseSelectorEventLooprxZBaseProactorEventLoopr r ZBaseSubprocessTransportrr ZBaseDefaultEventLoopPolicyrrr rrrrZsP        0J4;P2