o
    g                     @   s*  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 zddl	Z	W n e
y/   dZ	Y nw ddlmZmZmZmZ erIddlmZ ed G d	d
 d
ejZG d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dZdedededejfddZdedefddZdefddZdS )z
Utilities.	to_threadopen_connection    N)ReadTimeoutErrorWriteTimeoutError)TYPE_CHECKINGAnyCallableTuple)MySQLConnectionAbstractStreamWriterc                   @   s$   e Zd ZdZdejddfd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                 C   s&   |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    r   S/var/www/html/api-tag/env/lib/python3.10/site-packages/mysql/connector/aio/utils.py_replace_writer?   s   z$StreamReaderProtocol._replace_writer)__name__
__module____qualname____doc__asyncior   r   r   r   r   r   r   8   s    r   c                	   @   s4   e Zd ZdZddddejdededdfd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                   sV   | j jdu}| j }|  I dH  | jj| j|||||dI dH }|| _||  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_transportr   r   r   r&   R   s   
zStreamWriter.start_tls)	r   r   r   r   ssl
SSLContextstrintr&   r   r   r   r   r   K   s    
i   )limithostportr-   kwdsr   c          	         sd   t  }t jj||d}t||d |j fdd| |fi |I dH \}}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.
    )r-   loop)r1   c                      s    S Nr   r   r'   r   r   <lambda>   s    z!open_connection.<locals>.<lambda>N)r   get_running_loopstreamsStreamReaderr   create_connectionr   )	r.   r/   r-   r0   r1   readerr   _r   r   r3   r   r   r   s   (funcargskwargsc                    s@   t  }t }tj|j| g|R i |}|d|I dH 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   r5   contextvarscopy_context	functoolspartialrunrun_in_executor)r;   r<   r=   r1   ctx	func_callr   r   r   r      s
   
reasonc                    s   dt dt f fdd}|S )z&Use it to decorate deprecated methods.r;   r   c                    s*   t  dtdtdtf fdd}|S )Nr<   r=   r   c                     s4   t jd j d tdd  | i |I d H S )NzCall to deprecated function z
. Reason:    )category
stacklevel)warningswarnr   DeprecationWarning)r<   r=   )r;   rF   r   r   wrapper   s   z.deprecated.<locals>.decorator.<locals>.wrapper)r@   wrapsr   r   )r;   rM   rF   )r;   r   	decorator   s   zdeprecated.<locals>.decoratorr   )rF   rP   r   rO   r   
deprecated   s   rR   c                  C   s   dt dt fd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              	      s,   t  dddtdtdtf fdd}|S )Ncnxr
   r<   r=   r   c              
      sZ   z | g|R i |I d H W S  t y, } zt|ttfr&|  I d H  |d }~ww r2   )	Exception
isinstancer   r   close)rT   r<   r=   errrS   r   r   handle_cnx_method   s   zGhandle_read_write_timeout.<locals>.decorator.<locals>.handle_cnx_method)r@   rN   r   )rS   rZ   r   rY   r   rP      s   
z,handle_read_write_timeout.<locals>.decoratorrQ   )rP   r   r   r   handle_read_write_timeout   s   r[   )NN)r   __all__r   r>   r@   rJ   mysql.connector.errorsr   r   r)   ImportErrortypingr   r   r   r	   mysql.connector.aio.abstractsr
   appendr   r6   r   r+   r,   r7   r   Futurer   rR   r[   r   r   r   r   <module>   sF   
(
