
    l8'h>                    R   d Z ddlmZ ddlZddlZddlZddlZddlZddlm	Z	m
Z
mZmZmZmZmZmZ ddlmZ ddlmZmZmZ ddlmZ ddlmZmZ dd	lmZmZmZm Z  dd
l!m"Z"m#Z#m$Z$ ddl%m&Z&m'Z'm(Z( ddl)m*Z* ddl+m,Z,m-Z-m.Z.m/Z/m0Z0 ddl1m2Z2 e	rFddlm3Z3 ddlm4Z4m5Z5m6Z6 ddl)m7Z7 ddl8m9Z9 ddl:m;Z; ddl<m=Z= ddl>m?Z? ddl@mAZA ddlBmCZCmDZDmEZEmFZF ddlGmHZH dZI	 	 	 	 	 	 	 	 	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZJef	 	 	 	 	 	 	 d dZKd!dZLd"dZMy)#z&Internal network layer helper methods.    )annotationsN)TYPE_CHECKINGAnyMappingMutableMappingOptionalSequenceUnioncast)_decode_all_selective)_csothelpers_sharedmessage)MAX_MESSAGE_SIZE)_NO_COMPRESSION
decompress)NotPrimaryErrorOperationFailureProtocolError_OperationCancelled)_COMMAND_LOGGER_CommandStatusMessage
_debug_log)_UNPACK_REPLY_OpMsg_OpReply)_is_speculative_authenticate)_POLL_TIMEOUT_UNPACK_COMPRESSION_HEADER_UNPACK_HEADERBLOCKING_IO_ERRORSsendall)_errno_from_exception)CodecOptions)SnappyContextZlibContextZstdContext)_EventListeners)ReadConcern)_ServerMode)ClientSession)MongoClient)
Connection)_Address_CollationIn_DocumentOut_DocumentType)WriteConcernTc                   t        t        |            }|dz   }d}|}|r|s|J t        j                  ||      }|r=|r|j                  s/|j
                  r|j                  |d<   |r|j                  ||        |||d<   |duxr |j                  }t        j                  j                         }|rt        ||      }|r|j                         t        v rd}|rA|j                  r5|j                  j                  s|j                  j!                  |||      x}}|r| j#                  ||       t%        j&                  ||       |rl|rt(        j*                  nd}||rt(        j,                  ndz  }t        j.                  ||||||      \  }}}} |r?|=| |kD  r8t        j0                  |||       n t        j2                  d|dd|d||      \  }}}|>||t        j4                  z   kD  r(t        j0                  |||t        j4                  z          |t7        j8                  t:        j<                        rt?        t6        |j@                  jB                  tD        jF                  |t        t        |            |||| jH                  | jJ                  | jL                  d   | jL                  d	   | jN                  
       |r3|J |
J |jQ                  ||||
| jJ                  | jN                         	 tS        | jT                  |       |r	|rd}!dd	i}"nntW        | |      }!|!jX                  | _,        |!j[                  ||      }#|#d   }"|r|j]                  |"|       |r#t_        j`                  |"| jb                  |	|       t        j                  j                         |z
  }%|t7        j8                  t:        j<                        rt?        t6        |j@                  jB                  tD        jt                  |%|"t        t        |            |||| jH                  | jJ                  | jL                  d   | jL                  d	   | jN                  d|v        |r6|J |
J |jw                  |%|"|||
| jJ                  | jN                  ||	       |rQ|j                  rE|!rC|j                  jy                  |!j{                               }'t}        dt        |'||      d         }"|"S # td        $ rS}$t        j                  j                         |z
  }%tg        |$th        tj        f      r|$jl                  }&nt        jn                  |$      }&|t7        j8                  t:        j<                        rt?        t6        |j@                  jB                  tD        jp                  |%|&t        t        |            |||| jH                  | jJ                  | jL                  d   | jL                  d	   | jN                  tg        |$tj                     |r5|J |
J |js                  |%|&|||
| jJ                  | jN                  |        d}$~$ww xY w)a  Execute a command over the socket, or raise socket.error.

    :param conn: a Connection instance
    :param dbname: name of the database on which to run the command
    :param spec: a command document as an ordered dict type, eg SON.
    :param is_mongos: are we connected to a mongos?
    :param read_preference: a read preference
    :param codec_options: a CodecOptions instance
    :param session: optional ClientSession instance.
    :param client: optional MongoClient instance for updating $clusterTime.
    :param check: raise OperationFailure if there are errors
    :param allowable_errors: errors to ignore if `check` is True
    :param address: the (host, port) of `conn`
    :param listeners: An instance of :class:`~pymongo.monitoring.EventListeners`
    :param max_bson_size: The maximum encoded bson size for this server
    :param read_concern: The read concern for this command.
    :param parse_write_concern_error: Whether to parse the ``writeConcernError``
        field in the command response.
    :param collation: The collation for this command.
    :param compression_ctx: optional compression Context.
    :param use_op_msg: True if we should use OP_MSG.
    :param unacknowledged: True if this is an unacknowledged command.
    :param user_fields: Response fields that should be decoded
        using the TypeDecoders from codec_options, passed to
        bson._decode_all_selective.
    :param exhaust_allowed: True if we should enable OP_MSG exhaustAllowed.
    z.$cmdFNreadConcern	collationr   )ctx   )clientIdr   commandcommandNamedatabaseName	requestIdoperationIddriverConnectionIdserverConnectionId
serverHost
serverPort	serviceId)
service_idok)codec_optionsuser_fields)parse_write_concern_error)r9   r   
durationMSfailurer;   r<   r=   r>   r?   r@   rA   rB   rC   isServerSideError)rD   database_namespeculativeAuthenticate)r9   r   rI   replyr;   r<   r=   r>   r?   r@   rA   rB   rC   speculative_authenticate)rD   speculative_hellorL   r0   )@nextiterr   _maybe_add_read_preferencein_transactionleveldocument_update_read_concernenabled_for_commandsdatetimenowr   lowerr   
_encrypter_bypass_auto_encryptionencryptapply_timeoutr   apply_write_concernr   MORE_TO_COMEEXHAUST_ALLOWED_op_msg_raise_document_too_large_query_COMMAND_OVERHEADr   isEnabledForloggingDEBUGr   _topology_settings_topology_idr   STARTEDidserver_connection_idaddressrD   publish_command_startr"   connreceive_messagemore_to_comeunpack_response_process_responser   _check_command_responsemax_wire_version	Exception
isinstancer   r   details_convert_exceptionFAILEDpublish_command_failure	SUCCEEDEDpublish_command_successdecryptraw_command_responser   r   )(rq   dbnamespec	is_mongosread_preferencerF   sessionclientcheckallowable_errorsro   	listenersmax_bson_sizeread_concernrH   r5   compression_ctx
use_op_msgunacknowledgedrG   exhaust_allowedwrite_concernnamensrP   origpublishstartflags
request_idmsgsizemax_doc_sizerN   response_docunpacked_docsexcdurationrJ   	decrypteds(                                           W/var/www/html/trade_iq/venv/lib/python3.12/site-packages/pymongo/synchronous/network.pyr:   r:   G   s   f T
D	'	B D***11$HW)?)?"."7"7D((t4%[t#F	(F(FG!!#E8tD4::<?:&##F,=,=,U,U''//mLLt 64(	dM2'5##1?''A.5oo4-_/
+
C|
 m7L=<X--dD-H 'r1b$m_!

C  TMG<U<U,U%U))$mgF_F_6_`''622??-55 d,#$&#'77#'#<#<<<?<<?// $$$"""''%% 	( 	
=		3.E*.L#D*5E % 2 2D!11+ 2 M )+L((w?66 ))$.G	Z   $$&.H''622??-77#" d,#$&#'77#'#<#<<<?<<?//)Bd)J" $$$"""))%%/  	* 
	
 &##%%--e.H.H.JK	1)]KXYZ[
 a  &$$((*U2cO-=>?$'KKG005G++GMM:##66CC188'# $T$Z 0!'( *'+ww'+'@'@#||A#||A"oo&06F&G" (((&&&--))??$ . 	 	M&s   8BS& &
Y0EX>>Yc                   t        j                         rt        j                         }n6| j                  j	                         }|rt        j                         |z   }nd}t        t        | d|            \  }}}}|||k7  rt        d|d|      |dk  rt        d|d      ||kD  rt        d|d|d      |d	k(  r5t        t        | d
|            \  }}}	t        t        | |dz
  |      |	      }
nt        | |dz
  |      }
	 t        |   } ||
      S # t        $ r% t        d|dt        j                               dw xY w)z1Receive a raw BSON message or raise socket.error.N   zGot response id z but expected zMessage length (z3) not longer than standard message header size (16)z*) is larger than server max message size ()i  	      zGot opcode )r   get_timeoutget_deadlinerq   
gettimeouttime	monotonicr    _receive_data_on_socketr   r   r   r   KeyErrorkeys)rq   r   max_message_sizedeadlinetimeoutlength_response_toop_codecompressor_iddataunpack_replys               r   rr   rr   3  s    %%'))&&(~~''1HH&45LTSUW_5`&a#FA{G$"2;/PZ~ ^__|vj([\
 	
   vj )-03
 	
 $$>#D!X6%
!M 1$XNP]^&tVb[(C$W-
 	  'N=3E3E3G2JK
	s   		D .Ec                   | j                   }d}|j                         dk(  ry	 t        |d      r|j                         dkD  rd}n_|r9|t	        j
                         z
  }|dk  rd}t        t        |t              d      }nt        }| j                  j                  |d|      }| j                  j                  rt        d      |ry|rt        j                  d	      )
zABlock until at least one byte is read, or a timeout, or a cancel.Fr7   NTpendingr   )readr   zoperation cancelled	timed out)rq   filenohasattrr   r   r   maxminr   socket_checkerselectcancel_context	cancelledr   socketr   )rq   r   sock	timed_outreadable	remainingr   s          r   wait_for_readr   _  s    99DI{{}
4#(:H $t~~'77	 > $Ic)];Q?'**11$T71SH((%&;<<..--/     c                   t        |      }t        |      }d}||k  r	 t        | |       t        j                         r2|0| j                  t        |t        j                         z
  d             | j                  j                  ||d        }|dk(  rt        d      ||z  }||k  r|S # t        $ r t        j                  d      d t        $ r'}t        |      t         j"                  k(  rY d }~ۂ d }~ww xY w)Nr   r   zconnection closed)	bytearray
memoryviewr   r   r   set_conn_timeoutr   r   r   rq   	recv_intor!   r   r   OSErrorr#   errnoEINTR)rq   r   r   bufmv
bytes_readchunk_lengthr   s           r   r   r     s    
F
C	CBJ
v
	$)   "x';%%c(T^^5E*Eq&IJ99..r*+?L 1-..l"
% v
( I " 	8..-47 	$S)U[[8	s   A0B, ,'C:C54C55C:)TNNNNNFNNFFNFN).rq   r-   r   strr   zMutableMapping[str, Any]r   boolr   zOptional[_ServerMode]rF   zCodecOptions[_DocumentType]r   zOptional[ClientSession]r   zOptional[MongoClient]r   r   r   z#Optional[Sequence[Union[str, int]]]ro   zOptional[_Address]r   zOptional[_EventListeners]r   Optional[int]r   zOptional[ReadConcern]rH   r   r5   zOptional[_CollationIn]r   z4Union[SnappyContext, ZlibContext, ZstdContext, None]r   r   r   r   rG   zOptional[Mapping[str, Any]]r   r   r   zOptional[WriteConcern]returnr1   )rq   r-   r   r   r   intr   zUnion[_OpReply, _OpMsg])rq   r-   r   Optional[float]r   None)rq   r-   r   r   r   r   r   r   )N__doc__
__future__r   rY   r   rh   r   r   typingr   r   r   r   r   r	   r
   r   bsonr   pymongor   r   r   pymongo.commonr   pymongo.compression_supportr   r   pymongo.errorsr   r   r   r   pymongo.loggerr   r   r   pymongo.messager   r   r   pymongo.monitoringr   pymongo.network_layerr   r   r    r!   r"   pymongo.socket_checkerr#   r$   r%   r&   r'   r(   pymongo.read_concernr)   pymongo.read_preferencesr*   "pymongo.synchronous.client_sessionr+    pymongo.synchronous.mongo_clientr,   pymongo.synchronous.poolr-   pymongo.typingsr.   r/   r0   r1   pymongo.write_concernr2   _IS_SYNCr:   rr   r   r    r   r   <module>r      s   - "     	 	 	 ' 2 2 + C  N M ; ; ;  9!SS204@<3SS2 <@"&+/#'*.&+(,LP /3!,0-i
ii #i 	i
 +i /i %i "i i :i  i )i !i (i  $i  &!i" J#i$ %i& 'i( -)i* +i, *-i. /iZ JZ)
)"/)CF))X.Br   