B €/g^5dc@sdZdZddlZddlZddlZddlZddlmZddlmZddlm Z ddlm Z dd lm Z dd lm Z dd lm Z dd lmZGd d„de je jƒZGdd„dee jƒZGdd„dee jƒZGdd„deƒZGdd„deee jƒZGdd„deee jƒZGdd„dejƒZdS)z¦Event loop using a proactor and related classes. A proactor is a "notify-on-completion" multiplexer. Currently a proactor is only implemented on Windows with IOCP. )ÚBaseProactorEventLoopéNé)Ú base_events)Ú constants)Úevents)Úfutures)Ú protocols)Ússlproto)Ú transports)Úloggercs|eZdZdZd‡fdd„ Zdd„Zdd„Zd d „Zd d „Zd d„Z dd„Z dd„Z ddd„Z dd„Z dd„Zdd„Z‡ZS)Ú_ProactorBasePipeTransportz*Base class for pipe and socket transports.Ncsœtƒ ||¡| |¡||_| |¡||_d|_d|_d|_d|_ d|_ d|_ d|_ |jdk rl|j  ¡|j |jj|¡|dk r˜|j tj|d¡dS)NrF)ÚsuperÚ__init__Ú _set_extraÚ_sockÚ set_protocolÚ_serverÚ_bufferÚ _read_futÚ _write_futÚ_pending_writeÚ _conn_lostÚ_closingÚ _eof_writtenZ_attachÚ_loopÚ call_soonÚ _protocolZconnection_maderZ_set_result_unless_cancelled©ÚselfÚloopÚsockÚprotocolÚwaiterÚextraÚserver©Ú __class__©ú//usr/lib64/python3.7/asyncio/proactor_events.pyr s$     z#_ProactorBasePipeTransport.__init__cCsÄ|jjg}|jdkr | d¡n|jr0| d¡|jdk rP| d|j ¡›¡|jdk rl| d|j›¡|jdk rˆ| d|j›¡|jr¤| dt |jƒ›¡|j r´| d¡d  d   |¡¡S) NZclosedZclosingzfd=zread=zwrite=zwrite_bufsize=z EOF writtenz<{}>z ) r%Ú__name__rZappendrÚfilenorrrÚlenrZformatZjoin)rZinfor&r&r'Ú__repr__2s         z#_ProactorBasePipeTransport.__repr__cCs||jd<dS)NZpipe)Ú_extra©rrr&r&r'rDóz%_ProactorBasePipeTransport._set_extracCs ||_dS©N©r)rr r&r&r'rGr.z'_ProactorBasePipeTransport.set_protocolcCó|jSr/r0©rr&r&r'Ú get_protocolJr.z'_ProactorBasePipeTransport.get_protocolcCr1r/)rr2r&r&r'Ú is_closingMr.z%_ProactorBasePipeTransport.is_closingcCs\|jr dSd|_|jd7_|js>|jdkr>|j |jd¡|jdk rX|j ¡d|_dS)NTr) rrrrrrÚ_call_connection_lostrÚcancelr2r&r&r'ÚclosePs  z _ProactorBasePipeTransport.closecCs,|jdk r(tjd|›t|d| ¡dS)Nzunclosed transport )Zsource)rÚwarningsZwarnZResourceWarningr7r2r&r&r'Ú__del__[s z"_ProactorBasePipeTransport.__del__úFatal error on pipe transportc CsVzDt|tƒr*|j ¡rBtjd||ddn|j ||||jdœ¡Wd| |¡XdS)Nz%r: %sT©Zexc_info)ÚmessageÚ exceptionZ transportr ) Ú isinstanceÚOSErrorrÚ get_debugr ÚdebugÚcall_exception_handlerrÚ _force_close)rÚexcr<r&r&r'Ú _fatal_erroras  z'_ProactorBasePipeTransport._fatal_errorcCs |jdk r6|j ¡s6|dkr*|j d¡n |j |¡|jr@dSd|_|jd7_|jrj|j ¡d|_|jr€|j ¡d|_d|_ d|_ |j   |j |¡dS)NTrr)Ú _empty_waiterÚdoneÚ set_resultZ set_exceptionrrrr6rrrrrr5)rrDr&r&r'rCps"   z'_ProactorBasePipeTransport._force_closec Cs^z|j |¡Wdt|jdƒr,|j tj¡|j ¡d|_|j}|dk rX|  ¡d|_XdS)NÚshutdown) rZconnection_lostZhasattrrrIÚsocketZ SHUT_RDWRr7rZ_detach)rrDr#r&r&r'r5„s  z0_ProactorBasePipeTransport._call_connection_lostcCs"|j}|jdk r|t|jƒ7}|Sr/)rrr*)rZsizer&r&r'Úget_write_buffer_size•s z0_ProactorBasePipeTransport.get_write_buffer_size©NNN)r:)r(Ú __module__Ú __qualname__Ú__doc__r r+rrr3r4r7r9rErCr5rKÚ __classcell__r&r&r$r'r s  r csTeZdZdZd‡fdd„ Zdd„Zdd„Zd d „Zd d „Zd d„Z ddd„Z ‡Z S)Ú_ProactorReadPipeTransportzTransport for read pipes.Ncs:d|_d|_tƒ ||||||¡|j |j¡d|_dS)NTF)Ú _pending_dataÚ_pausedr r rrÚ _loop_readingrr$r&r'r  s z#_ProactorReadPipeTransport.__init__cCs|j o|j Sr/)rSrr2r&r&r'Ú is_reading©r.z%_ProactorReadPipeTransport.is_readingcCs0|js |jrdSd|_|j ¡r,t d|¡dS)NTz%r pauses reading)rrSrr@r rAr2r&r&r'Ú pause_reading¬s   z(_ProactorReadPipeTransport.pause_readingcCsn|js |jsdSd|_|jdkr0|j |jd¡|j}d|_|dk rT|j |j|¡|j ¡rjt   d|¡dS)NFz%r resumes reading) rrSrrrrTrRÚ_data_receivedr@r rA©rÚdatar&r&r'Úresume_reading¿s   z)_ProactorReadPipeTransport.resume_readingc Csd|j ¡rt d|¡y|j ¡}Wn.tk rR}z| |d¡dSd}~XYnX|s`| ¡dS)Nz%r received EOFz1Fatal error: protocol.eof_received() call failed.) rr@r rArZ eof_receivedÚ ExceptionrEr7)rZ keep_openrDr&r&r'Ú _eof_receivedÑs  z(_ProactorReadPipeTransport._eof_receivedc Cs‚|jr||_dS|s | ¡dSt|jtjƒrryt |j|¡Wq~tk rn}z|  |d¡dSd}~XYq~Xn |j  |¡dS)Nz3Fatal error: protocol.buffer_updated() call failed.) rSrRr\r>rrZBufferedProtocolZ_feed_data_to_buffered_protor[rEZ data_received)rrYrDr&r&r'rWßsz)_ProactorReadPipeTransport._data_receivedc Csdd}zBy`|dk r2d|_| ¡r*| ¡}n| ¡|jr@d}dS|dkrLdS|jsf|jj |j d¡|_WnÆt k r¶}z0|jsŽ|  |d¡n|j  ¡r¦t jdddWdd}~XYntk rà}z| |¡Wdd}~XYnftk r}z|  |d¡Wdd}~XYn8tjk r.|js*‚YnX|jsF|j |j¡Wd|dk r^| |¡XdS)Nsi€z"Fatal read error on pipe transportz*Read error on pipe transport while closingTr;)rrGÚresultr6rrSrÚ _proactorÚrecvrZConnectionAbortedErrorrEr@r rAÚConnectionResetErrorrCr?rÚCancelledErrorÚadd_done_callbackrTrW)rÚfutrYrDr&r&r'rTös>   z(_ProactorReadPipeTransport._loop_readingrLr/) r(rMrNrOr rUrVrZr\rWrTrPr&r&r$r'rQœsrQcs^eZdZdZdZ‡fdd„Zdd„Zddd „Zd d „Zd d „Z dd„Z dd„Z dd„Z ‡Z S)Ú_ProactorBaseWritePipeTransportzTransport for write pipes.Tcstƒj||Žd|_dSr/)r r rF©rZargsZkwr$r&r'r .sz(_ProactorBaseWritePipeTransport.__init__cCsÈt|tttfƒs$tdt|ƒj›ƒ‚|jr2tdƒ‚|j dk rDtdƒ‚|sLdS|j rz|j t j krht  d¡|j d7_ dS|jdkr–|jt|ƒdn.|js°t|ƒ|_| ¡n|j |¡| ¡dS)Nz/data argument must be a bytes-like object, not zwrite_eof() already calledz(unable to write; sendfile is in progresszsocket.send() raised exception.r)rY)r>ZbytesZ bytearrayZ memoryviewZ TypeErrorZtyper(rÚ RuntimeErrorrFrrZ!LOG_THRESHOLD_FOR_CONNLOST_WRITESr ÚwarningrÚ _loop_writingrÚ_maybe_pause_protocolZextendrXr&r&r'Úwrite2s*       z%_ProactorBaseWritePipeTransport.writeNc CsTyò|dk r|jdkr|jrdSd|_d|_|r6| ¡|dkrJ|j}d|_|s‚|jrd|j |jd¡|jrx|j   t j ¡|  ¡nN|jj |j |¡|_|j ¡sÂt|ƒ|_|j |j¡| ¡n|j |j¡|jdk rð|jdkrð|j d¡Wn\tk r }z| |¡Wdd}~XYn0tk rN}z| |d¡Wdd}~XYnXdS)Nrz#Fatal write error on pipe transport)rrrr]rrrr5rrrIrJÚSHUT_WRZ_maybe_resume_protocolr^ÚsendrGr*rbrhrirFrHr`rCr?rE)rÚfrYrDr&r&r'rhXs8    z-_ProactorBaseWritePipeTransport._loop_writingcCódS©NTr&r2r&r&r'Ú can_write_eofr.z-_ProactorBaseWritePipeTransport.can_write_eofcCs | ¡dSr/)r7r2r&r&r'Ú write_eof„r.z)_ProactorBaseWritePipeTransport.write_eofcCs| d¡dSr/)rCr2r&r&r'Úabort‡r.z%_ProactorBaseWritePipeTransport.abortcCs:|jdk rtdƒ‚|j ¡|_|jdkr4|j d¡|jS)NzEmpty waiter is already set)rFrfrZ create_futurerrHr2r&r&r'Ú_make_empty_waiterŠs     z2_ProactorBaseWritePipeTransport._make_empty_waitercCs d|_dSr/)rFr2r&r&r'Ú_reset_empty_waiter’r.z3_ProactorBaseWritePipeTransport._reset_empty_waiter©NN)r(rMrNrOZ_start_tls_compatibler rjrhrprqrrrsrtrPr&r&r$r'rd(s & )rdcs$eZdZ‡fdd„Zdd„Z‡ZS)Ú_ProactorWritePipeTransportcs4tƒj||Ž|jj |jd¡|_|j |j¡dS)Ni) r r rr^r_rrrbÚ _pipe_closedrer$r&r'r —sz$_ProactorWritePipeTransport.__init__cCs@| ¡r dS|jrdSd|_|jdk r4| tƒ¡n| ¡dSr/)Z cancelledrrrrCZBrokenPipeErrorr7)rrcr&r&r'rwœs z(_ProactorWritePipeTransport._pipe_closed)r(rMrNr rwrPr&r&r$r'rv–s rvc@s eZdZdZdd„Zdd„ZdS)Ú_ProactorDuplexPipeTransportzTransport for duplex pipes.cCrn)NFr&r2r&r&r'rp±r.z*_ProactorDuplexPipeTransport.can_write_eofcCst‚dSr/)ZNotImplementedErrorr2r&r&r'rq´r.z&_ProactorDuplexPipeTransport.write_eofN)r(rMrNrOrprqr&r&r&r'rx¬srxcsBeZdZdZejjZd ‡fdd„ Zdd„Z dd„Z d d „Z ‡Z S) Ú_ProactorSocketTransportz Transport for connected sockets.Ncs$tƒ ||||||¡t |¡dSr/)r r rZ _set_nodelayrr$r&r'r ¿sz!_ProactorSocketTransport.__init__c Cs¨||jd<y| ¡|jd<Wn4tjtfk rP|j ¡rLtjd|ddYnXd|jkr¤y|  ¡|jd<Wn4tjtfk r¢|j ¡ržtjd|ddYnXdS)NrJZsocknamezgetsockname() failed on %rTr;Úpeernamezgetpeername() failed on %r) r,Z getsocknamerJZerrorÚAttributeErrorrr@r rgZ getpeernamer-r&r&r'rÄs    z#_ProactorSocketTransport._set_extracCrnror&r2r&r&r'rpÖr.z&_ProactorSocketTransport.can_write_eofcCs2|js |jrdSd|_|jdkr.|j tj¡dSro)rrrrrIrJrkr2r&r&r'rqÙs   z"_ProactorSocketTransport.write_eofrL) r(rMrNrOrZ _SendfileModeZ TRY_NATIVEZ_sendfile_compatibler rrprqrPr&r&r$r'ry¸srycsäeZdZ‡fdd„Zd1dd„Zd2ddddddœdd „Zd3d d „Zd4d d „Zd5dd„Z‡fdd„Z dd„Z dd„Z dd„Z dd„Z dd„Zdd„Zdd„Zd d!„Zd"d#„Zd6d$d%„Zd&d'„Zd7d)d*„Zd+d,„Zd-d.„Zd/d0„Z‡ZS)8rcsHtƒ ¡t d|jj¡||_||_d|_i|_ |  |¡|  ¡dS)NzUsing proactor: %s) r r r rAr%r(r^Ú _selectorÚ_self_reading_futureÚ_accept_futuresZset_loopÚ_make_self_pipe)rZproactorr$r&r'r ãs  zBaseProactorEventLoop.__init__NcCst||||||ƒSr/)ry)rrr r!r"r#r&r&r'Ú_make_socket_transportís z,BaseProactorEventLoop._make_socket_transportF)Ú server_sideÚserver_hostnamer"r#Ússl_handshake_timeoutc Cs0tj||||||| d} t||| ||d| jS)N)rƒ©r"r#)rZ SSLProtocolryZ_app_transport) rZrawsockr Ú sslcontextr!rr‚r"r#rƒZ ssl_protocolr&r&r'Ú_make_ssl_transportòs z)BaseProactorEventLoop._make_ssl_transportcCót|||||ƒSr/)rx©rrr r!r"r&r&r'Ú_make_duplex_pipe_transportÿsz1BaseProactorEventLoop._make_duplex_pipe_transportcCr‡r/)rQrˆr&r&r'Ú_make_read_pipe_transportóz/BaseProactorEventLoop._make_read_pipe_transportcCr‡r/)rvrˆr&r&r'Ú_make_write_pipe_transportsz0BaseProactorEventLoop._make_write_pipe_transportcsP| ¡rtdƒ‚| ¡rdS| ¡| ¡|j ¡d|_d|_tƒ ¡dS)Nz!Cannot close a running event loop) Z is_runningrfÚ is_closedÚ_stop_accept_futuresÚ_close_self_piper^r7r|r r2r$r&r'r7s zBaseProactorEventLoop.closecÃó|j ||¡IdHSr/)r^r_)rrZnr&r&r'Ú sock_recv r.zBaseProactorEventLoop.sock_recvcÃrr/)r^Z recv_into)rrZbufr&r&r'Úsock_recv_into#r.z$BaseProactorEventLoop.sock_recv_intocÃrr/)r^rl)rrrYr&r&r'Ú sock_sendall&r.z"BaseProactorEventLoop.sock_sendallcÃrr/)r^Zconnect)rrZaddressr&r&r'Ú sock_connect)r.z"BaseProactorEventLoop.sock_connectcÃs|j |¡IdHSr/)r^Úacceptr-r&r&r'Ú sock_accept,r.z!BaseProactorEventLoop.sock_acceptc Ãs(y | ¡}Wn2ttjfk r>}zt d¡‚Wdd}~XYnXyt |¡j}Wn,t k r|}zt d¡‚Wdd}~XYnX|r†|n|}|s’dSt |dƒ}|r®t |||ƒn|} t ||ƒ}d} zLxFt | ||ƒ}|dkrÞ| S|j   ||||¡IdH||7}| |7} qÄWWd| dkr"|  |¡XdS)Nznot a regular filerlÿÿ)r)r{ÚioZUnsupportedOperationrZSendfileNotAvailableErrorÚosZfstatZst_sizer?Zminr^ZsendfileZseek) rrÚfileÚoffsetÚcountr)ZerrZfsizeZ blocksizeZend_posZ total_sentr&r&r'Ú_sock_sendfile_native/s2     z+BaseProactorEventLoop._sock_sendfile_nativecÃsV| ¡}| ¡| ¡IdHz|j|j|||ddIdHS| ¡|rP| ¡XdS)NF)Zfallback)rUrVrsZ sock_sendfilerrtrZ)rZtranspr™ršr›rZr&r&r'Ú_sendfile_nativeLsz&BaseProactorEventLoop._sendfile_nativecCsL|jdk r|j ¡d|_|j ¡d|_|j ¡d|_|jd8_dS)Nr)r}r6Ú_ssockr7Ú_csockÚ _internal_fdsr2r&r&r'rXs    z&BaseProactorEventLoop._close_self_pipecCsFt ¡\|_|_|j d¡|j d¡|jd7_| |j¡dS)NFr)rJZ socketpairržrŸZ setblockingr rÚ_loop_self_readingr2r&r&r'rbs   z%BaseProactorEventLoop._make_self_pipec Cs„y$|dk r| ¡|j |jd¡}WnHtjk r:dStk rl}z| d||dœ¡Wdd}~XYnX||_|  |j ¡dS)Niz.Error on reading from the event loop self pipe)r<r=r) r]r^r_ržrrar[rBr}rbr¡)rrmrDr&r&r'r¡jsz(BaseProactorEventLoop._loop_self_readingcCs>y|j d¡Wn(tk r8|jr4tjdddYnXdS)Nsz3Fail to write a null byte into the self-pipe socketTr;)rŸrlr?Ú_debugr rAr2r&r&r'Ú_write_to_self|s z$BaseProactorEventLoop._write_to_selfédcs(d‡‡‡‡‡‡‡fdd„ ‰ˆ ˆ¡dS)Nc s$yˆ|dk rn| ¡\}}ˆjr,t dˆ||¡ˆƒ}ˆdk rXˆj||ˆdd|iˆˆdnˆj||d|iˆdˆ ¡rzdSˆj ˆ¡}Wn~t k rè}zBˆ  ¡dkrˆ  d|ˆdœ¡ˆ  ¡nˆjrØtjd ˆdd Wdd}~XYn8t jk rˆ  ¡YnX|ˆjˆ  ¡<| ˆ¡dS) Nz#%r got a new connection from %r: %rTrz)rr"r#rƒr„iÿÿÿÿzAccept failed on a socket)r<r=rJzAccept failed on socket %rr;)r]r¢r rAr†r€rr^r•r?r)rBr7rrar~rb)rmZconnZaddrr rD©rÚprotocol_factoryrr#rrƒr…r&r'r‰s@      z2BaseProactorEventLoop._start_serving..loopr/)r)rr¦rr…r#Zbacklogrƒr&r¥r'Ú_start_serving…s%z$BaseProactorEventLoop._start_servingcCsdSr/r&)rZ event_listr&r&r'Ú_process_events°r‹z%BaseProactorEventLoop._process_eventscCs*x|j ¡D] }| ¡q W|j ¡dSr/)r~Zvaluesr6Zclear)rÚfuturer&r&r'rŽ´s z*BaseProactorEventLoop._stop_accept_futurescCs6|j | ¡d¡}|r| ¡|j |¡| ¡dSr/)r~Zpopr)r6r^Ú _stop_servingr7)rrr©r&r&r'rª¹s  z#BaseProactorEventLoop._stop_servingrLr/rururur/)NNr¤N)r(rMrNr r€r†r‰rŠrŒr7r‘r’r“r”r–rœrrrr¡r£r§r¨rŽrªrPr&r&r$r'rás<            )r)rOZ__all__r—r˜rJr8Zrrrrrrr Zlogr Z_FlowControlMixinZ BaseTransportr Z ReadTransportrQZWriteTransportrdrvZ TransportrxryZ BaseEventLooprr&r&r&r'Zs:          m  '