a `N@s,dZdZddlZddlZddlZddlZddlZddlZddlZz ddl Z Wne ybdZ Yn0ddl m Z ddl m Z ddl mZddl mZdd l mZdd l mZdd l mZdd l mZdd lmZddZddZGddde jZGdddejejZGdddeZGdddeZdS)zEvent loop using a selector and related classes. A selector is a "notify-when-ready" multiplexer. For a subclass which also includes support for signal handling, see the unix_events sub-module. )BaseSelectorEventLoopN) base_events) constants)events)futures) protocols)sslproto) transports)trsock)loggercCs6z||}Wnty"YdS0t|j|@SdSNF)get_keyKeyErrorboolr)selectorfdeventkeyr:/usr/local/src/Python-3.9.6/Lib/asyncio/selector_events.py_test_selector_event s  rcCs tdurt|tjrtddS)Nz"Socket cannot be of type SSLSocket)ssl isinstanceZ SSLSocket TypeError)sockrrr_check_ssl_socket+srcseZdZdZdSfdd ZdTdddddZdUddddejd d d ZdVd d Z fddZ ddZ ddZ ddZ ddZddZdddejfddZdddejfddZddejfdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4ZdWd5d6Zd7d8Zd9d:Zd;d<Z d=d>Z!d?d@Z"dAdBZ#dCdDZ$dXdEdFZ%dGdHZ&dIdJZ'dKdLZ(dMdNZ)dOdPZ*dQdRZ+Z,S)YrzJSelector event loop. See events.EventLoop for API specification. NcsFt|durt}td|jj||_| t |_ dS)NzUsing selector: %s) super__init__ selectorsDefaultSelectorr debug __class____name__ _selector_make_self_pipeweakrefWeakValueDictionary _transports)selfrr"rrr6s zBaseSelectorEventLoop.__init__extraservercCst||||||SN)_SelectorSocketTransport)r)rprotocolwaiterr,r-rrr_make_socket_transport@s z,BaseSelectorEventLoop._make_socket_transportF) server_sideserver_hostnamer,r-ssl_handshake_timeoutc Cs0tj||||||| d} t||| ||d| jS)N)r5r+)r SSLProtocolr/_app_transport) r)rawsockr0 sslcontextr1r3r4r,r-r5 ssl_protocolrrr_make_ssl_transportEsz)BaseSelectorEventLoop._make_ssl_transportcCst||||||Sr.)_SelectorDatagramTransport)r)rr0addressr1r,rrr_make_datagram_transportRsz.BaseSelectorEventLoop._make_datagram_transportcsL|rtd|rdS|t|jdurH|jd|_dS)Nz!Cannot close a running event loop) is_running RuntimeError is_closed_close_self_pipercloser$r)r*rrrCWs   zBaseSelectorEventLoop.closecCsB||j|jd|_|jd|_|jd8_dS)Nr)_remove_reader_ssockfilenorC_csock _internal_fdsrDrrrrBbs   z&BaseSelectorEventLoop._close_self_pipecCsNt\|_|_|jd|jd|jd7_||j|jdS)NFr) socket socketpairrFrH setblockingrI _add_readerrG_read_from_selfrDrrrr%js   z%BaseSelectorEventLoop._make_self_pipecCsdSr.rr)datarrr_process_self_datarsz(BaseSelectorEventLoop._process_self_datacCsTz"|jd}|sWqP||Wqty8YqYqtyLYqPYq0qdS)Ni)rFrecvrQInterruptedErrorBlockingIOErrorrOrrrrNus   z%BaseSelectorEventLoop._read_from_selfcCsL|j}|durdSz|dWn&tyF|jrBtjdddYn0dS)Nz3Fail to write a null byte into the self-pipe socketTexc_info)rHsendOSError_debugr r!)r)Zcsockrrr_write_to_selfs z$BaseSelectorEventLoop._write_to_selfdc Cs"|||j||||||dSr.)rMrG_accept_connection)r)protocol_factoryrr9r-backlogr5rrr_start_servings z$BaseSelectorEventLoop._start_servingc Cst|D]}z0|\}} |jr0td|| ||dWntttfyXYdSt y} zl| j t j t j t j t jfvr|d| t|d|||tj|j||||||nWYd} ~ qd} ~ 00d| i} |||| |||} || qdS)Nz#%r got a new connection from %r: %rFz&socket.accept() out of system resource)message exceptionrJpeername)rangeacceptrZr r!rLrTrSConnectionAbortedErrorrYerrnoZEMFILEZENFILEZENOBUFSZENOMEMcall_exception_handlerr TransportSocketrErG call_laterrACCEPT_RETRY_DELAYr`_accept_connection2 create_task) r)r^rr9r-r_r5_connaddrexcr,rerrrr]s@    z(BaseSelectorEventLoop._accept_connectionc sd}d}zr|}|} |r8|j|||| d|||d}n|j||| ||d}z| IdHWntyv|Yn0WntttfyYn^ty} zF|jrd| d} |dur|| d<|dur|| d<|| WYd} ~ n d} ~ 00dS)NT)r1r3r,r-r5)r1r,r-z3Error on transport creation for incoming connection)rarbr0 transport) create_futurer;r2 BaseExceptionrC SystemExitKeyboardInterruptrZrh) r)r^ror,r9r-r5r0rrr1rqcontextrrrrls@  z)BaseSelectorEventLoop._accept_connection2c Cs|}t|tsHzt|}Wn(tttfyFtd|dYn0z|j|}WntyhYn0|st d|d|dS)NzInvalid file object: zFile descriptor z is used by transport ) rintrGAttributeErrorr ValueErrorr(r is_closingr@)r)rrGrrrrr_ensure_fd_no_transports   z-BaseSelectorEventLoop._ensure_fd_no_transportc Gs|t|||d}z|j|}Wn(tyP|j|tj|dfYn>0|j|j }\}}|j ||tjB||f|dur| |Sr.) _check_closedrHandler$rrregisterr EVENT_READrPmodifycancel r)rcallbackargshandlermaskreaderwriterrrrrMs   z!BaseSelectorEventLoop._add_readercCs|r dSz|j|}Wnty0YdS0|j|j}\}}|tjM}|sb|j|n|j ||d|f|dur| dSdSdS)NFT) rAr$rrrrPrr unregisterrrr)rrrrrrrrrEs  z$BaseSelectorEventLoop._remove_readerc Gs|t|||d}z|j|}Wn(tyP|j|tjd|fYn>0|j|j }\}}|j ||tjB||f|dur| |Sr.) r}rr~r$rrrr EVENT_WRITErPrrrrrr _add_writer&s   z!BaseSelectorEventLoop._add_writercCs|r dSz|j|}Wnty0YdS0|j|j}\}}|tjM}|sb|j|n|j |||df|dur| dSdSdS)Remove a writer callback.FNT) rAr$rrrrPrrrrrrrrr_remove_writer6s  z$BaseSelectorEventLoop._remove_writercGs"|||j||g|RdS)zAdd a reader callback.N)r|rMr)rrrrrr add_readerMs z BaseSelectorEventLoop.add_readercCs||||S)zRemove a reader callback.)r|rEr)rrrr remove_readerRs z#BaseSelectorEventLoop.remove_readercGs"|||j||g|RdS)zAdd a writer callback..N)r|rrrrr add_writerWs z BaseSelectorEventLoop.add_writercCs||||S)r)r|rrrrr remove_writer\s z#BaseSelectorEventLoop.remove_writerc st||jr"|dkr"tdz ||WSttfyDYn0|}|}| || ||j |||}| t j|j||d|IdHS)zReceive data from the socket. The return value is a bytes object representing the data received. The maximum amount of data to be received at once is specified by nbytes. rthe socket must be non-blockingrN)rrZ gettimeoutrzrRrTrSrsrGr|rM _sock_recvadd_done_callback functoolspartial_sock_read_done)r)rnfutrrrrr sock_recvas  zBaseSelectorEventLoop.sock_recvcCs|dus|s||dSr.) cancelledrr)rrrrrrrwsz%BaseSelectorEventLoop._sock_read_donec Cs|r dSz||}WnZttfy2YdSttfyHYn8tyt}z||WYd}~nd}~00||dSr.) donerRrTrSrurvrt set_exception set_result)r)rrrrPrqrrrr{s z BaseSelectorEventLoop._sock_recvc st||jr"|dkr"tdz ||WSttfyDYn0|}|}| || ||j |||}| t j|j||d|IdHS)zReceive data from the socket. The received data is written into *buf* (a writable buffer). The return value is the number of bytes written. rrrN)rrZrrz recv_intorTrSrsrGr|rM_sock_recv_intorrrr)r)rbufrrrrrrsock_recv_intos  z$BaseSelectorEventLoop.sock_recv_intoc Cs|r dSz||}WnZttfy2YdSttfyHYn8tyt}z||WYd}~nd}~00||dSr.) rrrTrSrurvrtrr)r)rrrnbytesrqrrrrs z%BaseSelectorEventLoop._sock_recv_intoc st||jr"|dkr"tdz||}WnttfyJd}Yn0|t|kr\dS|}| }| || ||j ||t ||g}|tj|j||d|IdHS)aSend data to the socket. The socket must be connected to a remote socket. This method continues to send data from data until either all data has been sent or an error occurs. None is returned on success. On error, an exception is raised, and there is no way to determine how much data, if any, was successfully processed by the receiving end of the connection. rrNr)rrZrrzrXrTrSlenrsrGr|r _sock_sendall memoryviewrrr_sock_write_done)r)rrPrrrrrrr sock_sendalls&     z"BaseSelectorEventLoop.sock_sendallc Cs|r dS|d}z|||d}Wn\ttfyBYdSttfyXYn0ty}z||WYd}~dSd}~00||7}|t|kr| dn||d<dS)Nr) rrXrTrSrurvrtrrr)r)rrviewposstartrrqrrrrs    z#BaseSelectorEventLoop._sock_sendallcst||jr"|dkr"tdttdr8|jtjkrf|j||j|j |dIdH}|d\}}}}}| }| ||||IdHS)zTConnect to a remote socket at address. This method is a coroutine. rrAF_UNIX)familyprotoloopN) rrZrrzhasattrrJrr_ensure_resolvedrrs _sock_connect)r)rr=resolvedrnrrrr sock_connects z"BaseSelectorEventLoop.sock_connectc Cs|}z||Wnttfyb|||||j|||}|tj |j ||dYnNt t fyxYn8t y}z||WYd}~nd}~00|ddS)Nr)rGconnectrTrSr|r_sock_connect_cbrrrrrurvrtrr)r)rrr=rrrqrrrrs    z#BaseSelectorEventLoop._sock_connectcCs|dus|s||dSr.)rrrrrrr sz&BaseSelectorEventLoop._sock_write_donec Cs|r dSz,|tjtj}|dkr6t|d|WnXttfyNYnNtt fydYn8t y}z| |WYd}~nd}~00| ddS)NrzConnect call failed ) r getsockoptrJ SOL_SOCKETSO_ERRORrYrTrSrurvrtrr)r)rrr=errrqrrrrs z&BaseSelectorEventLoop._sock_connect_cbcs@t||jr"|dkr"td|}||||IdHS)aWAccept a connection. The socket must be bound to an address and listening for connections. The return value is a pair (conn, address) where conn is a new socket object usable to send and receive data on the connection, and address is the address bound to the socket on the other end of the connection. rrN)rrZrrzrs _sock_accept)r)rrrrr sock_accept$s  z!BaseSelectorEventLoop.sock_acceptc Cs|}z|\}}|dWnttfyl|||||j||}|t j |j ||dYnRt t fyYn<ty}z||WYd}~nd}~00|||fdS)NFr)rGrerLrTrSr|rMrrrrrrurvrtrr)r)rrrror=rrqrrrr3s   z"BaseSelectorEventLoop._sock_acceptc s|j|j=|}||IdHzL|j|j|||ddIdHW||rZ|||j|j<S||j|j<n"||r|||j|j<0dS)NF)fallback) r(_sock_fd is_reading pause_reading_make_empty_waiter sock_sendfile_sock_reset_empty_waiterresume_reading)r)transpfileoffsetcountrrrr_sendfile_nativeDs*   z&BaseSelectorEventLoop._sendfile_nativecCs|D]v\}}|j|j}\}}|tj@rL|durL|jrB||n |||tj@r|dur|jrp||q||qdSr.) fileobjrPrr _cancelledrE _add_callbackrr)r) event_listrrrrrrrr_process_eventsRs    z%BaseSelectorEventLoop._process_eventscCs|||dSr.)rErGrC)r)rrrr _stop_serving`sz#BaseSelectorEventLoop._stop_serving)N)N)N)NNN)N)N)-r# __module__ __qualname____doc__rr2rSSL_HANDSHAKE_TIMEOUTr;r>rCrBr%rQrNr[r`r]rlr|rMrErrrrrrrrrrrrrrrrrrrrrr __classcell__rrr*rr0sj        . )  rcseZdZdZeZdZdfdd ZddZddZ d d Z d d Z d dZ ddZ ejfddZdddZddZddZddZddZZS) _SelectorTransportiNcst||t||jd<z||jd<WntyLd|jd<Yn0d|jvrz||jd<Wntj yd|jd<Yn0||_ | |_ d|_ ||||_||_d|_d|_|jdur|j||j|j <dS)NrJZsocknamercFr)rrr ri_extra getsocknamerY getpeernamerJerrorrrGr_protocol_connected set_protocol_server_buffer_factory_buffer _conn_lost_closing_attachr()r)rrr0r,r-r*rrrqs,       z_SelectorTransport.__init__cCs|jjg}|jdur |dn|jr0|d|d|j|jdur|jst|jj |jt j }|rz|dn |dt|jj |jt j }|rd}nd}| }|d|d |d d d |S) Nclosedclosingzfd=z read=pollingz read=idlepollingidlezwrite=z<{}> )r"r#rappendrr_looprArr$rrrget_write_buffer_sizeformatjoin)r)inforstatebufsizerrr__repr__s.      z_SelectorTransport.__repr__cCs|ddSr.) _force_closerDrrrabortsz_SelectorTransport.abortcCs||_d|_dSNT) _protocolrr)r0rrrrsz_SelectorTransport.set_protocolcCs|jSr.)rrDrrr get_protocolsz_SelectorTransport.get_protocolcCs|jSr.)rrDrrrr{sz_SelectorTransport.is_closingcCsT|jr dSd|_|j|j|jsP|jd7_|j|j|j|jddSNTr) rrrErrrr call_soon_call_connection_lostrDrrrrCsz_SelectorTransport.closecCs,|jdur(|d|t|d|jdS)Nzunclosed transport )source)rResourceWarningrC)r)_warnrrr__del__s z_SelectorTransport.__del__Fatal error on transportcCsNt|tr(|jr@tjd||ddn|j||||jd||dS)Nz%r: %sTrV)rarbrrr0) rrYr get_debugr r!rhrr)r)rqrarrr _fatal_errors  z_SelectorTransport._fatal_errorcCsd|jr dS|jr(|j|j|j|jsBd|_|j|j|jd7_|j|j |dSr) rrclearrrrrrErrr)rqrrrrs z_SelectorTransport._force_closecCszN|jr|j|W|jd|_d|_d|_|j}|dur|d|_n:|jd|_d|_d|_|j}|dur|d|_0dSr.)rrconnection_lostrrCrr_detach)r)rqr-rrrrs&  z(_SelectorTransport._call_connection_lostcCs t|jSr.)rrrDrrrrsz(_SelectorTransport.get_write_buffer_sizecGs$|jr dS|jj||g|RdSr.)rrrMrrrrrMsz_SelectorTransport._add_reader)NN)r)r#rrmax_size bytearrayrrrrrrrr{rCwarningswarnrrrrrrMrrrr*rres    rcseZdZdZejjZd#fdd ZfddZ ddZ d d Z d d Z d dZ ddZddZddZddZddZddZddZfddZdd Zd!d"ZZS)$r/TNcs~d|_t|||||d|_d|_d|_t|j|j |j j ||j |j |j|j|durz|j tj|ddSr )_read_ready_cbrr_eof_paused _empty_waiterr _set_nodelayrrrrconnection_maderMr _read_readyr_set_result_unless_cancelled)r)rrr0r1r,r-r*rrrs   z!_SelectorSocketTransport.__init__cs.t|tjr|j|_n|j|_t|dSr.)rrBufferedProtocol_read_ready__get_bufferr_read_ready__data_receivedrrrr*rrrs  z%_SelectorSocketTransport.set_protocolcCs|j o|j Sr.)rrrDrrrrsz#_SelectorSocketTransport.is_readingcCs>|js |jrdSd|_|j|j|jr:td|dS)NTz%r pauses reading)rrrrErrr r!rDrrrrs   z&_SelectorSocketTransport.pause_readingcCs@|js |jsdSd|_||j|j|jr      z_SelectorSocketTransport.writec Cs&|jsJd|jrdSz|j|j}Wnttfy@YnttfyVYnty}zF|j |j |j | |d|jdur|j|WYd}~nxd}~00|r|jd|=||js"|j |j |jdur|jd|jr |dn|jr"|jtjdS)NzData should not be emptyr&)rrrrXrTrSrurvrtrrrr rrr_maybe_resume_protocolrrrrshutdownrJSHUT_WR)r)rrqrrrr+s4   "    z%_SelectorSocketTransport._write_readycCs.|js |jrdSd|_|js*|jtjdSr)rrrrr0rJr1rDrrr write_eofs  z"_SelectorSocketTransport.write_eofcCsdSrrrDrrr can_write_eofsz&_SelectorSocketTransport.can_write_eofcs*t||jdur&|jtddS)NzConnection is closed by peer)rrrrConnectionErrorr r*rrrs   z._SelectorSocketTransport._call_connection_lostcCs6|jdurtd|j|_|js0|jd|jS)NzEmpty waiter is already set)rr@rrsrrrDrrrrs    z+_SelectorSocketTransport._make_empty_waitercCs d|_dSr.)rrDrrrrsz,_SelectorSocketTransport._reset_empty_waiter)NNN)r#rr_start_tls_compatibler _SendfileMode TRY_NATIVE_sendfile_compatiblerrrrrrrrrr.r+r2r3rrrrrrr*rr/s( %' r/csFeZdZejZd fdd ZddZddZd dd Z d d Z Z S)r<Ncs^t||||||_|j|jj||j|j|j|j |durZ|jt j |ddSr.) rr_addressrrrrrMrrrr)r)rrr0r=r1r,r*rrrs  z#_SelectorDatagramTransport.__init__cCstdd|jDS)Ncss|]\}}t|VqdSr.)r).0rPrnrrr zC_SelectorDatagramTransport.get_write_buffer_size..)sumrrDrrrrsz0_SelectorDatagramTransport.get_write_buffer_sizec Cs|jr dSz|j|j\}}Wnttfy6Yntyd}z|j|WYd}~n\d}~0t t fyzYn>t y}z| |dWYd}~nd}~00|j ||dS)Nz&Fatal read error on datagram transport)rrrecvfromr rTrSrYrerror_receivedrurvrtrdatagram_receivedr)rPrprqrrrrs "z&_SelectorDatagramTransport._read_readyc Cst|tttfs$tdt|j|s,dS|jrV|d|jfvrPtd|j|j}|j r|jr|j t j krxt d|j d7_ dS|jsdz,|jdr|j|n|j||WdSttfy|j|j|jYn~ty}z|j|WYd}~dSd}~0ttfy0Yn4tyb}z||dWYd}~dSd}~00|j t||f|!dS)Nr$z!Invalid address: must be None or r%rrc'Fatal write error on datagram transport)"rr'rrrr(r#r9rzrrr)r r*rrrrXsendtorTrSrrr _sendto_readyrYrr?rurvrtrrr-rArrrrCsJ      z!_SelectorDatagramTransport.sendtoc Cs|jr|j\}}z*|jdr.|j|n|j||Wqttfyh|j||fYqYqt y}z|j |WYd}~dSd}~0t t fyYqty}z||dWYd}~dSd}~00q||js|j|j|jr|ddS)NrcrB)rpopleftrrrXrCrTrS appendleftrYrr?rurvrtrr/rrrrrrArrrrD2s0  z(_SelectorDatagramTransport._sendto_ready)NNN)N) r#rr collectionsdequerrrrrCrDrrrr*rr<s  +r<)r__all__rGrgrrrJrr&r ImportErrorrrrrrr r r logr rr BaseEventLoopr_FlowControlMixin Transportrr/r<rrrrsF             9o