o
    `^h#/                     @   sP  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
mZ ddlZddlZzddlZddlmZ W n eyI   dZY nw ddlmZ dZeeeek r^dZg dZdZed	pjd
Zdev rqdZdejd v ZG dd deZ G dd dZ!G dd dZ"dd Z#dd Z$dd Z%dd Z&dd Z'dd Z(dS ) z
Generic test utilities.

    N)module_from_specspec_from_file_location)version)_pep440z3.0.8)PytestTestercheck_free_memory_TestPythranFuncIS_MUSLFHOST_GNU_TYPE muslTeditablec                   @   s   e Zd ZdZdS )FPUModeChangeWarningzWarning about FPU mode changeN)__name__
__module____qualname____doc__ r   r   S/home/air/shanriGPT/back/venv/lib/python3.10/site-packages/scipy/_lib/_testutils.pyr   3   s    r   c                   @   s&   e Zd ZdZdd Z		d
dd	ZdS )r   a"  
    Run tests for this namespace

    ``scipy.test()`` runs tests for all of SciPy, with the default settings.
    When used from a submodule (e.g., ``scipy.cluster.test()``, only the tests
    for that namespace are run.

    Parameters
    ----------
    label : {'fast', 'full'}, optional
        Whether to run only the fast tests, or also those marked as slow.
        Default is 'fast'.
    verbose : int, optional
        Test output verbosity. Default is 1.
    extra_argv : list, optional
        Arguments to pass through to Pytest.
    doctests : bool, optional
        Whether to run doctests or not. Default is False.
    coverage : bool, optional
        Whether to run tests with code coverage measurements enabled.
        Default is False.
    tests : list of str, optional
        List of module names to run tests for. By default, uses the module
        from which the ``test`` function is called.
    parallel : int, optional
        Run tests in parallel with pytest-xdist, if number given is larger than
        1. Default is 1.

    c                 C   s
   || _ d S N)module_name)selfr   r   r   r   __init__V   s   
zPytestTester.__init__fast   NFc              
   C   sJ  dd l }tj| j }	tj|	jd }
ddg}|r|t|7 }|r4t	|dkr4|ddt	|d   g7 }|r=|d|
 g7 }|dkrH|d	d
g7 }n
|dkrR|d	|g7 }|d u rZ| jg}|d ury|dkryt
 rn|dt|g7 }ndd l}|jddd |dgt| 7 }z
||}W |dkS  ty } z|j}W Y d }~|dkS d }~ww )Nr   z--showlocalsz
--tb=shortr   -vz--cov=r   z-mznot slowfullz-nzMCould not run tests in parallel because pytest-xdist plugin is not available.   )
stacklevelz--pyargs)pytestsysmodulesr   ospathabspath__path__listint_pytest_has_xdiststrwarningswarnmain
SystemExitcode)r   labelverbose
extra_argvdoctestscoveragetestsparallelr    modulemodule_pathpytest_argsr+   r/   excr   r   r   __call__Y   s@   zPytestTester.__call__)r   r   NFFNN)r   r   r   r   r   r;   r   r   r   r   r   8   s    r   c                   @   s|   e Zd ZdZejejejejej	ej
gZejejgZejejgZdd Zdd Zdd Zdd	 Zd
d Zdd Zdd ZdS )r   aL  
    These are situations that can be tested in our pythran tests:
    - A function with multiple array arguments and then
      other positional and keyword arguments.
    - A function with array-like keywords (e.g. `def somefunc(x0, x1=None)`.
    Note: list/tuple input is not yet tested!

    `self.arguments`: A dictionary which key is the index of the argument,
                      value is tuple(array value, all supported dtypes)
    `self.partialfunc`: A function used to freeze some non-array argument
                        that of no interests in the original function
    c                 C   s   i | _ d | _d | _d S r   )	argumentspartialfuncexpected)r   r   r   r   setup_method   s   
z_TestPythranFunc.setup_methodc                 C   s>   t |}i }|j D ]\}}|jt jjur|j||< q|S r   )inspect	signature
parametersitemsdefault	Parameterempty)r   funcrA   optional_argskr   r   r   r   get_optional_args   s   

z"_TestPythranFunc.get_optional_argsc                 C   s2   d}| j D ]}t| j | d }||kr|}q|S )Nr   r   )r<   len)r   max_lenarg_idxcur_lenr   r   r   get_max_dtype_list_length   s   
z*_TestPythranFunc.get_max_dtype_list_lengthc                 C   s    |t |d kr|d S || S )Nr   )rK   )r   
dtype_list	dtype_idxr   r   r   	get_dtype   s   z_TestPythranFunc.get_dtypec                 C   s`   t |  D ]'}g }| jD ]}| | j| d |}|| j| d | q| j|  qd S )Nr   r   )rangerO   r<   rS   appendastypepythranfunc)r   type_idx
args_arrayrM   	new_dtyper   r   r   test_all_dtypes   s   
z _TestPythranFunc.test_all_dtypesc                 C   sF   g }| j D ]}|| j | d d d d d d d  q| j|  d S )Nr   rP   )r<   rU   rW   r   rY   rM   r   r   r   
test_views   s   
*z_TestPythranFunc.test_viewsc                 C   sH   g }| j D ]}|tj| j | d dddd d d  q| j|  d S )Nr   r   )axis)r<   rU   nprepeatrW   r\   r   r   r   test_strided   s   
z_TestPythranFunc.test_stridedN)r   r   r   r   r_   int8int16int32int64intcintpALL_INTEGERfloat32float64	ALL_FLOAT	complex64
complex128ALL_COMPLEXr?   rJ   rO   rS   r[   r]   ra   r   r   r   r   r      s    
		r   c                  C   s   ddl m}  | dduS )zQ
    Check if the pytest-xdist plugin is installed, providing parallel tests
    r   	find_specxdistN)importlib.utilrp   ro   r   r   r   r)      s   r)   c                 C   s   ddl }zttjd }d| tjd }W n ty5   t }|du r)|d |  d|d  d}Y nw || d k rC|| dS dS )	zJ
    Check *free_mb* of memory is available, otherwise do pytest.skip
    r   NSCIPY_AVAILABLE_MEMz={} MB memory required, but environment SCIPY_AVAILABLE_MEM={}zlCould not determine available memory; set SCIPY_AVAILABLE_MEM variable to free memory in MB to run the test.z MB memory required, but     .Az MB available)r    _parse_sizer#   environformatKeyError_get_mem_availableskip)free_mbr    mem_freemsgr   r   r   r      s   

r   c                 C   sx   dddddddddddddd	d
}t dd| | t j}|r*|d|vr.tdt|d||d  S )Nrt   g      ?     @@g    eAg   mBg      @g      0Ag      Ag      pB)r   brI   MGTkbMbGbTbkibMibGibTibz^\s*(\d+)\s*({})\s*$|r   zInvalid size stringr   )	rematchrw   joinkeysIgroup
ValueErrorfloat)size_strsuffixesmr   r   r   ru      s   ru   c               	   C   s   z
ddl } |  jW S  ttfy   Y nw tjdr\i }td"}|D ]}|	 }t
|d d ||d d < q%W d   n1 sGw   Y  d|v rT|d S |d	 |d
  S dS )zD
    Get information about memory available, not counting swap.
    r   Nlinuxz/proc/meminfor   r~   :memavailablememfreecached)psutilvirtual_memory	availableImportErrorAttributeErrorr!   platform
startswithopensplitr   striplower)r   infoflinepr   r   r   ry      s$   
$ry   c              	      sd  ddl }z	tddg W n ty   |d Y nw | tt  } tj	
|d }t|| |  | | d d }|d	 tjd
d t|d }t|d}|d |dtj d W d   n1 slw   Y  tjdkrtjdddd|dt|gd ntjddd|t|gd tjg dd td  fdd}|d|dfS )z
    Helper function to test building and importing Cython modules that
    make use of the Cython APIs for BLAS, LAPACK, optimize, and special.
    r   Nmesonz	--versionzNo usable 'meson' foundr   r5   _cython_examplesbuildT)exist_okzinterpreter-native-file.iniwz[binaries]
z
python = ''win32setupz--buildtype=releasez--native-filez--vsenv)cwd)r   compilez-vv
EXT_SUFFIXc                    s0   |    }t| |}t|}|j| |S r   )with_suffixr   r   loaderexec_module)modnamesospecmodsuffix
target_dirr   r   loadA  s
   
z$_test_cython_extension.<locals>.load	extendingextending_cpp)r    
subprocess
check_callFileNotFoundErrorrz   r*   	threading	get_identr#   r$   r   shutilcopytreemakedirsr   writer!   
executabler   	sysconfigget_config_var)tmp_pathsrcdirr    mod_name	build_dirnative_filer   r   r   r   r   _test_cython_extension  sD   



r   c                    sp   t |   fdd}g }td| D ]}|t j||f| |d q|D ]}|  q&|D ]}|  q/dS )a  
    Run a given function concurrently across a given number of threads.

    This is equivalent to using a ThreadPoolExecutor, but using the threading
    primitives instead. This function ensures that the closure passed by
    parameter gets called concurrently by setting up a barrier before it gets
    called before any of the threads.

    Arguments
    ---------
    n_workers: int
        Number of concurrent threads to spawn.
    fn: callable
        Function closure to execute concurrently. Its first argument will
        be the thread id.
    *args: tuple
        Variable number of positional arguments to pass to the function.
    **kwargs: dict
        Keyword arguments to pass to the function.
    c                    s"       | g|R i | d S r   )wait)iargskwargsbarrierfnr   r   closurec  s   z(_run_concurrent_barrier.<locals>.closurer   )targetr   r   N)r   BarrierrT   rU   Threadstartr   )	n_workersr   r   r   r   workersr   workerr   r   r   _run_concurrent_barrierL  s   




r   ))r   r@   r#   r   r   r   r!   r   r   rr   r   r   numpyr_   scipycythonCython.Compiler.Versionr   cython_versionr   
scipy._libr   required_versionparseVersion__all__r	   r   _vr&   IS_EDITABLERuntimeWarningr   r   r   r)   r   ru   ry   r   r   r   r   r   r   <module>   sJ    NH	8