a
    =_>                     @   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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ddlZddlmZ ddlmZ e
ejZe
edd Zd	ad	ad	Zd
d ZedduZedduZedduZedduZdd Z dd Z!dd Z"d+ddZ#d,ddZ$dd Z%e&ej'dslG dd dZ(G dd  d e(Z)G d!d" d"e)Z*G d#d$ d$ej'Z+G d%d& d&e,Z-G d'd( d(e,Z.G d)d* d*e+Z/dS )-z$Utilities for PyTables' test suites.    N)LooseVersionresource_filename)detect_number_of_cores)min_blosc_bitshuffle_versionblosc   Fc                 C   sF   d| v rda | d d| v r,da | d d| v rBda| d | S )NverboseTZsilentFz--heavy)r	   removeheavy)argv r   2lib/python3.9/site-packages/tables/tests/common.py
parse_argv2   s    


r   zliblzobzip2c                 C   s"   | rt d nt d t d d S )Nz#Performing the complete test suite!aQ  Performing only a light (yet comprehensive) subset of the test suite.
If you want a more complete test, try passing the --heavy flag to this script
(or set the 'heavy' parameter in case you are using tables.test() call).
The whole suite will take more than 4 hours to complete on a relatively
modern CPU and around 512 MB of main memory.L-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=)print)r   r   r   r   print_heavyJ   s    
r   c                  C   s4  t d t dtj  t dtdd   t dtj  td} tjrlt }t	d|d	 }d
| }nd}t dtj|f  | durt d| d df  td} | durt d| d | d f  td} | durt d| d | d f  td} | dur| d 
 d }t d| d |f  t }dd t| D }t dd|  dg}| d tkrx|d t dd|  zd	dlm} t d|  W n   Y n0 t d tj  t d!t   t d"tj  t d#t   t d$t   t d%t   t d&t   t d tj  dS )'z;Print all the versions of software that PyTables relies on.r   zPyTables version:    %szHDF5 version:        %sZhdf5r   zNumPy version:       %sr   z[0-9.]+r   zusing VML/MKL %sznot using Intel's VML/MKLzNumexpr version:     %s (%s)NzZlib version:        %s (%s)zin Python interpreterr   zLZO version:         %s (%s)   r   zBZIP2 version:       %s (%s)r   zBlosc version:       %s (%s)c                 S   s    g | ]\}}d ||d f qS )z%s (%s)r   r   ).0kvr   r   r   
<listcomp>u   s   z"print_versions.<locals>.<listcomp>zBlosc compressors:   %sz, ZshuffleZ
bitshufflezBlosc filters:       %s)__version__zCython version:      %szPython version:      %szPlatform:            %szByte-ordering:       %szDetected cores:      %szDefault encoding:    %szDefault FS encoding: %szDefault locale:      (%s, %s))r   tablesr   which_lib_versionnumpynumexprZuse_vmlZget_vml_versionrefindallsplitZblosc_get_complib_infosorteditemsjoinr   appendZCythonsysversionplatform	byteorderr   getdefaultencodinggetfilesystemencodinglocaleZgetdefaultlocalestdoutflush)ZtinfoZvml_versionZ	vml_availZ
blosc_dateZblosc_cinfoZblosc_finfoZcython_versionr   r   r   print_versionsW   s`    








r0   c                 C   s
   t d| S )Nztables.testsr   )filenamer   r   r   test_filename   s    r2   c                 C   s&   t sdS |rt| dd nt|  dS )z4Print out the `string` if verbose output is enabled.N )end)r	   r   )stringZnonlr   r   r   verbosePrint   s
    r6   r   c                 C   s`  t |ds| |kS t | dr&| jdkrBt |dr:|jdkrB| |kS | j|jkrhtrdtd| jd|j dS t |dr| jjdd |jjdd krtrtd	| jd|j dS t| jdkr| d |d krdS trtd| jd|j dS | jdkr6|jdkrdS tr2td
 tdt| j tdt|j dS | |k}t	
|}|s\tr\td |S )z*Checks if two numerical objects are equal.shaper   zShape is not equal:z!=r   dtyper   Nzdtype is not equal:zlength is not equalzlen(a.data) ==>zlen(b.data) ==>z,Some of the elements in arrays are not equal)hasattrr7   r	   r   r8   strlensizedatar   all)abZflavorresultr   r   r   allequal   sF    
*
rB   c                 C   sL   t | }t |}t| dr&| j|jks>t||s>t||s>dS t| |kS )zAre both `arr1` and `arr2` equal arrays?

    Arguments can be regular NumPy arrays, chararray arrays or
    structured arrays (including structured record arrays). They are
    checked for type and value equality.

    r8   F)typer9   r8   
issubclassr   r>   )Zarr1Zarr2t1t2r   r   r   areArraysEqual   s    	rG   assertWarnsc                   @   s   e Zd Zdd Zdd ZdS )_BaseTestCaseContextc                 C   s
   || _ d S N)	test_case)selfrK   r   r   r   __init__   s    z_BaseTestCaseContext.__init__c                 C   s    | j | j|}| j |d S rJ   )rK   Z_formatMessagemsgZfailureException)rL   ZstandardMsgrN   r   r   r   _raiseFailure   s    z"_BaseTestCaseContext._raiseFailureN)__name__
__module____qualname__rM   rO   r   r   r   r   rI      s   rI   c                   @   s   e Zd ZdddZdd ZdS )_AssertRaisesBaseContextNc                 C   st   t | | || _|| _|d urLz|j| _W qR tyH   t|| _Y qR0 nd | _|d urdt	|}|| _
d | _d S rJ   )rI   rM   expectedrK   rP   obj_nameAttributeErrorr:   r    compileexpected_regexrN   )rL   rT   rK   callable_objrX   r   r   r   rM      s    
z!_AssertRaisesBaseContext.__init__c                 C   sP   |du r| dd| _| S |  ||i | W d   n1 sB0    Y  dS )z
            If callable_obj is None, assertRaises/Warns is being used as a
            context manager, so check for a 'msg' kwarg and return self.
            If callable_obj is not None, call it passing args and kwargs.
            NrN   )poprN   )rL   namerY   argskwargsr   r   r   handle  s
    z_AssertRaisesBaseContext.handle)NN)rP   rQ   rR   rM   r^   r   r   r   r   rS      s     
rS   c                   @   s   e Zd Zdd Zdd ZdS )_AssertWarnsContextc                 C   sR   t tj D ]}t|dd ri |_qtjdd| _| j	 | _t
d| j | S )N__warningregistry__T)recordalways)listr'   modulesvaluesgetattrr`   warningscatch_warningswarnings_manager	__enter__simplefilterrT   )rL   r   r   r   r   rj     s    z_AssertWarnsContext.__enter__c                 C   s   | j ||| |d urd S z| jj}W n tyD   t| j}Y n0 d }| jD ]Z}|j}t|| jshqP|d u rt|}| j	d ur| j	
t|sqP|| _|j| _|j| _ d S |d ur| d| j	jt| | jr| d|| j n| d| d S )Nz"{0}" does not match "{1}"z{0} not triggered by {1}z{0} not triggered)ri   __exit__rT   rP   rV   r:   rg   message
isinstancerX   searchZwarningr1   linenorO   formatpatternrU   )rL   exc_type	exc_valuetbZexc_nameZfirst_matchingmwr   r   r   rl     sB    

z_AssertWarnsContext.__exit__N)rP   rQ   rR   rj   rl   r   r   r   r   r_     s   	r_   c                       s^   e Zd Z fddZdd Zdd Zdd Zeej	d	sBdddZ
dddZdddZ  ZS )PyTablesTestCasec                    s:   t t|   | jD ] }| j| jjdvrd | j|< qd S )NZinstancemethod)superrx   tearDown__dict__	__class__rP   )rL   keyr|   r   r   rz   @  s    
zPyTablesTestCase.tearDownc                 C   s   |   dd S )zGet the name of this test case..idr"   rL   r   r   r   _getNameF  s    zPyTablesTestCase._getNamec                 C   s   |   dd S )z>Get the name of the method currently running in the test case.r   r   r   r   r   r   _getMethodNameJ  s    zPyTablesTestCase._getMethodNamec                 C   s<   t r8|  }|  }d||f }td|dt| f  dS )z;Print a nice header for the current test method if verbose.zRunning %s.%sz%s
%s-N)r	   r   r   r   r;   )rL   r[   Z
methodNametitler   r   r   _verboseHeaderN  s
    zPyTablesTestCase._verboseHeaderrH   Nc                 O   s   t || |}|d|||S )NrH   )r_   r^   )rL   Zexpected_warningrY   r\   r]   contextr   r   r   rH   Z  s    zPyTablesTestCase.assertWarnsFc                 C   s^   t rtd| td| |r2| |j|jkd n| |j|jkd | |j|jkd d S )NzGroup 1:zGroup 2:(node1 and node2 have the same pathnames.1node1 and node2 does not have the same pathnames.z0node1 and node2 does not have the same children.)r	   r   
assertTrue_v_pathnameZ_v_childrenrL   Znode1Znode2Zhardlinkr   r   r   _checkEqualityGroup_  s     




z$PyTablesTestCase._checkEqualityGroupc                 C   sl   t rtd| td| |r2| |j|jkd n| |j|jkd | t|d d  |d d  d d S )NzLeaf 1:zLeaf 2:r   r   z.node1 and node2 does not have the same values.)r	   r   r   r   rG   r   r   r   r   _checkEqualityLeafo  s     



z#PyTablesTestCase._checkEqualityLeaf)N)F)F)rP   rQ   rR   rz   r   r   r   r9   unittestTestCaserH   r   r   __classcell__r   r   r~   r   rx   ?  s   

rx   c                       s0   e Zd ZdZi Z fddZ fddZ  ZS )TestFileMixinNc                    s2   t t|   tj| jfd|  i| j| _d S )Nr   )	ry   r   setUpr   	open_fileh5fnamer   open_kwargsh5filer   r~   r   r   r     s    zTestFileMixin.setUpc                    s   | j   tt|   dS )zClose ``h5file``.N)r   closery   r   rz   r   r~   r   r   rz     s    
zTestFileMixin.tearDown)rP   rQ   rR   r   r   r   rz   r   r   r   r~   r   r     s   r   c                       sB   e Zd ZdZi Zdd Z fddZ fddZdd	d
Z  Z	S )TempFileMixinrw   c                 C   s   t j|  ddS )Nz.h5)prefixsuffix)tempfileZmktempr   r   r   r   r   _getTempFileName  s    zTempFileMixin._getTempFileNamec                    s@   t t|   |  | _tj| j| jfd|  i| j	| _
dS )zSet ``h5file`` and ``h5fname`` instance attributes.

        * ``h5fname``: the name of the temporary HDF5 file.
        * ``h5file``: the writable, empty, temporary HDF5 file.

        r   N)ry   r   r   r   r   r   r   	open_moder   r   r   r   r~   r   r   r     s    
zTempFileMixin.setUpc                    s.   | j   d| _ t| j tt|   dS )z(Close ``h5file`` and remove ``h5fname``.N)r   r   osr
   r   ry   r   rz   r   r~   r   r   rz     s    
zTempFileMixin.tearDownrc                 K   s&   | j   tj| j|fi || _ dS )zReopen ``h5file`` in the specified ``mode``.

        Returns a true or false value depending on whether the file was
        reopenend or not.  If not, nothing is changed.

        T)r   r   r   r   r   )rL   moder]   r   r   r   _reopen  s    
zTempFileMixin._reopen)r   )
rP   rQ   rR   r   r   r   r   rz   r   r   r   r   r~   r   r     s   r   c                   @   s   e Zd Ze Zdd ZdS )ShowMemTimec                 C   s  t dD ]}|dr(t| d }q|drDt| d }q|dr`t| d }q|dr|t| d }q|drt| d }q|drt| d }qtd	t | j  td
|    td||f  td||f  td||f  dS )z$Showing memory and time consumption.z/proc/self/statuszVmSize:r   zVmRSS:zVmData:zVmStk:zVmExe:zVmLib:z
WallClock time:z Memory usage: ******* %s *******zVmSize: %7s kB	VmRSS: %7s kBzVmData: %7s kB	VmStk: %7s kBzVmExe:  %7s kB	VmLib: %7s kBN)open
startswithintr"   r   timetrefr   )rL   lineZvmsizeZvmrssZvmdataZvmstkZvmexeZvmlibr   r   r   test00  s$    





zShowMemTime.test00N)rP   rQ   rR   r   r   r   r   r   r   r   r     s   r   )F)r   )0__doc__r   r    r'   r   r-   r)   r   rg   Zdistutils.versionr   Zpkg_resourcesr   r   r   r   r   Ztables.utilsr   Ztables.req_versionsr   Zhdf5_versionr   Zblosc_versionr	   r   Zshow_memoryr   Z
zlib_availZ	lzo_availZbzip2_availZblosc_availr   r0   r2   r6   rB   rG   r9   r   rI   rS   r_   rx   objectr   r   r   r   r   r   r   <module>   sP   
;


5.A*