3 \l@sddlZddlZddlZddlZddlZddlZddlmZddlmZddlm Z ddlm Z ddlm Z ddlm Z dd lm Z dd lmZdd lmZdd lmZd dddgZdZdZdZdZdZdZGddde jZGddde jZGdddeZGdddeZGdddeZ Gd d!d!e j!Z"Gd"dde j#Z$Gd#ddZ%Gd$d%d%ej&Z'e"Z(Gd&d'd'ej)Z*e*Z+dS)(N)events)base_subprocess)futures)proactor_events)selector_events)tasks) windows_utils) _overlapped) coroutine)loggerSelectorEventLoopProactorEventLoop IocpProactorDefaultEventLoopPolicyliigMbP?g?csZeZdZddfdd ZfddZddZfd d Zfd d Zfd dZZ S)_OverlappedFutureN)loopcs&tj|d|jr|jd=||_dS)N)rr)super__init___source_traceback_ov)selfovr) __class__./usr/lib64/python3.6/asyncio/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__rrr-r'r.r/ __classcell__rr)rrr's    rcsjeZdZddfdd ZddZfddZd d Zd d Zfd dZfddZ fddZ Z S)_BaseWaitHandleFutureN)rcs8tj|d|jr|jd=||_||_||_d|_dS)N)rrTr)rrrr_handle _wait_handle _registered)rrhandle wait_handler)rrrrZsz_BaseWaitHandleFuture.__init__cCstj|jdtjkS)Nr)_winapiZWaitForSingleObjectr6Z 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)rrappendr6r<r7)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&) r8r7r ZUnregisterWaitr(winerrorERROR_IO_PENDINGrr)r*r?)rr:r+r,rrr_unregister_wait|s"   z&_BaseWaitHandleFuture._unregister_waitcs|jtjS)N)rBrr')r)rrrr'sz_BaseWaitHandleFuture.cancelcs|jtj|dS)N)rBrr.)rr$)rrrr.sz#_BaseWaitHandleFuture.set_exceptioncs|jtj|dS)N)rBrr/)rr0)rrrr/sz _BaseWaitHandleFuture.set_result) r1r2r3rr<rr?rBr'r.r/r4rr)rrr5Ws   r5csBeZdZddfdd ZddZfddZfd d ZZS) _WaitCancelFutureN)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/rD)rr0)rrrr/s  z_WaitCancelFuture.set_resultcs$tj||jdk r |j|dS)N)rr.rD)rr$)rrrr.s  z_WaitCancelFuture.set_exception)r1r2r3rr'r/r.r4rr)rrrCs rCcs6eZdZddfdd ZfddZddZZS) _WaitHandleFutureN)rcs<tj||||d||_d|_tjdddd|_d|_dS)N)rTF)rr _proactorZ_unregister_proactorr Z CreateEvent_event _event_fut)rrr9r:proactorr)rrrrs z_WaitHandleFuture.__init__csF|jdk r"tj|jd|_d|_|jj|jd|_tj|dS)N) rIr; CloseHandlerJrH _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&)r8r7r ZUnregisterWaitExrIr(r@rArr)r*rH _wait_cancelr?rJ)rr:r+r,rrrrBs$    z"_WaitHandleFuture._unregister_wait)r1r2r3rr?rBr4rr)rrrGs rGc@s8eZdZddZddZddZddZd d ZeZd S) PipeServercCs,||_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)rTrV)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_INSTANCEZCreateNamedPiperPZPIPE_TYPE_MESSAGEZPIPE_READMODE_MESSAGEZ PIPE_WAITZPIPE_UNLIMITED_INSTANCESr ZBUFSIZEZNMPWAIT_WAIT_FOREVERNULL PipeHandlerSadd)rfirstflagshpiperrrrVs      zPipeServer._server_pipe_handlecCs |jdkS)N)rP)rrrrrYszPipeServer.closedcCsV|jdk r|jjd|_|jdk rRx|jD] }|jq,Wd|_d|_|jjdS)N)rUr'rPrScloserTclear)rr`rrrras     zPipeServer.closeN) r1r2r3rrXrVrYra__del__rrrrrOs   rOc@seZdZddZdS)_WindowsSelectorEventLoopcCstjS)N)r socketpair)rrrr _socketpair+sz%_WindowsSelectorEventLoop._socketpairN)r1r2r3rfrrrrrd(srdcsLeZdZd fdd ZddZeddZedd Zed d d ZZ S)rNcs|dkrt}tj|dS)N)rrr)rrK)rrrr2szProactorEventLoop.__init__cCstjS)N)r re)rrrrrf7szProactorEventLoop._socketpairccs8|jj|}|EdH}|}|j||d|id}||fS)Naddr)extra)rH connect_pipe_make_duplex_pipe_transport)rprotocol_factoryr fr`protocoltransrrrcreate_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) Nrg)rhrzPipe accept failed)r#r$r`zAccept pipe failed on pipe %rT)exc_infor)r0rSdiscardrYrarjrXrH accept_piper(filenor*Z_debugr ZwarningrCancelledErrorrUadd_done_callback)rlr`rmr+)r loop_accept_piperkrserverrrrvGs<   z>ProactorEventLoop.start_serving_pipe..loop_accept_pipe)N)rOZ call_soon)rrkr r)r rvrkrrwrstart_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)waiterrh) create_future_WindowsSubprocessTransport ExceptionraZ_wait)rrmargsshellstdinstdoutstderrbufsizerhkwargsryZtranspr+errrrr_make_subprocess_transportrs  z,ProactorEventLoop._make_subprocess_transport)N)N) r1r2r3rrfr rorxrr4rr)rrr/s  /c@seZdZd0ddZddZddZd1d d Zd d Zd2ddZd3ddZ ddZ ddZ ddZ e ddZd4ddZddZddZd d!Zd"d#Zd$d%Zd&d'Zd5d(d)Zd*d+Zd,d-Zd.d/ZdS)6rcCsDd|_g|_tjtjtd||_i|_tj |_ g|_ tj |_ dS)Nr) r)_resultsr CreateIoCompletionPortINVALID_HANDLE_VALUErZ_iocp_cacherQrRr8 _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<)rtimeoutrWrrrselects  zIocpProactor.selectcCs|jj}|j||S)N)r)rzr/)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(r@r ERROR_NETNAME_DELETEDConnectionResetErrorr})rnkeyrr+rrr finish_recvs   z&IocpProactor.recv..finish_recv) _register_with_iocpr OverlappedrZ isinstancesocketZWSARecvrsZReadFileBrokenPipeErrorr _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(r@r rrr})rnrrr+rrr finish_sends   z&IocpProactor.send..finish_send) rr rrZrrZWSASendrsZ WriteFiler)rrbufr^rrrrrsends    zIocpProactor.sendcsz|j|jjtjt}|jjjfdd}tdd}|j ||}||}t j ||j d|S)NcsD|jtjdj}jtjtj|j j j fS)Nz@P) rstructZpackrs setsockoptr SOL_SOCKETr ZSO_UPDATE_ACCEPT_CONTEXT settimeoutZ gettimeoutZ getpeername)rnrrr)rlistenerrr finish_accepts  z*IocpProactor.accept..finish_acceptc ss4y|EdHWn tjk r.|jYnXdS)N)rrtra)r%rrrr accept_coros z(IocpProactor.accept..accept_coro)r) r_get_accept_socketfamilyr rrZZAcceptExrsr 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)rnrr)rrrfinish_connects z,IocpProactor.connect..finish_connect)rr Z BindLocalrsrr(r@errnoZ WSAEINVALZ getsocknamerrZZ ConnectExr)rrr errr)rrconnects    zIocpProactor.connectcsJ|jtjt}|jj}|r0|jSfdd}|j||S)Ncs |jS)N)r)rnrr)r`rrfinish_accept_pipesz4IocpProactor.accept_pipe..finish_accept_pipe)rr rrZZConnectNamedPipersrr)rr`rZ connectedrr)r`rrr s    zIocpProactor.accept_pipeccszt}xjytj|}PWn0tk rF}z|jtjkr6WYdd}~XnXt|dt}tj ||j dEdHqWt j |S)N)r) CONNECT_PIPE_INIT_DELAYr Z ConnectPiper(r@ZERROR_PIPE_BUSYminCONNECT_PIPE_MAX_DELAYrZsleepr)r r[)rr Zdelayr9r+rrrris  zIocpProactor.connect_pipecCs|j||dS)NF)_wait_for_handle)rr9rrrrwait_for_handle/szIocpProactor.wait_for_handlecCs|j|dd}||_|S)NT)rrD)rrEZ done_callbackr>rrrrN7szIocpProactor._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<)rnrr)rlrrfinish_wait_for_handleRsz=IocpProactor._wait_for_handle..finish_wait_for_handlerr)r;INFINITEmathceilr rrZZRegisterWaitWithQueuerr rCr)rGrr)rr9rZ _is_cancelmsrr:rr)rlrr>s    zIocpProactor._wait_for_handlecCs0||jkr,|jj|tj|j|jdddS)Nr)r8r\r rrsr)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 )rrrcallbackrlrrrrrrhs zIocpProactor._registercCs|jj|dS)N)rr=)rrrrrrMszIocpProactor._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;rLrr'doner(r.rr=r/rr rb)rrrrrZ transferredrr rlrrrrrrrrr<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 cancelledrrCr'r(r)rr*r<r debugrrr;rL)rr r>rrrr+r,rrrras,     "   zIocpProactor.closecCs |jdS)N)ra)rrrrrcszIocpProactor.__del__)r)N)r)r)N)N)r1r2r3rrrrrrrrrrrr rirrNrrrrMrr<rrarcrrrrrs,          7 c@seZdZddZdS)r{c  sPtj|f|||||d|_fdd}jjjtjj} | j|dS)N)r~rrrrcsjj}j|dS)N)_procZpollZ_process_exited)rl returncode)rrrrs z4_WindowsSubprocessTransport._start..callback) r Popenrr)rHrintr6ru) rr}r~rrrrrrrlr)rr_starts   z"_WindowsSubprocessTransport._startN)r1r2r3rrrrrr{sr{c@seZdZeZdS)_WindowsDefaultEventLoopPolicyN)r1r2r3r Z _loop_factoryrrrrrsr),r;rrrrrQrrrrrrr r Z coroutinesr logr __all__rZrZERROR_CONNECTION_REFUSEDZERROR_CONNECTION_ABORTEDrrZFuturerr5rCrGobjectrOZBaseSelectorEventLooprdZBaseProactorEventLooprrZBaseSubprocessTransportr{r ZBaseDefaultEventLoopPolicyrrrrrrsJ          0J4;]k