
    g                        d Z ddgZddlZddlZddlZddlZddlmZmZ 	 ddl	Z	n# e
$ r dZ	Y nw xY wddlmZmZmZmZ erddlmZ e                    d            G d	 d
ej                  Z G d dej        j                  Z	 ddddededededeej        ef         f
dZdedededej        fdZdedefdZdefdZdS )z
Utilities.	to_threadopen_connection    N)ReadTimeoutErrorWriteTimeoutError)TYPE_CHECKINGAnyCallableTuple)MySQLConnectionAbstractStreamWriterc                   ,    e Zd ZdZdej        ddfdZdS )StreamReaderProtocolzExtends asyncio.streams.StreamReaderProtocol for adding start_tls().

    The ``start_tls()`` is based on ``asyncio.streams.StreamWriter`` introduced
    in Python 3.11. It provides the same functionality for older Python versions.
    writerreturnNc                 h    |j         }|| _        || _        |                    d          du| _        dS )zQReplace stream writer.

        Args:
            writer: Stream Writer.
        
sslcontextN)	transport_stream_writer
_transportget_extra_info	_over_ssl)selfr   r   s      e/var/www/html/nodeJS/PythonScripts/venv3.11/lib/python3.11/site-packages/mysql/connector/aio/utils.py_replace_writerz$StreamReaderProtocol._replace_writer?   s9     $	$#"11,??tK    )__name__
__module____qualname____doc__asyncior   r    r   r   r   r   8   sK         	Lg&: 	Lt 	L 	L 	L 	L 	L 	Lr   r   c            	       <    e Zd ZdZddddej        dededdfdZdS )	r   zExtends asyncio.streams.StreamWriter for adding start_tls().

    The ``start_tls()`` is based on ``asyncio.streams.StreamWriter`` introduced
    in Python 3.11. It provides the same functionality for older Python versions.
    N)server_hostnamessl_handshake_timeoutssl_contextr#   r$   r   c                   K   | j         j        du}| j         }|                                  d{V  | j                            | j        |||||           d{V }|| _        |                    |            dS )zUpgrade an existing stream-based connection to TLS.

        Args:
            ssl_context: Configured SSL context.
            server_hostname: Server host name.
            ssl_handshake_timeout: SSL handshake timeout.
        N)server_sider#   r$   )	_protocol_client_connected_cbdrain_loop	start_tlsr   r   )r   r%   r#   r$   r'   protocolnew_transports          r   r,   zStreamWriter.start_tlsR   s       n9E>jjll"j22O#+"7 3 
 
 
 
 
 
 
 
  	 	  &&&&&r   )	r   r   r   r   ssl
SSLContextstrintr,   r!   r   r   r   r   K   sl           $%)' ' '^' 	'
  #' 
' ' ' ' ' 'r   i   )limithostportr3   kwdsr   c                   	K   t          j                    }t           j                            ||          }t	          ||          	 |j        	fd| |fi | d{V \  }}t          |	||          }||fS )a  A wrapper for create_connection() returning a (reader, writer) pair.

    This function is based on ``asyncio.streams.open_connection`` and adds a custom
    stream reader.

    MySQL expects TLS negotiation to happen in the middle of a TCP connection, not at
    the start.
    This function in conjunction with ``_StreamReaderProtocol`` and ``_StreamWriter``
    allows the TLS negotiation on an existing connection.

    Args:
        host: Server host name.
        port: Server port.
        limit: The buffer size limit used by the returned ``StreamReader`` instance.
               By default the limit is set to 64 KiB.

    Returns:
        tuple: Returns a pair of reader and writer objects that are instances of
               ``StreamReader`` and ``StreamWriter`` classes.
    )r3   loop)r8   c                       S Nr!   )r-   s   r   <lambda>z!open_connection.<locals>.<lambda>   s     r   N)r    get_running_loopstreamsStreamReaderr   create_connectionr   )
r4   r5   r3   r6   r8   readerr   _r   r-   s
            @r   r   r   r   s      . #%%D_))D)AAF#F666H//0@0@0@0@$UUPTUUUUUUUULIq)Xvt<<F6>r   funcargskwargsc                    K   t          j                    }t          j                    }t	          j        |j        | g|R i |}|                    d|           d{V S )aQ  Asynchronously run function ``func`` in a separate thread.

    This function is based on ``asyncio.to_thread()`` introduced in Python 3.9, which
    provides the same functionality for older Python versions.

    Returns:
        coroutine: A coroutine that can be awaited to get the eventual result of
                   ``func``.
    N)r    r<   contextvarscopy_context	functoolspartialrunrun_in_executor)rB   rC   rD   r8   ctx	func_calls         r   r   r      sp       #%%D

"
$
$C!#'4A$AAA&AAI%%dI666666666r   reasonc                 0     dt           dt           f fd}|S )z&Use it to decorate deprecated methods.rB   r   c                 v     t          j                   dt          dt          dt          f fd            }|S )NrC   rD   r   c                  r   K   t          j        dj         d t          d            | i | d {V S )NzCall to deprecated function z
. Reason:    )category
stacklevel)warningswarnr   DeprecationWarning)rC   rD   rB   rN   s     r   wrapperz.deprecated.<locals>.decorator.<locals>.wrapper   sd      MPt}PPPP+   
 t.v.........r   )rH   wrapsr   r	   )rB   rX   rN   s   ` r   	decoratorzdeprecated.<locals>.decorator   sX    				/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 
		/ r   r	   )rN   rZ   s   ` r   
deprecatedr\      s7    
 
X 
 
 
 
 
 
 r   c                  *    dt           dt           fd} | S )z
    Decorator to close the current connection if a read or a write timeout
    is raised by the method passed via the func parameter.
    
cnx_methodr   c           	      v     t          j                   dddt          dt          dt          f fd            }|S )Ncnxr   rC   rD   r   c                    K   	  | g|R i | d {V S # t           $ r=}t          |t          t          f          r|                                  d {V  |d }~ww xY wr:   )	Exception
isinstancer   r   close)r`   rC   rD   errr^   s       r   handle_cnx_methodzGhandle_read_write_timeout.<locals>.decorator.<locals>.handle_cnx_method   s      'Z=d===f=========   c$46G#HII &))++%%%%%%%	s    
A8AA)rH   rY   r   )r^   rf   s   ` r   rZ   z,handle_read_write_timeout.<locals>.decorator   s^    		$	$	*	36	BE		 	 	 	 	 
%	$	 ! r   r[   )rZ   s    r   handle_read_write_timeoutrg      s,    !h !8 ! ! ! ! r   )NN)r   __all__r    rF   rH   rU   mysql.connector.errorsr   r   r/   ImportErrortypingr   r   r	   r
   mysql.connector.aio.abstractsr   appendr   r=   r   r1   r2   r>   r   Futurer   r\   rg   r!   r   r   <module>ro      s  @  )
*           F F F F F F F FJJJJ   
CCC 7 6 6 6 6 6 6 6 6 6 6 6 #EEEEEENN>"""L L L L L77 L L L&$' $' $' $' $'7?/ $' $' $'P #'8=  
25GJ
7-.   >7( 73 7# 7'. 7 7 7 7 s x    $8      s   % //