
    gh iM                       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	rddl%m&Z& ddl'm(Z( 	 ddl)m*Z*m+Z+m,Z,m)Z) d'dZ-dZ/dZ0dZ1ejd                  dk(  rG	 ddl3Z4d Z5	  e4jl                  e4jn                  d      5 Z8 e5e8dd      Z9 e5e8dd      Z:ddd       d Z<nd(dZ=d)dZ<	 	 d*	 	 	 	 	 	 	 	 	 d+dZ> G d d       Z?d,d!Z@	 	 	 	 	 	 d-d"ZA	 	 	 	 	 	 d.d#ZBd,d$ZCd-d%ZDd/d&ZEy# e.$ r d'dZ-Y w xY w# e.$ r ddl4Z4Y w xY w# 1 sw Y   rxY w# e;$ r dZ9dZ:Y w xY w)0z)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fcntlc                T    t        | t              }t        | t        |t        z         y)z8Set the close-on-exec flag on the given file descriptor.N)r   r   r   r   )fdflagss     O/opt/movilizia/ApiFast/venv/lib/python3.12/site-packages/pymongo/pool_shared.py_set_non_inheritable_non_atomicr"   4   s    b'"b'5:-.    c                     y)z6Dummy function for platforms that don't provide fcntl.N )r   s    r!   r"   r"   =   s    r#   x   
   	   win32c                z    	 t        j                  | |      \  }}t        |      S # t        t        f$ r |cY S w xY wN)winregQueryValueExintOSError
ValueError)keynamedefaultvalue_s        r!   _queryr6   K   sB    	**35HE1u:$ 	 N	s   #& ::z2SYSTEM\CurrentControlSet\Services\Tcpip\ParametersKeepAliveTimei m KeepAliveInterval  c                    t        t        t        dz        }t        t        t        dz        }|t        k  s	|t        k  r$| j                  t        j                  d||f       y y )Nr9      )min_WINDOWS_TCP_IDLE_MS_MAX_TCP_KEEPIDLE_WINDOWS_TCP_INTERVAL_MS_MAX_TCP_KEEPINTVLioctlsocketSIO_KEEPALIVE_VALS)sockidle_msinterval_mss      r!   _set_keepalive_timesrG   a   sZ    *,=,DE24F4MN))[;S-SJJv001g{2KL .Tr#   c                    t        t        |      rYt        t        |      }	 | j                  t        j                  |      }||kD  r"| j                  t        j                  ||       y y y # t        $ r Y y w xY wr+   )hasattrrB   getattr
getsockoptIPPROTO_TCP
setsockoptr/   )rD   
tcp_option	max_valuesockoptr3   s        r!   _set_tcp_optionrQ   i   sq    6:&fj1G //&*<*<gFY&OOF$6$6K ' '  s   AA+ +	A76A7c                j    t        | dt               t        | dt               t        | dt               y )NTCP_KEEPIDLETCP_KEEPINTVLTCP_KEEPCNT)rQ   r>   r@   _MAX_TCP_KEEPCNTrD   s    r!   rG   rG   v   s)    n.?@o/ABm-=>r#   c                   | \  }}|	d|||fz  }n| d| }|r||z   }d|vr|t        |      z  }t        |t        j                        rt	        |      |t        |t
              rdt        |      v rt	        |      |t        |      |)z9Convert a socket.error to ConnectionFailure and raise it.z	%s:%d: %sz: zconfigured timeouts	timed out)r   
isinstancerB   timeoutr   	SSLErrorsstrr   )addresserror
msg_prefixtimeout_detailshostportmsgs          r!   _raise_connection_failurere   |   s     JD$T4//b 3C'%o66%(S!u,	E9	%+U*C
 S!u,C e+r#   c                  .    e Zd ZddZddZedd       Zy)_CancellationContextc                    d| _         y )NF
_cancelledselfs    r!   __init__z_CancellationContext.__init__   s	    r#   c                    d| _         y)zCancel this context.TNri   rk   s    r!   cancelz_CancellationContext.cancel   s	    r#   c                    | j                   S )zWas cancel called?ri   rk   s    r!   	cancelledz_CancellationContext.cancelled   s     r#   N)returnNone)rr   bool)__name__
__module____qualname__rm   ro   propertyrq   r%   r#   r!   rg   rg      s        r#   rg   c           
     r  K   | \  }}|j                  d      rt        t        d      st        d      t        j                  t        j                        }t        |j                                	 |j                  d       t        j                         j                  ||       d{    |S t        j                  }t        j                  r|dk7  rt        j                  }d}t!        |||t        j"                         d{   D ]Z  }|\  }}	}
}}	 t        j                  ||	t%        t        dd	      z  |
      }t        |j                                	 |j'                  t        j(                  t        j*                  d
       t-        j.                         }||j0                  }n|d	k  rt        j2                  d      |j'                  t        j4                  t        j6                  d       t9        |       |j                  d       t        j:                  t        j                         j                  ||      |       d{    |j=                  |       |c S  ||t        d      7 # t        $ r |j                           w xY w7 # t        $ r t        j                  ||	|
      }Y ~w xY w7 t# t        j>                  $ r7}|j                          t        j2                  d      }||_         Y d}~d}~wt        $ r}|j                          |}Y d}~#d}~ww xY ww)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   r;   rY   Tr[   getaddrinfo failed)!endswithrI   rB   r   r|   r"   filenosetblockingasyncioget_running_loopsock_connectr/   closeAF_INEThas_ipv6	AF_UNSPECr
   SOCK_STREAMrJ   rM   rL   TCP_NODELAYr	   	remainingconnect_timeoutr[   
SOL_SOCKETSO_KEEPALIVErG   wait_for
settimeoutTimeoutError	__cause__r^   optionsrb   rc   rD   r   errresafsocktypeprotodummysar[   es                  r!   _async_create_connectionr      s     JD$ }}Wvy)#$STT}}V^^,'6	U#**,99$EEEK ^^F4;.!!
C!$V&BTBTUU $),&HeUB	6==XPQ0R%RTYZD 	(6	OOF..0B0BAFoo'G!11Ann[11OOF--v/B/BDI &U#""((*77bA7   OOG$K;$L 	 *++w F 	JJL	 V  	6 ==Xu5D	6$
 ## 	JJL..-CCMM 	JJLC	s   A.L718I< )I9*I< /AL7JL7)JL7C3KKK&L79I< <JL7 K=L7 KL7KL4,LL7L4L/)L7/L44L7c                  K   t        | |       d{   }|j                  }||j                  |j                         |S | d   }	 t	        d      rOt        j                         }|j                  dt        j                  |j                  ||             d{   }n9t        j                         }|j                  d|j                  |       d{   }|j"                  r>|j$                  s2|j&                  s&	 t)        j*                  j-                         |       j                  |j                         |S 7 07 7 p# t        $ r |j                           t        gt        $ r4}|j                          t        |      }t!        | |d|       Y d}~d}~ww xY w# t        $ r j                           w xY ww)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: ra   hostname)r   _ssl_contextr   socket_timeoutr   r   r   run_in_executor	functoolspartialwrap_socketr   r   r/   r\   r   re   verify_modecheck_hostnametls_allow_invalid_hostnamessslmatch_hostnamegetpeercert)	r^   r   rD   ssl_contextrb   loopssl_sockexcdetailss	            r!   _async_configured_socketr      s     *'7;;D&&K../1:Dc E?++-D!11!!+"9"94QUV H
 ++-D!11$8O8OQUVVH 	**33	x335E
 ../OY < W 

 	 i  c

 'w/!'30HZabbc ! 	NN	sv   GE4GAE	 E9E	 EE	 $G %F* %GE	 E	 	*F'3*F"G"F''G*GGc                  	K   t        | |       d{   }|j                  }|j                  	|9t        t	        j
                         j                  	fd|       d{         S | d   }	 t	        j
                         j                  	fd|||       d{   \  }}|j                  r?|j                  s3|j                  s'	 t        j                   j#                  d	      |
       t        f      S 7 7 7 c# t        $ r  t        gt        $ r$}t        |      }t        | |d|       Y d}~d}~ww xY w# t        $ r j%                           w xY ww)zGiven (host, port) and PoolOptions, return a configured AsyncNetworkingInterface.

    Can raise socket.error, ConnectionFailure, or _CertificateError.

    Sets protocol's SSL and timeout options.
    Nc                     t               S Nr   r   r   s   r!   <lambda>z0_configured_protocol_interface.<locals>.<lambda>?  s    8 r#   rW   r   c                     t               S r   r   r   s   r!   r   z0_configured_protocol_interface.<locals>.<lambda>H  s    OG4 r#   )rD   r   r   r   r   peercertr   )r   r   r   r   r   r   create_connectionr   r/   r\   r   re   r   r   r   r   r   get_extra_infoabort)
r^   r   rD   r   rb   	transportprotocolr   r   r[   s
            @r!   _configured_protocol_interfacer   /  st     *'7;;D&&K$$G'**,>>8t ?  
 	
 1:Dc %,$<$<$>$P$P4 	 %Q %
 
	8" 	**33	y77
CdS
 $Y$9::U <
   	 i  c 'w/!'30HZabbc ! 	OO	sv   E#C>AE#"D #E#1-D DD &$E#&E 1E# E#D ED=8E#=EE#E  E#c           
        | \  }}|j                  d      rkt        t        d      st        d      t        j                  t        j                        }t        |j                                	 |j                  |       |S t        j                  }t        j                  r|dk7  rt        j                  }d}t        j                  |||t        j                        D ]  }|\  }}	}
}}	 t        j                  ||	t        t        dd      z  |
      }t        |j                                	 |j!                  t        j"                  t        j$                  d	       t'        j(                         }||j*                  }n|dk  rt        j,                  d
      |j/                  |       |j!                  t        j0                  t        j2                  d       t5        |       |j                  |       |c S  ||t        d      # t        $ r |j                           w xY w# t        $ r t        j                  ||	|
      }Y 7w xY w# t        $ r}|}|j                          Y d}~d}~ww xY w)rz   r{   r|   r}   r~   Nr   r   r   r;   rY   Tr   )r   rI   rB   r   r|   r"   r   connectr/   r   r   r   r   getaddrinfor   rJ   rM   rL   r   r	   r   r   r[   r   r   r   rG   r   s                  r!   _create_connectionr   e  s    JD$ }}Wvy)#$STT}}V^^,'6	LLK ^^F4;.!!
C!!$V&BTBTU ),&HeUB	6==XPQ0R%RTYZD 	(6	OOF..0B0BAFoo'G!11Ann[11OOG$OOF--v/B/BDI &LLK3< 	 *++c  	JJL	&  	6 ==Xu5D	6&  	CJJLL	s7   /H -)H*0C	IH'* II	I7I22I7c                   t        | |      }|j                  }||j                  |j                         |S | d   }	 t	        d      r|j                  ||      }n|j                  |      }|j                  r>|j                  s2|j                  s&	 t        j                   j#                         |       j                  |j                         |S # t        $ r |j                           t        gt        $ r4}|j                          t        |      }t        | |d|       Y d}~d}~ww xY w# t        $ r j                           w xY w)r   Nr   Tr   r   r   r   )r   r   r   r   r   r   r   r   r/   r\   r   re   r   r   r   r   r   r   r^   r   rD   r   rb   r   r   r   s           r!   _configured_socketr     sB    gw/D&&K../1:Dc D>"..tT.JH"..t4H 	**33	x335E
 ../O1  

 	 i  c

 'w/!'30HZabbc ! 	NN	s#   0C %D6 *D3?*D..D36Ec                   t        | |      }|j                  }|&|j                  |j                         t	        |      S | d   }	 t        d      r|j                  ||      }n|j                  |      }|j                  r>|j                  s2|j                  s&	 t!        j"                  j%                         |       j                  |j                         t	        |      S # t        $ r |j                           t        gt        $ r4}|j                          t        |      }t        | |d|       Y d}~d}~ww xY w# t        $ r j                           w xY w)zGiven (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   re   r   r   r   r   r   r   r   s           r!   _configured_socket_interfacer     sN    gw/D&&K../"4((1:Dc D>"..tT.JH"..t4H 	**33	x335E
 ../x((1  

 	 i  c

 'w/!'30HZabbc ! 	NN	s$   0C' %E '*E*E  EE#)r   r.   rr   rs   )rD   socket.socketrN   r]   rO   r.   rr   rs   )rD   r   rr   rs   )NN)
r^   r   r_   	Exceptionr`   zOptional[str]ra   zOptional[dict[str, float]]rr   r   )r^   r   r   r   rr   r   )r^   r   r   r   rr   zUnion[socket.socket, _sslConn])r^   r   r   r   rr   r   )r^   r   r   r   rr   r   )F__doc__
__future__r   r   r   rB   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@   rV   platform_winregr,   r6   OpenKeyHKEY_LOCAL_MACHINEr1   r=   r?   r/   rG   rQ   re   rg   r   r   r   r   r   r   r%   r#   r!   <module>r      s   0 "    
 
   5  P ` ` , > >"	2(E99/    <<7 
(V^^%%'\
 	N#)##H '-c3F'M$		NM? !%26	,,, , 0	,
 ,: M,`55 +5#5p3;3; +3;3;lD,N.b.)I  EE	E  	N 	N
  (  '#' 	(sN   8D+ D: &E E	E +	D76D7:	EE	EE 	E! E!