o
    Zh-                     @   s  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  m	Z	 ddl
mZmZ ejeje ejeZdd ZG dd deZG dd	 d	eZG d
d deZejdd Zejdd Zejdd Zejdd Zejdd Zejdd Zejdd Z ejdd Z!dd Z"ej#j$e	j%du ddd d! Z&ej#j$ed"dej#'d#d$d%gd&d' Z(d(d) Z)d*d+ Z*ej#j$e	j%du ddd,d- Z+ej#j'd.g d/ddgd0d1d2 Z,e-d3kre.  dS dS )4z
Tests for iofuncs.py.
    N)is_text_stringPY2c              
   C   s   | d u r
|d u r
dS d}t | D ];}z|o#tt|| | | k}W n ty@   |o=tdd t|| | | D }Y nw tt	|d t	|  q|S )NTc                 S   s   g | ]\}}t ||kqS  )npall).0Zobj1obj2r   r   ^/var/www/html/lang_env/lib/python3.10/site-packages/spyder_kernels/utils/tests/test_iofuncs.py
<listcomp>-   s    z(are_namespaces_equal.<locals>.<listcomp>z: )
sortedkeysboolr   mean
ValueErrorr   zipprintstr)actualexpectedZ	are_equalvarr   r   r	   are_namespaces_equal#   s"   

r   c                   @   s    e Zd ZdZdd Zdd ZdS )	CustomObjz&A custom class of objects for testing.c                 C   s   d | _ |r
|| _ d S d S N)data)selfr   r   r   r	   __init__5   s   
zCustomObj.__init__c                 C   s   | j |j kS r   )__dict__)r   otherr   r   r	   __eq__:   s   zCustomObj.__eq__N)__name__
__module____qualname____doc__r   r   r   r   r   r	   r   3   s    r   c                   @      e Zd ZdZdd ZdS )UnDeepCopyableObjz-A class of objects that cannot be deepcopied.c                 C   s   t  r   )RuntimeError)r   r   r   r	   __getstate__@   s   zUnDeepCopyableObj.__getstate__N)r   r    r!   r"   r&   r   r   r   r	   r$   >       r$   c                   @   r#   )UnPickleableObjz8A class of objects that can deepcopied, but not pickled.c                 C   s    | j | j }|j| j |S r   )	__class____new__r   update)r   memoZnew_oner   r   r	   __deepcopy__F   s   zUnPickleableObj.__deepcopy__N)r   r    r!   r"   r-   r   r   r   r	   r(   D   r'   r(   c                  C   sf   d} d}t d}dt jdt jdd}t jdt jdd	t jdt jdt jdtd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   )Zdtype)ab   g      E@ABCDE)r   eyeZ
complex128Zint64Zbool_objectr5   r   r   r	   spydata_valuesO   s   
.r=   c                  C   s\   t jtd} tj| d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)Zallow_pickler6   r7   r8   r9   r:   r5   )ospathjoinLOCATIONr   loaditem)r?   Zfile_sr6   r7   r8   r9   r:   r   r   r	   real_values_   s   rD   c                 C   sV   t | }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_stringZ
module_obj	class_objfunction_objspamunpickleable_instancer/   undeepcopyable_instanceeggscustom_instance)
copydeepcopyio	Exceptionr>   r?   r@   r(   r$   r   r=   namespace_dictr   r   r	   namespace_objects_fullw   s   
rS   c                 C   s   t | }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.
    rK   rL   )rM   rN   r   rQ   r   r   r	   namespace_objects_filtered   s   
rT   c                  C   s$   i } d| d< t | d< td| d< | S )z;
    Define a dictionary of that cannot be deepcopied.
    No supported objects to saverE   rF   r/   rJ   )rP   r$   rR   r   r   r	   namespace_objects_nocopyable   s
   rW   c                  C   s(   i } d| d< t jj| d< td| d< | S )z@
    Define a dictionary of objects that cannot be pickled.
    rU   rE   rG   rH   rI   )r>   r?   r@   r(   rV   r   r   r	   namespace_objects_nopickleable   s
   rX   c                 C      | j d u rd S | | j S r   paramZgetfixturevaluerequestr   r   r	   input_namespace      
r^   c                 C   rY   r   rZ   r\   r   r   r	   expected_namespace   r_   r`   c                  C   sL   t jtd} t| }t|tsJ |\}}|d t	dkr"|r$J dS )z6
    Test the load of .npz files as dictionaries.
    zimport_data.npzZval1r.   N)
r>   r?   r@   rA   iofuncsZ
load_array
isinstancetupler   array)filenamer   	variableserrorr   r   r	   test_npz_import   s
   
rh   zSciPy required)reasonc                 C   s\   t jtd}t|\}}d}t|  D ]}|o&tt	
| | || k}q|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)r>   r?   r@   rA   ra   load_matlabr   r   r   r   r   )rD   r?   inf_validr   r   r   r	   test_matlab_import   s    rn   zFails on Python 2spydata_file_namezexport_data.spydatazexport_data_renamed.spydatac                 C   s:   t jt| }t|\}}|du s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)r>   r?   r@   rA   ra   load_dictionaryr   )ro   r=   r?   r   rg   r   r   r	   test_spydata_import   s   rq   c                  C   sT   t  } t jtd}t|\}}|rt|sJ |du s J t  | 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)r>   getcwdr?   r@   rA   ra   rp   r   )original_cwdr?   r   rg   r   r   r	   test_spydata_import_witherror   s   rt   c                  C   sP   t  } t jtd}zt| W n	 ty   Y nw J t  | 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)r>   rr   r?   r@   rA   ra   rp   IOError)rs   r?   r   r   r	    test_spydata_import_missing_file  s   rv   c                  C   s   t  } 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 }t | | |d t 	|\}}|du sIJ |d dksQJ |d	 jdksZJ |d 
 g dgksgJ dS )
z%Test support for matlab stlye struct.rH   r3   rK   d)cr3   )r.   r4   r0   r   Nrx   )ra   ZMatlabStructr3   rx   rw   rO   BytesIOZsave_matlabseekrj   tolist)r2   bufr   rg   r   r   r	   test_matlabstruct  s   

r}   z+input_namespace,expected_namespace,filename))r=   r=   Zexport_data_copy)rS   rT   Zexport_data_2)rW   NZexport_data_none_1)rX   NZexport_data_none_2)Zindirectc           	      C   s,  t jt|d }d}d| v r| d }| d= t  }z`t| |}||ks'J |du r4t j|r3J n t|\}}|du sAJ t	|
  t	|
  t||sTJ |t  ks\J W t j|ryzt | W dS  tttfyx   Y dS w dS t j|rzt | W w  tttfy   Y w w 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.spydataNrE   )r>   r?   r@   rA   rr   ra   Zsave_dictionaryisfilerp   r   r   r   removeru   OSErrorPermissionError)	r^   r`   re   r?   Zexpected_errorZcwd_originalZexport_errorZdata_actualimport_errorr   r   r	   test_spydata_export-  s<   r   __main__)/r"   rO   r>   rM   Zpytestnumpyr   Zspyder_kernels.utils.iofuncsutilsra   Zspyder_kernels.py3compatr   r   r?   realpathr@   rr   dirname__file__rA   r   r<   r   r$   r(   Zfixturer=   rD   rS   rT   rW   rX   r^   r`   rh   markZskipifrj   rn   Zparametrizerq   rt   rv   r}   r   r   mainr   r   r   r	   <module>   sd   













$