a
    –§»a}  ã                   @   s¨   d dl mZ d dlmZ d dlmZ d dlmZ d dlZd dl	Z	d dl
m
Z
 d dl Z zd dlmZ dd	„ ZW n& ey’   d dlmZ d
d	„ ZY n0 G dd„ deƒZdS )é    )ÚTestCase)Úutils)Úconvert)ÚreadsN)Úcopy)ÚEmptyc                 C   s
   t | tƒS ©N)Ú
isinstanceÚstr©Ús© r   úP/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/numba/testing/notebook.pyÚisstr   s    r   c                 C   s
   t | tƒS r   )r	   Ú
basestringr   r   r   r   r      s    c                   @   sd   e Zd ZdZg d¢Zg d¢ZdZdd„ Zdd„ Zd	d
„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ ZdS )ÚNotebookTestaV  Validate a notebook. All code cells are executed in order. The output is either checked
    for errors (if no reference output is present), or is compared against expected output.


    Useful references:
    http://nbformat.readthedocs.org/en/latest/format_description.html
    http://jupyter-client.readthedocs.org/en/latest/messaging.html
)Zexecute_requestZexecute_inputÚstatusZpyin)Zexecution_countÚ	tracebackZprompt_numberÚsourceé   c           	      C   s€   t |ƒ�$}tt| ¡ ƒ| jƒ}W d   ƒ n1 s20    Y  t ¡ \}}tdd„ |jD ƒƒD ]\}}|  	|  
|¡|||¡ q\d S )Nc                 S   s   g | ]}|j d kr|‘qS )Úcode)Z	cell_type)Ú.0Úcr   r   r   Ú
<listcomp>-   ó    z/NotebookTest._test_notebook.<locals>.<listcomp>)Úopenr   r   ÚreadÚNBFORMAT_VERSIONr   Zstart_new_kernelÚ	enumerateÚcellsÚ_test_notebook_cellÚsanitize_cell)	ÚselfZnotebookÚtestÚfÚnbÚ_ÚkernelÚir   r   r   r   Ú_test_notebook(   s
    
2zNotebookTest._test_notebookc                    s8  t |dƒr|j}n|j}|j}| |¡ g }d }d}	d}
d}ˆ  |¡röz|jddd�}W n tyn   Y q<Y n0 |d ˆ jvr<|d dkrºd	|d
 d  d |d
 d  }d}	|
dkrº|}
t	|ƒ}|t	|j
ƒk rÚ|j
| pÜg }ˆ  ||¡}| |¡ q<|dk�rˆ  |	d|
|f ¡ n‡ fdd„}||ƒ}||j
ƒ}d S )Nr   TéÿÿÿÿÚ é   )ÚblockÚtimeoutÚmsg_typeÚerrorz  ÚcontentZenamez
  ZevalueFZcheck_errorz*Executing cell %d resulted in an error:
%sc                    s   ˆ   tˆ  | ¡ƒ¡S r   )Údump_canonicalÚlistÚscrub_outputs)Úx©r"   r   r   Ú<lambda>S   r   z2NotebookTest._test_notebook_cell.<locals>.<lambda>)Úhasattrr   ÚinputZiopub_channelÚexecuteÚshould_continueZget_msgr   ÚIGNORE_TYPESÚlenÚoutputsÚtransform_messageÚappendÚ
assertTrue)r"   Úcellr(   r'   r#   r   Ziopubr>   ÚmsgZno_errorZfirst_errorÚ	error_msgÚexpectedÚoÚscrubZscrubbedr   r6   r   r    0   s<    



 
z NotebookTest._test_notebook_cellc                 C   s   t j|ddd�S )Né   T)ÚindentÚ	sort_keys)ÚjsonÚdumps)r"   Úobjr   r   r   r2   Z   s    zNotebookTest.dump_canonicalc                 #   sž   |D ]”}t |ƒ}dD ]|\‰ ‰‡ ‡fdd„}d|v rB||d ƒ|d< d|v rt|d tƒr€|d  ¡ D ]\}}||ƒ|d |< qdq||d ƒ|d< q|V  qdS )z=
        remove all scrubs from output data and text
        r   c                    s.   t | ƒrt ˆ ˆ| ¡S ‡ ‡fdd„| D ƒS d S )Nc                    s   g | ]}t  ˆ ˆ|¡‘qS r   )ÚreÚsub)r   Úline©rG   rO   r   r   r   i   r   zCNotebookTest.scrub_outputs.<locals>._scrubLines.<locals>.<listcomp>)r   rN   rO   )ÚlinesrQ   r   r   Ú_scrubLinese   s    z/NotebookTest.scrub_outputs.<locals>._scrubLinesÚtextÚdataN)r   r	   ÚdictÚitems)r"   r>   ÚoutputÚoutrS   ÚmimerU   r   rQ   r   r4   ]   s    zNotebookTest.scrub_outputsc                 C   s   | j D ]}| |d¡ q|S )zE
        remove keys from STRIP_KEYS to ensure comparability
        N)Ú
STRIP_KEYSÚpop)r"   ÚdÚkeyr   r   r   Ú
strip_keysv   s    
zNotebookTest.strip_keysc                 C   s   |j D ]}|  |¡ q|S )z0
        remove non-reproducible things
        )r>   r_   )r"   rB   rX   r   r   r   r!   ~   s    
zNotebookTest.sanitize_cellc                 C   sŽ   ddddœi}d|d i}|  |d ¡ |  |¡}| ¡ D ],\}}||v r:|| |v r:|||  ||< q:d|v rŠd|vrŠ|d |d< |d= |S )	zF
        transform a message into something like the notebook
        Úoutput_typeZexecute_resultr0   )ZpyoutZpyerrr/   r1   rU   rT   )Úupdater_   rW   )r"   rC   rE   Z	SWAP_KEYSrX   r^   Zswapsr   r   r   r?   †   s     þÿÿ
zNotebookTest.transform_messagec                 C   s*   |du rdS |d dko&|d d dk S )zQ
        determine whether the current message is the last for this cell
        NTr/   r   r1   Zexecution_stateZidler   )r"   rC   r   r   r   r;   ¡   s
    ÿzNotebookTest.should_continueN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r<   r[   r   r)   r    r2   r4   r_   r!   r?   r;   r   r   r   r   r      s   
*r   )Zunittestr   Zipykernel.testsr   Znbformat.converterr   Znbformat.readerr   rN   rK   r   Úqueuer   r   ÚImportErrorÚQueuer   r   r   r   r   Ú<module>   s   