o ?Og@s$dZdZddlZddlZddlZddlZddlZddlZddlZzddl Z Wn e y1dZ Ynwddl m Z ddl m Z ddl mZddl mZdd l mZdd l mZdd l mZdd l mZdd lmZddZGddde 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)loggercCs2z||}Wn tyYdSwt|j|@SNF)get_keyKeyErrorboolr)selectorfdZeventkeyr>/opt/alt/python310/lib64/python3.10/asyncio/selector_events.py_test_selector_event s  rcseZdZdZdSfdd ZdSdddddZ dSddddejd d d Z dTd d Z fddZ ddZ ddZ ddZ ddZddZdddejfddZdddejfddZddejfdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4ZdSd5d6Zd7d8Zd9d:Zd;d<Z d=d>Z!d?d@Z"dAdBZ#dCdDZ$dSdEdFZ%dGdHZ&dIdJZ'dKdLZ(dMdNZ)dOdPZ*dQdRZ+Z,S)UrzJSelector event loop. See events.EventLoop for API specification. NcsFt|dur t}td|jj||_| t |_ dS)NzUsing selector: %s) super__init__ selectorsZDefaultSelectorr debug __class____name__ _selector_make_self_pipeweakrefZWeakValueDictionary _transports)selfrrrrr1s zBaseSelectorEventLoop.__init__extraservercCt||||||SN)_SelectorSocketTransport)r!sockprotocolwaiterr$r%rrr_make_socket_transport;s z,BaseSelectorEventLoop._make_socket_transportF) server_sideserver_hostnamer$r%ssl_handshake_timeoutc Cs0tj||||||| d} t||| ||d| jS)N)r/r#)r Z SSLProtocolr(Z_app_transport) r!Zrawsockr* sslcontextr+r-r.r$r%r/Z ssl_protocolrrr_make_ssl_transport@sz)BaseSelectorEventLoop._make_ssl_transportcCr&r')_SelectorDatagramTransport)r!r)r*addressr+r$rrr_make_datagram_transportMsz.BaseSelectorEventLoop._make_datagram_transportcsP|rtd|rdS|t|jdur&|jd|_dSdS)Nz!Cannot close a running event loop)Z is_running RuntimeError is_closed_close_self_pipercloserr!r"rrr8Rs    zBaseSelectorEventLoop.closecCsB||j|jd|_|jd|_|jd8_dS)Nr)_remove_reader_ssockfilenor8_csock _internal_fdsr9rrrr7]s   z&BaseSelectorEventLoop._close_self_pipecCsNt\|_|_|jd|jd|jd7_||j|jdS)NFr) socketZ socketpairr;r= setblockingr> _add_readerr<_read_from_selfr9rrrres   z%BaseSelectorEventLoop._make_self_pipecCsdSr'rr!datarrr_process_self_datamz(BaseSelectorEventLoop._process_self_datacCsN z|jd}|s WdS||WntyYqty%YdSwq)NTi)r;recvrEInterruptedErrorBlockingIOErrorrCrrrrBps   z%BaseSelectorEventLoop._read_from_selfcCsR|j}|dur dSz|dWdSty(|jr%tjdddYdSYdSw)Nz3Fail to write a null byte into the self-pipe socketTexc_info)r=sendOSError_debugr r)r!Zcsockrrr_write_to_self|s  z$BaseSelectorEventLoop._write_to_selfdc Cs"|||j||||||dSr')rAr<_accept_connection)r!protocol_factoryr)r0r%backlogr/rrr_start_servings z$BaseSelectorEventLoop._start_servingc Cst|D]}}z|\}} |jrtd|| ||dWnOtttfy,YdSt ym} z6| j t j t j t j t jfvrb|d| t|d|||tj|j||||||nWYd} ~ qd} ~ wwd| i} |||| |||} || qdS)Nz#%r got a new connection from %r: %rFz&socket.accept() out of system resource)message exceptionr?peername)rangeacceptrOr rr@rIrHConnectionAbortedErrorrNerrnoZEMFILEZENFILEZENOBUFSZENOMEMcall_exception_handlerr TransportSocketr:r<Z call_laterrZACCEPT_RETRY_DELAYrU_accept_connection2Z create_task) r!rSr)r0r%rTr/_connaddrexcr$rZrrrrRsH      z(BaseSelectorEventLoop._accept_connectionc sd}d}z7|}|} |r|j|||| d|||d}n |j||| ||d}z | IdHWWdSty<|wttfyFty|} z+|jrqd| d} |dur]|| d<|dure|| d<|| WYd} ~ dSWYd} ~ dSd} ~ ww)NT)r+r-r$r%r/)r+r$r%z3Error on transport creation for incoming connection)rVrWr* transport) create_futurer1r, BaseExceptionr8 SystemExitKeyboardInterruptrOr]) r!rSrar$r0r%r/r*rdr+rccontextrrrr_sJ z)BaseSelectorEventLoop._accept_connection2c Cs|}t|ts"zt|}Wntttfy!td|dwz|j|}Wn ty3YdSw|sBt d|d|dS)NzInvalid file object: zFile descriptor z is used by transport ) isinstanceintr<AttributeError TypeError ValueErrorr r is_closingr5)r!rr<rdrrr_ensure_fd_no_transports&  z-BaseSelectorEventLoop._ensure_fd_no_transportc Gs|t|||d}z|j|}Wnty)|j|tj|dfY|Sw|j|j }\}}|j ||tjB||f|durH| |Sr') _check_closedrHandlerrrregisterr EVENT_READrDmodifycancel r!rcallbackargshandlermaskreaderwriterrrrrA"  z!BaseSelectorEventLoop._add_readercCs|rdSz|j|}Wn tyYdSw|j|j}\}}|tjM}|s1|j|n |j ||d|f|durE| dSdS)NFT) r6rrrrrDrrt unregisterrurvr!rrr{r|r}rrrr: s   z$BaseSelectorEventLoop._remove_readerc Gs|t|||d}z|j|}Wnty)|j|tjd|fY|Sw|j|j }\}}|j ||tjB||f|durH| |Sr') rqrrrrrrrsr EVENT_WRITErDrurvrwrrr _add_writer!r~z!BaseSelectorEventLoop._add_writercCs|rdSz|j|}Wn tyYdSw|j|j}\}}|tjM}|s1|j|n |j |||df|durE| dSdS)Remove a writer callback.FNT) r6rrrrrDrrrrurvrrrr_remove_writer1s   z$BaseSelectorEventLoop._remove_writercG"|||j||g|RdS)zAdd a reader callback.N)rprAr!rrxryrrr add_readerH z BaseSelectorEventLoop.add_readercC||||S)zRemove a reader callback.)rpr:r!rrrr remove_readerM  z#BaseSelectorEventLoop.remove_readercGr)zAdd a writer callback..N)rprrrrr add_writerRrz BaseSelectorEventLoop.add_writercCr)r)rprrrrr remove_writerWrz#BaseSelectorEventLoop.remove_writerc t||jr|dkrtdz||WSttfy$Ynw|}| }| || ||j |||}| tj|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-blockingrzN)r_check_ssl_socketrO gettimeoutrnrGrIrHrer<rprA _sock_recvadd_done_callback functoolspartial_sock_read_done)r!r)nfutrrzrrr sock_recv\s"    zBaseSelectorEventLoop.sock_recvcC"|dus|s||dSdSr') cancelledrr!rrrzrrrrrz%BaseSelectorEventLoop._sock_read_donec C|rdSz||}Wn,ttfyYdSttfy"ty9}z ||WYd}~dSd}~ww||dSr') donerGrIrHrgrhrf set_exception set_result)r!rr)rrDrcrrrrvsz BaseSelectorEventLoop._sock_recvc r)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)rrrOrrn recv_intorIrHrer<rprA_sock_recv_intorrrr)r!r)bufrrrzrrrsock_recv_intos"    z$BaseSelectorEventLoop.sock_recv_intoc Crr') rrrIrHrgrhrfrr)r!rr)rnbytesrcrrrrsz%BaseSelectorEventLoop._sock_recv_intoc st||jr|dkrtdz||}Wn ttfy'd}Ynw|t|kr0dS| }| }| || ||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)rrrOrrnrMrIrHlenrer<rpr _sock_sendall memoryviewrrr_sock_write_done)r!r)rDrrrrzrrr sock_sendalls*     z"BaseSelectorEventLoop.sock_sendallc Cs|rdS|d}z |||d}Wn,ttfy!YdSttfy*tyA}z ||WYd}~dSd}~ww||7}|t|krS| ddS||d<dS)Nr) rrMrIrHrgrhrfrrr)r!rr)Zviewposstartrrcrrrrs$   z#BaseSelectorEventLoop._sock_sendallcst||jr|dkrtd|jtjks"tjr;|jtj kr;|j ||j|j |j |dIdH}|d\}}}}}| }||||z|IdHWd}Sd}w)zTConnect to a remote socket at address. This method is a coroutine. rr)familytypeprotoloopN)rrrOrrnrr?ZAF_INETZ _HAS_IPv6ZAF_INET6Z_ensure_resolvedrrre _sock_connect)r!r)r3Zresolvedr`rrrr sock_connects$      z"BaseSelectorEventLoop.sock_connectc Cs|}zaz||WnEttfy2|||||j|||}|tj |j ||dYn*t t fy;t yQ}z ||WYd}~nd}~ww|dWd}dSWd}dSWd}dSd}w)Nr)r<ZconnectrIrHrpr_sock_connect_cbrrrrrgrhrfrr)r!rr)r3rrzrcrrrrs.    z#BaseSelectorEventLoop._sock_connectcCrr')rrrrrrrrz&BaseSelectorEventLoop._sock_write_donec Cs|rdSzUz|tjtj}|dkrt|d|Wn*ttfy(Yn*tt fy1t yG}z | |WYd}~nd}~ww| dWd}dSWd}dSWd}dSd}w)NrzConnect call failed ) rZ getsockoptr?Z SOL_SOCKETZSO_ERRORrNrIrHrgrhrfrr)r!rr)r3errrcrrrrs* z&BaseSelectorEventLoop._sock_connect_cbcsDt||jr|dkrtd|}||||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)rrrOrrnre _sock_accept)r!r)rrrr sock_accept*s   z!BaseSelectorEventLoop.sock_acceptc Cs|}z |\}}|dWnFttfy7|||||j||}|t j |j ||dYdSt t fy@tyW}z ||WYd}~dSd}~ww|||fdS)NFr)r<rZr@rIrHrprArrrrrrgrhrfrr)r!rr)rrar3rzrcrrrr9s"   z"BaseSelectorEventLoop._sock_acceptc s|j|j=|}||IdHz|j|j|||ddIdHW||r.|||j|j<S||r?|||j|j<w)NF)Zfallback) r _sock_fd is_reading pause_reading_make_empty_waiterZ sock_sendfile_sock_reset_empty_waiterresume_reading)r!Ztranspfileoffsetcountrrrr_sendfile_nativeJs"  z&BaseSelectorEventLoop._sendfile_nativecCs|D];\}}|j|j}\}}|tj@r&|dur&|jr!||n|||tj@r=|dur=|jr8||q||qdSr') fileobjrDrrtZ _cancelledr:Z _add_callbackrr)r!Z event_listrr{rr|r}rrr_process_eventsXs     z%BaseSelectorEventLoop._process_eventscCs|||dSr')r:r<r8)r!r)rrr _stop_servingfs z#BaseSelectorEventLoop._stop_servingr'NNN)-r __module__ __qualname____doc__rr,rZSSL_HANDSHAKE_TIMEOUTr1r4r8r7rrErBrPrUrRr_rprAr:rrrrrrrrrrrrrrrrrrrrrr __classcell__rrr"rr+sl         . )  rcseZdZdZeZdZdfdd ZddZddZ d d Z d d Z d dZ ddZ ejfddZdddZddZddZddZddZZS) _SelectorTransportiNcst||t||jd<z ||jd<Wnty&d|jd<Ynwd|jvrEz ||jd<Wntj yDd|jd<Ynw||_ | |_ d|_ ||||_||_d|_d|_|jdurm|j||j|j <dS)Nr?ZsocknamerXFr)rrr r^_extraZ getsocknamerNZ getpeernamer?errorrr<r_protocol_connected set_protocol_server_buffer_factory_buffer _conn_lost_closingZ_attachr )r!rr)r*r$r%r"rrrws0       z_SelectorTransport.__init__cCs|jjg}|jdur|dn|jr|d|d|j|jdurc|jsct|jj |jt j }|r=|dn|dt|jj |jt j }|rQd}nd}| }|d|d |d d d |S) Nclosedclosingzfd=z read=pollingz read=idlepollingZidlezwrite=z<{}> )rrrappendrr_loopr6rrrrtrget_write_buffer_sizeformatjoin)r!inforstatebufsizerrr__repr__s.      z_SelectorTransport.__repr__cCs|ddSr') _force_closer9rrrabortsz_SelectorTransport.abortcCs||_d|_dSNT) _protocolrr!r*rrrrs z_SelectorTransport.set_protocolcC|jSr')rr9rrr get_protocolz_SelectorTransport.get_protocolcCrr')rr9rrrrorz_SelectorTransport.is_closingcCsX|jrdSd|_|j|j|js*|jd7_|j|j|j|jddSdSNTr) rrr:rrrr call_soon_call_connection_lostr9rrrr8sz_SelectorTransport.closecCs0|jdur|d|t|d|jdSdS)Nzunclosed transport )source)rResourceWarningr8)r!Z_warnrrr__del__s z_SelectorTransport.__del__Fatal error on transportcCsNt|tr|jrtjd||ddn |j||||jd||dS)Nz%r: %sTrK)rVrWrdr*) rjrNr get_debugr rr]rr)r!rcrVrrr _fatal_errors  z_SelectorTransport._fatal_errorcCsd|jrdS|jr|j|j|j|js!d|_|j|j|jd7_|j|j |dSr) rrclearrrrrr:rrr!rcrrrrs z_SelectorTransport._force_closecCsz*|jr |j|W|jd|_d|_d|_|j}|dur)|d|_dSdS|jd|_d|_d|_|j}|durG|d|_wr')rrZconnection_lostrr8rrZ_detach)r!rcr%rrrrs*    z(_SelectorTransport._call_connection_lostcCs t|jSr')rrr9rrrr z(_SelectorTransport.get_write_buffer_sizecGs$|jrdS|jj||g|RdSr')rrrArrrrrAsz_SelectorTransport._add_reader)NN)r)rrrmax_size bytearrayrrrrrrrror8warningswarnrrrrrrArrrr"rrks"  rcseZdZdZejjZ  d#fdd ZfddZ ddZ d d Z d d Z d dZ ddZddZddZddZddZddZddZfddZdd Zd!d"ZZS)$r(TNcsd|_t|||||d|_d|_d|_t|j|j |j j ||j |j |j|j|dur?|j tj|ddSdSr )_read_ready_cbrr_eof_paused _empty_waiterrZ _set_nodelayrrrrconnection_maderAr _read_readyr_set_result_unless_cancelled)r!rr)r*r+r$r%r"rrrs   z!_SelectorSocketTransport.__init__cs.t|tjr |j|_n|j|_t|dSr')rjrZBufferedProtocol_read_ready__get_bufferr_read_ready__data_receivedrrrr"rrrs  z%_SelectorSocketTransport.set_protocolcCs|j o|j Sr')rrr9rrrrsz#_SelectorSocketTransport.is_readingcCsB|js|jrdSd|_|j|j|jrtd|dSdS)NTz%r pauses reading)rrrr:rrr rr9rrrr"s  z&_SelectorSocketTransport.pause_readingcCsD|js|jsdSd|_||j|j|jr td|dSdS)NFz%r resumes reading) rrrArrrrr rr9rrrr*s  z'_SelectorSocketTransport.resume_readingcCs |dSr')rr9rrrr2s z$_SelectorSocketTransport._read_readyc CsD|jrdSz|jd}t|stdWn"ttfyty7}z ||dWYd}~dSd}~wwz|j |}Wn-t t fyLYdSttfyUtym}z ||dWYd}~dSd}~ww|sv| dSz |j|WdSttfyty}z ||dWYd}~dSd}~ww)Nz%get_buffer() returned an empty bufferz/Fatal error: protocol.get_buffer() call failed.$Fatal read error on socket transportz3Fatal error: protocol.buffer_updated() call failed.)rrZ get_bufferrr5rgrhrfrrrrIrH_read_ready__on_eofZbuffer_updated)r!rrcrrrrr5sP  z0_SelectorSocketTransport._read_ready__get_bufferc Cs|jrdSz |j|j}Wn-ttfyYdSttfy#ty;}z | |dWYd}~dSd}~ww|sD| dSz |j |WdSttfyWtyo}z | |dWYd}~dSd}~ww)Nrz2Fatal error: protocol.data_received() call failed.) rrrGrrIrHrgrhrfrrrZ data_received)r!rDrcrrrrZs4 z3_SelectorSocketTransport._read_ready__data_receivedc Cs|jr td|z|j}Wn"ttfyty4}z | |dWYd}~dSd}~ww|r@|j |j dS| dS)Nz%r received EOFz1Fatal error: protocol.eof_received() call failed.) rrr rrZ eof_receivedrgrhrfrr:rr8)r!Z keep_openrcrrrrss   z,_SelectorSocketTransport._read_ready__on_eofc Cs(t|tttfstdt|j|jrtd|j dur"td|s&dS|j r=|j t j kr4t d|j d7_ dS|jsz|j|}Wn,ttfySYn,ttfy\tyt}z ||dWYd}~dSd}~ww||d}|sdS|j|j|j|j||dS)N/data argument must be a bytes-like object, not z%Cannot call write() after write_eof()z(unable to write; sendfile is in progresssocket.send() raised exception.r%Fatal write error on socket transport)rjbytesrrrmrrrr5rrr!LOG_THRESHOLD_FOR_CONNLOST_WRITESr warningrrrMrIrHrgrhrfrrrr _write_readyextend_maybe_pause_protocol)r!rDrrcrrrwritesB       z_SelectorSocketTransport.writec Cs*|jrdSz |j|j}WnKttfyYdSttfy#tyY}z+|j |j |j | |d|jdurN|j|WYd}~dSWYd}~dSd}~ww|rb|jd|=||js|j |j |jdur{|jd|jr|ddS|jr|jtjdSdSdS)Nr )rrrMrrIrHrgrhrfrrrrrrr_maybe_resume_protocolrrrrshutdownr?SHUT_WR)r!rrcrrrrs<      z%_SelectorSocketTransport._write_readycCs2|js|jrdSd|_|js|jtjdSdSr)rrrrrr?rr9rrr write_eofs z"_SelectorSocketTransport.write_eofcCsdSrrr9rrr can_write_eofrFz&_SelectorSocketTransport.can_write_eofcs.t||jdur|jtddSdS)NzConnection is closed by peer)rrrrConnectionErrorrr"rrrs  z._SelectorSocketTransport._call_connection_lostcCs6|jdur td|j|_|js|jd|jS)NzEmpty waiter is already set)rr5rrerrr9rrrrs   z+_SelectorSocketTransport._make_empty_waitercCs d|_dSr')rr9rrrrrz,_SelectorSocketTransport._reset_empty_waiterr)rrrZ_start_tls_compatiblerZ _SendfileModeZ TRY_NATIVEZ_sendfile_compatiblerrrrrrrrrrrrrrrrrrrr"rr(s* %' r(csJeZdZejZ  d fdd ZddZddZd dd Z d d Z Z S)r2Ncsbt||||||_|j|jj||j|j|j|j |dur/|jt j |ddSdSr') rr_addressrrrrrArrrr)r!rr)r*r3r+r$r"rrrs  z#_SelectorDatagramTransport.__init__cCstdd|jDS)Ncss|] \}}t|VqdSr')r).0rDr`rrr szC_SelectorDatagramTransport.get_write_buffer_size..)sumrr9rrrrsz0_SelectorDatagramTransport.get_write_buffer_sizec Cs|jrdSz |j|j\}}WnEttfyYdSty4}z |j|WYd}~dSd}~wt t fy=t yU}z | |dWYd}~dSd}~ww|j ||dS)Nz&Fatal read error on datagram transport)rrZrecvfromrrIrHrNrerror_receivedrgrhrfrZdatagram_receivedr!rDrbrcrrrrs z&_SelectorDatagramTransport._read_readyc Cs|t|tttfstdt|j|sdS|jr+|d|jfvr(td|j|j}|j rE|jrE|j t j kr  z(_SelectorDatagramTransport._sendto_readyrr') rrr collectionsdequerrrrr r!rrrr"rr2s  +r2)r__all__r$r\rrr?rrZssl ImportErrorrrrrrr r r logr rZ BaseEventLooprZ_FlowControlMixinZ Transportrr(r2rrrrsH            Do