U
    ?hn                  	   @   s   d Z ddgZddlZddlZddlZzddlZW n ek
rH   dZY nX ddlmZm	Z	m
Z
mZ erped G dd dejZG d	d dejjZdd
deeee	eejef dddZe
e	e	ejdddZdS )z
Utilities.	to_threadopen_connection    N)TYPE_CHECKINGAnyCallableTupleStreamWriterc                   @   s"   e Zd ZdZejdd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.
    N)writerreturnc                 C   s&   |j }|| _|| _|ddk	| _dS )zQReplace stream writer.

        Args:
            writer: Stream Writer.
        Z
sslcontextN)	transportZ_stream_writer
_transportZget_extra_infoZ	_over_ssl)selfr
   r    r   =/tmp/pip-unpacked-wheel-v4eb2wv5/mysql/connector/aio/utils.py_replace_writer:   s    z$StreamReaderProtocol._replace_writer)__name__
__module____qualname____doc__asyncior   r   r   r   r   r   r	   3   s   r	   c                   @   s.   e Zd ZdZdddejeedd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                   sT   | j jdk	}| 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   )	_protocolZ_client_connected_cbZdrainZ_loop	start_tlsr   r   )r   r   r   r   r   protocolZnew_transportr   r   r   r   M   s    
zStreamWriter.start_tls)	r   r   r   r   ssl
SSLContextstrintr   r   r   r   r   r   F   s   
i   )limit)hostportr"   kwdsr   c          	         s^   t  }t jj||d}t||d |j fdd| |f|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)r&   c                      s    S )Nr   r   r   r   r   <lambda>       z!open_connection.<locals>.<lambda>N)r   get_running_loopstreamsStreamReaderr	   create_connectionr   )	r#   r$   r"   r%   r&   readerr   _r
   r   r'   r   r   m   s    $)funcargskwargsr   c                    s8   t  }t }tj|j| f||}|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   r*   contextvarsZcopy_context	functoolspartialrunZrun_in_executor)r0   r1   r2   r&   ctxZ	func_callr   r   r   r      s    
)NN)r   __all__r   r3   r4   r   ImportErrortypingr   r   r   r   appendr	   r+   r   r    r!   r,   r   ZFuturer   r   r   r   r   <module>    s2   

(      