a
    ’éiµ  ã                	   @   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yF   dZY n0 ddlmZm	Z	m
Z
mZ erzddlmZ e 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)ÚMySQLConnectionAbstractÚStreamWriterc                   @   s"   e Zd ZdZejddœdd„ZdS )ÚStreamReaderProtocolzìExtends 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u| _dS )zQReplace stream writer.

        Args:
            writer: Stream Writer.
        Z
sslcontextN)Ú	transportZ_stream_writerÚ
_transportZget_extra_infoZ	_over_ssl)Úselfr   r   © r   úI/home/Claro/venv/lib/python3.9/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
   5   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	   zäExtends 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u}| j }|  ¡ I dH  | jj| j|||||d�I dH }|| _| | ¡ dS )zåUpgrade 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   O   s    ù
ÿzStreamWriter.start_tls)	r   r   r   r   ÚsslÚ
SSLContextÚstrÚintr   r   r   r   r   r	   H   s   
ûúi   )Úlimit)ÚhostÚportr#   Úkwdsr   c          	      ‹   sb   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)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   o   s    ()ÚfuncÚargsÚkwargsr   c                 Ï   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   r+   ÚcontextvarsZcopy_contextÚ	functoolsÚpartialÚrunZrun_in_executor)r1   r2   r3   r'   ÚctxZ	func_callr   r   r   r   Ž   s    
)NN)r   Ú__all__r   r4   r5   r   ÚImportErrorÚtypingr   r   r   r   Zmysql.connector.aio.abstractsr   Úappendr
   r,   r	   r!   r"   r-   r   ZFuturer   r   r   r   r   Ú<module>    s,   

( ÿÿþ