a
    v=ic                     @   s   d dl 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Z d dlm	Z	 d dl
Zd dlZd dlZzeed\ZZW n ey   dZY n0 dZee ee [[G dd dejZed	d
 ejdd d D ZedddZdd Zdd ZdS )    N)contextmanagerwrapsi  FTc                   @   sh   e Zd ZdZedd Zedd Zdd	d
ZdddZdd Z	dd Z
dd ZdddZdddZdS )TestCasez(
        Base class for unit tests.
    c                 C   s   t jdd| _d S )Nz
h5py-test_)prefix)tempfilemkdtemptempdircls r   b/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/h5py/tests/common.py
setUpClass/   s    zTestCase.setUpClassc                 C   s   t | j d S N)shutilrmtreer	   r
   r   r   r   tearDownClass3   s    zTestCase.tearDownClass.hdf5 Nc                 C   s   |d u r| j }tj|||dS )N)dir)r	   r   mktemp)selfsuffixr   r   r   r   r   r   7   s    zTestCase.mktempc                 C   sH   |d u rddl m} |j}d }| dkr8| |||}||d}|S )Nr   )MPI)Zmpi4pyr   Z
COMM_WORLDZGet_rankr   Zbcast)r   Zcommr   r   r   r   fnamer   r   r   
mktemp_mpi<   s    zTestCase.mktemp_mpic                 C   s   t |  d| _d S )Nw)h5pyFiler   fr   r   r   r   setUpF   s    zTestCase.setUpc                 C   s&   z| j r| j   W n   Y n0 d S r   )r   closer    r   r   r   tearDownI   s
    zTestCase.tearDownc                 C   sl   |D ].}d}|D ]}||krd}q|st d| q|D ].}d}|D ]}||krDd}qD|s8t d| q8d S )NFTz Item '%s' appears in a but not bz Item '%s' appears in b but not a)AssertionError)r   abxmatchyr   r   r   assertSameElementsP   s    zTestCase.assertSameElementsc                 C   sv  |du rd}|du rd}nd| }t |s6t |r|t |rJt |s\J d|||f || |k sxJ d||f dS |j|jksJ d|j|j|f |j|jksJ d|j|j|f |jjdur|jjD ]*}d	||f }| j|| || ||d
 qnn|jjdv rFt t |d |d  |k srJ d||f n,t |d |d ksrJ d|jj	|f dS )z Make sure dset and arr have the same shape, dtype and contents, to
            within the given precision.

            Note that dset may be a NumPy array or an HDF5 dataset.
        Ngh㈵>r   z (%s)z&Scalar/array mismatch ("%r" vs "%r")%sz"Scalars differ by more than %.3f%szShape mismatch (%s vs %s)%szDtype mismatch (%s vs %s)%sz[FIELD %s] %s)message	precision)ir   .z!Arrays differ by more than %.3f%sz"Arrays are not equal (dtype %s) %s)
npZisscalarshapeZdtypenamesassertArrayEqualkindallabsstr)r   dsetarrr+   r,   nr   r   r   r1   a   s<    
$
zTestCase.assertArrayEqualFc           	   
   C   s   d}z|| }W n, t y< } zt|}W Y d}~n
d}~0 0 t|trL|n|f}|du r| || | |s| |j|| nl| | ||  W d   n1 s0    Y  |s| | |j| W d   n1 s0    Y  dS )a   Apply slicing arguments "s" to both dset and arr.

        Succeeds if the results of the slicing are identical, or the
        exception raised is of the same type for both.

        "arr" must be a Numpy array; "dset" may be a NumPy array or dataset.
        N)	Exceptiontype
isinstancetupler1   Z_fast_readerreadassertRaises)	r   r6   r7   sZskip_fast_readerexcZ
arr_resulteZs_fastr   r   r   assertNumpyBehavior   s$    
&zTestCase.assertNumpyBehavior)r   r   N)Nr   r   N)NN)F)__name__
__module____qualname____doc__classmethodr   r   r   r   r!   r#   r*   r1   rB   r   r   r   r   r   )   s   





$r   c                 C   s   g | ]}t |qS r   )int).0r-   r   r   r   
<listcomp>       rJ   .   r   c                 c   sh   t jd| dd0}|j}|dur2|| |  W d   n1 sF0    Y  |V  tj|dd dS )z
    Context manager which yields the path to a closed temporary file with the
    suffix `suffix`. The file will be deleted on exiting the context. An
    additional argument `text` can be provided to have the file contain `text`.
    w+tF)r   deleteNT)ignore_errors)r   NamedTemporaryFilenamewriteflushr   r   )r   textZ	test_file	file_namer   r   r   closed_tempfile   s    
&rW   c                    s   t   fdd}|S )z!Runs a test in its own subprocessc              	      sF  t  d | jj }d| }dD ]}||d}q"tj|d }|r\ | g|R i |S dtj|< tj }d||< |	t
 di  t }t|d,}	tjtjdd	|g|	|	|d
}
W d    n1 s0    Y  t|d}	|	 }W d    n1 s0    Y  W d    n1 s"0    Y  |
dksBJ d| d S )Nz::ZIN_SUBPROCESS_z/\,:._1subproc_envrN   z-mZpytest)stdoutstderrenvrtr   
)inspectgetsourcefilenoderR   replaceosenvirongetcopyupdategetattrrW   open
subprocesscallsys
executabler=   )requestargskwargsZ	curr_testZinsubcZdefinedr]   r[   fhZrtnoutr   r   r   wrapper   s&    

$Hzinsubprocess.<locals>.wrapperr   )r   rv   r   ru   r   insubprocess   s    rw   c                    s    fdd}|S )z9Set environment variables for the @insubprocess decoratorc                    s
    | _ | S r   )rZ   ru   dr   r   	decorator   s    zsubproc_env.<locals>.decoratorr   )ry   rz   r   rx   r   rZ      s    rZ   )r   N)rm   rd   r   r`   r   rk   
contextlibr   	functoolsr   numpyr.   r   ZunittestutmkstempchrZtestfiler   UnicodeErrorZUNICODE_FILENAMESr"   unlinkr   r<   __version__splitZNUMPY_RELEASE_VERSIONrW   rw   rZ   r   r   r   r   <module>
   s2   


|"