o
    Zh                      @   sr   d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	m
Z
mZ ddlmZmZ G d	d
 d
eZee dS )z A client for in-process kernels.    N)KernelClient)KernelClientABC)run_sync)InstanceTypedefault   )InProcessChannelInProcessHBChannelc                       s2  e Zd ZdZeeZeeZeeZeeZ	ee
ZedddZeddd Z fdd	Z fd
dZedd Zedd Zedd Zedd Zedd Z	d6ddZd7ddZd8ddZd9d d!Zd:d"d#Zd$d% Zd7d&d'Zd(d) Zd*d+ Z d,d- Z!d;d.d/Z"d;d0d1Z#d;d2d3Z$d;d4d5Z%  Z&S )<InProcessKernelClienta  A client for an in-process kernel.

    This class implements the interface of
    `jupyter_client.clientabc.KernelClientABC` and allows
    (asynchronous) frontends to be used seamlessly with an in-process kernel.

    See `jupyter_client.client.KernelClient` for docstrings.
    z,ipykernel.inprocess.ipkernel.InProcessKernelT)
allow_noneZblocking_classc                 C   s   ddl m} |S )Nr   )BlockingInProcessKernelClient)blockingr   )selfr    r   Q/var/www/html/lang_env/lib/python3.10/site-packages/ipykernel/inprocess/client.py_default_blocking_class6   s   z-InProcessKernelClient._default_blocking_classc                    s   t   }| j|d< |S )z'Get the connection info for the client.kernel)superget_connection_infor   )r   d	__class__r   r   r   <   s   

z)InProcessKernelClient.get_connection_infoc                    s&   t    | jr| jj|  dS dS )z!Start the channels on the client.N)r   start_channelsr   Z	frontendsappend)r   argskwargsr   r   r   r   B   s   
z$InProcessKernelClient.start_channelsc                 C      | j d u r| | | _ | j S N)Z_shell_channelshell_channel_classr   r   r   r   shell_channelH      
z#InProcessKernelClient.shell_channelc                 C   r   r   )Z_iopub_channeliopub_channel_classr    r   r   r   iopub_channelN   r"   z#InProcessKernelClient.iopub_channelc                 C   r   r   )Z_stdin_channelstdin_channel_classr    r   r   r   stdin_channelT   r"   z#InProcessKernelClient.stdin_channelc                 C   r   r   )Z_control_channelcontrol_channel_classr    r   r   r   control_channelZ   r"   z%InProcessKernelClient.control_channelc                 C   r   r   )Z_hb_channelhb_channel_classr    r   r   r   
hb_channel`   r"   z InProcessKernelClient.hb_channelFNc                 C   sH   |du r| j }t||||pi |d}| jd|}| | |d d S )zExecute code on the client.N)codesilentstore_historyuser_expressionsallow_stdinZexecute_requestheadermsg_id)r/   dictsessionmsg_dispatch_to_kernel)r   r+   r,   r-   r.   r/   contentr4   r   r   r   executei   s   
zInProcessKernelClient.executec                 C   s@   |du rt |}t||d}| jd|}| | |d d S )zGet code completion.N)r+   
cursor_posZcomplete_requestr0   r1   lenr2   r3   r4   r5   )r   r+   r8   r6   r4   r   r   r   completez   s   
zInProcessKernelClient.completer   c                 C   sB   |du rt |}t|||d}| jd|}| | |d d S )zGet code inspection.N)r+   r8   detail_levelZinspect_requestr0   r1   r9   )r   r+   r8   r<   r6   r4   r   r   r   inspect   s   
zInProcessKernelClient.inspectrangec                 K   s:   t d|||d|}| jd|}| | |d d S )zGet code history.)rawoutputhist_access_typeZhistory_requestr0   r1   Nr   r2   r3   r4   r5   )r   r?   r@   rA   kwdsr6   r4   r   r   r   history      
zInProcessKernelClient.historyc                 C   s   d}t |)zHandle shutdown.z!Cannot shutdown in-process kernel)NotImplementedError)r   Zrestartr4   r   r   r   shutdown   s   zInProcessKernelClient.shutdownc                 C   s"   | j d}| | |d d S )zRequest kernel info.Zkernel_info_requestr0   r1   r3   r4   r5   )r   r4   r   r   r   kernel_info   s   
z!InProcessKernelClient.kernel_infoc                 C   s:   |du ri nt |d}| jd|}| | |d d S )z6Request a dictionary of valid comms and their targets.N)target_nameZcomm_info_requestr0   r1   rB   )r   rJ   r6   r4   r   r   r   	comm_info   rE   zInProcessKernelClient.comm_infoc                 C   s"   | j du rd}t||| j _dS )zHandle kernel input.Nz*Cannot send input reply. No kernel exists.)r   RuntimeErrorZraw_input_str)r   stringr4   r   r   r   input   s   
zInProcessKernelClient.inputc                 C   s(   | j dd|i}| | |d d S )zHandle an is_complete request.Zis_complete_requestr+   r0   r1   rH   )r   r+   r4   r   r   r   is_complete   s   
z!InProcessKernelClient.is_completec           	      C   s   | j }|du rd}t||j}| j|| | }tdur)t|j}|| nt	 }|
|| | jj|dd\}}| j| dS )z0Send a message to the kernel and handle a reply.Nz&Cannot send request. No kernel exists.F)copy)r   rL   Zshell_streamr3   sendZrecv_multipartr   dispatch_shellasyncioget_event_looprun_until_completerecvr!   Zcall_handlers_later)	r   r4   r   streamZ	msg_partsrR   loopZidentsZ	reply_msgr   r   r   r5      s   

z)InProcessKernelClient._dispatch_to_kernelc                 C      | j ||S )zGet a shell message.)r!   get_msgr   blocktimeoutr   r   r   get_shell_msg      z#InProcessKernelClient.get_shell_msgc                 C   rY   )zGet an iopub message.)r$   rZ   r[   r   r   r   get_iopub_msg   r_   z#InProcessKernelClient.get_iopub_msgc                 C   rY   )zGet a stdin message.)r&   rZ   r[   r   r   r   get_stdin_msg   r_   z#InProcessKernelClient.get_stdin_msgc                 C   rY   )zGet a control message.)r(   rZ   r[   r   r   r   get_control_msg   r_   z%InProcessKernelClient.get_control_msg)FTNNr   )Nr   )TFr>   )F)TN)'__name__
__module____qualname____doc__r   r	   r   r#   r%   r'   r
   r)   r   r   r   r   r   r   propertyr!   r$   r&   r(   r*   r7   r;   r=   rD   rG   rI   rK   rN   rO   r5   r^   r`   ra   rb   __classcell__r   r   r   r   r      sJ    






	


	





r   )rf   rS   Zjupyter_client.clientr   Zjupyter_client.clientabcr   Zjupyter_core.utilsr   Z	traitletsr   r   r   Zchannelsr	   r
   r   registerr   r   r   r   <module>   s     A