U
    /e?                     @   sr  U d Z ddlZddlZddl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mZmZmZmZmZmZmZ dd	lmZmZmZmZmZ dae	ed
< dd ZG dd deZ G dd deZG dd de Z!G dd de Z"e#dZ$G dd de Z%G dd de Z&G dd de&Z'G dd de&Z(G dd de&Z)G d d! d!e&Z*G d"d# d#e&e%Z+G d$d% d%e Z,G d&d' d'e Z-G d(d) d)e&Z.G d*d+ d+e Z/G d,d- d-e&Z0G d.d/ d/e Z1G d0d1 d1e&Z2G d2d3 d3e Z3G d4d5 d5e Z4G d6d7 d7e Z5G d8d9 d9e)Z6G d:d; d;e Z7G d<d= d=e%Z8G d>d? d?e%Z9G d@dA dAe&Z:e' e+ e- e. e0 e1 e2 e3 e5 e9 e: e6 e7 e8 e" dBZ;dvdCdDZ<dEdF Z=dGdH Z>dIdJ Z?dKdL Z@dMdN ZAdOdP ZBdQdR ZCdSdT ZDdUdV ZEdWdX ZFdYdZ ZGd[d\ ZHd]d^ ZId_d` ZJdadb ZKejLjMedck dddedfdg ZNdhdi ZOdjdk ZPdldm ZQdndo ZRdpdq ZSdrds ZTdtdu ZUdS )wz6Test suite for our zeromq-based message specification.    N)Empty)version_info)BlockingKernelClient)Version)	BoolDictEnum	HasTraitsIntegerList
TraitErrorUnicodeobserve   )TIMEOUTexecuteflush_channels	get_replystart_global_kernelKCc                   C   s
   t  ad S N)r   r    r   r   E/tmp/pip-unpacked-wheel-g43y689f/ipykernel/tests/test_message_spec.pysetup   s    r   c                   @   s   e Zd ZdZdd ZdS )	Referencea.  
    Base class for message spec specification testing.

    This class is the core of the message specification test.  The
    idea is that child classes implement trait attributes for each
    message keys, so that message keys can be tested against these
    traits using :meth:`check` method.

    c                 C   st   |   D ]f}||kst|| dkr&qzt| |||  W q tk
rl } zds\tt|W 5 d}~X Y qX qdS )z"validate a dict against our traitsNF)Ztrait_namesAssertionErrorsetattrr   str)selfdkeyer   r   r   check.   s    zReference.checkN)__name__
__module____qualname____doc__r"   r   r   r   r   r   "   s   
r   c                       s$   e Zd Z fddZdd Z  ZS )r   c                    s8   | dd | _| dd | _| j|d< t j|| d S )Nminmaxdefault_value)popr'   r(   super__init__)r   argskwargs	__class__r   r   r,   <   s    
zVersion.__init__c                 C   s`   | j r.t|t| j k r.td| d| j  | jr\t|t| jkr\td| d| j d S )Nzbad version: z < z > )r'   Vr   r(   )r   objvaluer   r   r   validateB   s    zVersion.validate)r#   r$   r%   r,   r4   __classcell__r   r   r/   r   r   ;   s   r   c                       s:   e Zd Ze Ze Ze Ze Ze Z	 fddZ
  ZS )RMessagec                    s2   t  | t | j | jr.t | j d S r   )r+   r"   RHeaderheaderparent_headerr   r   r/   r   r   r"   P   s    zRMessage.check)r#   r$   r%   r   msg_idmsg_typer   r8   r9   contentr"   r5   r   r   r/   r   r6   I   s   r6   c                   @   s.   e Zd Ze Ze Ze Ze ZeddZ	dS )r7   5.0r'   N)
r#   r$   r%   r   r;   r<   sessionusernamer   versionr   r   r   r   r7   W   s
   r7   z^[\w\-\+\.]+/[\w\-\+\.]+$c                   @   s(   e Zd Ze Ze Zeddd ZdS )
MimeBundledatac                 C   s6   |d   D ]$\}}t|s"tt|tstqd S )Nnew)itemsmime_patmatchr   
isinstancer   )r   Zchangekvr   r   r   _on_data_changedf   s    zMimeBundle._on_data_changedN)r#   r$   r%   r   metadatarD   r   rL   r   r   r   r   rC   b   s   rC   c                   @   s   e Zd ZedddZdS )Reply)okerrorrO   r)   Nr#   r$   r%   r   statusr   r   r   r   rN   n   s   rN   c                   @   s   e Zd Ze Zdd ZdS )ExecuteReplyc                 C   s\   t | | |d dkr&t | n2|d dkr@t | n|d dkrXt | d S )NrS   rO   rP   aborted)r   r"   ExecuteReplyOkayExecuteReplyErrorExecuteReplyAbortedr:   r   r   r   r"   u   s    zExecuteReply.checkN)r#   r$   r%   r
   execution_countr"   r   r   r   r   rT   r   s   rT   c                   @   s   e Zd ZedZe ZdS )rV   )rO   N)r#   r$   r%   r   rS   r   user_expressionsr   r   r   r   rV      s   rV   c                   @   s*   e Zd ZedZe Ze Zee Z	dS )rW   )rP   N)
r#   r$   r%   r   rS   r   enameZevaluer   	tracebackr   r   r   r   rW      s   rW   c                   @   s   e Zd ZedZdS )rX   )rU   NrR   r   r   r   r   rX      s   rX   c                   @   s   e Zd Ze ZdS )InspectReplyN)r#   r$   r%   r   foundr   r   r   r   r]      s   r]   c                   @   s(   e Zd Zee Ze Ze Ze ZdS )ArgSpecN)	r#   r$   r%   r   r   r-   varargsvarkwdefaultsr   r   r   r   r_      s   
r_   c                   @   s   e Zd ZedddZdS )Status)busyidleZstartingrd   rQ   N)r#   r$   r%   r   execution_stater   r   r   r   rc      s   rc   c                   @   s(   e Zd Zee Ze Ze Ze Z	dS )CompleteReplyN)
r#   r$   r%   r   r   matchesr
   Zcursor_startZ
cursor_endrS   r   r   r   r   rg      s   
rg   c                   @   s&   e Zd ZedZeej d ZdS )LanguageInfopythonr   N)r#   r$   r%   r   namesysrB   splitr   r   r   r   ri      s   ri   c                   @   s<   e Zd ZeddZedZeddZe Z	e Z
dd ZdS )KernelInfoReplyr>   r?   Zipythonz2.1c                 C   s    t | | t |d  d S )Nlanguage_info)r   r"   ri   r:   r   r   r   r"      s    zKernelInfoReply.checkN)r#   r$   r%   r   Zprotocol_versionr   implementationimplementation_versionr   ro   Zbannerr"   r   r   r   r   rn      s   

rn   c                   @   s*   e Zd Ze Ze Ze Ze Ze ZdS )ConnectReplyN)	r#   r$   r%   r
   Z
shell_portZcontrol_portZ
stdin_portZ
iopub_portZhb_portr   r   r   r   rr      s
   rr   c                   @   s   e Zd Ze ZdS )CommInfoReplyN)r#   r$   r%   r   Zcommsr   r   r   r   rs      s   rs   c                   @   s    e Zd ZedddZdd ZdS )IsCompleteReply)complete
incompleteinvalidunknownru   rQ   c                 C   s(   t | | |d dkr$t | d S )NrS   rv   )r   r"   IsCompleteReplyIncompleter:   r   r   r   r"      s    zIsCompleteReply.checkN)r#   r$   r%   r   rS   r"   r   r   r   r   rt      s   rt   c                   @   s   e Zd Ze ZdS )ry   N)r#   r$   r%   r   indentr   r   r   r   ry      s   ry   c                   @   s   e Zd Ze Ze ZdS )ExecuteInputN)r#   r$   r%   r   coder
   rY   r   r   r   r   r{      s   r{   c                   @   s   e Zd ZdZdZdS )Errorz7Errors are the same as ExecuteReply, but without statusN)r#   r$   r%   r&   rS   r   r   r   r   r}      s   r}   c                   @   s   e Zd ZedddZe ZdS )Stream)stdoutstderrr   rQ   N)r#   r$   r%   r   rk   r   textr   r   r   r   r~      s   r~   c                   @   s   e Zd ZdS )DisplayDataN)r#   r$   r%   r   r   r   r   r      s   r   c                   @   s   e Zd Ze ZdS )ExecuteResultN)r#   r$   r%   r
   rY   r   r   r   r   r      s   r   c                   @   s   e Zd Zee ZdS )HistoryReplyN)r#   r$   r%   r   historyr   r   r   r   r      s   r   )execute_replyinspect_replyrS   complete_replykernel_info_replyconnect_replycomm_info_replyis_complete_replyZexecute_inputZexecute_resulthistory_replyrP   streamdisplay_datar8   c                 C   sZ   t  |  |r | d |ks t|r8| d d |ks8t| d }t| d  }|| dS )zvalidate a message

    This is a generator, and must be iterated through to actually
    trigger each test.

    If msg_type and/or parent are given, the msg_type and/or parent msg_id
    are compared with the given values.
    r<   r9   r;   r=   N)r6   r"   r   
references)msgr<   parentr=   refr   r   r   validate_message  s    	r   c                  C   s.   t   tjdd} tt| t}t|d|  d S )Nx=1r|   r   )r   r   r   r   r   r   r;   replyr   r   r   test_execute  s    r   c               	   C   s   t   tddd\} }tjtd}t|d|  |d d dksBttt	 tjd	d W 5 Q R X |d
 }tddd\} }tjtd}t|d|  |d d dksttt	 tjd	d W 5 Q R X |d
 }||kstd S )Nr   T)r|   ZsilenttimeoutrS   r=   rf   re   g?rY   x=2)
r   r   r   get_iopub_msgr   r   r   pytestZraisesr   )r;   r   rS   countcount_2r   r   r   test_execute_silent'  s     r   c                  C   sP   t   tdd\} }|d dks$t|d dks4ttjtd}t|d|  d S )Nz1/0r   rS   rP   r[   ZeroDivisionErrorr   )r   r   r   r   r   r   r   )r;   r   rP   r   r   r   test_execute_errorC  s    r   c                  C   sL   t   tdd\} }|d }t   tdd\} }|d }||d ksHtdS )z0execute request should increment execution_countr   r   rY   r   r   N)r   r   r   )_r   r   r   r   r   r   test_execute_incN  s    r   c                  C   s   t   ddddg} tj| d tjdd tjdd tjtd}t| tjtd}|d	 d
 dksnttjtd}|d	 d
 dkstt   tj| dd tjdd tjtd tjtd}|d	 d
 dkstdS )zIexecute request should not abort execution queue with stop_on_error False
zimport asynciozawait asyncio.sleep(1)zraise ValueError()r   zprint("Hello")zprint("world")r   r=   rS   rU   F)r|   Zstop_on_errorrO   N)r   joinr   r   get_shell_msgr   printr   Zfailr   r   r   r   test_execute_stop_on_error\  s,    	r   c                  C   s   t   ddddg} tj| d t  t  tjdd tjtd}|d d	 d
ks^t	tjtd}|d d	 dks~t	tjtd}|d d	 dkst	tjtd}|d d	 dkst	dS )z>test that non-execute_request's are not aborted after an errorr   zimport timeztime.sleep(0.5)zraise ValueErrorr   r   r   r=   rS   rP   rO   N)
r   r   r   r   kernel_info	comm_infoinspectr   r   r   r   r   r   r   test_non_execute_stop_on_error}  s&    r   c                  C   sD   t   tdtddd\} }|d }|dddd	ii d
iks@td S )Nr   zx+1foor|   rZ   rZ   r   rO   
text/plain2)rS   rD   rM   r   r   dictr   )r;   r   rZ   r   r   r   test_user_expressions  s    r   c                  C   sP   t   tdtddd\} }|d }|d }|d dks<t|d	 d
ksLtd S )Nzx=0Z
nosuchnamer   r   rZ   r   rS   rP   r[   	NameErrorr   )r;   r   rZ   r   r   r   r   test_user_expressions_fail  s    r   c                  C   s,   t   td} tt| t}t|d|  d S )Nar   )r   r   r   r   r   r   r   r   r   r   
test_oinfo  s    
r   c                  C   sr   t   tdd\} }td} tt| t}t|d|  |d }|d sJt|d d }d	|ksbtd
|ksntd S )Nza=5r   r   r   r=   r^   rD   r   zType:z
Docstring:r   r   r   r   r   r   r   r   r;   r   r=   r   r   r   r   test_oinfo_found  s    
r   c                  C   sx   t   tdd\} }tjdddd} tt| t}t|d|  |d }|d	 sPt|d
 d }d|kshtd|ksttd S )Nzip=get_ipython()r   zip.object_inspect
   r   )Z
cursor_posZdetail_levelr   r=   r^   rD   r   z
Signature:zSource:r   r   r   r   r   test_oinfo_detail  s    r   c                  C   s@   t   td} tt| t}t|d|  |d }|d r<td S )NZdner   r=   r^   )r   r   r   r   r   r   r   r;   r   r=   r   r   r   test_oinfo_not_found  s    
r   c                  C   s^   t   tdd\} }tdd} tt| t}t|d|  |d d }dD ]}||ksHtqHd S )	Nzalpha = albert = 5r   al   r   r=   rh   )alphaZalbert)r   r   r   ru   r   r   r   r   )r;   r   rh   rk   r   r   r   test_complete  s    r   c                  C   s*   t   t } tt| t}t|d|  d S )Nr   )r   r   r   r   r   r   r   r   r   r   test_kernel_info_request  s    r   c                  C   sF   t   tjd} tj|  | d d }tt|t}t|d| d S )NZconnect_requestr8   r;   r   )	r   r   r@   r   Zshell_channelsendr   r   r   )r   r;   r   r   r   r   test_connect_request  s    r   )   r   z+earlier Jupyter Client don't have comm_info)reasonc                  C   s*   t   t } tt| t}t|d|  d S )Nr   )r   r   r   r   r   r   r   r   r   r   test_comm_info_request  s    r   c                  C   s>   t   tdd\} }|d }dd |D }t|dks:tdS )a  
    We want to test the set_next_input is not triggered several time per cell.
    This is (was ?) mostly due to the fact that `?` in a loop would trigger
    several set_next_input.

    I'm tempted to thing that we actually want to _allow_ multiple
    set_next_input (that's users' choice). But that `?` itself (and ?'s
    transform) should avoid setting multiple set_next_input).
    zJip = get_ipython()
for i in range(3):
   ip.set_next_input('Hello There')
r   payloadc                 S   s   g | ]}|d  dkr|qS )sourceZset_next_inputr   ).0plr   r   r   
<listcomp>  s      z'test_single_payload.<locals>.<listcomp>r   N)r   r   lenr   )r;   r   r   Znext_input_plsr   r   r   test_single_payload  s    

r   c                  C   s,   t   td} tt| t}t|d|  d S )Nza = 1r   )r   r   Zis_completer   r   r   r   r   r   r   test_is_complete  s    
r   c                  C   sn   t   tjddd tjtd tjddddddd	} tt| t}t|d
|  |d }t|d dksjt	d S )Nr   Tr|   Zstore_historyr   ranger   r   r   )hist_access_typerawoutputstartstopr@   r   r=   r   
r   r   r   r   r   r   r   r   r   r   r   r   r   r   test_history_range#  s    r   c                  C   sl   t   tjddd tjtd tjdddddd} tt| t}t|d	|  |d
 }t|d dksht	d S )Nr   Tr   r   tailr   r   )r   r   r   nr@   r   r=   r   r   r   r   r   r   test_history_tail0  s    r   c                  C   sn   t   tjddd tjtd tjddddddd	} tt| t}t|d
|  |d }t|d dksjt	d S )Nr   Tr   r   searchr   *r   )r   r   r   r   patternr@   r   r=   r   r   r   r   r   r   test_history_search=  s         r   c                  C   sF   t   td\} }tjtd}t|d|  |d }|d dksBtd S )Nzprint('hi')r   r   r=   r   zhi
r   r   r   r   r   r   r   )r;   r   r   r=   r   r   r   test_streamO  s    r   c                  C   sL   t   td\} }tjtd}t|d| d |d d }|d dksHtd S )	Nz/from IPython.display import display; display(1)r   r   )r   r=   rD   r   1r   )r;   r   ZdisplayrD   r   r   r   test_display_dataZ  s    r   )NN)Vr&   rerl   queuer   r   Zjupyter_client._versionr   Zjupyter_client.blocking.clientr   Zpackaging.versionr   r1   Z	traitletsr   r   r   r	   r
   r   r   r   r   utilsr   r   r   r   r   r   __annotations__r   r   r6   r7   compilerG   rC   rN   rT   rV   rW   rX   r]   r_   rc   rg   ri   rn   rr   rs   rt   ry   r{   r}   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   markZskipifr   r   r   r   r   r   r   r   r   r   r   r   <module>   s   ,

	
!

	
