Vc9hcs~dZddlZddlZddlZddlZddlZeedredz ZddlmZddlm Z ddlm Z dd lm Z dd lm Z dd l mZdd lmZd ZdeddZdeddZeedrdeddZdeddZGdde jZGddee jZGddZGddZdS)) StreamReader StreamWriterStreamReaderProtocolopen_connection start_serverNZAF_UNIX)open_unix_connectionstart_unix_server) coroutines)events) exceptions)format_helpers) protocols)logger)sleepi)limitcs Ktj}t||}t|| |j fd||fi|d{V\}}t | ||}||fS)aA wrapper for create_connection() returning a (reader, writer) pair. The reader returned is a StreamReader instance; the writer is a StreamWriter instance. The arguments are all the usual arguments to create_connection() except protocol_factory; most common are positional host and port, with various optional keyword arguments following. Additional optional keyword arguments are loop (to set the event loop instance to use) and limit (to set the buffer limit passed to the StreamReader). (If you want to customize the StreamReader and/or StreamReaderProtocol classes, just copy the code -- there's really nothing special here except some convenience.) rlooprcSNprotocol(/usr/lib64/python3.11/asyncio/streams.pyz!open_connection..1sN)r get_running_looprrZcreate_connectionr) hostportrkwdsrreader transport_writerrs @rrrs&  " $ $D D 1 1 1F#F666H//$..(,........LIq )Xvt < rcslKtjfd}j|||fi|d{VS)aStart a socket server, call back for each client connected. The first parameter, `client_connected_cb`, takes two parameters: client_reader, client_writer. client_reader is a StreamReader object, while client_writer is a StreamWriter object. This parameter can either be a plain callback function or a coroutine; if it is a coroutine, it will be automatically converted into a Task. The rest of the arguments are all the usual arguments to loop.create_server() except protocol_factory; most common are positional host and port, with various optional keyword arguments following. The return value is the same as loop.create_server(). Additional optional keyword arguments are loop (to set the event loop instance to use) and limit (to set the buffer limit passed to the StreamReader). The return value is the same as loop.create_server(), i.e. a Server object which can be used to stop the service. cNt}t|}|SNrrrrr!rclient_connected_cbrr rfactoryzstart_server..factoryOs6E555'0C-1333rN)r rZ create_server)r)rrrr r+rs` ` @rrr6so.  " $ $D $#GT4@@4@@ @ @ @ @ @ @@rcsKtj}t||}t|||jfd|fi|d{V\}}t |||}||fS)z@Similar to `open_connection` but works with UNIX Domain Sockets.rrcrrrrrrrz&open_unix_connection..csHrN)r rrrZcreate_unix_connectionr) pathrr rr!r"r#r$rs @rrr[s&((E555'T:::8T8    d,,&*,,,,,,,, 1i64@@v~rcsjKtjfd}j||fi|d{VS)z=Similar to `start_server` but works with UNIX Domain Sockets.cr%r&r'r(r*rr+z"start_unix_server..factoryls6!D999F+F4G15777HOrN)r rZcreate_unix_server)r)r,rr r+rs` ` @rrrgsm&((        -T,WdCCdCCCCCCCCCrcs8eZdZdZd dZdZdZdZdZdZ dS) FlowControlMixina)Reusable flow control logic for StreamWriter.drain(). This implements the protocol methods pause_writing(), resume_writing() and connection_lost(). If the subclass overrides these it must call the super methods. StreamWriter.drain() must wait for _drain_helper() coroutine. Ncs|tjd|_n||_d|_t j|_d|_dS)Ni)Z stacklevelF)r _get_event_loop_loop_paused collectionsZdeque_drain_waiters_connection_lost)selfr r__init__zFlowControlMixin.__init__sN  /1===DJJDJ )/11 %rcstd|_|jrtjd|dSdS)NTz%r pauses writing)r0r/ get_debugrdebugr4 r pause_writingzFlowControlMixin.pause_writingsB :   ! ! 4 L,d 3 3 3 3 3 4 4rcsd|_|jrtjd||jD]+}|s|d,dS)NFz%r resumes writing)r0r/r7rr8r2done set_resultr4waiterr5rresume_writingzFlowControlMixin.resume_writingss :   ! ! 5 L-t 4 4 4) ( (F;;== (!!$''' ( (rcsd|_|jsdS|jD]C}|s-||d.||DdSNT)r3r0r2r<r= set_exceptionr4excr? rconnection_lostz FlowControlMixin.connection_lostsy $|  F) . .F;;== ..%%d++++((---  . .rcs2K|jrtd|jsdS|j}|j| |d{V|j|dS#|j|wxYw)NzConnection lost)r3ZConnectionResetErrorr0r/ create_futurer2appendZremover>r5r _drain_helperzFlowControlMixin._drain_helpers   :&'899 9|  F))++ ""6*** /LLLLLLL   & &v . . . . .D  & &v . . . .s A::Bcstr)NotImplementedErrorr4Zstreamr5r_get_close_waiterz"FlowControlMixin._get_close_waiters!!rr) __name__ __module__ __qualname____doc__r6r;r@rFrIrLrrrr-r-us}&&&&444 ((( . . . / / /"""""rr-csleZdZdZdZd fd ZedZdZdZ fdZ dZ d Z d Z d ZxZS) ra=Helper class to adapt between Protocol and StreamReader. (This is a helper class instead of making StreamReader itself a Protocol subclass, because the StreamReader has other potential uses, and to prevent the user of the StreamReader to accidentally call inappropriate methods of the protocol.) NcsLt||&tj||_|j|_nd|_|||_d|_d|_d|_ d|_ ||_ d|_ |j |_dS)NrF)superr6weakrefZref_stream_reader_wr_source_traceback_strong_reader_reject_connection_stream_writer_task _transport_client_connected_cb _over_sslr/rG_closed)r4Z stream_readerr)r __class__s rr6zStreamReaderProtocol.__init__s d###  *%,[%?%?D "%2%DD " "%)D "  0#0D "'" $7!z//11 rcs<|jdS|Sr)rSr9r:r_stream_readerz#StreamReaderProtocol._stream_readers$  ! 4%%'''rcsv|j}|j}||_||_|ddu|_dS)N sslcontext)r/r"rWrYget_extra_infor[)r4r$rr" r_replace_writerz$StreamReaderProtocol._replace_writers>z$ $#"11,??tKrcs|jrEddi}|jr |j|d<|j||dS||_|j}||||ddu|_ |j st||||j|_ | ||j }tj|r|j||_d|_dSdS)NZmessagezpAn open stream was garbage collected prior to establishing network connection; call "stream.close()" explicitly.Zsource_tracebackr_)rVrTr/Zcall_exception_handlerZabortrYr^ set_transportr`r[rZrrWr Z iscoroutineZ create_taskrXrU)r4r"Zcontextr!Zres rconnection_madez$StreamReaderProtocol.connection_mades5  " @G % E.2.D*+ J - -g 6 6 6 OO    F#$  ,   + + +"11,??tK  $ '".y$/5/3z#;#;D ++F,0,?AAC%c** 9!Z33C88 "&D    ' 'rcs|j}|,||n|||js7||jdn|j|t |d|_d|_ d|_ d|_ dSr) r^feed_eofrBr\r<r=rQrFrSrWrXrY)r4rDr!r]s rrFz$StreamReaderProtocol.connection_losts$  * *!!!!$$S)))|  "" 0 0 ''---- **3/// $$$!%" rcsF|j}|||dSdSr)r^ feed_data)r4datar!rEr data_receivedz"StreamReaderProtocol.data_receiveds6$  #   T " " " " " # #rcsR|j}|||jrdSdS)NFT)r^rfr[)r4r!r5r eof_receivedz!StreamReaderProtocol.eof_receiveds8$   OO    > 5trc|jSr)r\rKr5rrLz&StreamReaderProtocol._get_close_waiters |rcs |j}|r*|s|dSdSdS#t$rYdSwxYwr)r\r< cancelled exceptionZAttributeError)r4Zclosedr5r__del__zStreamReaderProtocol.__del__!s #\F{{}} #V%5%5%7%7 #  """"" # # # #    DD sA AANN)rMrNrOrPrTr6propertyr^rbrerFrirjrLrnZ __classcell__)r]s@rrrs222222(((X( LLL'''6$###    # # # # # # #rrcszeZdZdZdZdZedZdZdZ dZ dZ d Z d Z d Zdd ZdZd d ddZd S)ra'Wraps a Transport. This exposes write(), writelines(), [can_]write_eof(), get_extra_info() and close(). It adds drain() which returns an optional Future on which you can wait for flow control. It also adds a transport property which references the Transport directly. cs||_||_||_||_|j|_|jddSr)rY _protocol_readerr/rGZ _complete_futr=)r4r"rr!rrdrr6zStreamWriter.__init__7sS#!  !Z5577 %%d+++++rcs|jjd|jg}|j|d|jdd|S)N transport=zreader=<{}> )r]rMrYrrrHformatjoinr4Zinfor5r__repr__zStreamWriter.__repr__As_')Ido)I)IJ < 4 KK2$,22 3 3 3}}SXXd^^,,,rcrkrrYr9r:rr"zStreamWriter.transportGs rc:|j|dSr)rYwriter4rhr5rr|zStreamWriter.writeKs d#####rcr{r)rY writelinesr}r5rr~zStreamWriter.writelinesNs ""4(((((rc4|jSr)rY write_eofr9r:rrzStreamWriter.write_eofQs((***rcrr)rY can_write_eofr9r:rrzStreamWriter.can_write_eofTs,,...rcrr)rYcloser9r:rrzStreamWriter.closeWs$$&&&rcrr)rY is_closingr9r:rrzStreamWriter.is_closingZs))+++rcsJK|j|d{VdSr)rqrLr9r:r wait_closedzStreamWriter.wait_closed]s4n..t44444444444rNcs8|j||Sr)rYr`)r4ZnameZdefaultrErr`zStreamWriter.get_extra_info`s--dG<<jjll"j22 OXz#_"7399999999 (  &&&&&rr)rMrNrOrPr6ryrpr"r|r~rrrrrr`rrrrrrr-s,,,--- X$$$)))+++///''',,,555====---4)-.2 ' ' ' ' ' ' 'rrcseZdZdZedfdZdZdZdZdZ dZ dZ d Z d Z d Zd Zd ZddZddZdZdZdZdS)rNcsz|dkrtd||_|tj|_n||_t |_d|_d|_d|_ d|_ d|_ |j r-tjtjd|_dSdS)NrzLimit cannot be <= 0Fr) ValueError_limitr r.r/Z bytearray_buffer_eof_waiter _exceptionrYr0r7r Z extract_stacksysZ _getframerT)r4rrrErr6zStreamReader.__init__s A: 5344 4  /11DJJDJ {{    :   ! ! "%3%A a  &"&"D " " " " "rcs\dg}|jr*|t|jd|jr|d|jt kr|d|j|jr|d|j|jr|d|j|jr|d|j|j r|dd d |S) Nrz bytesZeofzlimit=zwaiter=z exception=rsZpausedrtru) rrHlenrr_DEFAULT_LIMITrrrYr0rvrwrxr5rryzStreamReader.__repr__s. < 6 KK3t|,,444 5 5 5 9  KK    ;. ( 0 KK... / / / < 4 KK2$,22 3 3 3 ? : KK8T_88 9 9 9 ? : KK8T_88 9 9 9 < " KK ! ! !}}SXXd^^,,,rcrkr)rr9r:rrmzStreamReader.exceptions rcs||_|j}|2d|_|s||dSdSdSr)rrrlrBrCrErrBzStreamReader.set_exceptionsa  *DL##%% *$$S))))) * * * *rcs|j}|2d|_|s|ddSdSdS)z1Wakeup read*() functions waiting for data or EOF.N)rrlr=r>r5r_wakeup_waiterzStreamReader._wakeup_waitersZ  (DL##%% (!!$''''' ( ( ( (rcs||_dSrrz)r4r"r5rrczStreamReader.set_transports #rcs|jr?t|j|jkr$d|_|jdSdSdS)NF)r0rrrrYresume_readingr9r:r_maybe_resume_transportz$StreamReader._maybe_resume_transportsY < -C --< - DL O * * , , , , , - - - -rcs<d|_|dSrA)rrr9r:rrfzStreamReader.feed_eofs!  rcs |jo|j S)z=Return True if the buffer is empty and 'feed_eof' was called.)rrr9r:rat_eofzStreamReader.at_eofsy---rcsD|sdS|j|||jb|js]t |jd|jzkr? |jd|_dS#t$r d|_YdSwxYwdSdSdS)NiT) rZextendrrYr0rrZ pause_readingrJr}r5rrgzStreamReader.feed_datas  F D!!!  O $L $DL!!A O3 $ $--/// $ ' ' ' '#'  ' $ $ $ $ $ $s"BBBcsK|jt|d|jr d|_|j|j|_ |jd{Vd|_dS#d|_wxYw)zpWait until feed_data() or feed_eof() is called. If stream was paused, automatically resume it. NzF() called while another coroutine is already waiting for incoming dataF)rZ RuntimeErrorr0rYrr/rG)r4Z func_namer5r_wait_for_datazStreamReader._wait_for_datas < 655566 6 < - DL O * * , , ,z//11  ,       DLLL4DL    s " A88 BcsKd}t|} ||d{V}n#tj$r}|jcYd}~Sd}~wtj$r}|j||jr|jd|j|z=n|j | t|j dd}~wwxYw|S)aRead chunk of data from the stream until newline (b' ') is found. On success, return chunk that ends with newline. If only partial line can be read due to EOF, return incomplete line without terminating newline. When EOF was reached while no bytes read, empty bytes object is returned. If limit is reached, ValueError will be raised. In that case, if newline was found, complete line including newline will be removed from internal buffer. Else, internal buffer will be cleared. Limit is compared against part of the line without newline. If stream was paused, this function will automatically resume it if needed.  Nr) r readuntilr IncompleteReadErrorZpartialLimitOverrunErrorrZ startswithZconsumedclearrrZargs)r4ZsepseplenZlineZerdrreadlinezStreamReader.readlines  S (,,,,,,,,DD-   9      + ( ( (|&&sAJ77 %L!5!*v"5!566 ""$$$  ( ( * * *QVAY'' '  ( s(1CA C CA:CCrcsKt|}|dkrtd|j|jd} t|j}||z |krJ|j||}|dkrn|dz|z }||jkrt jd||jrBt|j}|j t j |d| dd{V||jkrt jd ||jd||z}|jd||z=| t|S) aVRead data from the stream until ``separator`` is found. On success, the data and separator will be removed from the internal buffer (consumed). Returned data will include the separator at the end. Configured stream limit is used to check result. Limit sets the maximal length of data that can be returned, not counting the separator. If an EOF occurs and the complete separator is still not found, an IncompleteReadError exception will be raised, and the internal buffer will be reset. The IncompleteReadError.partial attribute may contain the separator partially. If the data cannot be read because of over limit, a LimitOverrunError exception will be raised, and the data will be left in the internal buffer, so it can be read again. rz,Separator should be at least one-byte stringNTrz2Separator is not found, and chunk exceed the limitrz2Separator is found, but chunk is longer than limit)rrrrZfindrr rrbytesrrrr)r4Z separatorrZoffsetZbuflenZisepZchunkrrrzStreamReader.readuntil-s(Y Q; MKLL L ? "/ !* 3&&F&( |((F;;2: !f,DK' $6L   y Bdl++ ""$$$ 4UDAAA%%k22 2 2 2 2 2 2 2= 3@ $+  L.DdLL L ^dVm^, L$- ( $$&&&U||rrcsK|j|j|dkrdS|dkrQg} ||jd{V}|sn||9d|S|js"|js|dd{Vt|jd|}|jd|=| |S)aRead up to `n` bytes from the stream. If n is not provided, or set to -1, read until EOF and return all read bytes. If the EOF was received and the internal buffer is empty, return an empty bytes object. If n is zero, return empty bytes object immediately. If n is positive, this function try to read `n` bytes, and may return less or equal bytes than requested, but at least one byte. If EOF was received before any byte is read, this function returns empty byte object. Returned value is not limited with limit, configured at stream creation. If stream was paused, this function will automatically resume it if needed. NrrTread) rrrrHrwrrrrr)r4nZblocksZblockrhrdrrzStreamReader.reads* ? "/ ! 6 3 q5 $ F %"ii 44444444 e$$$  % 88F## #| .DI .%%f-- - - - - - - -T\"1"%&& L!  $$&&& rcsK|dkrtd|j|j|dkrdSt|j|kr||jrBt |j}|jtj||| dd{Vt|j|k|t|j|kr.t |j}|jn&t |jd|}|jd|=| |S)aRead exactly `n` bytes. Raise an IncompleteReadError if EOF is reached before `n` bytes can be read. The IncompleteReadError.partial attribute of the exception will contain the partial read bytes. if n is zero, return empty bytes object. Returned value is not limited with limit, configured at stream creation. If stream was paused, this function will automatically resume it if needed. rz*readexactly size can not be less than zeroNr readexactly) rrrrrrrr rrr)r4rZ incompleterhrarrzStreamReader.readexactlysZ q5 KIJJ J ? "/ ! 6 3$,!# 5y D"4<00  ""$$$ 4ZCCC%%m44 4 4 4 4 4 4 4 $,!# 5 t|   ! !&&D L   bqb)**D RaR  $$&&& rcs|Srrr9r:r __aiter__zStreamReader.__aiter__s rcsXK|d{V}|dkrt|S)Nr)rZStopAsyncIteration)r4Zvalr5r __anext__zStreamReader.__anext__s<MMOO###### #: %$ $ r)r)r)rMrNrOrTrr6ryrmrBrrcrrfrrgrrrrrrrrrrrrs4+$"""",---$***((($$$--- ...$$$,   8>YYYYv0000d'''Rrrror)Z__all__r1ZsocketrZwarningsrRZhasattrZr r r r r ZlogrZtasksrrrrrrZProtocolr-rrrrrrrs^ '  769= <