
    wg}                         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 	 d dlmZ d Z G d
 de      Zy# e$ r d dlmZ d	 ZY w xY w)    )TestCase)utils)convert)readsN)copy)Emptyc                 "    t        | t              S N)
isinstancestrss    [/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/numba/testing/notebook.pyisstrr      s    !S!!    c                 "    t        | t              S r
   )r   
basestringr   s    r   r   r      s    !Z((r   c                   T    e Zd ZdZg dZg dZdZd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zy)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
)execute_requestexecute_inputstatuspyin)execution_count	tracebackprompt_numbersource   c                    t        |      5 }t        t        |j                               | j                        }d d d        t        j                         \  }}t        j                  D cg c]  }|j                  dk(  s| c}      D ](  \  }}| j                  | j                  |      |||       * y # 1 sw Y   xY wc c}w )Ncode)openr   r   readNBFORMAT_VERSIONr   start_new_kernel	enumeratecells	cell_type_test_notebook_cellsanitize_cell)	selfnotebooktestfnb_kernelcis	            r   _test_notebookzNotebookTest._test_notebook(   s    (^ 	Aqqvvx$*?*?@B	A**,	6"((LQakkV6KqLM 	MDAq$$T%7%7%:AvtL	M	A 	A Ms   .B8,CC8Cc                     t        |d      r|j                  }n|j                  }|j                  }|j	                  |       g }d }d}	d}
d} j                  |      r	 |j                  dd      }|d    j                  vr|d   dk(  r d	|d
   d   z   dz   |d
   d   z   }d}	|
dk(  r|}
t        |      }|t        |j                        k  xr |j                  |   xs g } j                  ||      }|j                  |        j                  |      r|dk(  r j                  |	d|
|fz         y  fd} ||      } ||j                        }y # t        $ r Y w xY w)Nr   T    )blocktimeoutmsg_typeerrorz  contentenamez
  evalueFcheck_errorz*Executing cell %d resulted in an error:
%sc                 V    j                  t        j                  |                   S r
   )dump_canonicallistscrub_outputs)xr*   s    r   <lambda>z2NotebookTest._test_notebook_cell.<locals>.<lambda>S   s"    d11$t7I7I!7L2MN r   )hasattrr   inputiopub_channelexecuteshould_continueget_msgr   IGNORE_TYPESlenoutputstransform_messageappend
assertTrue)r*   cellr2   r0   r,   r    iopubrN   msgno_errorfirst_error	error_msgexpectedoscrubscrubbeds   `               r   r(   z NotebookTest._test_notebook_cell0   s   4";;D::D$$t	""3'mm$m: :d&7&77z?g- $s9~g'> > G#i.YaJb bI$H"b(&'Ls4<<00DT\\!_J**39q! ""3'  M!OOH&SU`bkTl&lm
 OEW~HT\\*H+  s    E 	E#"E#c                 2    t        j                  |dd      S )N   T)indent	sort_keys)jsondumps)r*   objs     r   rA   zNotebookTest.dump_canonicalZ   s    zz#a488r   c              #      K   |D ]  }t        |      }dD ]m  \  fd}d|v r ||d         |d<   d|v s#t        |d   t              r*|d   j                         D ]  \  }} ||      |d   |<    ` ||d         |d<   o |  yw)z=
        remove all scrubs from output data and text
         c                     t        |       rt        j                  |       S | D cg c]  }t        j                  |       c}S c c}w r
   )r   resub)lineslinerZ   rg   s     r   _scrubLinesz/NotebookTest.scrub_outputs.<locals>._scrubLinese   s>    U|!vveS%88EJKTuc4 8KKKs   A	textdataN)r   r   dictitems)	r*   rN   outputoutrj   mimerl   rZ   rg   s	          @@r   rC   zNotebookTest.scrub_outputs]   s       	Fv,C  ?
sL S="-c&k":CKS=!#f+t4*-f+*;*;*= BJD$0;D0ACK-B '2#f+&>F?  I'	s   5BABc                 L    | j                   D ]  }|j                  |d        |S )zE
        remove keys from STRIP_KEYS to ensure comparability
        N)
STRIP_KEYSpop)r*   dkeys      r   
strip_keyszNotebookTest.strip_keysv   s*     ?? 	CEE#t	r   c                 J    |j                   D ]  }| j                  |        |S )z0
        remove non-reproducible things
        )rN   rw   )r*   rR   ro   s      r   r)   zNotebookTest.sanitize_cell~   s(     ll 	$FOOF#	$r   c                     ddddi}d|d   i}|j                  |d          | j                  |      }|j                         D ]  \  }}||v s||   |v s|||      ||<    d|v rd|vr|d   |d<   |d= |S )	zF
        transform a message into something like the notebook
        output_typeexecute_resultr;   )pyoutpyerrr:   r<   rl   rk   )updaterw   rn   )r*   rT   rX   	SWAP_KEYSro   rv   swapss          r   rO   zNotebookTest.transform_message   s    
 ) 
	 C
O
 	c)n%(#//+ 	1JCf}!5#F3K0s	1 Vh 6#F^F6Nvr   c                 4    |y|d   dk(  xr |d   d   dk(   S )zQ
        determine whether the current message is the last for this cell
        Tr:   r   r<   execution_stateidlerd   )r*   rT   s     r   rJ   zNotebookTest.should_continue   s<     ;
Ox/ @	N#45?A 	Ar   N)__name__
__module____qualname____doc__rL   rs   r#   r3   r(   rA   rC   rw   r)   rO   rJ   rd   r   r   r   r      sE     JLLJM%+T926Ar   r   )unittestr   ipykernel.testsr   nbformat.converterr   nbformat.readerr   rf   r`   r   queuer   r   ImportErrorQueuer   rd   r   r   <module>r      sS     ! & ! 	   )"PA8 PA  ))	)s   	A AA