U
    /eL                     @   s   d Z ddlZddlZddlZddlZddlZddlmZ ddlm	Z	m
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 d	Zd
Zejdkrejddd edd Zedddd Zedddd Zedddd Zdd ZdS )ztest embed_kernel    N)contextmanager)PIPEPopen)flaky)BlockingKernelClient)paths)IPKernelAppembed_kernel<      ntzskipping tests on windowsT)Zallow_module_levelc           
   
   c   sB  dd }t tjd| gttdd}ztj	
t d|j }t }||sr| dkrrt |t k rrtd	 qBtd	 | dk	r| \}}td
| tj	|s| dkr|  td| t|d}	|	  |	  |	  z
|	V  W 5 |	  X W 5 |  |  dD ]}t||}|r|  qX dS )zstart an embedded kernel in a subprocess, and wait for it to be ready

    Returns
    -------
    kernel_manager: connected KernelManager instance
    c              	   S   sP   t j| sdS z$t| }t| W 5 Q R X W dS  tk
rJ   Y dS X dS )z1Check if connection_file is a readable json file.FTN)ospathexistsopenjsonload
ValueError)connection_filef r   E/tmp/pip-unpacked-wheel-g43y689f/ipykernel/tests/test_embed_kernel.pyconnection_file_ready&   s    
z+setup_kernel.<locals>.connection_file_readyz-czutf-8)stdoutstderrencoding)r   r   stdinzkernel-%i.jsonNg?zKernel failed to start:
%sz Connection file %r never arrived)r   )r   sys
executabler   	terminatewaitgetattrcloser   r   joinr   Zjupyter_runtime_dirpidtimepollSETUP_TIMEOUTsleepcommunicateOSErrorr   r   Zload_connection_fileZstart_channelsZwait_for_readyZstop_channels)
cmdr   kernelattrZfidr   Zticoeclientr   r   r   setup_kernel   sH    	




r1      )Zmax_runsc               	   C   s   d dddddddg} t| }|d	 |jtd
}|d }|d sLt|d |jtd
}|d }|d dkszt|d |jtd
}|d }|d st|d d }d|kstW 5 Q R X dS )z.IPython.embed_kernel() is basically functional
 from IPython import embed_kernel	def go():    a=5    b="hi there"    embed_kernel()go() atimeoutcontentfoundzc=a*2statusokcdata
text/plain10N)r#   r1   inspectget_shell_msgTIMEOUTAssertionErrorexecuter+   r0   msgr>   textr   r   r   test_embed_kernel_basic^   s2    



rN   c               	   C   s   d dddddddg} t| }|d	 |jtd
}|d }|d sLt|d d }d|ksdt|d |jtd
}|d }|d st|d d }d|kst|d |jtd
}|d }|d rtW 5 Q R X dS )z1IPython.embed_kernel() inherits calling namespacer3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r>   r?   rC   rD   5bzhi thererB   N)r#   r1   rF   rG   rH   rI   rK   r   r   r   test_embed_kernel_namespace   s6    



rQ   c                  C   s   d dddddddd	dg	} t| z}td
D ]j}|d |jtd}|d }|d s\t|d d }t||ksxt|d |jtd}t	
d q.W 5 Q R X dS )z3IPython.embed_kernel() can be called multiple timesr3   r4   z	count = 0r5   z    global countr8   z    count = count + 1r:   zwhile True:    go()   countr<   r>   r?   rC   rD   zget_ipython().exit_now = Trueg?N)r#   r1   rangerF   rG   rH   rI   strrJ   r%   r(   )r+   r0   irL   r>   rM   r   r   r   test_embed_kernel_reentrant   s.    


rW   c                  C   s@   ddl m}  | d}dd }tj|d}|  t|d d d S )Nr   )
ModuleTypetestc                  S   s.   t d t } | j| jj t  d S )N   )r%   r(   r   instanceZio_loopZadd_callbackstopZclear_instance)Zappr   r   r   trigger_stop   s    
z,test_embed_kernel_func.<locals>.trigger_stop)target)Zoutstream_class)typesrX   	threadingThreadstartr	   )rX   moduler]   threadr   r   r   test_embed_kernel_func   s    re   )__doc__r   r   r   r`   r%   
contextlibr   
subprocessr   r   Zpytestr   Zjupyter_client.blocking.clientr   Zjupyter_corer   Zipykernel.embedr   r	   r'   rH   nameskipr1   rN   rQ   rW   re   r   r   r   r   <module>   s2   

@
#
&
