o
    C©ÌhÕM  ã                   @  sX  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 ddlmZ ddlmZ ddlmZmZmZmZ ddl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# e!e"fZ$e	rvddl%m&Z& ddl'm(Z( zddl)m*Z*m+Z+m,Z,m)Z) dJdd„Z-W n e.y—   dJdd„Z-Y nw dZ/dZ0dZ1ej2dkrózddl3Z4W n e.y¶   ddl4Z4Y nw dd„ Z5z%e4 6e4j7d¡�Z8e5e8ddƒZ9e5e8dd ƒZ:W d  ƒ n1 sÚw   Y  W n e;yí   dZ9d Z:Y nw d!d"„ Z<n
dKd(d)„Z=dLd*d"„Z<		dMdNd4d5„Z>G d6d7„ d7ƒZ?dOd;d<„Z@dPd>d?„ZAdQdAdB„ZBdOdCdD„ZCdPdEdF„ZDdRdHdI„ZEdS )Sz)Pool utilities and shared helper methods.é    )ÚannotationsN)ÚTYPE_CHECKINGÚAnyÚNoReturnÚOptionalÚUnion)Ú_csot)Ú_getaddrinfo)ÚAutoReconnectÚConnectionFailureÚNetworkTimeoutÚ_CertificateError)Ú_get_timeout_detailsÚformat_timeout_details)ÚAsyncNetworkingInterfaceÚNetworkingInterfaceÚPyMongoProtocol)ÚPoolOptions)Ú
PYSSLErrorÚSSLErrorÚ_has_sni)Ú_sslConn)Ú_Address)ÚF_GETFDÚF_SETFDÚ
FD_CLOEXECÚfcntlÚfdÚintÚreturnÚNonec                 C  s   t | tƒ}t | t|tB ƒ dS )z8Set the close-on-exec flag on the given file descriptor.N)r   r   r   r   )r   Úflags© r"   úb/var/www/html/Moveonwheels/move_on_wheels/venv/lib/python3.10/site-packages/pymongo/pool_shared.pyÚ_set_non_inheritable_non_atomic4   s   
r$   c                 C  s   dS )z6Dummy function for platforms that don't provide fcntl.Nr"   )r   r"   r"   r#   r$   =   s    éx   é
   é	   Úwin32c              	   C  s6   zt  | |¡\}}t|ƒW S  ttfy   | Y S w ©N)ÚwinregÚQueryValueExr   ÚOSErrorÚ
ValueError)ÚkeyÚnameÚdefaultÚvalueÚ_r"   r"   r#   Ú_queryK   s   
ýr3   z2SYSTEM\CurrentControlSet\Services\Tcpip\ParametersÚKeepAliveTimei Ým ÚKeepAliveIntervaléè  c                 C  sH   t ttd ƒ}t ttd ƒ}|tk s|tk r"|  tjd||f¡ d S d S )Nr6   é   )ÚminÚ_WINDOWS_TCP_IDLE_MSÚ_MAX_TCP_KEEPIDLEÚ_WINDOWS_TCP_INTERVAL_MSÚ_MAX_TCP_KEEPINTVLÚioctlÚsocketÚSIO_KEEPALIVE_VALS)ÚsockÚidle_msÚinterval_msr"   r"   r#   Ú_set_keepalive_timesa   s
   ÿrC   r@   úsocket.socketÚ
tcp_optionÚstrÚ	max_valuec                 C  s`   t t|ƒr.tt|ƒ}z|  tj|¡}||kr!|  tj||¡ W d S W d S  ty-   Y d S w d S r)   )Úhasattrr>   ÚgetattrÚ
getsockoptÚIPPROTO_TCPÚ
setsockoptr,   )r@   rE   rG   Úsockoptr0   r"   r"   r#   Ú_set_tcp_optioni   s   

ÿÿ÷rN   c                 C  s(   t | dtƒ t | dtƒ t | dtƒ d S )NÚTCP_KEEPIDLEÚTCP_KEEPINTVLÚTCP_KEEPCNT)rN   r:   r<   Ú_MAX_TCP_KEEPCNT©r@   r"   r"   r#   rC   v   s   Úaddressr   ÚerrorÚ	ExceptionÚ
msg_prefixúOptional[str]Útimeout_detailsúOptional[dict[str, float]]r   c                 C  sŽ   | \}}|durd|||f }n|› d|› �}|r|| }d|vr'|t |ƒ7 }t|tjƒr2t|ƒ|‚t|tƒrBdt|ƒv rBt|ƒ|‚t|ƒ|‚)z9Convert a socket.error to ConnectionFailure and raise it.Nz	%s:%d: %sz: zconfigured timeoutsú	timed out)r   Ú
isinstancer>   Útimeoutr   Ú	SSLErrorsrF   r
   )rT   rU   rW   rY   ÚhostÚportÚmsgr"   r"   r#   Ú_raise_connection_failure|   s   


rb   c                   @  s.   e Zd Zddd„Zddd„Zeddd	„ƒZd
S )Ú_CancellationContextr   r    c                 C  s
   d| _ d S )NF©Ú
_cancelled©Úselfr"   r"   r#   Ú__init__š   s   
z_CancellationContext.__init__c                 C  s
   d| _ dS )zCancel this context.TNrd   rf   r"   r"   r#   Úcancel�   s   
z_CancellationContext.cancelÚboolc                 C  s   | j S )zWas cancel called?rd   rf   r"   r"   r#   Ú	cancelled¡   s   z_CancellationContext.cancelledN)r   r    )r   rj   )Ú__name__Ú
__module__Ú__qualname__rh   ri   Úpropertyrk   r"   r"   r"   r#   rc   ™   s
    

rc   r   Úoptionsr   c                 Ã  s,  �| \}}|  d¡r?ttdƒstdƒ‚t tj¡}t| ¡ ƒ z| d¡ t 	¡  
||¡I dH  |W S  ty>   | ¡  ‚ w tj}tjrL|dkrLtj}d}t|||tjd�I dH D ]°}|\}}	}
}}zt ||	ttdd	ƒB |
¡}W n ty‚   t ||	|
¡}Y nw t| ¡ ƒ zJ| tjtjd
¡ t ¡ }|du rŸ|j}n	|d	kr¨t d¡‚| tjtjd¡ t|ƒ | d¡ tjt 	¡  
||¡|d�I dH  | |¡ |W   S  tjyò } z| ¡  t d¡}||_ W Y d}~qZd}~w t�y
 } z| ¡  |}W Y d}~qZd}~ww |du�r|‚tdƒ‚)ú·Given (host, port) and PoolOptions, connect and return a raw socket object.

    Can raise socket.error.

    This is a modified version of create_connection from CPython >= 2.7.
    ú.sockÚAF_UNIXú-UNIX-sockets are not supported on this systemFNÚ	localhost©ÚfamilyÚtypeÚSOCK_CLOEXECr   r7   r[   T©r]   úgetaddrinfo failed)!ÚendswithrH   r>   r   rs   r$   ÚfilenoÚsetblockingÚasyncioÚget_running_loopÚsock_connectr,   ÚcloseÚAF_INETÚhas_ipv6Ú	AF_UNSPECr	   ÚSOCK_STREAMrI   rL   rK   ÚTCP_NODELAYr   Ú	remainingÚconnect_timeoutr]   Ú
SOL_SOCKETÚSO_KEEPALIVErC   Úwait_forÚ
settimeoutÚTimeoutErrorÚ	__cause__©rT   rp   r_   r`   r@   rw   ÚerrÚresÚafÚsocktypeÚprotoÚdummyÚsar]   Úer"   r"   r#   Ú_async_create_connection§   sn   €


þý

ÿ


€€þ
r™   úUnion[socket.socket, _sslConn]c           	   
   Ã  sB  �t | |ƒI dH }|j}|du r| |j¡ |S | d }z*tdƒr6t ¡ }| dtj	|j
||d�¡I dH }nt ¡ }| d|j
|¡I dH }W n1 tyR   | ¡  ‚  tgt¢R yw } z| ¡  t|ƒ}t| |d|d� W Y d}~nd}~ww |jr™|js™|js™ztj| ¡ |d� W n ty˜   | ¡  ‚ w | |j¡ |S )úºGiven (host, port) and PoolOptions, return a raw configured socket.

    Can raise socket.error, ConnectionFailure, or _CertificateError.

    Sets socket's SSL and timeout options.
    Nr   F©Úserver_hostnameúSSL handshake failed: ©rY   ©Úhostname)r™   Ú_ssl_contextr�   Úsocket_timeoutr   r   r€   Úrun_in_executorÚ	functoolsÚpartialÚwrap_socketr   r‚   r,   r^   r   rb   Úverify_modeÚcheck_hostnameÚtls_allow_invalid_hostnamesÚsslÚmatch_hostnameÚgetpeercert)	rT   rp   r@   Ússl_contextr_   ÚloopÚssl_sockÚexcÚdetailsr"   r"   r#   Ú_async_configured_socket÷   sP   €	þ€€úÿþýþr³   r   c           	   
   ƒ  s"  �t | |ƒI dH }|j}|j‰ |du r%tt ¡ j‡ fdd„|d�I dH ƒS | d }zt ¡ j‡ fdd„|||d�I dH \}}W n) tyG   ‚  tgt	¢R yh } zt
|ƒ}t| |d|d	� W Y d}~nd}~ww |jr‹|js‹|js‹ztj| d
¡|d� W n tyŠ   | ¡  ‚ w t||fƒS )zÊGiven (host, port) and PoolOptions, return a configured AsyncNetworkingInterface.

    Can raise socket.error, ConnectionFailure, or _CertificateError.

    Sets protocol's SSL and timeout options.
    Nc                     ó
   t ˆ d�S ©Nrz   ©r   r"   rz   r"   r#   Ú<lambda>?  ó   
 z0_configured_protocol_interface.<locals>.<lambda>rS   r   c                     r´   rµ   r¶   r"   rz   r"   r#   r·   H  r¸   )r@   r�   r«   rž   rŸ   Úpeercertr    )r™   r¢   r£   r   r   r€   Úcreate_connectionr   r,   r^   r   rb   r¨   r©   rª   r«   r¬   Úget_extra_infoÚabort)	rT   rp   r@   r®   r_   Ú	transportÚprotocolr±   r²   r"   rz   r#   Ú_configured_protocol_interface/  sL   €	
ÿÿ
ü€ûÿþýþr¿   c                 C  s°  | \}}|  d¡r3ttdƒstdƒ‚t tj¡}t| ¡ ƒ z| |¡ |W S  ty2   | 	¡  ‚ w tj
}tjr@|dkr@tj}d}tj|||tjd�D ]�}|\}}	}
}}zt ||	ttddƒB |
¡}W n tyt   t ||	|
¡}Y nw t| ¡ ƒ z:| tjtjd	¡ t ¡ }|du r‘|j}n	|dkršt d
¡‚| |¡ | tjtjd¡ t|ƒ | |¡ |W   S  tyÍ } z|}| 	¡  W Y d}~qLd}~ww |durÔ|‚tdƒ‚)rq   rr   rs   rt   ru   Nrv   ry   r   r7   r[   Tr{   )r|   rH   r>   r   rs   r$   r}   Úconnectr,   r‚   rƒ   r„   r…   Úgetaddrinfor†   rI   rL   rK   r‡   r   rˆ   r‰   r]   r�   rŠ   r‹   rC   r�   r"   r"   r#   Ú_create_connectione  sZ   


þý



€þrÂ   c              
   C  s  t | |ƒ}|j}|du r| |j¡ |S | d }ztdƒr%|j||d�}n| |¡}W n1 ty7   | ¡  ‚  tgt	¢R y\ } z| ¡  t
|ƒ}t| |d|d� W Y d}~nd}~ww |jr~|js~|js~ztj| ¡ |d� W n ty}   | ¡  ‚ w | |j¡ |S )r›   Nr   Trœ   rž   rŸ   r    )rÂ   r¢   r�   r£   r   r§   r   r‚   r,   r^   r   rb   r¨   r©   rª   r«   r¬   r­   ©rT   rp   r@   r®   r_   r°   r±   r²   r"   r"   r#   Ú_configured_socket¬  sD   

€€úÿþýþrÄ   r   c              
   C  s  t | |ƒ}|j}|du r| |j¡ t|ƒS | d }ztdƒr'|j||d�}n| |¡}W n1 ty9   | ¡  ‚  t	gt
¢R y^ } z| ¡  t|ƒ}t| |d|d� W Y d}~nd}~ww |jr€|js€|js€ztj| ¡ |d� W n ty   | ¡  ‚ w | |j¡ t|ƒS )zÕGiven (host, port) and PoolOptions, return a NetworkingInterface wrapping a configured socket.

    Can raise socket.error, ConnectionFailure, or _CertificateError.

    Sets socket's SSL and timeout options.
    Nr   Trœ   rž   rŸ   r    )rÂ   r¢   r�   r£   r   r   r§   r   r‚   r,   r^   r   rb   r¨   r©   rª   r«   r¬   r­   rÃ   r"   r"   r#   Ú_configured_socket_interfaceÝ  sD   

€€úÿþýþrÅ   )r   r   r   r    )r@   rD   rE   rF   rG   r   r   r    )r@   rD   r   r    )NN)
rT   r   rU   rV   rW   rX   rY   rZ   r   r   )rT   r   rp   r   r   rD   )rT   r   rp   r   r   rš   )rT   r   rp   r   r   r   )rT   r   rp   r   r   r   )FÚ__doc__Ú
__future__r   r   r¥   r>   r«   ÚsysÚtypingr   r   r   r   r   Úpymongor   Úpymongo.asynchronous.helpersr	   Úpymongo.errorsr
   r   r   r   Úpymongo.helpers_sharedr   r   Úpymongo.network_layerr   r   r   Úpymongo.pool_optionsr   Úpymongo.ssl_supportr   r   r   r^   Úpymongo.pyopenssl_contextr   Úpymongo.typingsr   r   r   r   r   r$   ÚImportErrorr:   r<   rR   ÚplatformÚ_winregr*   r3   ÚOpenKeyÚHKEY_LOCAL_MACHINEr.   r9   r;   r,   rC   rN   rb   rc   r™   r³   r¿   rÂ   rÄ   rÅ   r"   r"   r"   r#   Ú<module>   sz   ü
ÿ
ÿü€ü


	ü

P
8
6
G1