+\htdZddlmZddlZddlZddlZddlZddlZddlZddl Z ddlm Z m Z m Z m Z mZddlmZmZmZmZddlmZmZddlmZddlmZdd lmZdd lmZmZdd l m!Z!m"Z"m#Z#dd l$m%Z% dd l&m'Z'm(Z(dZ) ddl+m,Z,dZ-ddl.m/Z/m0Z0m1Z1er ddl2m3Z3ddl4m5Z5ejldjnZ8ejldjnZ9dZ:e;ge/ejxZ._is_readycxxz NN4 !Underlying socket has been closedr>rreturnNone) memoryviewlensendBLOCKING_IO_ERRORSfilenorr*r create_future add_reader remove_readerr add_writer remove_writer_HAVE_PYOPENSSLr) r0r1r2viewsentr?r3fdr>s r4r,r,]s# ! SXo / $tu+..SXo& /[[]8"#FGTQc#9:,,.COOB 37/! **2.**2.c#:;,,.COOB 37/! **2.**2."z#7O'P,,.COOB 37/Is;! **2.**2.**2.**2.7 /s G4A G4 G4 G1AG,*C/B20C5G,C6G,D.DD.G,.EF?FF?#G,?$G##G,&G4,G11G4c Ktt|}d}dd}||kr7 |j||d}|dk(r td|r|d|S||z }||kr7|S#t$r}|j } | dk(r t ddt|trW|j} |j| ||  | d{7|j| n#|j| wxYwt|trW|j} |j| ||  | d{7|j| n#|j| wxYwtrt|t r|j} |j| ||  |j| || | d{7|j| |j| n'#|j| |j| wxYwYd}~d}~wwxYww)NrcH|jry|jdyr9r:r=s r4r?z,_async_socket_receive_ssl.._is_readyr@rAconnection closedrBrCrD)rG bytearray recv_intoOSErrorrJrKrr*rrLrMrNrrOrPrQr) connlengthr2oncemv total_readr?readr3rTr>s r4_async_socket_receive_sslras &) *  ! 6!# /~~bo619!"566et9$d" 6!J 9& /[[]8"#FGTQc#9:,,.COOB 37/! **2.**2.c#:;,,.COOB 37/! **2.**2."z#7O'P,,.COOB 37/Is;! **2.**2.**2.**2.7 /s H*A HAHH H%AG<:C?CCG<C**6G<!D>&D)'D>,G<>E 0B-<B->!B*B" B*'B-)B**B-c&|j|yr9)sendall)r0r1s r4rorosLLrAcK |jjrytjtd{97wr9)cancel_context cancelledr'rf _POLL_TIMEOUTr[s r4_poll_cancellationrus7     ( ( mmM***  +s 5?=?c8K|j}|}|jdtj} trSt |t tfr=tjt|||d|d{|j|Stjt||||d{|j|S7P7#tj$r}tjd|d}~wwxYw#|j|wxYww)Nr!T)r]r"r$)r%r&r'r(r)r*rrr+ra_async_socket_receiver.r/r#)r0r\ sock_timeoutr#r2errs r4async_receive_data_socketrzs??$LGOOC  # # %D& D9h*?@ )))$4H  %!))%dFD9  %    3nn[)s23  %se8DAC>C?CD%C9C:C=DCCD&C<<DDDDcKtt|}d}||kr8|j|||dd{}|dk(r td||z }||kr8|S7!w)NrrW)rGrXsock_recv_intorZ)r[r\r2r^ bytes_read chunk_lengths r4rwrwst If% &BJ v !00r*+GG 1 -. .l" v  I Hs4AAAAPyPyc|jj}d}|jdk(ry t|dr|j dkDrd}n_|r9|t j z }|dkrd}tt|td}nt}|jj|d|}|jjr td|ry|rtj d ) zABlock until at least one byte is read, or a timeout, or a cancel.FrBNTpendingr)r`r#operation cancelledr$)r[r0rKhasattrrtime monotonicrgrhrssocket_checkerselectrqrrrr/r#)r[deadliner0 timed_outreadable remainingr#s r4 wait_for_readr$s 99>>DI {{}  4 # (:H$t~~'77 > $Ic)];Q?'**11$T71SH    ( (%&;< <   ..- -/ rAct|}t|}d}|jj} ||kr tst rSt ||tjr||z|jt|tjz dnI|0tt|tjz dt}nt}|j||jj||d}|dk(r t/d||z }||kr|j||S#t $r9|j"j$r t'ddt)j*ddt(j*$r[|j"j$r t'ddts.t s(|j,s||tjz dkrYt.$rJ} |j"j$r t'ddt1| t2j4k(rYd} ~ d} ~ wwxYw#|j|wxYw)Nrrr$rW)rXrGr[r%_PYPY_WINDOWSrr get_timeoutset_conn_timeoutrgrrrhrsrYrJrqrrrr/r#is_sdamrZrerrnoEINTR) r[r\rr1r^r} orig_timeout short_timeoutr~r3s r4 receive_datarEs F C CBJ99'')L.,6!& H!$1((*x/C--c(T^^=M2Mq.QR+(+C4>>;K0KQ,OQ^(_ (5 ))-8#yy222jk?C 2q 122 , &JW6!Z l+ I?& <&&00-.CD$Nnn[1t;>> &&00-.CD$N<< , 4>>#33a7 &&00-.CD$N(-<   l+sHH0CD.H0.B*H-H0H-#>H(!H0'H((H--H00IcbeZdZd dZed dZd dZd dZd dZed dZ ed dZ y) NetworkingInterfaceBasec||_yr9rt)selfr[s r4__init__z NetworkingInterfaceBase.__init__s  rActr9NotImplementedErrorrs r4r%z"NetworkingInterfaceBase.gettimeout!!rActr9rrr#s r4r&z"NetworkingInterfaceBase.settimeout!!rActr9rrs r4closezNetworkingInterfaceBase.closerrActr9rrs r4 is_closingz"NetworkingInterfaceBase.is_closingrrActr9rrs r4get_connz NetworkingInterfaceBase.get_connrrActr9rrs r4r0zNetworkingInterfaceBase.sockrrAN)r[r )rEr r# float | NonerErFrEbool) __name__ __module__ __qualname__rpropertyr%r&rrrr0rAr4rrsR"""""""""rArcneZdZdfd Zed dZd dZd dZd dZed dZ eddZ xZ S)AsyncNetworkingInterfacec$t||yr9superrrr[ __class__s r4rz!AsyncNetworkingInterface.__init__ rAc4|jdjSNr[r%rs r4r%z#AsyncNetworkingInterface.gettimeoutsyy|&&&rAc@|jdj|yrr[r&rs r4r&z#AsyncNetworkingInterface.settimeouts ! (rAcK|jdj|jdjd{y7wr)r[r wait_closedrs r4rzAsyncNetworkingInterface.closes4 ! iil&&(((s>AAAc<|jdjS)Nrr[rrs r4rz#AsyncNetworkingInterface.is_closingsyy|&&((rAc |jdSrrtrs r4rz!AsyncNetworkingInterface.get_connsyy|rAc>|jdjdS)Nrr/)r[get_extra_infors r4r0zAsyncNetworkingInterface.socksyy|**844rA)r[z!tuple[Transport, PyMongoProtocol]rErrrErFr)rEPyMongoProtocol)rE socket.socket) rrrrrr%r&rrrr0 __classcell__rs@r4rrsS'')))55rArcteZdZd fd Zd dZd dZd dZddZeddZ eddZ ddZ dd Z xZ S)NetworkingInterfacec$t||yr9rrs r4rzNetworkingInterface.__init__rrAc6|jjSr9rrs r4r%zNetworkingInterface.gettimeoutyy##%%rAc:|jj|yr9rrs r4r&zNetworkingInterface.settimeouts W%rAc8|jjyr9)r[rrs r4rzNetworkingInterface.closes rAc6|jjSr9rrs r4rzNetworkingInterface.is_closingrrAc|jSr9rtrs r4rzNetworkingInterface.get_conn yyrAc|jSr9rtrs r4r0zNetworkingInterface.sockrrAc6|jjSr9)r[rKrs r4rKzNetworkingInterface.filenosyy!!rAc8|jj|Sr9)r[rY)rbuffers r4rYzNetworkingInterface.recv_intosyy""6**rA)r[Union[socket.socket, _sslConn]rrrr)rEr)rEint)rbytesrEr)rrrrr%r&rrrrr0rKrYrrs@r4rrsN&&&"+rArceZdZdddZddZeddZddZddZddZ ddZ dd Z dd Z dd Z ddd Zddd ZdddZddZy)rNcd|_ttd|_d|_ttd|_d|_d|_d|_d|_ d|_ d|_ d|_ d|_ d|_||_d|_d|_t$|_d|_t+j,j/|_t3j4|_t3j4|_y)Nr TF) transportrGrX_header _header_index_compression_header_compression_index_message_message_index_expecting_header_expecting_compression _message_size_op_code_connection_lost _read_waiter_timeout_is_compressed_compressor_idr_max_message_size _response_tor'r(rL_closed collectionsdeque_pending_messages_done_messagesrs r4rzPyMongoProtocol.__init__s$(")B-0 #-il#; "#.2 !%&+# %.2 #-1!1+///1??A > $ $ &01 1 W% %%'sAAcK|jr |jj||_|j r#|j j d{}n|jr%|jjr tdtjj}|jj| |d{}||j vr|j j| |r1|\}}}}|||k7rtd|d|| t||}||fStd#t$r tddwxYw77#||j vr|j j|wwxYww)zARead a single MongoDB Wire Protocol message from this connection.zconnection is already closedNGot response id  but expected rW)rrAttributeErrorrZrrpopleftrr'r(rLrappendremoverr) r request_idmax_message_sizer read_waiterop_code compressor_id response_todatas r4r`zPyMongoProtocol.read s >> H--/"2    //7799G~~$..";";"=<==!224BBDK  " " ) )+ 6 < ++$"5"55''..{; 8? 5G]K%,'*;/ ~V(!$ 6= )**5" H<=4G H:,$"5"55''..{;6sS F E0F EA3F EEEA(F EF E,F  F cB|jr1|jsttd|_|jS|jr|j |j dS|jr|j|jdS|j|jdS)a8Called to allocate a new receive buffer. The asyncio loop calls this method expecting to receive a non-empty buffer to fill with data. If any data does not fit into the returned buffer, this method will be called again until either no data remains or an empty buffer is returned. i@N) rrrGrXrrrrrrr)rsizehints r4 get_bufferzPyMongoProtocol.get_buffer+s  == *9U+; < ==   ! !<< 2 2 45 5  & &++D,C,C,EF F}}T00233rAc|dk(r|jtdy|jry|jry|xj|z c_|jdk\rTd|_ |j \|_|_|_|_ tt|j |_ y|jrI|xj|z c_|jdk\r$d|_ |j\|_|_y|xj"|z c_|j"|j k\rd|_|j$j'|j(r|j(j+}n"t-j.j1}|j3r|jdy|j5|j|j |j|jf|j6j9|d|_d|_d|_d|_d|_ d|_d|_d|_yy#t$r}|j|Yd}~yd}~wwxYw)z9Called when the buffer was updated with the received datarrWNrFrT)rrZrrrprocess_headerrrrrrrGrXrrprocess_compression_headerrrr pause_readingrr r'r(rLr;r<rr )rnbytesr3results r4buffer_updatedzPyMongoProtocol.buffer_updated@s" Q; JJw23 4     ! !   & ( !!R').&  ++- * )3 !+9T5G5G+H I   & &  # #v - #&&!+.3+595T5T5V2 t2  v%   $"4"4 4%)D " NN ( ( *%%//779 113AAC{{} 4     3 3T5F5F V     & &v .!"D &'D #"#D !"D  DMDM"&D  $D 3 5%JJsOs&)I I9I44I9c t|j\}}}}d}|dk(r|dkrtd|dd}|dz}|dkrtd|d ||jkDrtd|d |jd |dz |||fS) z&Unpack a MongoDB Wire Protocol header.FMessage length (zA) not longer than standard OP_COMPRESSED message header size (25)Trr3) not longer than standard message header size (16)*) is larger than server max message size ())_UNPACK_HEADERrrr)rr\_rrexpecting_compressions r4rzPyMongoProtocol.process_header{s*8*F'; % d?|#&vj0qr%) ! aKF R<"6*,_`  D** *"6*-!!%!7!7 :!=  {G[2GGGrAc<t|j\}}}||fS)z2Unpack a MongoDB Wire Protocol compression header.)_UNPACK_COMPRESSION_HEADERr)rrr&rs r4rz*PyMongoProtocol.process_compression_headers$$>t?W?W$X!M %%rAct|j}|D]R}|js%||jdn|j ||j j |Tyr9)listrr;r< set_exceptionrr )rr3rmsgs r4_resolve_pending_messagesz)PyMongoProtocol._resolve_pending_messagess]t--. ,C88:;NN4(%%c*    & &s +  ,rAch|jj|j|d|_y)NT)rabortr.rrr3s r4rzPyMongoProtocol.closes(  &&s+ $rAc|j||jjs|jjdyyr9)r.rr;r<r1s r4connection_lostzPyMongoProtocol.connection_losts7 &&s+||  " LL # #D )#rAc8K|jd{y7wr9)rrs r4rzPyMongoProtocol.wait_closedslls r9)r#Optional[float]rr)rrrErF)rrrErF)r  Optional[int]rrrEztuple[bytes, int])rrrErG)rrrErF)rEztuple[int, int, int, bool])rEztuple[int, int])r3zOptional[Exception]rErFr)rrrrr&rr%rrr`rrrrr.rr3rrrAr4rrs\M4 S( +D4*9%vH.& ,% * rArcK tj|j||jd{y7#tj$r}t j d|d}~wwxYww)Nr"r$)r'r+rr%r.r/r#)r[r1r3s r4 async_sendallr8sS3tzz#HHH   3nn[)s23s1A03?=?A0?A-A((A--A0cK|jj}tjrtj}n|rt j |z}nd}|r!t|t j z d}tt|}t|jjj||}||g} tj||tjd{\}} | D]} | j| rtj| d{t!|dk(rt#j$d||vr%|j'\} } t(| } | | St1d77]#t*$r%t-d| dt)j.dwxYw#tj2$r7|D]} | jtj|d{7wxYww)1Receive a raw BSON message or raise socket.error.Nr)r# return_whenr$ Got opcode rr)r[r%r r get_deadlinerrrgrrurr`r'waitFIRST_COMPLETEDcancelrHr/r#rrKeyErrorrkeysrCancelledError)r[r rr#rcancellation_task read_tasktasksr;rtaskrr unpack_replys r4async_receive_messagerIsii""G %%' ~~''1HHh!1115#$6t$<=DII..33J@PQRI ) *E%ll 70G0G  g D KKM  ,,w' ' ' t9>..- -  %,,.MD' ,W5  % %!"788% ( #!'N=;M;M;O:RS   ! ! D KKM ll5!!! sgCH  )F<2F38F<+F ,>F<+ F 4F<;H < F< F< .F99F<H?HH ctjrtj}n6|jj }|rt j |z}nd}tt|d|\}}}}|||k7rtd|d||dkrtd|d||kDrtd|d|d|d k(r5tt|d |\}}} tt||d z || } nt||dz |} t|} | | S#t$r%td |dtjdwxYw) r:Nrrrr!r"r#r$rrr r<)r rr=r[r%rrr%rrr)rrrArB) r[r rrr#r\r&rrrrrHs r4receive_messagerKs %%'))&&( ~~''1HH&4\$H5U&V#FA{G  $"2;/PZ~ ^_ _ |vj([ \    vj)-0 3  $$>|DRSU]?^$_!M,tVb[(C]SD&2+x8$W-   'N=3E3E3G2J K  s D.E)r0rr1rrErF)r0rr1rr2rrErF)F) r[rr\rr2rr]Optional[bool]rErG)r0rr1rrirrErF) r[rr\rrirr]rLrErG)r[rrErF)r0rr\rrErG)r[rr\rr2rrErG)r[rrr5rErF)r[rr\rrr5rErG)r[rr1rrErF)r[rr r6rrrEUnion[_OpReply, _OpMsg])r[rr r6rrrErM)Q__doc__ __future__rr'rrr/structsysrrrrrrtypingr r r r pymongor rpymongo._asyncio_taskrpymongo.commonrpymongo.compression_supportrpymongo.errorsrrpymongo.messagerrrpymongo.socket_checkerrsslrrr) ImportErrorpymongo.pyopenssl_contextrrQpymongo.ssl_supportrrrpymongo.asynchronous.poolrpymongo.synchronous.poolrStructunpackr%r)rsBlockingIOErrorrJr5platformr,rarorurzrwversionrrrrrrrrr8rIrKrrAr4res-" YY'-+2=;;8'I2O  93w'..*V]]6299 %b(@b;CaCab ! <<7)/,)/38)/@Q)/ )/XV[00 #0+<0DR0 0l,38AR ,W\ #,=ES 6+& (&25&&2   !$ ,=   #++ <<7 ".B<~""45656+1+>T&Tn3-0 000 0hJZ' '"/'CF''{IOHs$2 G=GG  G  GG