o
    Åîï]<0  ã                   @   sx  d Z ddlZddlZddlZddlZddlZdZdZzddlmZ ej	ej
B ejB ejB ZW n ey9   dZY nw zddlmZ W n eyM   eZY nw ddlmZ ddlmZ dd	l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# e $d¡j%Z&										ddd„Z'e $d¡j%Z(efdd„Z)esªdd„ Z*ndd„ Z*dd„ Z+G dd„ de,ƒZ-dS )z&Internal network layer helper methods.é    NT)ÚpollF)Úerror)Ú_decode_all_selective)ÚPY3)ÚhelpersÚmessage)ÚMAX_MESSAGE_SIZE)Ú
decompressÚ_NO_COMPRESSION)ÚAutoReconnectÚNotMasterErrorÚOperationFailureÚProtocolError)Ú_UNPACK_REPLYz<iiiic           (      C   sð  t t|ƒƒ}|d }|rdnd}|}|r|st ||¡}|r?|r#|js?|jr+|j|d< |r?|jjr?|j	dur?|j	| 
di ¡d< |durG||d< |duoM|j}|rUtj ¡ }|r_| ¡ tv r_d}|ru|jru|jjsu|j ||||¡ }}d}|r |r{d	nd}tj||||||||d
�\}}}}|rŸ|durŸ||krŸt |||¡ nt ||dd|d|||¡	\}}}|durÅ||tj krÅt |||tj ¡ |rÛtj ¡ | } | ||||¡ tj ¡ }z7|  |¡ |rì|rìd}!ddi}"n%t| |ƒ}!|!j||d�}#|#d }"|�r| |"|¡ |	�rtj|"d|
|d� W n4 t�yF }$ z'|�rAtj ¡ | |  }%t|$t t!fƒ�r3|$j"}&nt #|$¡}&| $|%|&|||¡ ‚ d}$~$ww |�r\tj ¡ | |  }%| %|%|"|||¡ |�rv|j�rv|!�rv|j &|! '¡ ¡}'t(|'||ƒd }"|"S )a  Execute a command over the socket, or raise socket.error.

    :Parameters:
      - `sock`: a raw socket instance
      - `dbname`: name of the database on which to run the command
      - `spec`: a command document as an ordered dict type, eg SON.
      - `slave_ok`: whether to set the SlaveOkay wire protocol bit
      - `is_mongos`: are we connected to a mongos?
      - `read_preference`: a read preference
      - `codec_options`: a CodecOptions instance
      - `session`: optional ClientSession instance.
      - `client`: optional MongoClient instance for updating $clusterTime.
      - `check`: raise OperationFailure if there are errors
      - `allowable_errors`: errors to ignore if `check` is True
      - `address`: the (host, port) of `sock`
      - `check_keys`: if True, check `spec` for invalid keys
      - `listeners`: An instance of :class:`~pymongo.monitoring.EventListeners`
      - `max_bson_size`: The maximum encoded bson size for this server
      - `read_concern`: The read concern for this command.
      - `parse_write_concern_error`: Whether to parse the ``writeConcernError``
        field in the command response.
      - `collation`: The collation for this command.
      - `compression_ctx`: optional compression Context.
      - `use_op_msg`: True if we should use OP_MSG.
      - `unacknowledged`: True if this is an unacknowledged command.
      - `user_fields` (optional): Response fields that should be decoded
        using the TypeDecoders from codec_options, passed to
        bson._decode_all_selective.
    z.$cmdé   r   ÚreadConcernNÚafterClusterTimeÚ	collationFé   )ÚctxéÿÿÿÿÚoké   )Úcodec_optionsÚuser_fields)Úparse_write_concern_error))ÚnextÚiterr   Ú_maybe_add_read_preferenceÚin_transactionÚlevelÚdocumentÚoptionsÚcausal_consistencyÚoperation_timeÚ
setdefaultÚenabled_for_commandsÚdatetimeÚnowÚlowerr
   Ú
_encrypterÚ_bypass_auto_encryptionÚencryptÚ_op_msgÚ_raise_document_too_largeÚqueryÚ_COMMAND_OVERHEADÚpublish_command_startÚsendallÚreceive_messageÚunpack_responseÚ_process_responser   Ú_check_command_responseÚ	ExceptionÚ
isinstancer   r   ÚdetailsÚ_convert_exceptionÚpublish_command_failureÚpublish_command_successÚdecryptÚraw_command_responser   )(ÚsockÚdbnameÚspecÚslave_okÚ	is_mongosÚread_preferencer   ÚsessionÚclientÚcheckÚallowable_errorsÚaddressÚ
check_keysÚ	listenersÚmax_bson_sizeÚread_concernr   r   Úcompression_ctxÚ
use_op_msgÚunacknowledgedr   ÚnameÚnsÚflagsÚorigÚpublishÚstartÚ
request_idÚmsgÚsizeÚmax_doc_sizeÚencoding_durationÚreplyÚresponse_docÚunpacked_docsÚexcÚdurationÚfailureÚ	decrypted© rc   úA/var/www/html/env/lib/python3.10/site-packages/pymongo/network.pyÚcommand5   sÂ   (

ÿÿÿ

ÿÿþ€
þÿ



ÿþ€

ÿ€÷

ÿÿÿre   z<iiBc           
      C   sÚ   t t| dƒƒ\}}}}|dur||krtd||f ƒ‚|dkr&td|f ƒ‚||kr2td||f ƒ‚|dkrKtt| dƒƒ\}}}tt| |d ƒ|ƒ}nt| |d ƒ}z	t| }	W |	|ƒS  tyl   td	|t ¡ f ƒ‚w )
z1Receive a raw BSON message or raise socket.error.é   Nz"Got response id %r but expected %rzEMessage length (%r) not longer than standard message header size (16)z?Message length (%r) is larger than server max message size (%r)iÜ  é	   é   zGot opcode %r but expected %r)Ú_UNPACK_HEADERÚ_receive_data_on_socketr   Ú_UNPACK_COMPRESSION_HEADERr	   r   ÚKeyErrorÚkeys)
r?   rW   Úmax_message_sizeÚlengthÚ_Úresponse_toÚop_codeÚcompressor_idÚdataÚunpack_replyrc   rc   rd   r3   À   s@   ÿÿÿÿ
ÿÿ
ý
ÿÿr3   c              
   C   s    t |ƒ}d}|rLz|  |¡}W n ttfy+ } zt|ƒtjkr&W Y d }~q‚ d }~ww |dkr4tdƒ‚||||t|ƒ …< |t|ƒ7 }|t|ƒ8 }|st	|ƒS )Nr   ó    úconnection closed)
Ú	bytearrayÚrecvÚIOErrorÚOSErrorÚ_errno_from_exceptionÚerrnoÚEINTRr   ÚlenÚbytes)r?   ro   ÚbufÚiÚchunkr_   rc   rc   rd   rj   æ   s$   €ýôrj   c              
   C   s�   t |ƒ}t|ƒ}d}||k rFz|  ||d … ¡}W n ttfy5 } zt|ƒtjkr0W Y d }~q
‚ d }~ww |dkr>tdƒ‚||7 }||k s|S )Nr   rw   )	rx   Ú
memoryviewÚ	recv_intorz   r{   r|   r}   r~   r   )r?   ro   r�   ÚmvÚ
bytes_readÚchunk_lengthr_   rc   rc   rd   rj   ù   s"   €ýöc                 C   s$   t | dƒr| jS | jr| jd S d S )Nr}   r   )Úhasattrr}   Úargs)r_   rc   rc   rd   r|     s
   

r|   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚSocketCheckerc                 C   s*   t rt ¡ | _tƒ | _d S d | _d | _d S )N)Ú	_HAS_POLLÚ	threadingÚLockÚ_lockr   Ú_poller)Úselfrc   rc   rd   Ú__init__  s
   

zSocketChecker.__init__c              
   C   s  	 zB| j r6| j�% | j  |t¡ z| j  d¡}W | j  |¡ n| j  |¡ w W d  ƒ n1 s0w   Y  nt |gg g d¡\}}}W n@ ttfyM   ‚  t	yV   Y dS  t
tfyz } zt|ƒtjtjfv roW Y d}~q W Y d}~dS d}~w tyƒ   Y dS w t|ƒdkS )zHReturn True if we know socket has been closed, False otherwise.
        Tr   N)r�   r�   ÚregisterÚ_EVENT_MASKr   Ú
unregisterÚselectÚRuntimeErrorrl   Ú
ValueErrorÚ_SELECT_ERRORrz   r|   r}   r~   ÚEAGAINr7   r   )r‘   r?   Úrdrp   r_   rc   rc   rd   Úsocket_closed  s2   û€€€ýzSocketChecker.socket_closedN)Ú__name__Ú
__module__Ú__qualname__r’   rœ   rc   rc   rc   rd   r‹     s    r‹   )TNNFNNNFNNFFN).Ú__doc__r'   r}   r–   Ústructr�   rŒ   r”   r   ÚPOLLINÚPOLLPRIÚPOLLERRÚPOLLHUPÚImportErrorr   r™   r{   Úbsonr   Úbson.py3compatr   Úpymongor   r   Úpymongo.commonr   Úpymongo.compression_supportr	   r
   Úpymongo.errorsr   r   r   r   Úpymongo.messager   ÚStructÚunpackri   re   rk   r3   rj   r|   Úobjectr‹   rc   rc   rc   rd   Ú<module>   s^   ÿÿÿ
ö 
%
	