B
    U[W(                 @   sb  d 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mZ ddlmZ ddlmZ ddlmZmZmZmZmZmZmZ d3ddZdd Zdd Zdd Zdd Ze ej!dkddd Z"dd Z#e ej!dkddd Z$dd Z%e ej&dd  Z'd!d" Z(e)d#d$d% Z*d&d' Z+d(d) Z,d*d+ Z-e)d,d-d. Z.d/d0 Z/d1d2 Z0dS )4ztest the IPython Kernel    N)
decoratorstools)	py3compat)locate_profile)TemporaryDirectory   )
new_kernelkernelTIMEOUTassemble_outputexecuteflush_channelswait_for_idleTstdoutc             C   sN   t | dd t|  t | d| d\}}t| j\}}| t|ksJtd S )Nz
import sys)kccodez#print (sys.%s._is_master_process()))r   r   r   iopub_channelstripreprAssertionError)r   expectedstreammsg_idcontentr   stderr r   :lib/python3.7/site-packages/ipykernel/tests/test_kernel.py_check_master   s
    r   c             C   s,   | d dkr(ds(t ddg| d  dS )z#If status=error, show the tracebackstatuserrorF 
	tracebackN)r   join)r   r   r   r   _check_status!   s    r$   c           	   C   s\   t  L} | j}t| dd\}}t|\}}|dks6t|dksBtt| dd W dQ R X dS )z simple print statement in kernelzprint ('hi'))r   r   zhi
r    T)r   N)r	   r   r   r   r   r   )r   iopubr   r   r   r   r   r   r   test_simple_print)   s    r&   c           	   C   s@   t  0} t| dd\}}t| j\}}|dks2tW dQ R X dS )z3test that sys.path doesn't get messed up by defaultz%import sys; print (repr(sys.path[0])))r   r   z''
N)r	   r   r   r   r   )r   r   r   r   r   r   r   r   test_sys_path4   s    r'   c           	   C   sJ   t dtdg0} t| dd\}}t| j\}}|dks<tW dQ R X dS )zJtest that sys.path doesn't get messed up when `--profile-dir` is specifiedz--profile-dirdefaultz%import sys; print (repr(sys.path[0])))r   r   z''
N)r   r   r   r   r   r   )r   r   r   r   r   r   r   r   test_sys_path_profile_dir;   s    r)   Zwin32z!subprocess prints fail on Windowsc        	   
   C   s   t  } | j}t| dd t|  d}ddddd| d	d
dg}t| |d\}}t|\}}t|	d|| x(t
|D ]}t|	t|d| q|W |dkstt| dd t| ddd W dQ R X dS )zprinting from forked mp.ProcessT)r      r!   z%from __future__ import print_functionzimport timezimport multiprocessing as mpzHpool = [mp.Process(target=print, args=('hello', i,)) for i in range(%i)]zfor p in pool: p.start()zfor p in pool: p.join()ztime.sleep(0.5),)r   r   Zhellor   r    r   )r   r   N)r   r   r   r   r#   r   r   ntassert_equalcountrangestrr   )	r   r%   npr   r   r   r   r   nr   r   r   test_subprocess_printC   s*    r2   c           	   C   s   t  t} | j}d}ddd| ddg}t| |d\}}t|\}}|dksPt|dks\tt| d	d
 t| d	dd W dQ R X dS )z9mp.Process without print doesn't trigger iostream mp_moder*   r!   zimport multiprocessing as mpz?pool = [mp.Process(target=range, args=(i,)) for i in range(%i)]zfor p in pool: p.start()zfor p in pool: p.join())r   r   r    T)r   r   )r   r   N)r	   r   r#   r   r   r   r   )r   r%   r0   r   r   r   r   r   r   r   r   test_subprocess_noprint`   s    r3   c           	   C   s|   t  l} | j}dddddg}t| |d\}}t|\}}|dksHtd|ksTtt| d	d
 t| d	dd W dQ R X dS )z!error in mp.Process doesn't crashr!   zimport multiprocessing as mpz(p = mp.Process(target=int, args=('hi',))z	p.start()zp.join())r   r   r    
ValueErrorT)r   r   )r   r   N)r   r   r#   r   r   r   r   )r   r%   r   r   r   r   r   r   r   r   test_subprocess_errorv   s    r5   c           	   C   s   t  } | j}tjrdnd}d}djf t }| j|dd}| jdtd}|d d	 d
ks^t	|d }|d |ksvt	d}| 
| | jdtd}	|	d d dkst	t|\}
}|
|d kst	W dQ R X dS )ztest [raw_]inputinput	raw_inputzprompt> zprint({input_f}("{theprompt}"))T)allow_stdin)blocktimeoutheadermsg_typeinput_requestr   promptz	some textr   okr!   N)r	   r   r   PY3formatlocalsr   get_stdin_msgr
   r   r6   get_shell_msgr   )r   r%   input_f	thepromptr   r   msgr   textreplyr   r   r   r   r   test_raw_input   s     
rJ   c           	   C   s   t  } | j}tjrdnd}d}djf t }| j|dd}| jdtd}|d d	 d
ks^t	|d }|d |ksvt	| 
d | jdtd}|d d dkst	t|\}	}
|	dkst	W dQ R X dS )ztest input() on Python 2r6   r7   zprompt> zprint(input("{theprompt}"))T)r8   )r9   r:   r;   r<   r=   r   r>   z1+1r   r?   z2
N)r	   r   r   r@   rA   rB   r   rC   r
   r   r6   rD   r   )r   r%   rE   rF   r   r   rG   r   rI   r   r   r   r   r   test_eval_input   s    
rK   c              C   s   t  } t }tj|d}td| d t|  td| d t|  td| | d\}}|d dksjttj	|dd	}|
 }W d Q R X d|kstd|kstW d Q R X W d Q R X d S )
Nzhist.outza=1)r   u
   b=u"abcþ"z	%hist -f r   r?   zutf-8)encoding)r	   r   ospathr#   r   r   r   ioopenread)r   Ztdfile_rI   fr   r   r   r   test_save_history   s    rU   Zfaulthandlerc           	   C   sV   t  F} dddddddg}t|| d\}}t|d	 d
|dd W d Q R X d S )Nr!   z
import syszimport faulthandlerzimport signalzfaulthandler.enable()z(if not sys.platform.startswith("win32"):z)    faulthandler.register(signal.SIGTERM))r   r   r?   r"   r    )r	   r#   r   r+   r,   get)r   r   rS   rI   r   r   r   test_smoke_faulthandler   s    rW   c               C   s   t d dS )zipython kernel --help-all worksr	   N)ttZhelp_all_output_testr   r   r   r   test_help_output   s    rY   c           	   C   s   t  } | d | jdtd}|d d dks4t| d | jdtd}|d d dks`t| d	 | jdtd}|d d d
kst|d d dkst| d | jdtd}|d d dkstW d Q R X d S )Nz2+2T)r9   r:   r   r   completez	raise = 2Zinvalidz
a = [1,
2,Z
incompleteindentr    z%%timeit
a

)r	   Zis_completerD   r
   r   )r   rI   r   r   r   test_is_complete   s    



r\   c           	   C   s   t  } td| d t|  d}| | | jdtd}|d }|d dksPt|d	 |d
ksft|d |d
d kst|d }t	t
|d x |D ]}|d d d
kstqW W d Q R X d S )Nza = 1)r   zimport IPython
b = a.T)r9   r:   r   r   r?   Zcursor_startza.Z
cursor_end   matchesr   )r	   r   r   rZ   rD   r
   r   findr+   Zassert_greaterlen)r   cellrI   cr^   matchr   r   r   test_complete   s    

rd   Z
matplotlibc           	   C   sj   t  Z} dddg}t|ddi| d\}}t| |d d }t| d|d d	 ks\tW d Q R X d S )
Nr!   z+import matplotlib, matplotlib.pyplot as pltz"backend = matplotlib.get_backend()Zbackend)user_expressionsr   re   Zbackend_inlinedataz
text/plain)r	   r#   r   r$   r   )r   ra   rS   rI   Zbackend_bundler   r   r    test_matplotlib_inline_on_import  s    rg   c        	   	   C   s   d} t  }td|d\}}t| |d d }d}g }x t| D ]}||| qBW xVt||D ]H\}}|jtd}t|d  |d d |kst|d	 d
 |ksftqfW W d Q R X d S )Nd   za = 1)r   Zexecution_countr   za += 1
a)r:   r   Zparent_headerr   )	r	   r   r$   r.   append	enumeraterD   r
   r   )	Nr   rS   rI   offsetra   Zmsg_idsir   r   r   r   test_message_order  s    rn   c           	   C   sl   t  \} | j}td| d t|  |   x&tdD ]}| rLtd q4P q4W | r^t	W dQ R X dS )z*Kernel exits after polite shutdown_requestza = 1)r   rh   g?N)
r   parentr   r   Zshutdownr.   Zis_alivetimeZsleepr   )r   Zkmrm   r   r   r   test_shutdown(  s    rq   )Tr   )1__doc__rO   os.pathrM   sysrp   Z
nose.toolsr   r+   ZIPython.testingr   ZdecrX   Zipython_genutilsr   ZIPython.pathsr   Zipython_genutils.tempdirr   Zutilsr   r	   r
   r   r   r   r   r   r$   r&   r'   r)   Zskipifplatformr2   r3   r5   rJ   r@   rK   rU   Zskip_withoutrW   rY   r\   rd   rg   rn   rq   r   r   r   r   <module>   s8   $
