a
    <+b:A                     @   sx  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Zddlm	Z	 ddl
m
Z
 ddlZddlmZ ddlZddlmZ ddlmZmZmZmZmZmZmZmZ dLddZdd Zdd Zejjdddd Zejjdddd Z dd Z!dd Z"e
ddejj#ejdkp ejdko ej$dkd dd!d" Z%e
ddd#d$ Z&e
ddejj#ejdkprejdkorej$dkd dd%d& Z'd'd( Z(d)d* Z)d+d, Z*d-d. Z+d/d0 Z,ejj#ejdkd1dd2d3 Z-d4d5 Z.d6d7 Z/ejj#ej0d8pej0dd9dd:d; Z1ejj#e2 d<kd=dd>d? Z3d@dA Z4ejj#ej5dBkdCddDdE Z6ejj#dFej7v dGddHdI Z8dJdK Z9dS )Mztest the IPython Kernel    N)TemporaryDirectory)flaky)version)locate_profile   )
new_kernelkernelTIMEOUTassemble_outputexecuteflush_channelswait_for_idle	get_replyTstdoutc                 C   sN   t | dd t|  t | d| d\}}t| j\}}| t|ksJJ d S )N
import syskccodez"print(sys.%s._is_master_process()))r   r   r
   get_iopub_msgstriprepr)r   expectedstreammsg_idcontentr   stderr r   :lib/python3.9/site-packages/ipykernel/tests/test_kernel.py_check_master   s
    r   c                 C   s,   | d dkr(ds(J d dg| d  dS )z#If status=error, show the tracebackstatuserrorF 
	tracebackN)join)r   r   r   r   _check_status#   s    r%   c                  C   sl   t  R} t| dd\}}t| j\}}|dks2J |dks>J t| dd W d   n1 s^0    Y  dS ) simple print statement in kernelzprint('hi')r   zhi
r!   Tr   N)r   r   r
   r   r   )r   r   r   r   r   r   r   r   test_simple_print+   s    r(   zDCurrently don't capture during test as pytest does its own capturingreasonc                  C   sp   t  V} | j}t| dd\}}t|\}}|dks6J |dksBJ t| dd W d   n1 sb0    Y  dS )r&   z#import os; os.system('echo capsys')r   zcapsys
r!   Tr'   Nr   iopub_channelr   r
   r   r   iopubr   r   r   r   r   r   r   test_capture_fd5   s    r/   c                  C   sp   t  V} | j}t| dd\}}t|\}}|dks6J |d ksBJ t| dd W d   n1 sb0    Y  dS )r!   zimport subprocess, sys; subprocess.run(['python', '-c', 'import os; os.system("echo CAP1"); print("CAP2")'], stderr=sys.stderr)r   z
CAP1
CAP2
Tr'   Nr+   r-   r   r   r   %test_subprocess_peek_at_stream_filenoC   s    
r0   c                  C   sn   t  .} t| dd\}}t| j\}}W d   n1 s:0    Y  tj| t|	 }d|v sjJ dS )z3test that sys.path doesn't get messed up by default!import sys; print(repr(sys.path))r   Nr!   )
r   r   r
   r   sysr   writeastliteral_evalr   r   r   r   r   r   Zsys_pathr   r   r   test_sys_pathT   s    ,r7   c                  C   sx   t dtdg.} t| dd\}}t| j\}}W d   n1 sD0    Y  tj| t	|
 }d|v stJ dS )zJtest that sys.path doesn't get messed up when `--profile-dir` is specifiedz--profile-dirdefaultr1   r   Nr!   )r   r   r   r
   r   r2   r   r3   r4   r5   r   r6   r   r   r   test_sys_path_profile_dir`   s    ,r9      )Zmax_runsZwin32darwin)r:      z7subprocess prints fail on Windows and MacOS Python 3.8+c               	   C   s   t  } t| dd t|  d}dddd| dd	d
g}t| |d\}}t| j\}}|d|ksnJ |t|D ]}|t	|dksvJ |qv|dksJ t| dd t| ddd W d   n1 s0    Y  dS )zprinting from forked mp.ProcessTr'      r"   zimport timeimport multiprocessing as mpzHpool = [mp.Process(target=print, args=('hello', i,)) for i in range(%i)]for p in pool: p.start()for p in pool: p.join()ztime.sleep(0.5),r   Zhellor   r!   r   r   r   N)
r   r   r   r$   r   r
   r   countrangestr)r   npr   r   r   r   r   nr   r   r   test_subprocess_printm   s(    	rG   c                  C   s   t  z} d}ddd| ddg}t| |d\}}t| j\}}|dksLJ |dksXJ t| d	d
 t| d	dd W d   n1 s0    Y  dS )z9mp.Process without print doesn't trigger iostream mp_moder=   r"   r>   z?pool = [mp.Process(target=range, args=(i,)) for i in range(%i)]r?   r@   r   r!   Tr'   r   rA   N)r   r$   r   r
   r   r   )r   rE   r   r   r   r   r   r   r   r   test_subprocess_noprint   s    rH   c                  C   s   t  n} dg d}t| |d\}}t| j\}}|dks@J d|v sLJ t| dd t| ddd	 W d
   n1 sz0    Y  d
S )z!error in mp.Process doesn't crashr"   )r>   z(p = mp.Process(target=int, args=('hi',))z	p.start()zp.join()r   r!   
ValueErrorTr'   r   rA   N)r   r$   r   r
   r   r   )r   r   r   r   r   r   r   r   r   test_subprocess_error   s    rJ   c                  C   s   t  } | j}d}d}djf i t }| j|dd}| jtd}|d d d	ksVJ |d
 }|d |ksnJ d}| | | jtd}	|	d
 d dksJ t	| j
\}
}|
|d ksJ W d   n1 s0    Y  dS )z
test inputinputzprompt> zprint({input_f}("{theprompt}"))T)Zallow_stdintimeoutheaderZmsg_typeZinput_requestr   promptz	some textr   okr"   N)r   r,   formatlocalsr   Zget_stdin_msgr	   rK   get_shell_msgr
   r   )r   r.   Zinput_fZ	thepromptr   r   msgr   textreplyr   r   r   r   r   test_raw_input   s     
rW   c               
   C   s   t  } t }tj|d}td| d t|  td| d t|  td| | d\}}|d dksjJ t|dd	}| }W d    n1 s0    Y  d|v sJ d|v sJ W d    n1 s0    Y  W d    n1 s0    Y  d S )
Nzhist.outza=1r   u	   b="abcþ"z	%hist -f r   rP   zutf-8)encoding)	r   r   ospathr$   r   r   openread)r   tdfile_rV   fr   r   r   r   test_save_history   s    &rb   c                  C   sr   t jddd} t J}dg d}t||d\}}|d dksPJ |d	d
W d    n1 sd0    Y  d S )NZfaulthandlerzthis test needs faulthandlerr)   r"   )r   zimport faulthandlerzimport signalzfaulthandler.enable()z(if not sys.platform.startswith("win32"):z)    faulthandler.register(signal.SIGTERM)rX   r   rP   r#   r!   )pytestimportorskipr   r$   r   get)Zfaulthadlerr   r   r`   rV   r   r   r   test_smoke_faulthandler   s
    rf   c                  C   sb   t jddddg} tj| ddd}|jdks4J |jd	|jvsBJ d
|jv sPJ d|jv s^J dS )zipython kernel --help-all worksz-mIPythonr   z
--help-all   T)rM   Zcapture_outputr   s	   Tracebacks   Optionss   ClassN)r2   
executable
subprocessrun
returncoder   r   )cmdprocr   r   r   test_help_output   s    ro   c                  C   s   t  } | d | jtd}|d d dks2J | d | jtd}|d d dks\J | d | jtd}|d d d	ksJ |d d
 dksJ | d | jtd}|d d dksJ W d    n1 s0    Y  d S )Nz2+2rL   r   r   completez	raise = 2Zinvalidz
a = [1,
2,Z
incompleteindentr!   z%%timeit
a

)r   Zis_completerS   r	   )r   rV   r   r   r   test_is_complete   s    



rr   zonly run on Windowsc            	      C   s   t  >} td| d t|  d}| | | jtd}W d    n1 sJ0    Y  |d }|d dkslJ |d}|d	 }|d
 |dd	 ksJ |d |ksJ |d }|sJ |D ]&}|d |d  | }||sJ qd S )Na = 1rX   zimport IPython
b = a.rL   r   r   rP   za.   Z
cursor_endZcursor_startmatches)r   r   r   rp   rS   r	   find
startswith)	r   cellrV   cstartendru   mZ	completedr   r   r   test_complete  s"    
*
r}   c                  C   s   t jddd t d} dddg}t|ddi| d\}}t| |d	 d }t| d
|d d v sjJ W d    n1 s~0    Y  d S )NZ
matplotlibzthis test requires matplotlibr)   r"   z+import matplotlib, matplotlib.pyplot as pltz"backend = matplotlib.get_backend()Zbackend)user_expressionsr   r~   Zbackend_inlinedataz
text/plain)rc   rd   r   r$   r   r%   )r   rx   r`   rV   Zbackend_bundler   r   r    test_matplotlib_inline_on_import1  s    
r   c            	      C   s   d} t  }td|d\}}t| |d d }d}g }t| D ]}||| q@t||D ]H\}}|jtd}t|d  |d d |ksJ |d	 d
 |ks`J q`W d    n1 s0    Y  d S )Nd   rs   rX   Zexecution_countr   za += 1
arL   r   Zparent_headerr   )r   r   r%   rC   append	enumeraterS   r	   )	Nr   r`   rV   offsetrx   Zmsg_idsir   r   r   r   test_message_orderA  s    r   Zlinuxztest only on windowsc            
   
   C   s  t  z} t P}tj|d}t|d}|d W d    n1 sL0    Y  d}tjtj|d }tj||dd  }| 	d|d | j
td}|d	 d
 dksJ t| j\}}	||v sJ t|  | j	dd | j
td}|d	 d
 dks
J t| j\}}	d|v s&J | j	dd | j
td}|d	 d
 dksTJ W d    n1 sj0    Y  W d    n1 s0    Y  d S )Nzunc.txtzw+z
# UNC testz\\localhost\C$r   zcd srL   r   r   rP   Zls)r   Zcd)r   r   rZ   r[   r$   r\   r3   
splitdrivedirnamer   rS   r	   r
   r   r   )
r   r^   Zdrive_file_pathra   Zunc_rootZ	file_pathZunc_file_pathrV   outerrr   r   r   test_unc_pathsT  s*    (r   ZPyPyzdoes not work on PyPyc                  C   s~   t  d} | j}td| d t|  |   tdD ]}| rJtd q2 qPq2| r\J W d   n1 sp0    Y  dS )z*Kernel exits after polite shutdown_requestrs   rX   i,  g?N)	r   parentr   r   ZshutdownrC   Zis_alivetimesleep)r   kmr   r   r   r   test_shutdowns  s    r   c                  C   sp   t  V} | j}| d}td |  ddlm} t| |t	}||d| W d   n1 sb0    Y  dS )z
    The kernel exits after being interrupted while waiting in input().

    input() appears to have issues other functions don't, and it needs to be
    interruptible in order for pdb to be interruptible.
    input()r   validate_messageexecute_replyN
r   r   r   r   r   interrupt_kerneltest_message_specr   r   r	   r   r   r   r   rV   r   r   r   test_interrupt_during_input  s    

r   ntz0Message based interrupt not supported on Windowsc                  C   sx   t  ^} | j}d|j_| d}td |  ddlm	} t
| |t}||d| W d   n1 sj0    Y  dS )z

    messager   r   r   r   N)r   r   Zkernel_specZinterrupt_moder   r   r   r   r   r   r   r	   r   r   r   r   test_interrupt_with_message  s    

r   Z__pypy__zfails on pypyc                  C   s   t  x} | j}| d}| d}td |  ddlm} t| |t	}||d| t| |t	}||d| W d   n1 s0    Y  dS )z
    The kernel exits after being interrupted while waiting in pdb.set_trace().

    Merely testing input() isn't enough, pdb has its own issues that need
    to be handled in addition.

    This test will fail with versions of IPython < 7.14.0.
    zimport pdb; pdb.set_trace()z3 + 4r   r   r   Nr   )r   r   r   Zmsg_id2r   rV   r   r   r   #test_interrupt_during_pdb_set_trace  s    


r   c               	   C   s8  d} t  }|d}t|| |d}g }t| D ]}||d|  q6td g }t| D ]0}|jdi }|j	
| ||d d  qht|| g }|D ]}|t|| qg }	|D ]}|	t||d	d
 qW d    n1 s0    Y  dd |D }
dd |	D }|d |
d ks4J d S )Nr=   passz&import asyncio; await asyncio.sleep(2)zi = g      ?Zkernel_info_requestrN   r   Zcontrol)Zchannelc                 S   s   g | ]}|d  d qS rN   dater   .0rT   r   r   r   
<listcomp>      z0test_control_thread_priority.<locals>.<listcomp>c                 S   s   g | ]}|d  d qS r   r   r   r   r   r   r     r   r   )r   r   r   rC   r   r   r   ZsessionrT   Zcontrol_channelsend)r   r   r   Zsleep_msg_idZshell_msg_idsr   Zcontrol_msg_idsrT   Zshell_repliesZcontrol_repliesZshell_datesZcontrol_datesr   r   r   test_control_thread_priority  s0    




4r   )Tr   ):__doc__r4   os.pathrZ   platformrj   r2   r   Ztempfiler   r   rc   Z	packagingr   rg   ZIPython.pathsr   Zutilsr   r   r	   r
   r   r   r   r   r   r%   r(   Zmarkskipr/   r0   r7   r9   Zskipifversion_inforG   rH   rJ   rW   rb   rf   ro   rr   r}   r   r   rw   r   Zpython_implementationr   r   namer   builtin_module_namesr   r   r   r   r   r   <module>   s   (











