
    
d-                     2   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mc m	Z	 ddl
mZmZ ej                            ej                             ej                    ej                            e                              Zd Z G d de          Z G d de          Z G d	 d
e          Zej        d             Zej        d             Zej        d             Zej        d             Zej        d             Zej        d             Zej        d             Z ej        d             Z!d Z"ej#        $                    e	j%        du d          d             Z&ej#        $                    ed          ej#        '                    dddg          d                         Z(d Z)d Z*ej#        $                    e	j%        du d          d             Z+ej#        '                    dg d d!d"g#          d$             Z,e-d%k    r ej.                     dS dS )&z
Tests for iofuncs.py.
    N)is_text_stringPY2c           
         | |dS d}t          |                                          D ]}	 |o0t          t          j        ||         | |         k                        }nE# t
          $ r8 |o2t          d t          ||         | |                   D                       }Y nw xY wt          t          |          dz   t          |          z              |S )NTc                 D    g | ]\  }}t          j        ||k              S  )npall).0obj1obj2s      Glib/python3.11/site-packages/spyder_kernels/utils/tests/test_iofuncs.py
<listcomp>z(are_namespaces_equal.<locals>.<listcomp>-   s<     J J J*$%% J J J    z: )
sortedkeysboolr   mean
ValueErrorr	   zipprintstr)actualexpected	are_equalvars       r   are_namespaces_equalr   #   s   ~(*tIhmmoo&& 0 0	K! /d27,,. ,. '/ '/II 	K 	K 	K! KcJ JHSM<B3K9I 9I J J J'K 'KIII	K 	c#hhoI.////s   3A!!?B#"B#c                       e Zd ZdZd Zd ZdS )	CustomObjz&A custom class of objects for testing.c                 *    d | _         |r	|| _         d S d S N)data)selfr!   s     r   __init__zCustomObj.__init__5   s&    	 	DIII	 	r   c                 "    | j         |j         k    S r    )__dict__)r"   others     r   __eq__zCustomObj.__eq__:   s    }..r   N)__name__
__module____qualname____doc__r#   r'   r   r   r   r   r   3   s8        00  
/ / / / /r   r   c                       e Zd ZdZd ZdS )UnDeepCopyableObjz-A class of objects that cannot be deepcopied.c                     t                      r    )RuntimeError)r"   s    r   __getstate__zUnDeepCopyableObj.__getstate__@   s    nnr   N)r(   r)   r*   r+   r0   r   r   r   r-   r-   >   s)        77    r   r-   c                       e Zd ZdZd ZdS )UnPickleableObjz8A class of objects that can deepcopied, but not pickled.c                     | j                             | j                   }|j                            | j                   |S r    )	__class____new__r%   update)r"   memonew_ones      r   __deepcopy__zUnPickleableObj.__deepcopy__F   s7    .((88...r   N)r(   r)   r*   r+   r9   r   r   r   r2   r2   D   s)        BB    r   r2   c                  >   d} d}t          j        d          }dt          j        dt           j                  d}t          j        dt           j                  d	t          j        dt           j                  t          j        dt
                    g}| ||||d
S )z
    Define spydata file ground truth values.

    The file export_data.spydata contains five variables to be loaded.
    This fixture declares those variables in a static way.
       ham   T   )dtype)ab   g      E@ABCDE)r   eye
complex128int64bool_objectrC   s        r   spydata_valuesrN   O   s     	
AA
q		A77788A		"	"	"D"&"(*C*C*CRVAU[E\E\E\]A333r   c                      t           j                            t          d          } t	          j        | d          }|d                                         }|d         }|d         }|d                                         }|d         }|||||d	S )
aZ  
    Load a Numpy pickled file.

    The file numpy_data.npz contains six variables, each one represents the
    expected test values after a manual conversion of the same variables
    defined and evaluated in MATLAB. The manual type conversion was done
    over several variable types, such as: Matrices/Vectors, Scalar and
    Complex numbers, Structs, Strings and Cell Arrays. The set of variables
    was defined to allow and test the deep conversion of a compound type,
    i.e., a struct that contains other types that need to be converted,
    like other structs, matrices and Cell Arrays.
    znumpy_data.npzT)allow_picklerD   rE   rF   rG   rH   rC   )ospathjoinLOCATIONr   loaditem)rR   file_srD   rE   rF   rG   rH   s          r   real_valuesrX   _   s     7<<"233DWT---FsAsAsAsAsA333r   c                     t          j        |           }d|d<   t          |d<   t          |d<   t          j        j        |d<   t          d          |d<   t          d          |d	<   t          d
          |d<   |S )z
    Define a dictionary of objects of a variety of different types to be saved.

    This fixture reprisents the state of the namespace before saving and
    filtering out un-deep-copyable, un-pickleable, and uninteresting objects.
    zOSome objects could not be saved: undeepcopyable_instance, unpickleable_instanceexpected_error_string
module_obj	class_objfunction_objspamunpickleable_instancer<   undeepcopyable_instanceeggscustom_instance)
copydeepcopyio	ExceptionrQ   rR   rS   r2   r-   r   rN   namespace_dicts     r   namespace_objects_fullri   w   s     ]>22N	9 *+ $&N< "+N;%'W\N>".=f.E.EN*+0A%0H0HN,-(1&(9(9N$%r   c                 R    t          j        |           }t          d          |d<   |S )z
    Define a dictionary of the objects from the namespace that can be saved.

    This fixture reprisents the state of the namespace after saving and
    filtering out un-deep-copyable, un-pickleable, and uninteresting objects.
    ra   rb   )rc   rd   r   rg   s     r   namespace_objects_filteredrk      s,     ]>22N(1&(9(9N$%r   c                  L    i } d| d<   t           | d<   t          d          | d<   | S )z;
    Define a dictionary of that cannot be deepcopied.
    No supported objects to saverZ   r\   r<   r`   )rf   r-   rh   s    r   namespace_objects_nocopyablero      s7    
 N.LN*+"+N;0A%0H0HN,-r   c                  `    i } d| d<   t           j        j        | d<   t          d          | d<   | S )z@
    Define a dictionary of objects that cannot be pickled.
    rm   rZ   r]   r^   r_   )rQ   rR   rS   r2   rn   s    r   namespace_objects_nopickleablerq      s;    
 N.LN*+%'W\N>".=f.E.EN*+r   c                 H    | j         d S |                     | j                   S r    paramgetfixturevaluerequests    r   input_namespacerx      %    }t&&w}555r   c                 H    | j         d S |                     | j                   S r    rs   rv   s    r   expected_namespacer{      ry   r   c                      t           j                            t          d          } t	          j        |           }t          |t                    sJ |\  }}|d         t          j	        d          k    r|rJ dS )z6
    Test the load of .npz files as dictionaries.
    zimport_data.npzval1r;   N)
rQ   rR   rS   rT   iofuncs
load_array
isinstancetupler   array)filenamer!   	variableserrors       r   test_npz_importr      ss     w||H&788Hh''DdE"""""IuV++E+++++r   zSciPy required)reasonc                 8   t           j                            t          d          }t	          j        |          \  }}d}t          |                                           D ]5}|o0t          t          j
        | |         ||         k                        }6|sJ dS )a  
    Test the automatic conversion and import of variables from MATLAB.

    This test loads a file stored in MATLAB, the variables defined are
    equivalent to the manually converted values done over Numpy. This test
    allows to evaluate the function which processes the conversion automa-
    tically. i.e., The automatic conversion results should be equal to the
    manual conversion of the variables.
    zdata.matTN)rQ   rR   rS   rT   r~   load_matlabr   r   r   r   r   )rX   rR   inf_validr   s         r   test_matlab_importr      s     7<<*--D &&FCEk&&(()) F FE$rw{3'73s8'CDDEELLLLLr   zFails on Python 2spydata_file_namezexport_data.spydatazexport_data_renamed.spydatac                     t           j                            t          |           }t	          j        |          \  }}|J t          ||          sJ dS )a*  
    Test spydata handling and variable importing.

    This test loads all the variables contained inside a spydata tar
    container and compares them against their static values.
    It tests both a file with the original name, and one that has been renamed
    in order to catch Issue #9 .
    N)rQ   rR   rS   rT   r~   load_dictionaryr   )r   rN   rR   r!   r   s        r   test_spydata_importr      sS     7<<"344D)$//KD%===n5555555r   c                     t          j                    } t           j                            t          d          }t          j        |          \  }}|rt          |          sJ |J t          j                    | k    sJ dS )z
    Test that import fails gracefully with a fn not present in the namespace.

    Checks that the error is caught, the message is passed back,
    and the current working directory is restored afterwards.
    z export_data_withfunction.spydataN)rQ   getcwdrR   rS   rT   r~   r   r   )original_cwdrR   r!   r   s       r   test_spydata_import_witherrorr      sx     9;;L7<<"DEED)$//KD%*^E*****<<<9;;,&&&&&&r   c                      t          j                    } t           j                            t          d          }	 t          j        |           J # t          $ r Y nw xY wt          j                    | k    sJ dS )zS
    Test that import fails properly when file is missing, and resets the cwd.
    z$non_existant_path_2019-01-23.spydataFN)rQ   r   rR   rS   rT   r~   r   IOError)r   rR   s     r    test_spydata_import_missing_filer     s     9;;L7<<"HIID%%%
 		    
 9;;,&&&&&&s   A 
AAc                     t          j                    } d| _        | d         dk    sJ d| j        d<   | j        j        dk    sJ | ddiddk    sJ g d| d<   t          j                    }t          j        | |           |                    d           t          j	        |          \  }}|J |d         dk    sJ |d	         j        dk    sJ |d         
                                g dgk    sJ dS )
z%Test support for matlab stlye struct.r^   rA   ra   d)crA   )r;   rB   r=   r   Nr   )r~   MatlabStructrA   r   r   re   BytesIOsave_matlabseekr   tolist)r@   bufr!   r   s       r   test_matlabstructr     s    	A
ACS6VACH35F????sFm&111111YYAcF
*,,C3HHQKKK%c**KD%===99;&    9))),,,,,,r   z+input_namespace,expected_namespace,filename))rN   rN   export_data_copy)ri   rk   export_data_2)ro   Nexport_data_none_1)rq   Nexport_data_none_2rx   r{   )indirectc                    t           j                            t          |dz             }d}d| v r| d         }| d= t          j                    }	 t          j        | |          }||k    sJ |"t           j                            |          rJ not          j        |          \  }}|J t          |
                                           t          |
                                           t          ||          sJ |t          j                    k    sJ 	 t           j                            |          r5	 t          j        |           dS # t          t          t          f$ r Y dS w xY wdS # t           j                            |          r3	 t          j        |           w # t          t          t          f$ r Y w w xY ww xY w)z
    Test spydata export and re-import.

    This test saves the variables in ``spydata`` format and then
    reloads and checks them to make sure they save/restore properly
    and no errors occur during the process.
    z.spydataNrZ   )rQ   rR   rS   rT   r   r~   save_dictionaryisfiler   r   r   r   remover   OSErrorPermissionError)	rx   r{   r   rR   expected_errorcwd_originalexport_errordata_actualimport_errors	            r   test_spydata_exportr   -  s    7<<(Z"788DN/11()@A349;;L.EE~----%w~~d++++++(/(?(E(E%K'''+""$$%%%$))++,,,'5GHHHHHry{{*****7>>$ 		$Wo6   	 	27>>$ 		$Wo6   	sC   C	E. 8E E)(E). GF$#G$F>;G=F>>G__main__)/r+   re   rQ   rc   pytestnumpyr   spyder_kernels.utils.iofuncsutilsr~   spyder_kernels.py3compatr   r   rR   realpathrS   r   dirname__file__rT   r   rM   r   r-   r2   fixturerN   rX   ri   rk   ro   rq   rx   r{   r   markskipifr   r   parametrizer   r   r   r   r   r(   mainr   r   r   <module>r      s   
 
			 				       / . . . . . . . . 8 8 8 8 8 8 8 8 7BGLL)+)B)BD D E E   / / / / / / / /    	       '    4 4 4 4 4 4.   * 
 
 
 	 	 	 	 	 	 6 6 6 6 6 6: : : G'4/8HII  JI$ C 344,/D/L/N O O6 6O O 546' ' '' ' '  G'4/8HII- - JI-* F I I I
 #$89  ; ;! !; ;!H zFKMMMMM r   