o
    gi>$                     @   sJ  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Zd dlmZ d dl	m
Z
 d dlZd dlmZ d dlZd dlmZ d dlZzeed\ZZW n eyV   dZY nw 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#e$ Z%dde&de&fddZ'de(fddZ)dS )    N)contextmanagerwraps)repack_fields)phili  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   O/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/h5py/tests/common.py
setUpClass1   s   zTestCase.setUpClassc                 C   s   t | j d S N)shutilrmtreer   r   r   r   r   tearDownClass5   s   zTestCase.tearDownClass.hdf5tmpNc                 C   s"   |d u r| j }tj|t||dS )N)dir)r   r	   mktemp	make_name)selfsuffixr   r   r   r   r   r   9   s   zTestCase.mktemp c                 C   sH   |d u rddl m} |j}d }| dkr| |||}||d}|S )Nr   )MPI)mpi4pyr   
COMM_WORLDGet_rankr   bcast)r   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   setUpH   s   zTestCase.setUpc                 C   s*   z| j r| j   W d S W d S    Y d S r   )r(   closer)   r   r   r   tearDownK   s   zTestCase.tearDownc                 C   sl   |D ]}d}|D ]}||krd}q|st d| q|D ]}d}|D ]}||kr*d}q"|s3t d| qd 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   assertSameElementsR   s&   zTestCase.assertSameElementsTc                 C   s  |du rd}|du rd}nd| }t |st |r8t |r%t |s.J d|||f t |}t |}|j|jksIJ d|j|j|f |j|jkr|rX|j}|j}n
t|j}t|j}||ksoJ d|||f |s||jkr}tt |}||jkrtt |}|jjdur|jjD ]}d||f }| j|| || |||d	 qdS |jjd
v rt 	t 
|d |d  |k sJ d||f dS |jjdkrt|j|jddD ]\}	}
| j|	|
|||d	 qdS t 	|d |d ksJ d|jj|f dS )z Make sure dset and arr have the same shape, dtype and contents, to
            within the given precision, optionally ignoring differences in dtype alignment.

            Note that dset may be a NumPy array or an HDF5 dataset.
        Ngh㈵>r   z (%s)z&Scalar/array mismatch ("%r" vs "%r")%szShape mismatch (%s vs %s)%szDtype mismatch (%s vs %s)%sz[FIELD %s] %s)message	precisioncheck_alignment)ir(   .z!Arrays differ by more than %.3f%sOT)strictz"Arrays are not equal (dtype %s) %s)npisscalarasarrayshapedtyper   namesassertArrayEqualkindallabszipflatstr)r   dsetarrr4   r5   r6   normalized_dset_dtypenormalized_arr_dtypenv1v2r   r   r   r@   c   sX   






"
zTestCase.assertArrayEqualFc           	   
   C   sZ  d}z|| }W n t y } z
t|}W Y d}~nd}~ww t|tr&|n|f}|du rX| || | |sVt | |j|| W d   dS 1 sOw   Y  dS dS | | ||  W d   n1 slw   Y  |s| |) t |j| W d   n1 sw   Y  W d   dS W d   dS 1 sw   Y  dS 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tupler@   r   _fast_readerreadassertRaises)	r   rG   rH   sskip_fast_readerexc
arr_resultes_fastr   r   r   assertNumpyBehavior   s4   
"
PzTestCase.assertNumpyBehavior)r   r   N)Nr   r   N)NNT)F)__name__
__module____qualname____doc__classmethodr   r   r   r$   r*   r,   r3   r@   r[   r   r   r   r   r   +   s    





4r   c                 C   s   g | ]}t |qS r   )int).0r7   r   r   r   
<listcomp>   s    rc   .   r   c                 c   sj    t jd| dd}|j}|dur|| |  W d   n1 s$w   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   text	test_file	file_namer   r   r   closed_tempfile   s   
rp   c                    s   t   fdd}|S )z!Runs a test in its own subprocessc              	      s6  t  d | jj }d| }dD ]}||d}qtj|d }|r. | g|R i |S tj }d||< |	t
 di  t D}t|d}	tjtjdd	|g|	|	|d
}
W d    n1 sbw   Y  t|d}	|	 }W d    n1 s{w   Y  W d    n1 sw   Y  |
dksJ d| d S )Nz::IN_SUBPROCESS_z/\,:._1subproc_envrf   z-mpytest)stdoutstderrenvrtr   
)inspectgetsourcefilenoderj   replaceosenvirongetcopyupdategetattrrp   open
subprocesscallsys
executablerS   )requestargskwargs	curr_testinsubcdefinedrx   rv   fhrtnoutr(   r   r   wrapper   s,   

zinsubprocess.<locals>.wrapperr   )r(   r   r   r   r   insubprocess   s   r   c                    s    fdd}|S )z9Set environment variables for the @insubprocess decoratorc                    s
    | _ | S r   )rt   r   dr   r   	decorator   s   zsubproc_env.<locals>.decoratorr   )r   r   r   r   r   rt      s   rt   footemplate_or_prefixreturnc                C   s8   t  }|tkr
dnd| }d| v r| |S | | S )a  Return a static name, to be used e.g. as dataset name.

    When running in pytest-run-parallel, append a thread ID to the name.
    This allows running tests on shared resources, e.g. two threads can attempt to write
    to separate datasets on the same File at the same time (even though the actual
    writes will be serialized by the `phil` lock).

    Calling this function twice from the same thread will return the same name.

    Parameters
    ----------
    template_or_prefix
        Either a prefix to which potentially append the thread ID, or a template
        containing exactly one "{}" to be replaced with the thread ID.
    r   -z{})	threading	get_identMAIN_THREAD_IDformat)r   tidr   r   r   r   r      s
   
r   c                  C   s   t  } | tkS )zReturn True if the test calling this function is being executed
    in the main thread; False otherwise.
    This can be used to detect when a test is running in pytest-run-parallel.
    that spawns multiple separate threads to run the tests.
    )r   r   r   )r   r   r   r   is_main_thread  s   r   )r   N)r   )*r   r   r   r{   r	   r   r   
contextlibr   	functoolsr   numpyr:   numpy.lib.recfunctionsr   r&   h5py._objectsr   unittestutmkstempchrtestfiler#   UnicodeErrorUNICODE_FILENAMESr+   unlinkr   rQ   __version__splitNUMPY_RELEASE_VERSIONrp   r   rt   r   r   rF   r   boolr   r   r   r   r   <module>   sD   	

 "	