fkldZddlZddlZddlZddlZddlZeedredz ZddlmZddlm Z ddlm Z dd lm Z dd lm Z dd l mZdd lmZd ZdeddZdeddZeedrdeddZdeddZGdde j,ZGddee j,ZGddZGddZy)) StreamReader StreamWriterStreamReaderProtocolopen_connection start_serverNAF_UNIX)open_unix_connectionstart_unix_server) coroutines)events) exceptions)format_helpers) protocols)logger)sleepi)limitc Ktj}t||}t|| |j fd||fi|d{\}}t | ||}||fS7w)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.) rlooprcSNprotocols&/usr/lib/python3.12/asyncio/streams.pyz!open_connection..1sN)r get_running_looprrcreate_connectionr) hostportrkwdsrreader transport_writerrs @rrrsx&  " " $D D 1F#F6H///$.(,..LIq )Xvt .sA A) A'A)cKtjfd}j|||fi|d{S7w)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 argument is 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. c>t}t|}|SNrrrrr%rclient_connected_cbrrs rfactoryzstart_server..factoryNs&E5'0C-13rN)r r create_server)r.r"r#rr$r/rs` ` @rrr6s@,  " " $D $##GT4@4@ @@ @s4A>AcKtj}t||}t|||jfd|fi|d{\}}t |||}||fS7w)z@Similar to `open_connection` but works with UNIX Domain Sockets.rrcSrrrsrrz&open_unix_connection..bsHrN)r r rrcreate_unix_connectionr) pathrr$rr%r&r'r(rs @rr r Zsv&&(E5'T:8T88 d,&*,, 1i64@v~,sA A( A& A(cKtjfd}j||fi|d{S7w)z=Similar to `start_server` but works with UNIX Domain Sockets.c>t}t|}|Sr+r,r-s rr/z"start_unix_server..factoryks&!D9F+F4G157HOrN)r r create_unix_server)r.r4rr$r/rs` ` @rr r fs>&&(  -T,,WdCdCCCCs 3?=?c6eZdZdZd dZdZdZdZdZdZ y) 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. Nc|tj|_n||_d|_t j |_d|_yNF)r get_event_loop_loop_paused collectionsdeque_drain_waiters_connection_lost)selfrs r__init__zFlowControlMixin.__init__~s> <..0DJDJ )//1 %rc|jrJd|_|jjrtjd|yy)NTz%r pauses writing)r>r= get_debugrdebugrCs r pause_writingzFlowControlMixin.pause_writings:<< ::   ! LL,d 3 "rc|jsJd|_|jjrtjd||j D]$}|j r|jd&y)NFz%r resumes writing)r>r=rFrrGrAdone set_resultrCwaiters rresume_writingzFlowControlMixin.resume_writings`||| ::   ! LL-t 4)) (F;;=!!$' (rcd|_|jsy|jD]8}|jr||j d(|j |:yNT)rBr>rArKrL set_exceptionrCexcrNs rconnection_lostz FlowControlMixin.connection_lostsS $|| )) .F;;=;%%d+((-  .rcNK|jr td|jsy|jj }|j j | |d{|j j|y7 #|j j|wxYww)NzConnection lost)rBConnectionResetErrorr>r= create_futurerAappendremoverMs r _drain_helperzFlowControlMixin._drain_helpers  &'89 9|| ))+ ""6* /LL    & &v .     & &v .s0AB%B"B#B'B%BB""B%ctr)NotImplementedErrorrCstreams r_get_close_waiterz"FlowControlMixin._get_close_waiters!!rr) __name__ __module__ __qualname____doc__rDrIrOrUr[r`rrrr9r9ts%&4 ( . /"rr9cfeZdZdZdZd 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.) Nc4t|||,tj||_|j |_nd|_|||_d|_d|_d|_ d|_ ||_ d|_ |jj|_y)NrF)superrDweakrefref_stream_reader_wr_source_traceback_strong_reader_reject_connection_stream_writer_task _transport_client_connected_cb _over_sslr=rX_closed)rC stream_readerr.r __class__s rrDzStreamReaderProtocol.__init__s d#  $%,[[%?D "%2%D%DD "%)D "  *#0D "'" $7!zz//1 rc<|jy|jSr)rjrHs r_stream_readerz#StreamReaderProtocol._stream_readers  ! ! )%%''rc|j}|j}||_||_|j ddu|_y)N sslcontext)r=r&rnrpget_extra_inforr)rCr(rr&s r_replace_writerz$StreamReaderProtocol._replace_writers<zz$$ $#"11,?tKrcxjrKddi}jrj|d<jj|j y_j }||jjddu_ jt|j_ j|j}tj|rAfd}jj|_j j#|d_yy)NmessagezpAn open stream was garbage collected prior to establishing network connection; call "stream.close()" explicitly.source_tracebackryc|jrjy|j}|0jj d|djyy)Nz*Unhandled exception in client_connected_cb)r} exceptionr&) cancelledcloserr=call_exception_handler)taskrTrCr&s rcallbackz6StreamReaderProtocol.connection_made..callbacks\~~'!)..*C 99'S),)2; ") 'r)rmrkr=rabortrprw set_transportrzrrrqrrnr iscoroutine create_taskroadd_done_callbackrl)rCr&contextr%resrs`` rconnection_madez$StreamReaderProtocol.connection_mades#  " "@G %%.2.D.D*+ JJ - -g 6 OO  #$$     +"11,?tK  $ $ 0".y$/5/3zz#;D ++F,0,?,?AC%%c* *"ZZ33C8  ,,X6"&D / 1rcf|j}|$||jn|j||jj s9||jj dn|jj|t ||d|_d|_ d|_ d|_ yr) rwfeed_eofrRrsrKrLrgrUrjrnrorp)rCrTr%rus rrUz$StreamReaderProtocol.connection_lost s$$  {!$$S)||  "{ ''- **3/ $!%" rcD|j}||j|yyr)rw feed_data)rCdatar%s r data_receivedz"StreamReaderProtocol.data_receiveds&$$     T " rcZ|j}||j|jryy)NFT)rwrrr)rCr%s r eof_receivedz!StreamReaderProtocol.eof_received!s,$$   OO  >>rc|jSr)rsr^s rr`z&StreamReaderProtocol._get_close_waiter,s ||rc |j}|jr"|js|jyyy#t$rYywxYwr)rsrKrrAttributeError)rCcloseds r__del__zStreamReaderProtocol.__del__/sM #\\F{{}V%5%5%7  "&8}   s A A  A NN)rarbrcrdrkrDpropertyrwr{rrUrrr`r __classcell__)rus@rrrsN2((( L('T$#  #rrczeZdZdZdZdZedZdZdZ dZ dZ d Z d Z d Zdd ZdZd d d ddZdZy )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. c||_||_|t|tsJ||_||_|j j |_|jjdyr) rp _protocol isinstancer_readerr=rX _complete_futrL)rCr&rr%rs rrDzStreamWriter.__init__Es[#!~FL!AAA  !ZZ557 %%d+rc|jjd|jg}|j|j d|jdj dj |S)N transport=zreader=<{}> )rurarprrYformatjoinrCinfos r__repr__zStreamWriter.__repr__Os['':doo5H)IJ << # KK'$,,!12 3}}SXXd^,,rc|jSrrprHs rr&zStreamWriter.transportUs rc:|jj|yr)rpwriterCrs rrzStreamWriter.writeYs d#rc:|jj|yr)rp writelinesrs rrzStreamWriter.writelines\s ""4(rc6|jjSr)rp write_eofrHs rrzStreamWriter.write_eof_s((**rc6|jjSr)rp can_write_eofrHs rrzStreamWriter.can_write_eofbs,,..rc6|jjSr)rprrHs rrzStreamWriter.closees$$&&rc6|jjSr)rp is_closingrHs rrzStreamWriter.is_closinghs))++rcVK|jj|d{y7wr)rr`rHs r wait_closedzStreamWriter.wait_closedksnn..t444s )')Nc:|jj||Sr)rprz)rCnamedefaults rrzzStreamWriter.get_extra_infons--dG<>jjl"jj22 OOXz#_"7!5 377 (  & 7s!8BB 3B.B/BBc|jjsc|jjrt j dt y|jt j d|t yy)Nzloop is closedz unclosed )rprr= is_closedwarningswarnResourceWarningrrHs rrzStreamWriter.__del__sT))+zz##% .@  $2OD ,rr)rarbrcrdrDrrr&rrrrrrrrzrrrrrrrr;sh,- $)+/',5=-4)-.2-1' ErrceZdZdZedfdZdZdZdZdZ dZ dZ d Z d Z d Zd Zd ZddZddZdZdZdZy)rNcl|dkr td||_|tj|_n||_t |_d|_d|_d|_ d|_ d|_ |jjr.tjtj d|_yy)NrzLimit cannot be <= 0Fr ) ValueError_limitr r<r= bytearray_buffer_eof_waiter _exceptionrpr>rFr extract_stacksys _getframerk)rCrrs rrDzStreamReader.__init__s A:34 4 <..0DJDJ {    ::   !%3%A%A a &"D " "rcdg}|jr'|jt|jd|jr|jd|jt k7r|jd|j|j r|jd|j |jr|jd|j|jr|jd|j|jr|jdd jd j|S) Nrz byteseofzlimit=zwaiter=z exception=rpausedrr) rrYlenrr_DEFAULT_LIMITrrrpr>rrrs rrzStreamReader.__repr__s << KK3t||,-V4 5 99 KK  ;;. ( KK& . / << KK'$,,!12 3 ?? KK*T__$78 9 ?? KK*T__$78 9 << KK !}}SXXd^,,rc|jSr)rrHs rrzStreamReader.exceptions rc||_|j}|*d|_|js|j|yyyr)rrrrRrSs rrRzStreamReader.set_exceptionsC  DL##%$$S)& rct|j}|*d|_|js|jdyyy)z1Wakeup read*() functions waiting for data or EOF.N)rrrLrMs r_wakeup_waiterzStreamReader._wakeup_waiters<  DL##%!!$'& rc8|jJd||_y)NzTransport already setr)rCr&s rrzStreamReader.set_transports&?(??&#rc|jrEt|j|jkr"d|_|jj yyyr;)r>rrrrpresume_readingrHs r_maybe_resume_transportz$StreamReader._maybe_resume_transports; <rr pause_readingr]rs rrzStreamReader.feed_datas99888}  D!  OO 'LLDLL!A O3 $--/ $ 4! ( ' '#'  'sB%%B87B8cRK|jt|d|jrJd|jr!d|_|jj |j j|_ |jd{d|_y7 #d|_wxYww)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 dataz_wait_for_data after EOFF)r RuntimeErrorrr>rprr=rX)rC func_names r_wait_for_datazStreamReader._wait_for_data s << #+456 699888} << DL OO * * ,zz//1  ,,  DL DLs0A:B'=B B BB'B B$$B'cKd}t|} |j|d{}|S7#tj$r}|jcYd}~Sd}~wtj $r}|j j||jr|j d|j|z=n|j j|jt|jdd}~wwxYww)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 readuntilrIncompleteReadErrorpartialLimitOverrunErrorr startswithconsumedclearrrargs)rCsepseplenlinees rreadlinezStreamReader.readline%s S (,,D --- 99 ++ (||&&sAJJ7LL!5!**v"5!56 ""$  ( ( *QVVAY' '  (sJC5.,.C5.C2 A C2 C5C2(BC--C22C5cKt|}|dk(r td|j |jd} t|j}||z |k\rO|jj ||}|dk7rn|dz|z }||j kDrt jd||jrEt|j}|jjt j|d|jdd{||j kDrt jd||jd||z}|jd||z=|jt|S7iw) 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 stringNr z2Separator is not found, and chunk exceed the limitrz2Separator is found, but chunk is longer than limit)rrrrfindrrrrbytesrrrr)rC separatorroffsetbuflenisepchunks rrzStreamReader.readuntilDsz(Y Q;KL L ?? &// !*&F&(||((F;2: !f,DKK'$66L  yydll+ ""$ 44UDAA%%k2 2 2=@ $++ ..DdL L ^dVm, LL$- ( $$&U| 3sDE6 E4 A*E6cK|j |j|dk(ry|dkrLg} |j|jd{}|sn|j|8dj |S|j s%|j s|jdd{tt|j d|}|j d|=|j|S77Hw)aRead up to `n` bytes from the stream. If `n` is not provided or set to -1, read until EOF, then return all read bytes. If EOF was received and the internal buffer is empty, return an empty bytes object. If `n` is 0, return an empty bytes object immediately. If `n` is positive, return at most `n` available bytes as soon as at least 1 byte is available in the internal buffer. If EOF is received before any byte is read, return an 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. Nrrread) rr rrYrrrrr memoryviewr)rCnblocksblockrs rr zStreamReader.reads, ?? &// ! 6 q5 F"ii 44 e$  88F# #||DII%%f- - -Z -bq12 LL!  $$& 5 .s&AC)C%AC)C'AC)'C)cK|dkr td|j |j|dk(ryt|j|kr|jrEt |j}|jj tj|||jdd{t|j|krt|j|k(r0t |j}|jj n0t t|jd|}|jd|=|j|S7w)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) rrrrrrrrrrr r)rCr  incompleters rrzStreamReader.readexactlys q5IJ J ?? &// ! 6$,,!#yy"4<<0  ""$ 44ZCC%%m4 4 4 $,,!# t||  !&D LL   DLL1"156D RaR  $$&  5sB,E.E/E B Ec|SrrrHs r __aiter__zStreamReader.__aiter__s rcXK|jd{}|dk(rt|S7w)Nr)rStopAsyncIteration)rCvals r __anext__zStreamReader.__anext__s+MMO# #:$ $ $s *(*)r)r)rarbrcrkrrDrrrRrrrrrrrrrr rrrrrrrrsf+$",-$*($- .$, 8>Yv1f'Rrrrr)__all__r?socketrrrhhasattrr r rrrlogrtasksrrrrr r Protocolr9rrrrrrr s '  69 <